Skip to content

Instantly share code, notes, and snippets.

View kiwiandroiddev's full-sized avatar

Matt Clarke kiwiandroiddev

  • Auckland, New Zealand
View GitHub Profile
@kiwiandroiddev
kiwiandroiddev / ThemeUtils.java
Created October 8, 2015 21:48
Resolves a custom Android attribute to a resource ID for an Activity's theme.
public class ThemeUtils {
public static int getResourceIdForAttribute(@AttrRes int attrId, Activity activity) {
TypedValue outValue = new TypedValue();
activity.getTheme().resolveAttribute(attrId, outValue, true);
return outValue.resourceId;
}
}
@kiwiandroiddev
kiwiandroiddev / rx-retry-backoff.js
Created February 7, 2016 15:33
Exponential backoff with RxJs (modified sample code from docs)
var Rx = require('rx')
var MAX_RETRIES = 4
Rx.Observable.throw(new Error("always fails"))
.retryWhen(function (errors) {
return Rx.Observable.zip(
Rx.Observable.range(1, MAX_RETRIES), errors, function (i, e) { return i })
.flatMap(function (i) {
console.log("delay retry by " + i + " second(s)");
return Rx.Observable.timer(i * 1000);
@kiwiandroiddev
kiwiandroiddev / git.gradle
Created April 22, 2016 03:37
Git interface functions for gradle build scripts (e.g. for Android). To use add this to your build script: apply from: 'git.gradle'
def getGitCommit() {
return 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
}
def getGitTag() {
def p = Runtime.getRuntime().exec("git describe HEAD --abbrev=0")
if (p.waitFor() != 0) {
return "none" // no tag
}
@kiwiandroiddev
kiwiandroiddev / KotlinUtils.kt
Last active June 1, 2016 06:04
Small general-purpose extension functions and utilities for Kotlin
/**
* If [assumed] is non-null, converts it to a non-null type and executes [block] on the non-null
* type as an extension function (like with() in the Standard kotlin library)
*
* Similar to Swift's "if let = ..." construct.
*/
fun <T> if_let(assumed : T?, block : T.() -> Unit) {
if (assumed == null)
return
@kiwiandroiddev
kiwiandroiddev / kill_adb_alias.sh
Last active September 9, 2016 02:22
Bash command to force-kill Genymotion and your ADB server (sometimes necessary after an Android Studio Invalidate caches/restart; Genymotion will restart the problematic adb server unless it is killed first)
function kill_one_ps() {
ps -A | grep $1 | egrep -m1 -o '^\d+' | xargs kill -9
}
function kill_all_ps() {
ps -A | grep $1 | egrep -o '^\d+' | xargs kill -9
}
# aliases
alias kill_adb="kill_all_ps Genymotion ; kill_one_ps 'adb.*server'"
@kiwiandroiddev
kiwiandroiddev / BouncyButton.java
Created August 15, 2016 10:32
Custom Android Button that makes use of Facebook's Rebound library to animate changes to its enabled state.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import com.facebook.rebound.BaseSpringSystem;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.facebook.rebound.SpringUtil;
@kiwiandroiddev
kiwiandroiddev / RxJavaExtensions.kt
Created October 3, 2016 05:20
filterNotNull for RxJava in Kotlin
/**
* Asynchronous equivalent of filterNotNull in Kotlin's stdlin (kotlin.collections)
*/
fun <T> Observable<T?>.filterNotNull() : Observable<T> {
return this.filter { item -> item != null }.map { it!! }
}
@kiwiandroiddev
kiwiandroiddev / RegexMaskTextWatcher.kt
Last active October 1, 2021 09:22
Android TextWatcher to restrict user input to match a given Regular Expression
/**
* Add this to (e.g.) an EditText via addTextChangedListener() to prevent any user input
* that doesn't match its supplied regex.
*
* Inspired by original Java code here: http://stackoverflow.com/a/11545229/1405990
*/
class RegexMaskTextWatcher(regexForInputToMatch : String) : TextWatcher {
private val regex = Pattern.compile(regexForInputToMatch)
private var previousText: String = ""
@kiwiandroiddev
kiwiandroiddev / PredicateTextWatcher.kt
Created October 19, 2016 03:52
Android TextWatcher to restrict user input to match a given predicate
/**
* Add this to (e.g.) an EditText via addTextChangedListener() to only allow user input that
* is accepted by the supplied predicate.
*
* e.g.
* emailEditText.addTextChangedListener(PredicateTextWatcher({ presenter.isValidEmail(it) })
*
* Inspired by original Java code here: http://stackoverflow.com/a/11545229/1405990
*/
class PredicateTextWatcher(val predicate : (String) -> Boolean) : TextWatcher {
@kiwiandroiddev
kiwiandroiddev / adb_airplane_mode_on.sh
Created January 4, 2017 00:47
Bash script to enable Airplane mode on the running Android emulator
#!/bin/bash
# adb must be running in root mode to broadcast intents
adb -e root
adb -e shell settings put global airplane_mode_on 1
# radios won't actually turn off until this is broadcasted
adb -e shell am broadcast -a android.intent.action.AIRPLANE_MODE