diff options
| author | 2020-04-07 18:23:09 -0700 | |
|---|---|---|
| committer | 2020-04-28 16:22:48 -0700 | |
| commit | 29b1874ffc19a3e520573b345eb306efe6248870 (patch) | |
| tree | 779a471ed55df722f3cc265cde84d82c2d5de7df | |
| parent | 02e5c590c3658023e32754cbac4433f92738b2b3 (diff) | |
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
| -rw-r--r-- | Android.bp | 1 | ||||
| -rw-r--r-- | telephony/java/android/telephony/SubscriptionManager.java | 91 |
2 files changed, 45 insertions, 47 deletions
diff --git a/Android.bp b/Android.bp index 935b2bb478bb..ee381a42d728 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 958ec08ea95a..ce5412f0fbcc 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<Void, Integer> sDefaultSubIdCache = - new PropertyInvalidatedCache<Void, Integer>( - MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_SUB_ID_PROPERTY) { - @Override - protected Integer recompute(Void query) { - return getDefaultSubscriptionIdInternal(); - }}; - - private static PropertyInvalidatedCache<Void, Integer> sDefaultDataSubIdCache = - new PropertyInvalidatedCache<Void, Integer>( - MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY) { - @Override - protected Integer recompute(Void query) { - return getDefaultDataSubscriptionIdInternal(); - }}; + private static class SubscriptionPropertyInvalidatedCache<T> + extends PropertyInvalidatedCache<Void, T> { + private final FunctionalUtils.ThrowingFunction<ISub, T> mSubscriptionInterfaceMethod; + private final String mCacheKeyProperty; + private final T mDefaultValue; + + SubscriptionPropertyInvalidatedCache( + FunctionalUtils.ThrowingFunction<ISub, T> 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<Integer> sDefaultSubIdCache = + new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSubId, + CACHE_KEY_DEFAULT_SUB_ID_PROPERTY, + INVALID_SUBSCRIPTION_ID); + + private static SubscriptionPropertyInvalidatedCache<Integer> sDefaultDataSubIdCache = + new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultDataSubId, + CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY, + INVALID_SUBSCRIPTION_ID); private static PropertyInvalidatedCache<Void, Integer> 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 |