Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 43cd1ddc authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Prune the resource cache before adding new caches"

parents c4e9215f 9267fda3
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -2425,6 +2425,9 @@ public class Resources {
                final String themeKey = theme == null ? "" : theme.mKey;
                LongSparseArray<WeakReference<ConstantState>> themedCache = caches.get(themeKey);
                if (themedCache == null) {
                    // Clean out the caches before we add more. This shouldn't
                    // happen very often.
                    pruneCaches(caches);
                    themedCache = new LongSparseArray<WeakReference<ConstantState>>(1);
                    caches.put(themeKey, themedCache);
                }
@@ -2433,6 +2436,40 @@ public class Resources {
        }
    }

    /**
     * Prunes empty caches from the cache map.
     *
     * @param caches The map of caches to prune.
     */
    private void pruneCaches(ArrayMap<String,
            LongSparseArray<WeakReference<ConstantState>>> caches) {
        final int N = caches.size();
        for (int i = N - 1; i >= 0; i--) {
            final LongSparseArray<WeakReference<ConstantState>> cache = caches.get(i);
            if (pruneCache(cache)) {
                caches.removeAt(i);
            }
        }
    }

    /**
     * Prunes obsolete weak references from a cache, returning {@code true} if
     * the cache is empty and should be removed.
     *
     * @param cache The cache of weak references to prune.
     * @return {@code true} if the cache is empty and should be removed.
     */
    private boolean pruneCache(LongSparseArray<WeakReference<ConstantState>> cache) {
        final int N = cache.size();
        for (int i = N - 1; i >= 0; i--) {
            final WeakReference entry = cache.valueAt(i);
            if (entry.get() == null) {
                cache.removeAt(i);
            }
        }
        return cache.size() == 0;
    }

    /**
     * Loads a drawable from XML or resources stream.
     */