Skip to content

Instantly share code, notes, and snippets.

/**
* Custom view that handles view state restoration properly.
* This process is to be used when using non-unique ids which causes the state restoration to lose its reference.
* It basically handles the state restoration by creating a SparseArray for each children views thus avoiding the non-unique id issue.
* @see <a href="http://trickyandroid.com/saving-android-view-state-correctly/">http://trickyandroid.com/saving-android-view-state-correctly/</a>
* Created by StephenVinouze on 05/02/2016.
*/
public class CustomViewWithStateRestoration extends ViewGroup {
public CustomViewWithStateRestoration(Context context) {

Keybase proof

I hereby claim:

  • I am StephenVinouze on github.
  • I am stephenvinouze (https://keybase.io/stephenvinouze) on keybase.
  • I have a public key whose fingerprint is 8D11 83FB 81BE 988A B81D 6924 3F6D 61C3 CC25 B479

To claim this, I am signing this object:

@StephenVinouze
StephenVinouze / word_counter.py
Created April 22, 2020 06:18
Script to count word between two separators
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
inputString = """
"""
list = re.findall(r'\[\<(.+?)\>\]', inputString)
subString = " ".join(list).replace('\xc2\xa0', ' ')
@StephenVinouze
StephenVinouze / View_postDelayed.java
Created May 6, 2020 15:09
Post delayed implementation in View.java
/**
* <p>Causes the Runnable to be added to the message queue, to be run
* after the specified amount of time elapses.
* The runnable will be run on the user interface thread.</p>
*
* @param action The Runnable that will be executed.
* @param delayMillis The delay (in milliseconds) until the Runnable
* will be executed.
*
* @return true if the Runnable was successfully placed in to the
@StephenVinouze
StephenVinouze / audio_permission.kt
Created May 12, 2020 16:21
Ask audio permission
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_AUDIO_REQUEST_CODE)
}
}
private val speech: SpeechRecognizer by lazy { SpeechRecognizer.createSpeechRecognizer(context) }
if (SpeechRecognizer.isRecognitionAvailable(context)) {
speechRecognizer.setRecognitionListener(this)
} else {
// Handle error
}
val recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH)
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.packageName)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true)
}
}
speechRecognizer.startListening(recognizerIntent)
override fun onPartialResults(partialResults: Bundle) {
val matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
if (matches != null) {
// Handle partial matches
}
}
override fun onResults(results: Bundle) {
val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
val scores = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES)
if (matches != null) {
speechRecognizer.stopListening()
speechRecognizer.destroy()
private var isActivated: Boolean = false
private val activationKeyword: String = "<YOUR_KEYWORD>"
override fun onResults(results: Bundle) {
val matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
val scores = results.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES)
if (matches != null) {
if (isActivated) {
isActivated = false
stopRecognition()