Skip to content

Instantly share code, notes, and snippets.

View patrickhammond's full-sized avatar

Patrick Hammond patrickhammond

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / bouncewifi.sh
Last active April 30, 2018 21:19
Utility to bounce the wifi connection every 55 minutes (to prevent an hourly boot).
#!/bin/sh
while true; do
sleep 3300 # 55 min * 60 sec
echo "Bouncing the wifi at `date`"
# You might need to change en1 to something else.
# ifconfig can tell you what network adapter to use
networksetup -setairportpower en1 off
sleep 2
@patrickhammond
patrickhammond / app_build.gradle
Created January 22, 2015 19:06
FindBugs snippet
...
apply plugin: 'findbugs'
...
task findbugs(type: FindBugs, dependsOn: assembleDebug) {
excludeFilter file("${project.rootDir}/config/findbugs/exclude.xml")
classes = fileTree('build/intermediates/classes/debug/') // Varies based on your app build configs and flavors...
source = fileTree('src/main/java/')
@patrickhammond
patrickhammond / checkHasPermission.java
Created February 5, 2015 00:42
Utility method to perform a runtime check that an app has been granted a permission.
public static void checkHasPermission(Context contxt, String permissionName) {
String packageName = context.getPackageName();
PackageManager packageManager = context.getPackageManager();
int permissionStatus = packageManager.checkPermission(permissionName, packageName);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Application requires <uses-permission android:name=\"" + permissionName + "\" />");
}
}