summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hui Wang <huiwang@google.com> 2023-08-11 23:16:22 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2023-08-11 23:16:22 +0000
commit1ec82d8ef300b9b1fee75aa64d6163397462f171 (patch)
treea2ea333fd5b11b89b430cdcbd02d7d406c78170a
parent2e457d31d6aba9af9eaba52b1a3a9c44fcc7ab69 (diff)
Revert "Change the resource cache key and limit the maximum size..."
Revert submission 24318542-PackageResourceCache Reason for revert: presubmit failure b/295414863 Reverted changes: /q/submissionid:24318542-PackageResourceCache Change-Id: I0199d9687373c5c0056a4af263017e84eb33e0eb
-rw-r--r--telephony/java/android/telephony/SubscriptionManager.java44
1 files changed, 12 insertions, 32 deletions
diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java
index 0af973a2f627..040c5b013c6a 100644
--- a/telephony/java/android/telephony/SubscriptionManager.java
+++ b/telephony/java/android/telephony/SubscriptionManager.java
@@ -85,11 +85,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -1318,24 +1318,10 @@ public class SubscriptionManager {
private final Context mContext;
- /**
- * In order to prevent the overflow of the heap size due to an indiscriminate increase in the
- * cache, the heap size of the resource cache is set sufficiently large.
- */
- private static final int MAX_RESOURCE_CACHE_ENTRY_COUNT = 10_000;
-
- /**
- * Cache of Resources that has been created in getResourcesForSubId. Key contains package name,
- * and Configuration of Resources. If more than the maximum number of resources are stored in
- * this cache, the least recently used Resources will be removed to maintain the maximum size.
- */
- private static final Map<Pair<String, Configuration>, Resources> sResourcesCache =
- Collections.synchronizedMap(new LinkedHashMap<>(16, 0.75f, true) {
- @Override
- protected boolean removeEldestEntry(Entry eldest) {
- return size() > MAX_RESOURCE_CACHE_ENTRY_COUNT;
- }
- });
+ // 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 =
+ new ConcurrentHashMap<>();
/**
* A listener class for monitoring changes to {@link SubscriptionInfo} records.
@@ -2821,20 +2807,14 @@ public class SubscriptionManager {
@NonNull
public static Resources getResourcesForSubId(Context context, int subId,
boolean useRootLocale) {
- // Check if the Resources already exists in the cache based on the given context. Find a
- // Resource that match Configuration.
- Pair<String, Configuration> cacheKey = null;
- if (isValidSubscriptionId(subId)) {
- Configuration configurationKey =
- new Configuration(context.getResources().getConfiguration());
- if (useRootLocale) {
- configurationKey.setLocale(Locale.ROOT);
- }
- cacheKey = Pair.create(context.getPackageName(), configurationKey);
- Resources cached = sResourcesCache.get(cacheKey);
- if (cached != null) {
+ // 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;
+ if (isValidSubscriptionId(subId) && !useRootLocale) {
+ cacheKey = Pair.create(context, subId);
+ if (sResourcesCache.containsKey(cacheKey)) {
// Cache hit. Use cached Resources.
- return cached;
+ return sResourcesCache.get(cacheKey);
}
}