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 {
class View {
Data data;
void setData(Data data) {
this.data = data;
setOnClickListener(v -> run(data));
}
}
class View {
Data data;
@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;
@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) {
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
@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
@NightlyNexus
NightlyNexus / TypeNames.java
Created March 13, 2018 09:02
A helper to add @generated to a JavaPoet's TypeSpec
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeSpec;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.lang.model.SourceVersion;
@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 / MarriageProblemSolver.kt
Created August 28, 2021 01:09
A Kotlin implementation of the Gale–Shapley algorithm for the stable marriage problem.
import MarriageProblemSolver.Person
class MarriageProblemSolver {
class Person(val name: String) {
internal lateinit var preferences: List<Person>
internal var proposals = 0
var match: Person? = null
internal set
// Do a lateinit to allow the circular dependency of people with their preferences of people.