Skip to content

Instantly share code, notes, and snippets.

View billmote's full-sized avatar

Bill Mote billmote

  • Boca Raton, FL
  • 06:01 (UTC -04:00)
View GitHub Profile
@billmote
billmote / MyApplication.java
Last active November 14, 2015 04:03
Enum variant to handle switching of environments. I had previously implemented PushConfig.java as an abstract class and had QaPushConfig.java and ProdPushConfig.java extend them, but that resulted in ugly switch-case logic everywhere I wanted to use them. Patrick refactored that implementation into what you see below.
public class MyApplication extends Application {
public static final PushConfig.Environment ENVIRONMENT = PushConfig.Environment.PROD; // can also be .QA
private static final String TAG = "MyApplication";
@Override
public void onCreate() {
super.onCreate();
final PushConfig pushConfig = ENVIRONMENT.getPushConfig();
String appId = pushConfig.etAppId;
<receiver
android:name=".DownloadBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
package com.androidfu.nowplaying.app.api;
import android.content.Context;
import com.androidfu.nowplaying.app.util.Log;
import com.androidfu.nowplaying.app.util.Preconditions;
import com.jakewharton.byteunits.DecimalByteUnit;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
@billmote
billmote / CachingOkHttpClient.java
Last active August 29, 2015 14:19
A cache for OkHttp calls that implements the Builder Pattern or can be called statically.
import android.content.Context;
import com.androidfu.nowplaying.app.util.Log;
import com.androidfu.nowplaying.app.util.Preconditions;
import com.jakewharton.byteunits.DecimalByteUnit;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import java.io.File;
import java.io.IOException;
@billmote
billmote / HaversineDistance.java
Created March 20, 2015 13:16
Calculate the distance between 2 points
/**
* This is the implementation Haversine Distance Algorithm between two places
* @author ananth
* R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
*
@billmote
billmote / Movie.java
Last active August 29, 2015 14:16
An alternative to the custom TypeAdapter to handle Rotten Tomatoes returning different JSON primitives for Movie runtime
@DebugLog
public class Movie {
@Expose
public Object runtime;
// ...
public Integer getMovieRuntime() {
if (runtime instanceof String && !TextUtils.isEmpty((String) runtime)) {
return Integer.valueOf((String) runtime);
} else if (runtime instanceof Integer) {
return (Integer) runtime;
@billmote
billmote / Movie.java
Last active August 29, 2015 14:16
A custom TypeAdapter to handle the case when Rotten Tomatoes returns "" rather than a number when runtime is null.
@Parcel
@DebugLog
public class Movie {
@Expose
public String id;
@Expose
public String title;
@Expose
public Integer year;
@billmote
billmote / PrettyDate.java
Created February 20, 2015 16:34
Formats dates to sortable UTC strings in compliance with ISO-8601.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Formats dates to sortable UTC strings in compliance with ISO-8601.
*/
public class PrettyDate {
public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
public static String LEGACY_FORMAT = "EEE MMM dd hh:mm:ss zzz yyyy";
@billmote
billmote / commands.sh
Last active August 29, 2015 14:15
Helpful Commands
# Search & Replace contents of a file -- in this case we're searching for jb4ASDK@ in all files and replacing with ~!
grep -IRlr jb4ASDK@ . | xargs sed -i "" -e 's/jb4ASDK@/~!/g'
# Human readable folder utilization from current folder
sudo du -sh $(ls -d $(pwd)/*)
# When git doesn't know who we are:
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa
@billmote
billmote / AndroidManifest.xml
Last active October 15, 2023 10:48 — forked from JakeWharton/gist:f50f3b4d87e57d8e96e9
When pushing code from your IDE, wake the device and show the activity.
<?xml version="1.0" encoding="utf-8"?>
<!-- Add this as a debug manifest so the permissions won't be required by your production app -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>