Skip to content

Instantly share code, notes, and snippets.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSource;
final class AsciiArtInterceptor implements Interceptor {
@NightlyNexus
NightlyNexus / AnalyticsNetworkLogger.java
Last active November 30, 2017 05:53
A Retrofit 2 Call Adapter Factory for logging. This mostly exists because Interceptors do not know if the Call was only canceled when the request failed (https://github.com/square/okhttp/issues/3039). It is the responsibility of the Logger to check for canceled failed calls. The AnalyticsNetworkLogger is only a sample implementation of the logge…
/*
* Copyright (C) 2016 Eric Cochran
*
* 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
public final class AutoRetryCallAdapterFactory extends CallAdapter.Factory {
final ArrayList<Action<?>> actions = new ArrayList<>();
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean connected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!connected) {
return;
}
import android.graphics.Color;
public final class ColorUtils {
public static boolean useLightOnPrimaryColor(int color) {
return computeContrastBetweenColors(color, Color.WHITE) > 3f;
}
/**
* Copyright (C) 2014 The Android Open Source Project
* Calculates the contrast between two colors, using the algorithm provided by the WCAG v2.
/*
* Copyright (C) 2016 Eric Cochran
*
* 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
final class GuessGame {
public static void main(String[] args) {
GuessGame game = new GuessGame(new SimpleGame(13, 0, 100));
System.out.println(game.play());
}
private static final class SimpleGame implements GameSetup {
private final int known;
private final int lowerBound;
private final int upperBound;
@NightlyNexus
NightlyNexus / FlagView.java
Created August 28, 2016 06:51
A quickly made Android View to show an American flag.
/*
* Copyright (C) 2016 Eric Cochran
*
* 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
/*
* Copyright (C) 2016 Eric Cochran
*
* 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
@NightlyNexus
NightlyNexus / OkHttpStack.java
Created May 13, 2016 21:27
OkHttp 3 implementation of Volley's HttpStack
/*
* Copyright (C) 2016 Eric Cochran
*
* 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
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
public final class GridViewPager extends android.support.wearable.view.GridViewPager {
private WindowInsets insets = null;