Skip to content

Instantly share code, notes, and snippets.

@tonykwok
tonykwok / CameraDumper.java
Last active May 4, 2016 01:26
A utility to check if camera device is being used by any application or not, if so, package name of the application will be returned.
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.os.Debug;
import java.io.File;
public class OomExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String FILENAME = "out-of-memory.hprof";
public static void install(Context context) {
Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
@tonykwok
tonykwok / Main.java
Last active May 4, 2016 01:24
Don't leave the previous camera preview hanging on the screen, that looks weird if the aspect ratio or scene changes.
// We need to do this with OpenGL ES (*not* Canvas -- the "software render" bits
// are sticky). We can't stay connected to the Surface after we're done because
// that will prevent the camera from attaching.
// @param surface should be either Surface or SurfaceTexture
public static void clear(object surface) {
try {
if (Log.IS_DEBUG) Log.logDebug(TAG, "clear() using OpenGL ES: E");
WindowSurface windowSurface = new WindowSurface(surface);
windowSurface.makeCurrent();
GLES20.glClearColor(0, 0, 0, 0);
@tonykwok
tonykwok / ps.txt
Last active September 14, 2016 08:18
Android commands
adb shell ps | grep <PACKAGE.NAME>
adb shell cat /proc/<PID>/status
adb shell ps --abi
@tonykwok
tonykwok / ask.sh
Created July 30, 2018 01:58
Bash General-Purpose Yes/No Prompt Function ("ask")
# This is a general-purpose function to ask Yes/No questions in Bash, either
# with or without a default answer. It keeps repeating the question until it
# gets a valid answer.
ask() {
# https://djm.me/ask
local prompt default reply
while true; do
@tonykwok
tonykwok / adb-wrapper.sh
Created July 30, 2018 07:45 — forked from elsnosrap/adb-wrapper.sh
A useful shell script that wraps Android adb commands when multiple devices or emulators are connected. The script will prompt for a device or emulator to run the command against, if it detects multiple devices / emulators.
#!/bin/bash
# This is a wrapper for adb. If there are multiple devices / emulators, this script will prompt for which device to use
# Then it'll pass whatever commands to that specific device or emulator.
# Run adb devices once, in event adb hasn't been started yet
BLAH=$(adb devices)
# Grab the IDs of all the connected devices / emulators
IDS=($(adb devices | sed '1,1d' | sed '$d' | cut -f 1 | sort))
@tonykwok
tonykwok / .md
Created August 10, 2019 08:09 — forked from rahulrajaram/.md
Python: Write to a file from multiple threads

I recently came across the need to spawn multiple threads, each of which needs to write to the same file. Since the file will experience contention from multiple resources, we need to guarantee thread-safety.

NOTE: The following examples work with Python 3.x. To execute the following programs using Python 2.7, please replace threading.get_ident() with thread.get_ident(). As a result, you would need to import thread and not threading.

  1. (The following example will take a very long time). It will create 200 threads, each of which will wait until a global lock is available for acquisition.
# threading_lock.py
import threading
#!/usr/bin/env python3
'''
A script to recursively compare two directories (including file size and file hash changes)
Usage: python3 compare_dirs.py DIR1 DIR2
'''
import os, sys, hashlib
COMPARE_FILES = True # should file sizes be compared if their names are the same?
@tonykwok
tonykwok / cmd.py
Created August 23, 2019 09:06 — forked from dhrrgn/cmd.py
Running a command in Python and optionally processing the output in realtime.
import shlex
import subprocess
import sys
def run_cmd(cmd, callback=None, watch=False):
"""Runs the given command and gathers the output.
If a callback is provided, then the output is sent to it, otherwise it
is just returned.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import signal
def my_handler():
print('Stopping')
for task in asyncio.Task.all_tasks():
task.cancel()