Skip to content

Instantly share code, notes, and snippets.

View patrickhammond's full-sized avatar

Patrick Hammond patrickhammond

View GitHub Profile
@patrickhammond
patrickhammond / SampleApplication.java
Last active April 30, 2018 21:20
Android Application class that is aware of if it is being created in the context of unit/instrumentation tests where the full application initialization is not desirable.
package com.myapp;
public class SampleApplication extends TestingAwareApplication {
@Override
protected String getFullyQualifiedTestFile() {
return "com.myapp.TestMode";
}
@Override
protected void initAppInstance() {
@patrickhammond
patrickhammond / EspressoTestCase.java
Last active September 21, 2021 13:42
Base class for Espresso (https://code.google.com/p/android-test-kit/wiki/Espresso) tests that deals with some of the library ugliness and gotchas. Implementations **must** ensure super.setUp() and super.tearDown() are called if they are overridden.
import android.app.Activity;
import android.content.Context;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.test.ActivityInstrumentationTestCase2;
/**
* This requires to be declared in the effective manifest:
* <uses-permission android:name="android.permission.WAKE_LOCK"/>
*
@patrickhammond
patrickhammond / HackedTouchDelegate.java
Last active June 25, 2021 09:15
There is a bug in TouchDelegate where the ancestor view can become untouchable after a TouchDelegate interacts with the delegate view. See https://code.google.com/p/android/issues/detail?id=36445 for details.
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewConfiguration;
/**
* There is a bug with TouchDelegate where ancestor views can get into an awkward state after
* a delegate view has been actioned upon by the touch delegate.
*
@patrickhammond
patrickhammond / CheckBoxHelper.java
Last active August 29, 2015 14:10
Creating checkboxes with bigger touch targets
import android.graphics.Rect;
import android.view.View;
import android.widget.CheckBox;
/**
* Enlarges the effective hit target of a checkbox to more closely match the dimensions of the
* provided root view. If the provided root isn't actually an ancestor view of the checkbox, bad
* things will happen.
* <p/>
* See: http://developer.android.com/training/gestures/viewgroup.html#delegate
@patrickhammond
patrickhammond / MockWebServerIssueTest.java
Last active September 14, 2015 22:35
Weird OkHttp API behavior where a thrown IOException (SocketTimeoutException) from onResponse does not propagate to onFailure. Discussion here: https://github.com/square/okhttp/issues/1066
package com.demo.okhttpissue;
import android.test.InstrumentationTestCase;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
@patrickhammond
patrickhammond / fml.java
Created November 11, 2014 21:29
Getting list value at position when you have added a header view. There really should be a Lint check around this type of subtle bug.
// If you do this...
list.addHeaderView(headerView);
// And then call this inside of onItemClick, the position value will not be what you expect (header is now index 0)
Fruit fruit = adapter.getItem(position);
// This is what you really want to call inside of onItemClick
Fruit fruit = (Fruit) parent.getItemAtPosition(position);
@patrickhammond
patrickhammond / crashlyticsInfo.sh
Last active April 30, 2018 21:22
Simple script to display info associated with your Crashlytics account.
#!/bin/sh
echo "Crashlytics username (e-mail):"
read email
echo "Crashlytics password:"
read -s password
data="{\"email\":\"${email}\",\"password\":\"${password}\"}"
@patrickhammond
patrickhammond / DevicePhoneNumber.java
Created September 25, 2014 19:30
User and device info helper code
public class DevicePhoneNumber {
public final String phoneNumber;
public DevicePhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "DevicePhoneNumber{" +
@patrickhammond
patrickhammond / MyActivity.java
Last active August 29, 2015 14:05
Lambdas on Android...its a brave new world. See: https://github.com/evant/gradle-retrolambda
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Old school
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
@patrickhammond
patrickhammond / ParcelableHelper.java
Last active April 20, 2021 02:22
A Parcelable is not always immediately deep copied; this forces an immediate deep copy into a new Parcelable instance.
import android.os.Parcel;
import android.os.Parcelable;
public class ParcelableHelper {
/**
* There is not always a guarantee that Parcelable values will be immediately written out and
* read back in. For data data that mutable (its own issue), this can be a problem. This is
* for the times when it would be great to have confidence that you will be working with a copy
* of that data.
*/