Skip to content

Instantly share code, notes, and snippets.

Jake Wharton JakeWharton

Block or report user

Report or block JakeWharton

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JakeWharton
JakeWharton / gist:f50f3b4d87e57d8e96e9
Created Feb 7, 2015
Rise and Shine™, unlock and wake up your device automatically when you deploy from the IDE. Put this somewhere in your `src/debug/` code and run it when the application or main activity starts. Apache 2.
View gist:f50f3b4d87e57d8e96e9
/**
* Show the activity over the lockscreen and wake up the device. If you launched the app manually
* both of these conditions are already true. If you deployed from the IDE, however, this will
* save you from hundreds of power button presses and pattern swiping per day!
*/
public static void riseAndShine(Activity activity) {
activity.getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED);
PowerManager power = (PowerManager) activity.getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock =
@JakeWharton
JakeWharton / Truss.java
Last active Oct 8, 2019
Extremely simple wrapper around SpannableStringBuilder to make the API more logical and less awful. Apache 2 licensed.
View Truss.java
import android.text.SpannableStringBuilder;
import java.util.ArrayDeque;
import java.util.Deque;
import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
/** A {@link SpannableStringBuilder} wrapper whose API doesn't make me want to stab my eyes out. */
public class Truss {
private final SpannableStringBuilder builder;
private final Deque<Span> stack;
@JakeWharton
JakeWharton / ContractFragment.java
Created May 6, 2012
Base fragment to ensure the parent activity implements a contract interface.
View ContractFragment.java
/* Base fragment to ensure the parent activity implements a contract interface. */
public abstract class ContractFragment<T> extends Fragment {
private T mContract;
@Override
public void onAttach(Activity activity) {
try {
mContract = (T)activity;
} catch (ClassCastException e) {
throw new IllegalStateException(activity.getClass().getSimpleName()
@JakeWharton
JakeWharton / OkConnectionFactory.java
Created Jun 17, 2013
A connection factory for using OkHttp with Kevin Sawicki's HttpRequest library.
View OkConnectionFactory.java
/**
* A {@link HttpRequest.ConnectionFactory connection factory} which uses OkHttp.
* <p/>
* Call {@link HttpRequest#setConnectionFactory(HttpRequest.ConnectionFactory)} with an instance of
* this class to enable.
*/
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
private final OkHttpClient client;
public OkConnectionFactory() {
@JakeWharton
JakeWharton / Oauth1SigningInterceptor.java
Last active Sep 27, 2019
An OkHttp interceptor which does OAuth1 signing. Requires Guava and Java 8, although those dependencies wouldn't be too hard to break if you didn't have them.
View Oauth1SigningInterceptor.java
/*
* Copyright (C) 2015 Jake Wharton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@JakeWharton
JakeWharton / README.md
Last active Aug 26, 2019
A special activity which facilitates restarting your application process.
View README.md
View ThreadedAsyncTaskHelper.java
/**
* Execute an {@link AsyncTask} on a thread pool.
*
* @param task Task to execute.
* @param args Optional arguments to pass to {@link AsyncTask#execute(Object[])}.
* @param <T> Task argument type.
*/
public static <T> void execute(AsyncTask<T, ?, ?> task, T... args) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) {
throw new UnsupportedOperationException("This class can only be used on API 4 and newer.");
@JakeWharton
JakeWharton / build.gradle
Last active Aug 26, 2019
Prevent wildcard versions in your Gradle project. These undermine deterministic and hermetic builds and are generally considered bad practice.
View build.gradle
allprojects {
afterEvaluate { project ->
project.configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.version.contains('+')) {
throw new GradleException("Wildcard dependency forbidden: ${requested.group}:${requested.name}:${requested.version}")
}
}
}
@JakeWharton
JakeWharton / README.md
Last active Aug 26, 2019
A JUnit @rule which launches an activity when your test starts. Stop extending gross ActivityInstrumentationBarfCase2!
View README.md
@JakeWharton
JakeWharton / ForegroundImageView.java
Created Jul 10, 2014
An ImageView which supports a foreground drawable.
View ForegroundImageView.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ForegroundImageView extends ImageView {
private Drawable foreground;
You can’t perform that action at this time.