Merge "Prepare PropertyInvalidatedCache for SystemApi"

This commit is contained in:
Lee Shombert
2022-01-06 23:14:05 +00:00
committed by Android (Google) Code Review
15 changed files with 43 additions and 45 deletions

View File

@@ -384,7 +384,7 @@ public class AccountManager {
new PropertyInvalidatedCache<UserIdPackage, Account[]>(
CACHE_ACCOUNTS_DATA_SIZE, CACHE_KEY_ACCOUNTS_DATA_PROPERTY) {
@Override
protected Account[] recompute(UserIdPackage userAndPackage) {
public Account[] recompute(UserIdPackage userAndPackage) {
try {
return mService.getAccountsAsUser(null, userAndPackage.userId, userAndPackage.packageName);
} catch (RemoteException e) {
@@ -392,11 +392,11 @@ public class AccountManager {
}
}
@Override
protected boolean bypass(UserIdPackage query) {
public boolean bypass(UserIdPackage query) {
return query.userId < 0;
}
@Override
protected boolean debugCompareQueryResults(Account[] l, Account[] r) {
public boolean resultEquals(Account[] l, Account[] r) {
if (l == r) {
return true;
} else if (l == null || r == null) {
@@ -455,7 +455,7 @@ public class AccountManager {
new PropertyInvalidatedCache<AccountKeyData, String>(CACHE_USER_DATA_SIZE,
CACHE_KEY_USER_DATA_PROPERTY) {
@Override
protected String recompute(AccountKeyData accountKeyData) {
public String recompute(AccountKeyData accountKeyData) {
Account account = accountKeyData.account;
String key = accountKeyData.key;

View File

@@ -802,7 +802,7 @@ public class ApplicationPackageManager extends PackageManager {
new PropertyInvalidatedCache<HasSystemFeatureQuery, Boolean>(
256, "cache_key.has_system_feature") {
@Override
protected Boolean recompute(HasSystemFeatureQuery query) {
public Boolean recompute(HasSystemFeatureQuery query) {
try {
return ActivityThread.currentActivityThread().getPackageManager().
hasSystemFeature(query.name, query.version);
@@ -1098,7 +1098,7 @@ public class ApplicationPackageManager extends PackageManager {
new PropertyInvalidatedCache<Integer, GetPackagesForUidResult>(
32, CACHE_KEY_PACKAGES_FOR_UID_PROPERTY) {
@Override
protected GetPackagesForUidResult recompute(Integer uid) {
public GetPackagesForUidResult recompute(Integer uid) {
try {
return new GetPackagesForUidResult(
ActivityThread.currentActivityThread().

View File

@@ -505,13 +505,13 @@ public abstract class PropertyInvalidatedCache<Query, Result> {
* block. If this function returns null, the result of the cache query is null. There is no
* "negative cache" in the query: we don't cache null results at all.
*/
protected abstract Result recompute(Query query);
public abstract Result recompute(Query query);
/**
* Return true if the query should bypass the cache. The default behavior is to
* always use the cache but the method can be overridden for a specific class.
*/
protected boolean bypass(Query query) {
public boolean bypass(Query query) {
return false;
}
@@ -519,7 +519,7 @@ public abstract class PropertyInvalidatedCache<Query, Result> {
* Determines if a pair of responses are considered equal. Used to determine whether
* a cache is inadvertently returning stale results when VERIFY is set to true.
*/
protected boolean debugCompareQueryResults(Result cachedResult, Result fetchedResult) {
protected boolean resultEquals(Result cachedResult, Result fetchedResult) {
// If a service crashes and returns a null result, the cached value remains valid.
if (fetchedResult != null) {
return Objects.equals(cachedResult, fetchedResult);
@@ -990,11 +990,11 @@ public abstract class PropertyInvalidatedCache<Query, Result> {
}
}
protected Result maybeCheckConsistency(Query query, Result proposedResult) {
private Result maybeCheckConsistency(Query query, Result proposedResult) {
if (VERIFY) {
Result resultToCompare = recompute(query);
boolean nonceChanged = (getCurrentNonce() != mLastSeenNonce);
if (!nonceChanged && !debugCompareQueryResults(proposedResult, resultToCompare)) {
if (!nonceChanged && !resultEquals(proposedResult, resultToCompare)) {
Log.e(TAG, TextUtils.formatSimple(
"cache %s inconsistent for %s is %s should be %s",
cacheName(), queryToString(query),

View File

@@ -84,7 +84,7 @@ public final class ChangeIdStateCache
}
@Override
protected Boolean recompute(ChangeIdStateQuery query) {
public Boolean recompute(ChangeIdStateQuery query) {
final long token = Binder.clearCallingIdentity();
try {
if (query.type == ChangeIdStateQuery.QUERY_BY_PACKAGE_NAME) {

View File

@@ -1062,7 +1062,7 @@ public final class BluetoothAdapter {
8, BLUETOOTH_GET_STATE_CACHE_PROPERTY) {
@Override
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Integer recompute(Void query) {
public Integer recompute(Void query) {
try {
return mService.getState();
} catch (RemoteException e) {
@@ -2085,7 +2085,7 @@ public final class BluetoothAdapter {
8, BLUETOOTH_FILTERING_CACHE_PROPERTY) {
@Override
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Boolean recompute(Void query) {
public Boolean recompute(Void query) {
try {
mServiceLock.readLock().lock();
if (mService != null) {
@@ -2540,7 +2540,7 @@ public final class BluetoothAdapter {
*/
@Override
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Integer recompute(Void query) {
public Integer recompute(Void query) {
try {
return mService.getAdapterConnectionState();
} catch (RemoteException e) {
@@ -2605,7 +2605,7 @@ public final class BluetoothAdapter {
8, BLUETOOTH_PROFILE_CACHE_PROPERTY) {
@Override
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Integer recompute(Integer query) {
public Integer recompute(Integer query) {
try {
mServiceLock.readLock().lock();
if (mService != null) {

View File

@@ -1604,7 +1604,7 @@ public final class BluetoothDevice implements Parcelable, Attributable {
8, BLUETOOTH_BONDING_CACHE_PROPERTY) {
@Override
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Integer recompute(BluetoothDevice query) {
public Integer recompute(BluetoothDevice query) {
try {
return sService.getBondState(query, mAttributionSource);
} catch (RemoteException e) {

View File

@@ -10187,16 +10187,15 @@ public abstract class PackageManager {
16, PermissionManager.CACHE_KEY_PACKAGE_INFO,
"getApplicationInfo") {
@Override
protected ApplicationInfo recompute(ApplicationInfoQuery query) {
public ApplicationInfo recompute(ApplicationInfoQuery query) {
return getApplicationInfoAsUserUncached(
query.packageName, query.flags, query.userId);
}
@Override
protected ApplicationInfo maybeCheckConsistency(
ApplicationInfoQuery query, ApplicationInfo proposedResult) {
public boolean resultEquals(ApplicationInfo cached, ApplicationInfo fetched) {
// Implementing this debug check for ApplicationInfo would require a
// complicated deep comparison, so just bypass it for now.
return proposedResult;
return true;
}
};
@@ -10289,16 +10288,15 @@ public abstract class PackageManager {
32, PermissionManager.CACHE_KEY_PACKAGE_INFO,
"getPackageInfo") {
@Override
protected PackageInfo recompute(PackageInfoQuery query) {
public PackageInfo recompute(PackageInfoQuery query) {
return getPackageInfoAsUserUncached(
query.packageName, query.flags, query.userId);
}
@Override
protected PackageInfo maybeCheckConsistency(
PackageInfoQuery query, PackageInfo proposedResult) {
public boolean resultEquals(PackageInfo cached, PackageInfo fetched) {
// Implementing this debug check for PackageInfo would require a
// complicated deep comparison, so just bypass it for now.
return proposedResult;
return true;
}
};

View File

@@ -128,7 +128,7 @@ public final class DisplayManagerGlobal {
8, // size of display cache
CACHE_KEY_DISPLAY_INFO_PROPERTY) {
@Override
protected DisplayInfo recompute(Integer id) {
public DisplayInfo recompute(Integer id) {
try {
return mDm.getDisplayInfo(id);
} catch (RemoteException ex) {

View File

@@ -1011,7 +1011,7 @@ public final class PowerManager {
new PropertyInvalidatedCache<Void, Boolean>(MAX_CACHE_ENTRIES,
CACHE_KEY_IS_POWER_SAVE_MODE_PROPERTY) {
@Override
protected Boolean recompute(Void query) {
public Boolean recompute(Void query) {
try {
return mService.isPowerSaveMode();
} catch (RemoteException e) {
@@ -1024,7 +1024,7 @@ public final class PowerManager {
new PropertyInvalidatedCache<Void, Boolean>(MAX_CACHE_ENTRIES,
CACHE_KEY_IS_INTERACTIVE_PROPERTY) {
@Override
protected Boolean recompute(Void query) {
public Boolean recompute(Void query) {
try {
return mService.isInteractive();
} catch (RemoteException e) {

View File

@@ -2754,7 +2754,7 @@ public class UserManager {
new PropertyInvalidatedCache<Integer, Boolean>(
32, CACHE_KEY_IS_USER_UNLOCKED_PROPERTY) {
@Override
protected Boolean recompute(Integer query) {
public Boolean recompute(Integer query) {
try {
return mService.isUserUnlocked(query);
} catch (RemoteException re) {
@@ -2762,7 +2762,7 @@ public class UserManager {
}
}
@Override
protected boolean bypass(Integer query) {
public boolean bypass(Integer query) {
return query < 0;
}
};
@@ -2772,7 +2772,7 @@ public class UserManager {
new PropertyInvalidatedCache<Integer, Boolean>(
32, CACHE_KEY_IS_USER_UNLOCKED_PROPERTY) {
@Override
protected Boolean recompute(Integer query) {
public Boolean recompute(Integer query) {
try {
return mService.isUserUnlockingOrUnlocked(query);
} catch (RemoteException re) {
@@ -2780,7 +2780,7 @@ public class UserManager {
}
}
@Override
protected boolean bypass(Integer query) {
public boolean bypass(Integer query) {
return query < 0;
}
};

View File

@@ -1447,7 +1447,7 @@ public final class PermissionManager {
new PropertyInvalidatedCache<PermissionQuery, Integer>(
2048, CACHE_KEY_PACKAGE_INFO, "checkPermission") {
@Override
protected Integer recompute(PermissionQuery query) {
public Integer recompute(PermissionQuery query) {
return checkPermissionUncached(query.permission, query.pid, query.uid);
}
};
@@ -1530,12 +1530,12 @@ public final class PermissionManager {
new PropertyInvalidatedCache<PackageNamePermissionQuery, Integer>(
16, CACHE_KEY_PACKAGE_INFO, "checkPackageNamePermission") {
@Override
protected Integer recompute(PackageNamePermissionQuery query) {
public Integer recompute(PackageNamePermissionQuery query) {
return checkPackageNamePermissionUncached(
query.permName, query.pkgName, query.userId);
}
@Override
protected boolean bypass(PackageNamePermissionQuery query) {
public boolean bypass(PackageNamePermissionQuery query) {
return query.userId < 0;
}
};

View File

@@ -77,11 +77,11 @@ public class PropertyInvalidatedCacheTests {
PropertyInvalidatedCache<Integer, Boolean> testCache =
new PropertyInvalidatedCache<>(4, CACHE_PROPERTY) {
@Override
protected Boolean recompute(Integer x) {
public Boolean recompute(Integer x) {
return tester.query(x);
}
@Override
protected boolean bypass(Integer x) {
public boolean bypass(Integer x) {
return x % 13 == 0;
}
};
@@ -131,21 +131,21 @@ public class PropertyInvalidatedCacheTests {
PropertyInvalidatedCache<Integer, Boolean> cache1 =
new PropertyInvalidatedCache<>(4, CACHE_PROPERTY) {
@Override
protected Boolean recompute(Integer x) {
public Boolean recompute(Integer x) {
return tester.query(x);
}
};
PropertyInvalidatedCache<Integer, Boolean> cache2 =
new PropertyInvalidatedCache<>(4, CACHE_PROPERTY) {
@Override
protected Boolean recompute(Integer x) {
public Boolean recompute(Integer x) {
return tester.query(x);
}
};
PropertyInvalidatedCache<Integer, Boolean> cache3 =
new PropertyInvalidatedCache<>(4, CACHE_PROPERTY, "cache3") {
@Override
protected Boolean recompute(Integer x) {
public Boolean recompute(Integer x) {
return tester.query(x);
}
};
@@ -171,7 +171,7 @@ public class PropertyInvalidatedCacheTests {
// Create a new cache1. Verify that the new instance is disabled.
cache1 = new PropertyInvalidatedCache<>(4, CACHE_PROPERTY) {
@Override
protected Boolean recompute(Integer x) {
public Boolean recompute(Integer x) {
return tester.query(x);
}
};

View File

@@ -35,7 +35,7 @@ public class PropertyInvalidatedCacheTest extends TestCase {
}
@Override
protected String recompute(Integer qv) {
public String recompute(Integer qv) {
mRecomputeCount += 1;
return "foo" + qv.toString();
}

View File

@@ -3640,7 +3640,7 @@ public class LocationManager {
}
@Override
protected Boolean recompute(Integer userId) {
public Boolean recompute(Integer userId) {
Preconditions.checkArgument(userId >= 0);
if (mManager == null) {

View File

@@ -194,7 +194,7 @@ public class SubscriptionManager {
}
@Override
protected T recompute(Void aVoid) {
public T recompute(Void aVoid) {
T result = mDefaultValue;
try {
@@ -228,7 +228,7 @@ public class SubscriptionManager {
}
@Override
protected T recompute(Integer query) {
public T recompute(Integer query) {
T result = mDefaultValue;
try {