Search in sources :

Example 91 with WeakReference

use of java.lang.ref.WeakReference in project android_frameworks_base by ResurrectionRemix.

the class ImageReader method postEventFromNative.

/**
     * Called from Native code when an Event happens.
     *
     * This may be called from an arbitrary Binder thread, so access to the ImageReader must be
     * synchronized appropriately.
     */
private static void postEventFromNative(Object selfRef) {
    @SuppressWarnings("unchecked") WeakReference<ImageReader> weakSelf = (WeakReference<ImageReader>) selfRef;
    final ImageReader ir = weakSelf.get();
    if (ir == null) {
        return;
    }
    final Handler handler;
    synchronized (ir.mListenerLock) {
        handler = ir.mListenerHandler;
    }
    if (handler != null) {
        handler.sendEmptyMessage(0);
    }
}
Also used : WeakReference(java.lang.ref.WeakReference) Handler(android.os.Handler)

Example 92 with WeakReference

use of java.lang.ref.WeakReference in project android_frameworks_base by ResurrectionRemix.

the class ThemedResourceCache method getThemedLocked.

/**
     * Returns the cached data for the specified theme, optionally creating a
     * new entry if one does not already exist.
     *
     * @param t the theme for which to return cached data
     * @param create {@code true} to create an entry if one does not already
     *               exist, {@code false} otherwise
     * @return the cached data for the theme, or {@code null} if the cache is
     *         empty and {@code create} was {@code false}
     */
@Nullable
private LongSparseArray<WeakReference<T>> getThemedLocked(@Nullable Theme t, boolean create) {
    if (t == null) {
        if (mNullThemedEntries == null && create) {
            mNullThemedEntries = new LongSparseArray<>(1);
        }
        return mNullThemedEntries;
    }
    if (mThemedEntries == null) {
        if (create) {
            mThemedEntries = new ArrayMap<>(1);
        } else {
            return null;
        }
    }
    final ThemeKey key = t.getKey();
    LongSparseArray<WeakReference<T>> cache = mThemedEntries.get(key);
    if (cache == null && create) {
        cache = new LongSparseArray<>(1);
        final ThemeKey keyClone = key.clone();
        mThemedEntries.put(keyClone, cache);
    }
    return cache;
}
Also used : ThemeKey(android.content.res.Resources.ThemeKey) WeakReference(java.lang.ref.WeakReference) Nullable(android.annotation.Nullable)

Example 93 with WeakReference

use of java.lang.ref.WeakReference in project android_frameworks_base by ResurrectionRemix.

the class SparseWeakArray method put.

/**
     * Adds a mapping from the specified key to the specified value,
     * replacing the previous mapping from the specified key if there
     * was one.
     */
public void put(long key, E value) {
    int i = binarySearch(mKeys, 0, mSize, key);
    if (i >= 0) {
        mValues[i] = new WeakReference(value);
    } else {
        i = ~i;
        if (i < mSize && (mValues[i] == DELETED || mValues[i].get() == null)) {
            mKeys[i] = key;
            mValues[i] = new WeakReference(value);
            return;
        }
        if (mSize >= mKeys.length && (mGarbage || hasReclaimedRefs())) {
            gc();
            // Search again because indices may have changed.
            i = ~binarySearch(mKeys, 0, mSize, key);
        }
        mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
        mValues = GrowingArrayUtils.insert(mValues, mSize, i, new WeakReference(value));
        mSize++;
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Example 94 with WeakReference

use of java.lang.ref.WeakReference in project Entitas-Java by Rubentxu.

the class TypeResolver method getTypeVariableMap.

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType, Class<?> functionalInterface) {
    Reference<Map<TypeVariable<?>, Type>> ref = TYPE_VARIABLE_CACHE.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;
    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();
        // Populate lambdas
        if (functionalInterface != null)
            populateLambdaArgs(functionalInterface, targetType, map);
        // Populate interfaces
        populateSuperTypeArgs(targetType.getGenericInterfaces(), map, functionalInterface != null);
        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, false);
            populateSuperTypeArgs(type.getGenericInterfaces(), map, false);
            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }
        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, functionalInterface != null);
            type = type.getEnclosingClass();
        }
        if (CACHE_ENABLED)
            TYPE_VARIABLE_CACHE.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }
    return map;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) WeakReference(java.lang.ref.WeakReference) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 95 with WeakReference

use of java.lang.ref.WeakReference in project Presentation by StanKocken.

the class BaseAppCompatActivityTest method testShouldWeakReferenceOnPresenter.

@Test
public void testShouldWeakReferenceOnPresenter() {
    BasePresenter basePresenter = new BasePresenter(null, null) {
    };
    final WeakReference<BasePresenter> weakReferencePresenter = new WeakReference<>(basePresenter);
    BaseAppCompatActivity baseActivity = new TestBaseAppCompatActivity() {

        @Override
        protected BasePresenter newPresenter() {
            return weakReferencePresenter.get();
        }
    };
    baseActivity.onCreate(null);
    assertEquals(basePresenter, baseActivity.getPresenter());
    basePresenter = null;
    for (int i = 0; i < 10 && weakReferencePresenter.get() != null; i++) {
        System.gc();
    }
    assertNull(basePresenter);
    assertNull(weakReferencePresenter.get());
    assertNull(baseActivity.getPresenter());
}
Also used : WeakReference(java.lang.ref.WeakReference) BasePresenter(com.skocken.presentation.presenter.BasePresenter) Test(org.junit.Test)

Aggregations

WeakReference (java.lang.ref.WeakReference)277 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Field (java.lang.reflect.Field)18 Activity (android.app.Activity)17 Method (java.lang.reflect.Method)15 File (java.io.File)14 IOException (java.io.IOException)13 URLClassLoader (java.net.URLClassLoader)13 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Iterator (java.util.Iterator)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