Skip to content

Instantly share code, notes, and snippets.

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.
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;
}
@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
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 / AmazonS3RequestFactory.java
Created March 11, 2017 20:34
Creates okhttp3.Requests for uploading files to an Amazon S3 storage bucket. Implements https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import okio.BufferedSource;
import okio.ByteString;
class View {
Data data;
void setData(Data data) {
this.data = data;
setOnClickListener(v -> run(data));
}
}
class View {
Data data;
@NightlyNexus
NightlyNexus / ConverterCallAdapterFactory.java
Last active September 11, 2018 10:47
ConverterCallAdapterFactory uses Retrofit's CallAdapter to pass the okhttp3.Request to a serializer like Jsoup. Idea and corrections from https://github.com/square/retrofit/issues/2267/.
/*
* Copyright (C) 2017 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 / App.java
Last active August 6, 2017 16:49
Does this Dagger subcomponent need an empty module in the parent component?
import dagger.BindsInstance;
import dagger.Component;
import dagger.Subcomponent;
import javax.inject.Inject;
final class App {
private final Sub.Builder subBuilder;
private final AppDependency appDependency;
@Inject App(AppDependency appDependency, Sub.Builder subBuilder) {
@NightlyNexus
NightlyNexus / EnumWithDefaultValueJsonAdapter.java
Last active November 3, 2022 17:58
Update: This is now in the Moshi adapters artifact. https://github.com/square/moshi/blob/5153295988e09e6a1bfe76eecff8b22bf49e25de/adapters/src/main/java/com/squareup/moshi/adapters/EnumJsonAdapter.java An enum JsonAdapter for Moshi that allows for a fallback value when deserializing unknown strings. NOTE: Allows null for the default.
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.IOException;
public final class EnumWithDefaultValueJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
private final Class<T> enumType;
private final String[] nameStrings;
private final T[] constants;
@NightlyNexus
NightlyNexus / DelegatingEventListenerFactory.java
Last active January 26, 2018 08:10
A DelegatingEventListenerFactory for OkHttp to allow new OkHttpClient instances to add onto a base client's EventListener.Factory.
/*
* Copyright (C) 2018 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