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

Commit 611b380c authored by sangyun's avatar sangyun
Browse files

Change the resource cache key from context to package name.

Multiple contexts can be created in one application, and redundant
caches can be accumulated if resources are cached by each context.
Change the one of resource cache key from context to package name
to avoid stacking caches.

Bug: 289045241
Test: atest CtsTelephonyTestCases:SubscriptionManagerTest
Change-Id: I0d4b17b1157d6f453213de864d00beffc2d628c4
parent d2b36a15
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -1306,8 +1306,11 @@ public class SubscriptionManager {
    private final Context mContext;

    // Cache of Resource that has been created in getResourcesForSubId. Key is a Pair containing
    // the Context and subId.
    private static final Map<Pair<Context, Integer>, Resources> sResourcesCache =
    // the Package Name and subId. Applications can create new contexts from
    // {@link android.content.Context#createPackageContext} with the same resources for different
    // purposes. Therefore, Cache can be wasted for resources from different contexts in the same
    // package. Use the package name rather than the context itself as a key value of cache.
    private static final Map<Pair<String, Integer>, Resources> sResourcesCache =
            new ConcurrentHashMap<>();

    /**
@@ -2774,12 +2777,13 @@ public class SubscriptionManager {
            boolean useRootLocale) {
        // Check if resources for this context and subId already exist in the resource cache.
        // Resources that use the root locale are not cached.
        Pair<Context, Integer> cacheKey = null;
        Pair<String, Integer> cacheKey = null;
        if (isValidSubscriptionId(subId) && !useRootLocale) {
            cacheKey = Pair.create(context, subId);
            if (sResourcesCache.containsKey(cacheKey)) {
            cacheKey = Pair.create(context.getPackageName(), subId);
            Resources cached = sResourcesCache.get(cacheKey);
            if (cached != null) {
                // Cache hit. Use cached Resources.
                return sResourcesCache.get(cacheKey);
                return cached;
            }
        }