Android – Serve Local Video to Chromecast
There are multiple use-cases for an embedded HTTP Server in an Android app, not the least of which is a hi-fidelity testing fake. Here's an example of how we adopted ServerSocket for use in sending downloaded, encrypted video streams to Chromecast by simply passing an HTTP Url stream. First, we needed some simple HTTP classes […]
Android – CloneView

To visually clone another view. This does not make a functional copy of the view – no hit areas or other behavior or state is cloned. This is a simple View instance that draws the pixels of the source `View` and positions them appropriately. package com.moagrius.widgets; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; /** […]

Android – Use callbacks to handle Exceptions

Just a simple alternative to specific try/catch blocks. public class Safely { public static void handle(Attempter attempter, Handler handler) { try { attempter.attempt(); } catch (Exception e) { if (handler != null) { handler.onException(e); } } } public static void handle(Attempter attempter) { handle(attempter, null); } public interface Attempter { void attempt(); } public interface […]

Android – Use lambda or function reference for BroadcastReceiver

public class ReceiverFactory { public static BroadcastReceiver create(BroadcastHandler handler) { return new Receiver(handler); } public interface BroadcastHandler { void onReceive(Context context, Intent intent); } private static class Receiver { private BroadcastHandler mHandler; private Receiver(@NonNull BroadcastHandler handler) { mHandler = handler; } @Override public void onReceive(Context context, Intent intent) { mHandler.onReceive(context, intent); } } } E.g., […]

Android – Simulate application resource reclamation

In android, when resources are low, the device may kill an application (usually in the background) to re-allocate those resources to the app the user is interacting with at the moment. This is a different process than force-stopping or killing the application, both of which are easy to simulate with ADB commands: adb shell am […]

Android – Throttle and Debounce

Throttle: one submission every {interval}, won’t start a new task until elapsed time has passed. Debounce: one submission every {interval}, will cancel all pending tasks to schedule a new one. Both are instantiated with and the only public API is attempt(Runnable), where Runnable is the task to be queued. In practical terms: int value = […]

Android – Application object as Server (instances instead of singletons)

Singletons have drawbacks. This is an old argument and I won’t get into it, but if you find static classes or Singletons are serving large portions of your app, here’s an alternative approach I’ve found success with. This is in directly violation of DI and I understand there are valid arguments against it, but at […]

Applying a Paint (including ColorFilters and ColorMatrix) to a View

Stackoverflow has a bunch of examples of how to apply a ColorFilter (and therefore a ColorMatrix) to a bitmap, or an ImageView (which has a built-in API), but applying color effects to an entire View are not nearly as well documented. The basic idea is to use your ColorMatrix instances in ColorFilters, then wrap that […]

Java – simple HashCode generator

/** * Created by Mike Dunn on 5/31/15. */ public class HashCode { public static int calculate(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object… members) { for (Object member : members) { initialNonZeroOddNumber = multiplierNonZeroOddNumber * initialNonZeroOddNumber + getHashCode(member); } return initialNonZeroOddNumber; } private static int getHashCode(Object object) { if (object == null) { return 0; } return […]

Android – pass touch events to a child view

AOSP offers a delegate mechanism to modify the touchable area of Views, but this may not always behave as expected. A common use-case is having a View shown within a constrained area, but wanted to react to scrolling gestures from anywhere on the screen – e.g., imagine a RecyclerView on a landscape tablet that design […]