Skip to content

Instantly share code, notes, and snippets.

View Assertions.kt
package com.keyboardr.testing
import com.google.common.truth.*
import com.google.common.truth.Truth.assertWithMessage
import com.google.common.truth.Truth.assert_
import java.math.BigDecimal
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
View LoudnessMeasurer.kt
package com.keyboardr.bluejay.audio
import com.keyboardr.bluejay.util.toFraction
import java.nio.ByteBuffer
import kotlin.math.*
class LoudnessMeasurer(private val channelCount: Int, sampleRate: Float, totalFrames: Int) {
private class LoudnessMeasurerChannel(var bufferFrames: Int,
/** How many frames are needed for a gating block. Will correspond to 400ms
View WaveformView.kt
package com.keyboardr.bluejay.ui.playlist
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.media.MediaCodec
import android.media.MediaExtractor
import android.media.MediaFormat
import android.util.AttributeSet
@keyboardr
keyboardr / FirebaseLiveData.java
Created Nov 19, 2017
A LiveData sourced from Firebase Realtime Database
View FirebaseLiveData.java
import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ValueEventListener;
@keyboardr
keyboardr / MusicKey.java
Last active Oct 15, 2017
A enum to parse and represent a music key
View MusicKey.java
package com.keyboardr.bluejay.model;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
@keyboardr
keyboardr / HeadlessFragmentFactory.java
Created Dec 8, 2016
Demonstration of why the HeadlessFragment gist can't be done as an abstract Factory class
View HeadlessFragmentFactory.java
/**
* What you'd think an abstract factory would look like. Unfortunately, this class won't compile.
*/
public abstract class HeadlessFragmentFactory<F extends Fragment & ParentedFragment<F, ? extends P>, P> {
protected abstract F newInstance();
/* There will be a compiler warning on P. When joining types, only the first type is allowed to
be a class. All others MUST be interfaces. In this situation, there is no way for the compiler
to know that P will be an interface. If P is removed, there is no longer a compile-time check
View CursorReaderExample.java
import android.support.annotation.WorkerThread;
import android.support.annotation.Nullable;
import android.database.Cursor;
public class CursorReaderExample {
// Example columns, would likely be in a different file (e.g. a contract class).
private static String COLUMN_FOO = "foo";
private static String COLUMN_BAR = "bar";
@keyboardr
keyboardr / gist:572a5eae9f57be286c24
Created Jul 7, 2015
Centered circular reveal animator
View gist:572a5eae9f57be286c24
@TargetApi(VERSION_CODES.LOLLIPOP)
private Animator createCenteredReveal(View view) {
// Could optimize by reusing a temporary Rect instead of allocating a new one
Rect bounds = new Rect();
view.getDrawingRect(bounds);
int centerX = bounds.centerX();
int centerY = bounds.centerY();
int finalRadius = Math.max(bounds.width(), bounds.height());
return ViewAnimationUtils.createCircularReveal(view, centerX, centerY, 0f, finalRadius);
}
@keyboardr
keyboardr / HeadlessFragment.java
Last active Apr 8, 2021
A file template for creating a headless Fragment. The attach() methods add the fragment to the parent if it doesn't already exist and returns the attached fragment. The methods ensure that the parent implements the parent interface. If needed, parameters may be added to the attach() methods to supply fragment arguments.
View HeadlessFragment.java
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
#parse("File Header.java")
public class ${NAME} extends Fragment {
private static final String FRAG_TAG = ${NAME}.class.getCanonicalName();
View UriDrawable.java
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.util.Log;
import com.squareup.picasso.Picasso;