Skip to content

Instantly share code, notes, and snippets.

@keyboardr
keyboardr / HeadlessFragment.java
Last active December 31, 2021 01:05
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.
#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();
@keyboardr
keyboardr / FirebaseLiveData.java
Created November 19, 2017 10:41
A LiveData sourced from Firebase Realtime Database
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;
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
@keyboardr
keyboardr / ParcelableUtils.java
Last active August 13, 2020 13:38
Utility class for dealing with Parcelables. Handles null checks for objects, and parcels some common non-Parcelable classes
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Parcel;
import android.os.Parcelable;
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
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 / FragmentUtils.java
Last active March 8, 2019 05:30
Utility method for getting the appropriate parent of a Fragment for a given callback interface.
import android.support.v4.app.Fragment;
public class FragmentUtils {
/**
* @param fragment
* The Fragment whose parent is to be found
* @param parentClass
* The interface that the parent should implement
* @return The parent of fragment that implements parentClass,
@keyboardr
keyboardr / ContentListFragment.java
Last active January 6, 2018 23:37
Demonstrate how to use the empty view to show progress indication and handle empty state. Note: if using a normal Fragment, you should call ListView.setEmptyView() to assign the empty view to the ListView.
public class ContentListFragment extends ListFragment implements LoaderCallbacks<List<Content>>{
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
getLoaderManager().initLoader(0, null, this);
@keyboardr
keyboardr / MusicKey.java
Last active October 15, 2017 11:47
A enum to parse and represent a music key
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 December 8, 2016 01:54
Demonstration of why the HeadlessFragment gist can't be done as an abstract Factory class
/**
* 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