Search in sources :

Example 36 with WeakReference

use of java.lang.ref.WeakReference in project cglib by cglib.

the class Enhancer method wrapCachedClass.

@Override
protected Object wrapCachedClass(Class klass) {
    Class[] argumentTypes = this.argumentTypes;
    if (argumentTypes == null) {
        argumentTypes = Constants.EMPTY_CLASS_ARRAY;
    }
    EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly);
    Field factoryDataField = null;
    try {
        // The subsequent dance is performed just once for each class,
        // so it does not matter much how fast it goes
        factoryDataField = klass.getField(FACTORY_DATA_FIELD);
        factoryDataField.set(null, factoryData);
        Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD);
        callbackFilterField.setAccessible(true);
        callbackFilterField.set(null, this.filter);
    } catch (NoSuchFieldException e) {
        throw new CodeGenerationException(e);
    } catch (IllegalAccessException e) {
        throw new CodeGenerationException(e);
    }
    return new WeakReference<EnhancerFactoryData>(factoryData);
}
Also used : Field(java.lang.reflect.Field) WeakReference(java.lang.ref.WeakReference)

Example 37 with WeakReference

use of java.lang.ref.WeakReference in project Talon-for-Twitter by klinker24.

the class ImageUtils method imageUrlAsyncTask.

private static void imageUrlAsyncTask(final Context context, final ImageView imageView, final BitmapLruCache mCache, final boolean profile, final String url) {
    final WeakReference<ImageView> mImageViewRef = new WeakReference<ImageView>(imageView);
    Thread imageDownload = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // Return early if the ImageView has disappeared.
                if (null == mImageViewRef.get()) {
                    return;
                }
                // Now we're not on the main thread we can check all caches
                CacheableBitmapDrawable result;
                result = mCache.get(url, null);
                if (null == result || profile) {
                    String mUrl = url;
                    if (url.contains("twitpic")) {
                        try {
                            URL address = new URL(url);
                            HttpURLConnection connection = (HttpURLConnection) address.openConnection(Proxy.NO_PROXY);
                            connection.setConnectTimeout(1000);
                            connection.setInstanceFollowRedirects(false);
                            connection.setReadTimeout(1000);
                            connection.connect();
                            String expandedURL = connection.getHeaderField("Location");
                            if (expandedURL != null) {
                                mUrl = expandedURL;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    // The bitmap isn't cached so download from the web
                    HttpURLConnection conn = (HttpURLConnection) new URL(mUrl).openConnection();
                    InputStream is = new BufferedInputStream(conn.getInputStream());
                    Bitmap b = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);
                    try {
                        is.close();
                    } catch (Exception e) {
                    }
                    try {
                        conn.disconnect();
                    } catch (Exception e) {
                    }
                    // Add to cache
                    try {
                        result = mCache.put(mUrl, b);
                    } catch (Exception e) {
                        result = null;
                    }
                }
                final CacheableBitmapDrawable fResult = result;
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            final ImageView iv = mImageViewRef.get();
                            if (null != iv && iv.getVisibility() != View.GONE) {
                                iv.setImageDrawable(fResult);
                                Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
                                iv.startAnimation(fadeInAnimation);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (IOException e) {
                Log.e("ImageUrlAsyncTask", e.toString());
            } catch (OutOfMemoryError e) {
                Log.v("ImageUrlAsyncTask", "Out of memory error here");
            } catch (Exception e) {
                // something else
                e.printStackTrace();
            }
        }
    });
    imageDownload.setPriority(8);
    imageDownload.start();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) WeakReference(java.lang.ref.WeakReference) Animation(android.view.animation.Animation) CacheableBitmapDrawable(uk.co.senab.bitmapcache.CacheableBitmapDrawable) ImageView(android.widget.ImageView)

Example 38 with WeakReference

use of java.lang.ref.WeakReference in project Klyph by jonathangerbaud.

the class FriendRequestService method onCreate.

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, new WeakReference<Service>(this));
}
Also used : HandlerThread(android.os.HandlerThread) WeakReference(java.lang.ref.WeakReference)

Example 39 with WeakReference

use of java.lang.ref.WeakReference in project Klyph by jonathangerbaud.

the class NotificationService method onCreate.

@Override
public void onCreate() {
    // Why initialize device values ?
    // Because we need the density to calculate image size
    // In notification request
    KlyphDevice.initDeviceValues(this);
    HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, new WeakReference<Service>(this));
}
Also used : HandlerThread(android.os.HandlerThread) WeakReference(java.lang.ref.WeakReference)

Example 40 with WeakReference

use of java.lang.ref.WeakReference in project Klyph by jonathangerbaud.

the class UploadPhotoService method onCreate.

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, new WeakReference<Service>(this));
}
Also used : HandlerThread(android.os.HandlerThread) WeakReference(java.lang.ref.WeakReference)

Aggregations

WeakReference (java.lang.ref.WeakReference)291 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)24 Map (java.util.Map)24 Field (java.lang.reflect.Field)19 Activity (android.app.Activity)17 Iterator (java.util.Iterator)17 IOException (java.io.IOException)16 Method (java.lang.reflect.Method)15 File (java.io.File)14 URLClassLoader (java.net.URLClassLoader)14 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Resources (android.content.res.Resources)10 Handler (android.os.Handler)10 AbstractModule (com.google.inject.AbstractModule)10 Injector (com.google.inject.Injector)10 ArrayMap (android.util.ArrayMap)9 URL (java.net.URL)8