Skip to content

Instantly share code, notes, and snippets.

View patrickhammond's full-sized avatar

Patrick Hammond patrickhammond

View GitHub Profile
@patrickhammond
patrickhammond / android_instructions.md
Last active March 29, 2024 20:14
Easily setup an Android development environment on a Mac

Here is a high level overview for what you need to do to get most of an Android environment setup and maintained.

Prerequisites (for Homebrew at a minimum, lots of other tools need these too):

  • XCode is installed (via the App Store)
  • XCode command line tools are installed (xcode-select --install will prompt up a dialog)
  • Java

Install Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

@patrickhammond
patrickhammond / gist:0b13ec35160af758d98c
Created March 8, 2015 02:30
Sample for how to use the Google Play Services dynamic security provider to keep the SSL library that the app will use to up date.
package com.mycompany.myapp.app;
import android.app.Application;
import android.content.Intent;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.security.ProviderInstaller;
import com.google.android.gms.security.ProviderInstaller.ProviderInstallListener;
public class MainApplication extends Application {
@patrickhammond
patrickhammond / ViewTreeObserverHelper.java
Created February 3, 2014 20:03
Safe usage of a ViewTreeObserver to get view measurements. This ensures that the layout listener isn't leaked and the right API calls are made.
public static void notifyWhenMeasured(final View view, final ViewTreeObserver.OnGlobalLayoutListener listener) {
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
listener.onGlobalLayout();
// Need to get a fresh ViewTreeObserver
ViewTreeObserver freshVto = view.getViewTreeObserver();
if (Build.VERSION.SDK_INT < 16) {
@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 / gist:7598623
Last active August 13, 2021 03:03
Adds list dividers between children in a linear layout. showDividers takes a set of flags (ex: you can specify beginning|middle|end to get dividers at the start of the layout, through the layout, and at the end of the layout. API 11+ http://developer.android.com/reference/android/widget/LinearLayout.html#setShowDividers(int)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"
android:divider="?android:listDivider"
android:dividerPadding="16dp">
</LinearLayout>
@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 / 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.
*/
@patrickhammond
patrickhammond / MainActivity.kt
Created January 9, 2017 02:59
Raspberry Pi 3 running Android Things driving an Arduino Uno to control an RGB LED
package com.madebyatomicrobot.things
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import com.google.android.things.pio.PeripheralManagerService
import com.google.android.things.pio.UartDevice
@patrickhammond
patrickhammond / main.dart
Last active December 16, 2019 20:44
polygon generator
import 'dart:math';
// -----------------------------------
// -----------------------------------
// These are the values that you will want to adjust before hitting run.
// Take note that this is in a lat/long order (what you would normally get from Google Maps, etc)
const centerPoint = [39.1094384, -84.5094552];
// 1 mile
find . -not -name 'ic_attachment_grey600_24dp.png' -not -type d | xargs rm