Add property invalidated cache for getSubscriptionId.
Telecom calls into TelephonyManager#getSubscriptionId quite often for phone account handle operations. Adding a property invalidated cache to TelephonyManager to cache those values. The cache is invalidated from Telephony whenever phone accounts are re-registered. Test: Added unit test which confirms the number of AIDL calls when the cache is operating as expected. Test: Manual test; verified that I can see the property invalidated cache logs which indicate that there are cache hits going on. Bug: 224654574 Change-Id: I62c992247921465d401442c2aff005384665258e
This commit is contained in:
@@ -41,6 +41,7 @@ import android.annotation.SystemService;
|
||||
import android.annotation.TestApi;
|
||||
import android.annotation.WorkerThread;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.PropertyInvalidatedCache;
|
||||
import android.app.role.RoleManager;
|
||||
import android.compat.Compatibility;
|
||||
import android.compat.annotation.ChangeId;
|
||||
@@ -361,6 +362,42 @@ public class TelephonyManager {
|
||||
@GuardedBy("sCacheLock")
|
||||
private static final DeathRecipient sServiceDeath = new DeathRecipient();
|
||||
|
||||
/**
|
||||
* Cache key for a {@link PropertyInvalidatedCache} which maps from {@link PhoneAccountHandle}
|
||||
* to subscription Id. The cache is initialized in {@code PhoneInterfaceManager}'s constructor
|
||||
* when {@link PropertyInvalidatedCache#invalidateCache(String)} is called.
|
||||
* The cache is cleared from {@code TelecomAccountRegistry#tearDown} when all phone accounts are
|
||||
* removed from Telecom.
|
||||
* @hide
|
||||
*/
|
||||
public static final String CACHE_KEY_PHONE_ACCOUNT_TO_SUBID =
|
||||
"cache_key.telephony.phone_account_to_subid";
|
||||
private static final int CACHE_MAX_SIZE = 4;
|
||||
|
||||
/**
|
||||
* A {@link PropertyInvalidatedCache} which lives in an app's {@link TelephonyManager} instance.
|
||||
* Caches any queries for a mapping between {@link PhoneAccountHandle} and {@code subscription
|
||||
* id}. The cache may be invalidated from Telephony when phone account re-registration takes
|
||||
* place.
|
||||
*/
|
||||
private PropertyInvalidatedCache<PhoneAccountHandle, Integer> mPhoneAccountHandleToSubIdCache =
|
||||
new PropertyInvalidatedCache<PhoneAccountHandle, Integer>(CACHE_MAX_SIZE,
|
||||
CACHE_KEY_PHONE_ACCOUNT_TO_SUBID) {
|
||||
@Override
|
||||
public Integer recompute(PhoneAccountHandle phoneAccountHandle) {
|
||||
try {
|
||||
ITelephony telephony = getITelephony();
|
||||
if (telephony != null) {
|
||||
return telephony.getSubIdForPhoneAccountHandle(phoneAccountHandle,
|
||||
mContext.getOpPackageName(), mContext.getAttributionTag());
|
||||
}
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowAsRuntimeException();
|
||||
}
|
||||
return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
}
|
||||
};
|
||||
|
||||
/** Enum indicating multisim variants
|
||||
* DSDS - Dual SIM Dual Standby
|
||||
* DSDA - Dual SIM Dual Active
|
||||
@@ -11904,19 +11941,7 @@ public class TelephonyManager {
|
||||
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
|
||||
@RequiresFeature(PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION)
|
||||
public int getSubscriptionId(@NonNull PhoneAccountHandle phoneAccountHandle) {
|
||||
int retval = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
try {
|
||||
ITelephony service = getITelephony();
|
||||
if (service != null) {
|
||||
retval = service.getSubIdForPhoneAccountHandle(
|
||||
phoneAccountHandle, mContext.getOpPackageName(),
|
||||
mContext.getAttributionTag());
|
||||
}
|
||||
} catch (RemoteException ex) {
|
||||
Log.e(TAG, "getSubscriptionId RemoteException", ex);
|
||||
ex.rethrowAsRuntimeException();
|
||||
}
|
||||
return retval;
|
||||
return mPhoneAccountHandleToSubIdCache.query(phoneAccountHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user