Skip to content

Instantly share code, notes, and snippets.

View billmote's full-sized avatar

Bill Mote billmote

  • Boca Raton, FL
  • 07:03 (UTC -05:00)
View GitHub Profile
@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 / 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;
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;
<receiver
android:name=".DownloadBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
@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;
@billmote
billmote / ActivityPermissionDelegate.java
Created October 6, 2015 16:10 — forked from patrickhammond/ActivityPermissionDelegate.java
Pushing permission ugliness handling to one place.
import android.app.Activity;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Size;
import android.support.v4.app.ActivityCompat;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class ActivityPermissionDelegate {
public interface PermissionRationaleRetryBehavior {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
BarcodeLoggerActivityPermissionsDispatcher.startBarcodeCaptureWithCheck(this);
}
@NeedsPermission(Manifest.permission.CAMERA)
void startBarcodeCapture() {
int orientation = WindowManagerHelper.getCurrentAccurateOrientation(this);
Intent intent = CaptureActivity.buildIntent(this, orientation);
// annotation processor will generate DevicePhoneNumberSumType (see: 4_DevicePhoneNumberSumType.java)
@SumType
public interface DevicePhoneNumber {
String phoneNumber();
void phoneNumberNotAvailable();
String[] permissionsRequired();
}
@billmote
billmote / DupeChecker.java
Last active November 10, 2015 13:25
"Write a method that determines whether all the characters in a string are the same, using only library string methods, but no loops or recursion." Tests ... assertEquals(true, allDuplicateChars("cccccc")); assertEquals(false, allDuplicateChars("abcdef"));
public class DupeChecker {
public boolean allDuplicateChars(final String input) {
if (input == null || input.length() == 0) {
return false;
}
String firstChar = input.substring(0,1);
String matchingChars[] = input.split(firstChar + "+");
return matchingChars.length == 0;
}
}
@billmote
billmote / build.gradle
Created December 4, 2015 16:22 — forked from PhoenixIllusion/build.gradle
Using the local.properties file of an Android Studio project to select the latest BuildTools version
apply plugin: 'com.android.library'
android {
compileSdkVersion 'Google Inc.:Google APIs:22'
Properties localProps = new Properties()
localProps.load(new FileInputStream(file('../local.properties')))
def buildToolVer = file("${localProps['sdk.dir']}/build-tools/").listFiles().sort().reverse().first().name;
buildToolsVersion buildToolVer;