Skip to content

Instantly share code, notes, and snippets.

View TWiStErRob's full-sized avatar

Róbert Papp TWiStErRob

View GitHub Profile
@TWiStErRob
TWiStErRob / gist:45c8e9937c5483191ba5
Created August 31, 2014 21:47
Check if Java environment hangs on to File descriptors when using FileOutputStream
public static boolean isFOSHoldingOnToFile() {
return !canDeleteCreatedFile() || !canRenameCreatedFile();
}
public static boolean canDeleteCreatedFile() {
File temp = null;
try {
temp = File.createTempFile("canDeleteCreatedFile", null, Robolectric.application.getCacheDir());
if(!temp.delete()) {
return false;
@TWiStErRob
TWiStErRob / ExternalCacheDiskCacheFactory.java
Last active August 29, 2015 14:19
Glide DiskCache.Factory implementation to use the context-provided externalCacheDir
import java.io.File;
import android.content.Context;
import com.bumptech.glide.load.engine.cache.*;
public final class ExternalCacheDiskCacheFactory implements DiskCache.Factory {
private final Context context;
private final String diskCacheName;
private final int diskCacheSize;
public ExternalCacheDiskCacheFactory(Context context, int diskCacheSize) {
@TWiStErRob
TWiStErRob / DrawableDrawableResoureDecoder.java
Last active August 29, 2015 14:20
Glide 3.6.0 proof of concept for loading a Drawable as Drawable through Glide
class DrawableResoureDecoder implements ResourceDecoder<Drawable, Drawable> {
@Override public Resource<Drawable> decode(Drawable source, int width, int height) throws IOException {
return new DrawableResource<Drawable>(source) {
@Override public int getSize() { return 1; }
@Override public void recycle() { }
};
}
@Override public String getId() { return "DrawableDrawableResourceDecoder"; }
}
@TWiStErRob
TWiStErRob / DrawableBitmapResourceDecoder.java
Last active October 24, 2016 15:15
Glide 3.6.0 proof of concept for loading a Drawable as Bitmap through Glide
class DrawableBitmapResourceDecoder implements ResourceDecoder<Drawable, Bitmap> {
private final BitmapPool pool;
public DrawableBitmapResourceDecoder(Context context) {
this(Glide.get(context).getBitmapPool());
}
public DrawableBitmapResourceDecoder(BitmapPool pool) {
this.pool = pool;
}
@Override public Resource<Bitmap> decode(Drawable drawable, int width, int height) throws IOException {
@TWiStErRob
TWiStErRob / BitmapBitmapResourceDecoder.java
Last active February 5, 2024 21:36
Glide 3.6.0 proof of concept for loading a Bitmap as Bitmap through Glide
class BitmapBitmapResourceDecoder implements ResourceDecoder<Bitmap, Bitmap> {
private final BitmapPool pool;
public BitmapBitmapResourceDecoder(Context context) {
this(Glide.get(context).getBitmapPool());
}
public BitmapBitmapResourceDecoder(BitmapPool pool) {
this.pool = pool;
}
@Override public Resource<Bitmap> decode(Bitmap source, int width, int height) throws IOException {
@TWiStErRob
TWiStErRob / SquareFrameLayout.java
Created September 30, 2015 23:33
Make GridView or GridLayoutManager items square.
package net.twisterrob.android.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(Context context) {
super(context);
}
@TWiStErRob
TWiStErRob / LimitOutput.java
Created November 26, 2015 19:00
LeetCode helper methods for console debugging
static {
System.setOut(new java.io.PrintStream(new java.io.FilterOutputStream(System.out) {
int count = 10 * 1024;
@Override public void write(int b) throws IOException {
if (count > 0) {
super.write(b);
--count;
}
}
@Override public void write(byte[] b, int off, int len) throws IOException {
@TWiStErRob
TWiStErRob / TravelingSalesmanHeldKarpX.java
Last active December 31, 2015 01:36
Programmatic version of Tushar Roy's Held-Karp TSP video (https://www.youtube.com/watch?v=-JjA4BLQyqE)
package problems.tushar;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.*;
import static problems.tushar.TravelingSalesmanHeldKarpX.Algo.*;
import static problems.tushar.TravelingSalesmanHeldKarpX.Utils.*;
/**
@TWiStErRob
TWiStErRob / OkHttpProgressGlideModule.java
Last active January 31, 2024 13:37
Full POC for showing progress of loading in Glide v3 via OkHttp v2
// TODO add <meta-data android:value="GlideModule" android:name="....OkHttpProgressGlideModule" />
// TODO add <meta-data android:value="GlideModule" tools:node="remove" android:name="com.bumptech.glide.integration.okhttp.OkHttpGlideModule" />
// or not use 'okhttp@aar' in Gradle depdendencies
public class OkHttpProgressGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) { }
@Override public void registerComponents(Context context, Glide glide) {
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(createInterceptor(new DispatchingProgressListener()));
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
@TWiStErRob
TWiStErRob / README.md
Last active January 11, 2017 07:03
Zoom to fit

See SO answer.

You can pan (click background), zoom (mousewheel), drag nodes around. Middle click to add more nodes (at mouse).