Skip to content

Instantly share code, notes, and snippets.

package com.firebase.utils;
import java.util.Date;
/**
* Fancy ID generator that creates 20-character string identifiers with the
* following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't
@vaibhav-jani
vaibhav-jani / ActivityGroupSample.java
Created June 15, 2016 06:02
Multiple activities in samescreen.
LocalActivityManager mgr = getLocalActivityManager();
Intent i = new Intent(this, SomeActivity.class);
Window w = mgr.startActivity("unique_per_activity_string", i);
View wd = w != null ? w.getDecorView() : null;
if(wd != null) {
mSomeContainer.addView(wd);
}
@vaibhav-jani
vaibhav-jani / Logger.java
Created July 4, 2016 08:01 — forked from anonymous/Logger.java
Android Log wrapper
import android.content.Context;
import android.util.Log;
public class Logger {
private static final boolean shouldLog = true;
public static void i(String tag, String msg) {
@vaibhav-jani
vaibhav-jani / AnimateViewSnippet.java
Created August 29, 2016 10:24
Animate from one position to another
int x = targetView.getTop() + targetView.getWidth() / 2;
int y = targetView.getTop() + targetView.getHeight() / 2;
int fromX = sourceView.getTop() + sourceView.getWidth() / 2;
int fromY = sourceView.getTop() + sourceView.getHeight() / 2;
final int dx = x - fromX;
final int dy = y - fromY;
@vaibhav-jani
vaibhav-jani / HelloWorld.java
Last active September 10, 2016 09:40
ANT build sample.
//In src folder
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
/**
private static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
@vaibhav-jani
vaibhav-jani / DateFormatterDemo.java
Last active August 2, 2017 11:34
Java date format quickey
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
public class DateFormatterDemo {
public static void main(String[] args) throws Exception {
@vaibhav-jani
vaibhav-jani / ScalableThreadPoolExecutor.java
Created August 15, 2018 11:16 — forked from mnadeem/ScalableThreadPoolExecutor.java
Salable thread pool executor (TPE) which first creates threads up to maximum pool size then queue up the work (Queue depends upon Executor)
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TransferQueue;
import java.util.concurrent.locks.ReentrantLock;
public final class ScalableThreadPoolExecutor extends ThreadPoolExecutor {
@vaibhav-jani
vaibhav-jani / Levenshtein distance in java
Created January 24, 2019 10:47
Levenshtein distance in java
// Find the Levenshtein distance
private int distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
int[] costs = new int[b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
costs[0] = i;