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
This commit is contained in:
committed by
Collin Fijalkovich
parent
02e5c590c3
commit
29b1874ffc
@@ -1121,6 +1121,7 @@ filegroup {
|
|||||||
"core/java/com/android/internal/os/SomeArgs.java",
|
"core/java/com/android/internal/os/SomeArgs.java",
|
||||||
"core/java/com/android/internal/util/BitwiseInputStream.java",
|
"core/java/com/android/internal/util/BitwiseInputStream.java",
|
||||||
"core/java/com/android/internal/util/BitwiseOutputStream.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/HexDump.java",
|
||||||
"core/java/com/android/internal/util/IndentingPrintWriter.java",
|
"core/java/com/android/internal/util/IndentingPrintWriter.java",
|
||||||
"core/java/com/android/internal/util/Preconditions.java",
|
"core/java/com/android/internal/util/Preconditions.java",
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ import com.android.internal.telephony.ISetOpportunisticDataCallback;
|
|||||||
import com.android.internal.telephony.ISub;
|
import com.android.internal.telephony.ISub;
|
||||||
import com.android.internal.telephony.PhoneConstants;
|
import com.android.internal.telephony.PhoneConstants;
|
||||||
import com.android.internal.telephony.util.HandlerExecutor;
|
import com.android.internal.telephony.util.HandlerExecutor;
|
||||||
|
import com.android.internal.util.FunctionalUtils;
|
||||||
import com.android.internal.util.Preconditions;
|
import com.android.internal.util.Preconditions;
|
||||||
import com.android.telephony.Rlog;
|
import com.android.telephony.Rlog;
|
||||||
|
|
||||||
@@ -149,21 +150,49 @@ public class SubscriptionManager {
|
|||||||
|
|
||||||
private static final int MAX_CACHE_SIZE = 4;
|
private static final int MAX_CACHE_SIZE = 4;
|
||||||
|
|
||||||
private static PropertyInvalidatedCache<Void, Integer> sDefaultSubIdCache =
|
private static class SubscriptionPropertyInvalidatedCache<T>
|
||||||
new PropertyInvalidatedCache<Void, Integer>(
|
extends PropertyInvalidatedCache<Void, T> {
|
||||||
MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_SUB_ID_PROPERTY) {
|
private final FunctionalUtils.ThrowingFunction<ISub, T> mSubscriptionInterfaceMethod;
|
||||||
@Override
|
private final String mCacheKeyProperty;
|
||||||
protected Integer recompute(Void query) {
|
private final T mDefaultValue;
|
||||||
return getDefaultSubscriptionIdInternal();
|
|
||||||
}};
|
|
||||||
|
|
||||||
private static PropertyInvalidatedCache<Void, Integer> sDefaultDataSubIdCache =
|
SubscriptionPropertyInvalidatedCache(
|
||||||
new PropertyInvalidatedCache<Void, Integer>(
|
FunctionalUtils.ThrowingFunction<ISub, T> subscriptionInterfaceMethod,
|
||||||
MAX_CACHE_SIZE, CACHE_KEY_DEFAULT_DATA_SUB_ID_PROPERTY) {
|
String cacheKeyProperty,
|
||||||
@Override
|
T defaultValue) {
|
||||||
protected Integer recompute(Void query) {
|
super(MAX_CACHE_SIZE, cacheKeyProperty);
|
||||||
return getDefaultDataSubscriptionIdInternal();
|
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 =
|
private static PropertyInvalidatedCache<Void, Integer> sDefaultSmsSubIdCache =
|
||||||
new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSmsSubId,
|
new SubscriptionPropertyInvalidatedCache<>(ISub::getDefaultSmsSubId,
|
||||||
@@ -1888,22 +1917,6 @@ public class SubscriptionManager {
|
|||||||
return sDefaultSubIdCache.query(null);
|
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.
|
* Returns the system's default voice subscription id.
|
||||||
*
|
*
|
||||||
@@ -2045,22 +2058,6 @@ public class SubscriptionManager {
|
|||||||
return sDefaultDataSubIdCache.query(null);
|
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
|
* 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
|
* the supplied subscription ID corresponds to; or throw a RuntimeException if the supplied
|
||||||
|
|||||||
Reference in New Issue
Block a user