Skip to content

Instantly share code, notes, and snippets.

@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 / 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;
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 {
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ProgressBar;
public class TintedProgressBar extends ProgressBar {
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