From 29b1874ffc19a3e520573b345eb306efe6248870 Mon Sep 17 00:00:00 2001 From: Hall Liu Date: Tue, 7 Apr 2020 18:23:09 -0700 Subject: [PATCH] Refactor SubscriptionManager caching code Create a genericized class to use for SubscriptionManager caching calls in order to avoid duplicating logic that fetches values from ISub. Bug: 151953109 Test: atest android.telephony.cts.SubscriptionManagerTest Merged-In: I6682ded8aec8cb3e50521584c177df6d5dae8c49 Change-Id: I6682ded8aec8cb3e50521584c177df6d5dae8c49 --- Android.bp | 1 + .../telephony/SubscriptionManager.java | 89 +++++++++---------- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/Android.bp b/Android.bp index 935b2bb478bba..ee381a42d7284 100644 --- a/Android.bp +++ b/Android.bp @@ -1121,6 +1121,7 @@ filegroup { "core/java/com/android/internal/os/SomeArgs.java", "core/java/com/android/internal/util/BitwiseInputStream.java", "core/java/com/android/internal/util/BitwiseOutputStream.java", + "core/java/com/android/internal/util/FunctionalUtils.java", "core/java/com/android/internal/util/HexDump.java", "core/java/com/android/internal/util/IndentingPrintWriter.java", "core/java/com/android/internal/util/Preconditions.java", diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java index 958ec08ea95ae..ce5412f0fbcc2 100644 --- a/telephony/java/android/telephony/SubscriptionManager.java +++ b/telephony/java/android/telephony/SubscriptionManager.java @@ -63,6 +63,7 @@ import com.android.internal.telephony.ISetOpportunisticDataCallback; import com.android.internal.telephony.ISub; import com.android.internal.telephony.PhoneConstants; import com.android.internal.telephony.util.HandlerExecutor; +import com.android.internal.util.FunctionalUtils; import com.android.internal.util.Preconditions; import com.android.telephony.Rlog; @@ -149,21 +150,49 @@ public class SubscriptionManager { private static final int MAX_CACHE_SIZE = 4; - private static PropertyInvalidatedCache sDefaultSubIdCache = - new PropertyInvalidatedCache( - MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_SUB_ID_PROPERTY) { - @Override - protected Integer recompute(Void query) { - return getDefaultSubscriptionIdInternal(); - }}; + private static class SubscriptionPropertyInvalidatedCache + extends PropertyInvalidatedCache { + private final FunctionalUtils.ThrowingFunction mSubscriptionInterfaceMethod; + private final String mCacheKeyProperty; + private final T mDefaultValue; - private static PropertyInvalidatedCache sDefaultDataSubIdCache = - new PropertyInvalidatedCache( - MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY) { - @Override - protected Integer recompute(Void query) { - return getDefaultDataSubscriptionIdInternal(); - }}; + SubscriptionPropertyInvalidatedCache( + FunctionalUtils.ThrowingFunction subscriptionInterfaceMethod, + String cacheKeyProperty, + T defaultValue) { + super(MAX_CACHE_SIZE, cacheKeyProperty); + mSubscriptionInterfaceMethod = subscriptionInterfaceMethod; + mCacheKeyProperty = cacheKeyProperty; + mDefaultValue = defaultValue; + } + + @Override + protected T recompute(Void aVoid) { + T result = mDefaultValue; + + try { + ISub iSub = TelephonyManager.getSubscriptionService(); + if (iSub != null) { + result = mSubscriptionInterfaceMethod.applyOrThrow(iSub); + } + } catch (Exception ex) { + Rlog.w(LOG_TAG, "Failed to recompute cache key for " + mCacheKeyProperty); + } + + if (VDBG) logd("recomputing " + mCacheKeyProperty + ", result = " + result); + return result; + } + } + + private static SubscriptionPropertyInvalidatedCache sDefaultSubIdCache = + new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSubId, + CACHE_KEY_DEFAULT_SUB_ID_PROPERTY, + INVALID_SUBSCRIPTION_ID); + + private static SubscriptionPropertyInvalidatedCache sDefaultDataSubIdCache = + new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultDataSubId, + CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY, + INVALID_SUBSCRIPTION_ID); private static PropertyInvalidatedCache sDefaultSmsSubIdCache = new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSmsSubId, @@ -1888,22 +1917,6 @@ public class SubscriptionManager { return sDefaultSubIdCache.query(null); } - private static int getDefaultSubscriptionIdInternal() { - int subId = INVALID_SUBSCRIPTION_ID; - - try { - ISub iSub = TelephonyManager.getSubscriptionService(); - if (iSub != null) { - subId = iSub.getDefaultSubId(); - } - } catch (RemoteException ex) { - // ignore it - } - - if (VDBG) logd("getDefaultSubId=" + subId); - return subId; - } - /** * Returns the system's default voice subscription id. * @@ -2045,22 +2058,6 @@ public class SubscriptionManager { return sDefaultDataSubIdCache.query(null); } - private static int getDefaultDataSubscriptionIdInternal() { - int subId = INVALID_SUBSCRIPTION_ID; - - try { - ISub iSub = TelephonyManager.getSubscriptionService(); - if (iSub != null) { - subId = iSub.getDefaultDataSubId(); - } - } catch (RemoteException ex) { - // ignore it - } - - if (VDBG) logd("getDefaultDataSubscriptionId, sub id = " + subId); - return subId; - } - /** * Set the subscription which will be used by default for data, with the subscription which * the supplied subscription ID corresponds to; or throw a RuntimeException if the supplied