Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cece389 authored by Lee Shombert's avatar Lee Shombert
Browse files

Prepare PropertyInvalidatedCache for SystemApi

Bug: 152453213
Tag: #refactor

This commit prepares PropertyInvalidatedCache to function as a system
api.  Specifically, the methods recompute() and bypass() which may be
overridden by clients are now public (instead of protected).  This
forces an update to all existing clients, to accommodate the change in
method visibility.

Two small changes have been made as cleanup:

 1. The awkwardly named debugCompareQueryResults() is now
    resultEquals(), which is more or less consistent with how other
    equality tests are named in Android.  This name change affects two
    clients.

 2. PackageManager has changed to use resultEquals() instead of
    maybeCheckConsistency().  This provides a simpler and more
    consistent use of the APIs.  maybeCheckConsistency() has been made
    private.

Test: atest PropertyInvalidatedCacheTests

Change-Id: I4110f8e887a4fd8c784141e8892557a9d1b80a94
parent 6ba962a9
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -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;

+2 −2
Original line number Diff line number Diff line
@@ -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().
+5 −5
Original line number Diff line number Diff line
@@ -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),
+1 −1
Original line number Diff line number Diff line
@@ -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) {
+4 −4
Original line number Diff line number Diff line
@@ -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) {
Loading