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

Commit bdd8e7c5 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Adding binder cache for AccountManagerService:getAccountsAsUser"

parents cb5cf620 dddb1bd3
Loading
Loading
Loading
Loading
+74 −6
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
@@ -336,6 +337,59 @@ public class AccountManager {
    public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
            "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";

    /**
     * @hide
    */
    public static final String CACHE_KEY_ACCOUNTS_DATA_PROPERTY = "cache_key.system_server.accounts_data";

    /**
     * @hide
    */
    public static final int CACHE_ACCOUNTS_DATA_SIZE = 4;

    private static final class UserIdPackage
    {
        public int userId;
        public String packageName;

        public UserIdPackage(int UserId, String PackageName) {
            this.userId = UserId;
            this.packageName = PackageName;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }
            if (o == this) {
                return true;
            }
            if (o.getClass() != getClass()) {
                return false;
            }
            UserIdPackage e = (UserIdPackage) o;
            return e.userId == userId && e.packageName.equals(packageName);
        }

        @Override
        public int hashCode() {
            return userId ^ packageName.hashCode();
        }
    }

    PropertyInvalidatedCache<UserIdPackage, Account[]> mAccountsForUserCache =
        new PropertyInvalidatedCache<UserIdPackage, Account[]>(CACHE_ACCOUNTS_DATA_SIZE, CACHE_KEY_ACCOUNTS_DATA_PROPERTY) {
        @Override
        protected Account[] recompute(UserIdPackage userAndPackage) {
            try {
                return mService.getAccountsAsUser(null, userAndPackage.userId, userAndPackage.packageName);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    };

    /**
     * @hide
    */
@@ -660,11 +714,8 @@ public class AccountManager {
     */
    @NonNull
    public Account[] getAccountsAsUser(int userId) {
        try {
            return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
        UserIdPackage userAndPackage = new UserIdPackage(userId, mContext.getOpPackageName());
        return mAccountsForUserCache.query(userAndPackage);
    }

    /**
@@ -3392,6 +3443,23 @@ public class AccountManager {
        }
    }

    /**
     * @hide
     * Calling this will invalidate Local Accounts Data Cache which
     * forces the next query in any process to recompute the cache
    */
    public static void invalidateLocalAccountsDataCaches() {
        PropertyInvalidatedCache.invalidateCache(CACHE_KEY_ACCOUNTS_DATA_PROPERTY);
    }

    /**
     * @hide
     * Calling this will disable account data caching.
    */
    public void disableLocalAccountCaches() {
        mAccountsForUserCache.disableLocal();
    }
    
    /**
     * @hide
     * Calling this will invalidate Local Account User Data Cache which
+10 −0
Original line number Diff line number Diff line
@@ -584,6 +584,7 @@ public class AccountManagerService
            Log.d(TAG, "Visibility was not initialized");
            accountVisibility = new HashMap<>();
            accounts.visibilityCache.put(account, accountVisibility);
            AccountManager.invalidateLocalAccountsDataCaches();
        }
        return accountVisibility;
    }
@@ -871,6 +872,7 @@ public class AccountManagerService
        Map<String, Integer> accountVisibility =
            getPackagesAndVisibilityForAccountLocked(account, accounts);
        accountVisibility.put(packageName, newVisibility);
        AccountManager.invalidateLocalAccountsDataCaches();
        return true;
    }

@@ -1241,6 +1243,7 @@ public class AccountManagerService
                        accounts.accountCache.put(accountType, accountsForType);
                    }
                    accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues());
                    AccountManager.invalidateLocalAccountsDataCaches();
                } finally {
                    if (accountDeleted) {
                        sendAccountsChangedBroadcast(accounts.userId);
@@ -1325,6 +1328,7 @@ public class AccountManagerService
                accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile);
                mUsers.append(userId, accounts);
                purgeOldGrants(accounts);
                AccountManager.invalidateLocalAccountsDataCaches();
                validateAccounts = true;
            }
            // open CE database if necessary
@@ -1406,6 +1410,7 @@ public class AccountManagerService
                                        getPackagesAndVisibilityForAccountLocked(account, accounts);
                                accountVisibility.remove(packageName);
                            }
                            AccountManager.invalidateLocalAccountsDataCaches();
                        }
                    }
              }
@@ -1419,6 +1424,7 @@ public class AccountManagerService
            accounts = mUsers.get(userId);
            mUsers.remove(userId);
            mLocalUnlockedUsers.delete(userId);
            AccountManager.invalidateLocalAccountsDataCaches();
        }
        if (accounts != null) {
            synchronized (accounts.dbLock) {
@@ -2121,6 +2127,7 @@ public class AccountManagerService
                    sendAccountRemovedBroadcast(accountToRename, packageName, accounts.userId);
                }

                AccountManager.invalidateLocalAccountsDataCaches();
                AccountManager.invalidateLocalAccountUserDataCaches();
            }
        }
@@ -5838,6 +5845,8 @@ public class AccountManagerService
        accounts.authTokenCache.remove(account);
        accounts.previousNameCache.remove(account);
        accounts.visibilityCache.remove(account);

        AccountManager.invalidateLocalAccountsDataCaches();
    }

    /**
@@ -5857,6 +5866,7 @@ public class AccountManagerService
                : UUID.randomUUID().toString();
        newAccountsForType[oldLength] = new Account(account, token);
        accounts.accountCache.put(account.type, newAccountsForType);
        AccountManager.invalidateLocalAccountsDataCaches();
        return newAccountsForType[oldLength];
    }