Skip to content

Instantly share code, notes, and snippets.

View christopherperry's full-sized avatar

Christopher Perry christopherperry

View GitHub Profile
@christopherperry
christopherperry / AndroidVersion.java
Created July 27, 2012 22:02
Robolectric test runner with Guice injection, custom class binding using annotations, and android version support.
package annotations;
import android.os.Build;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
@christopherperry
christopherperry / adb+
Created July 30, 2012 16:12
A bash script that let's you issue adb commands to multiple devices at once
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
#
# Examples
# ./adb+ version
# ./adb+ install apidemo.apk
# ./adb+ uninstall com.example.android.apis
@christopherperry
christopherperry / SwipeLinearLayout.java
Created August 27, 2012 18:38
Android LinearLayout that handles left swipes.
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
public class SwipeLinearLayout extends LinearLayout {
private GestureDetector gestureDetector;
private OnSwipeListener listener;
@christopherperry
christopherperry / robolectric-library-resource-loading.java
Created August 30, 2012 21:32 — forked from pplante/robolectric-library-resource-loading.java
Robolectric does not support loading resources from an Android Library project such as ActionBarSherlock of FlyInMenu, so this patch allows you to specify a library project to load resources in tests.
// In ResourceLoader.java I added the following method:
//
public void loadLibraryProjectResources(File libraryProjectRoot) throws Exception {
File systemResourceDir = getSystemResourceDir(getPathToAndroidResources());
File localValueResourceDir = getValueResourceDir(libraryProjectRoot);
File systemValueResourceDir = getValueResourceDir(systemResourceDir);
loadStringResources(localValueResourceDir, systemValueResourceDir);
loadPluralsResources(localValueResourceDir, systemValueResourceDir);
loadValueResources(localValueResourceDir, systemValueResourceDir);
@christopherperry
christopherperry / CheckableLinearLayout
Created September 18, 2012 22:43
A LinearLayout that implements the Checkable interface, allowing a LinearLayout to be put into a checked state.
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;
@christopherperry
christopherperry / html_directory_selection
Created June 29, 2013 07:16
Select directory with HTML input field.
<input type="file" name="files[]" multiple directory webkitdirectory mozdirectory>
^(.*(ClassNameOne|ClassNameTwo|ClassNameThree)).*$
@christopherperry
christopherperry / Intellij Unit Test VM options
Last active December 24, 2015 23:08
IntelliJ memory setting for unit tests
Set VM options to: -ea -Xms800m -Xmx800m -XX:MaxPermSize=500m
@christopherperry
christopherperry / ExpiringLruCache.java
Last active February 16, 2024 15:12
LruCache for Android with expiring keys. Instead of modifying LruCache directly I used delegation to get around final keyword usage and a dirty hack to override everything else.
import android.os.SystemClock;
import android.support.v4.util.LruCache;
import java.util.HashMap;
import java.util.Map;
/**
* An Lru Cache that allows entries to expire after
* a period of time. Items are evicted based on a combination
* of time, and usage. Adding items past the {@code maxSize}
@christopherperry
christopherperry / HappyDebuggingSuckers.java
Last active December 30, 2015 10:19
Evil reflective code
@Test
public void happyDebuggingSuckers() throws SecurityException, NoSuchFieldException, IllegalAccessException {
Field value = Boolean.class.getDeclaredField("value");
value.setAccessible(true);
Boolean trueBool = true;
Boolean falseBool = false;
value.setBoolean(trueBool, false);