Example usage for java.lang.ref WeakReference WeakReference

List of usage examples for java.lang.ref WeakReference WeakReference

Introduction

In this page you can find the example usage for java.lang.ref WeakReference WeakReference.

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:Main.java

public static synchronized Thread createTrackThread(Runnable run) {
    Thread t = new Thread(run);
    threadList.add(new WeakReference<>(t));
    return t;/*from   ww w  .  j a v  a  2  s .  com*/
}

From source file:Main.java

public static void add(Activity acitivity) {
    activitys.add(new WeakReference(acitivity));
}

From source file:Main.java

public static void setContext(Context c, Activity a) {
    context = c;
    activity = new WeakReference<Activity>(a);
}

From source file:Main.java

public static WeakReference<Bitmap> rotateBitmap(WeakReference<Bitmap> bitmap, int degress) {
    if (bitmap == null || bitmap.get() == null)
        return null;
    Matrix m = new Matrix();
    m.postRotate(degress);//from w w  w .j  av a 2  s . c  om
    return new WeakReference<Bitmap>(Bitmap.createBitmap(bitmap.get(), 0, 0, bitmap.get().getWidth(),
            bitmap.get().getHeight(), m, true));
}

From source file:Main.java

/**
 * Questo metodo statico serve per tracciare un ExecutorService  per la chiusura
 * @param exs ExecutorService da tracciare
 * @return l'ExecutorService tracciato/*from ww  w .j a v a2  s.  c  o  m*/
 */
public synchronized static ExecutorService TrackExecutorService(ExecutorService exs) {
    executorList.add(new WeakReference<ExecutorService>(exs));
    return exs;
}

From source file:Main.java

private static WeakReference<Bitmap> cprsBmpBySize(String path, int rqsW, int rqsH) {
    if (TextUtils.isEmpty(path))
        return null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// w w  w. j  a  v a2s . c  o  m
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = caculateInSampleSize(options, rqsW, rqsH);
    options.inJustDecodeBounds = false;
    return new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, options));
}

From source file:Main.java

public static BufferedImage getUIImage(Class c, String name) {

    try {//  w w w  .j  a  v a 2s  .  c  o  m
        String id = c + " - " + name;
        WeakReference wr = (WeakReference) UIImagesReferences.get(id);

        if (wr != null) {
            BufferedImage img = (BufferedImage) wr.get();
            if (img != null) {
                return img;
            }
        }
        BufferedImage img = ImageIO.read(c.getResource(name));

        UIImagesReferences.put(id, new WeakReference(img));

        return img;
    } catch (Exception e) {
        throw new RuntimeException("error loading " + c + " " + name, e);
    }

}

From source file:Main.java

/**
 * Execute an {@link AsyncTask} on a thread pool
 *
 * @param <T> Task argument type//from  w ww.ja v  a  2 s.  com
 * @param forceSerial True to force the task to run in serial order
 * @param task Task to execute
 * @param args Optional arguments to pass to
 *            {@link AsyncTask#execute(Object[])}
 */
@SuppressLint("NewApi")
public static <T> WeakReference<AsyncTask<T, ?, ?>> execute(final boolean forceSerial,
        final AsyncTask<T, ?, ?> task, final T... args) {
    final WeakReference<AsyncTask<T, ?, ?>> taskReference = new WeakReference<AsyncTask<T, ?, ?>>(task);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) {
        throw new UnsupportedOperationException("This class can only be used on API 4 and newer.");
    }

    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || forceSerial) {
            taskReference.get().execute(args);
        } else {
            taskReference.get().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return taskReference;
}

From source file:Main.java

static public boolean isHardwareAccelerated(Canvas canvas) {
    // Return cached value, faster than use reflection
    if (sLastCanvas != null && sLastCanvas.get() == canvas) {
        return sLastCanvasIsHardwareAccelerated;
    }/*from  ww w  .ja v  a 2s  .  c  o  m*/

    sLastCanvasIsHardwareAccelerated = false;
    if (methodIsHardwareAccelerated != null) {
        try {
            Object result = methodIsHardwareAccelerated.invoke(canvas);
            sLastCanvasIsHardwareAccelerated = (Boolean) result;
        } catch (Throwable t) {
        }
    }
    if (IS_HARDWARE_ACCELERATED_FAST_CALL)
        sLastCanvas = new WeakReference<Canvas>(canvas);
    return sLastCanvasIsHardwareAccelerated;
}

From source file:Main.java

/**
 * Execute an {@link AsyncTask} on a thread pool
 * /*  w w w . j  a  v a  2s. co m*/
 * @param forceSerial True to force the task to run in serial order
 * @param task Task to execute
 * @param args Optional arguments to pass to
 *            {@link AsyncTask#execute(Object[])}
 * @param <T> Task argument type
 */
@SuppressLint("NewApi")
public static <T> void execute(final boolean forceSerial, final AsyncTask<T, ?, ?> task, final T... args) {
    final WeakReference<AsyncTask<T, ?, ?>> taskReference = new WeakReference<AsyncTask<T, ?, ?>>(task);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) {
        throw new UnsupportedOperationException("This class can only be used on API 4 and newer.");
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || forceSerial) {
        taskReference.get().execute(args);
    } else {
        taskReference.get().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
    }
}