3535import android .content .pm .PackageManager .NameNotFoundException ;
3636import android .content .pm .RegisteredServicesCache ;
3737import android .content .pm .RegisteredServicesCacheListener ;
38+ import android .content .pm .UserInfo ;
3839import android .database .Cursor ;
3940import android .database .DatabaseUtils ;
4041import android .database .sqlite .SQLiteDatabase ;
5455import android .text .TextUtils ;
5556import android .util .Log ;
5657import android .util .Pair ;
58+ import android .util .Slog ;
5759import android .util .SparseArray ;
5860
5961import com .android .internal .R ;
7072import java .util .HashMap ;
7173import java .util .HashSet ;
7274import java .util .LinkedHashMap ;
75+ import java .util .List ;
7376import java .util .Map ;
7477import java .util .concurrent .atomic .AtomicInteger ;
7578import java .util .concurrent .atomic .AtomicReference ;
@@ -261,8 +264,7 @@ private UserAccounts initUser(int userId) {
261264 accounts = new UserAccounts (mContext , userId );
262265 mUsers .append (userId , accounts );
263266 purgeOldGrants (accounts );
264- mAuthenticatorCache .invalidateCache (accounts .userId );
265- validateAccountsAndPopulateCache (accounts );
267+ validateAccountsInternal (accounts , true /* invalidateAuthenticatorCache */ );
266268 }
267269 return accounts ;
268270 }
@@ -300,7 +302,28 @@ private void purgeOldGrants(UserAccounts accounts) {
300302 }
301303 }
302304
303- private void validateAccountsAndPopulateCache (UserAccounts accounts ) {
305+ /**
306+ * Validate internal set of accounts against installed authenticators for
307+ * given user. Clears cached authenticators before validating.
308+ */
309+ public void validateAccounts (int userId ) {
310+ final UserAccounts accounts = getUserAccounts (userId );
311+
312+ // Invalidate user-specific cache to make sure we catch any
313+ // removed authenticators.
314+ validateAccountsInternal (accounts , true /* invalidateAuthenticatorCache */ );
315+ }
316+
317+ /**
318+ * Validate internal set of accounts against installed authenticators for
319+ * given user. Clear cached authenticators before validating when requested.
320+ */
321+ private void validateAccountsInternal (
322+ UserAccounts accounts , boolean invalidateAuthenticatorCache ) {
323+ if (invalidateAuthenticatorCache ) {
324+ mAuthenticatorCache .invalidateCache (accounts .userId );
325+ }
326+
304327 final HashSet <AuthenticatorDescription > knownAuth = Sets .newHashSet ();
305328 for (RegisteredServicesCache .ServiceInfo <AuthenticatorDescription > service :
306329 mAuthenticatorCache .getAllServices (accounts .userId )) {
@@ -323,7 +346,7 @@ private void validateAccountsAndPopulateCache(UserAccounts accounts) {
323346 final String accountName = cursor .getString (2 );
324347
325348 if (!knownAuth .contains (AuthenticatorDescription .newKey (accountType ))) {
326- Log . d (TAG , "deleting account " + accountName + " because type "
349+ Slog . w (TAG , "deleting account " + accountName + " because type "
327350 + accountType + " no longer has a registered authenticator" );
328351 db .delete (TABLE_ACCOUNTS , ACCOUNTS_ID + "=" + accountId , null );
329352 accountDeleted = true ;
@@ -399,7 +422,8 @@ private void onUserRemoved(Intent intent) {
399422
400423 @ Override
401424 public void onServiceChanged (AuthenticatorDescription desc , int userId , boolean removed ) {
402- validateAccountsAndPopulateCache (getUserAccounts (userId ));
425+ Slog .d (TAG , "onServiceChanged() for userId " + userId );
426+ validateAccountsInternal (getUserAccounts (userId ), false /* invalidateAuthenticatorCache */ );
403427 }
404428
405429 public String getPassword (Account account ) {
@@ -1493,10 +1517,23 @@ public AccountAndUser[] getRunningAccounts() {
14931517 // Running in system_server; should never happen
14941518 throw new RuntimeException (e );
14951519 }
1520+ return getAccounts (runningUserIds );
1521+ }
1522+
1523+ /** {@hide} */
1524+ public AccountAndUser [] getAllAccounts () {
1525+ final List <UserInfo > users = getUserManager ().getUsers ();
1526+ final int [] userIds = new int [users .size ()];
1527+ for (int i = 0 ; i < userIds .length ; i ++) {
1528+ userIds [i ] = users .get (i ).id ;
1529+ }
1530+ return getAccounts (userIds );
1531+ }
14961532
1533+ private AccountAndUser [] getAccounts (int [] userIds ) {
14971534 final ArrayList <AccountAndUser > runningAccounts = Lists .newArrayList ();
14981535 synchronized (mUsers ) {
1499- for (int userId : runningUserIds ) {
1536+ for (int userId : userIds ) {
15001537 UserAccounts userAccounts = getUserAccounts (userId );
15011538 if (userAccounts == null ) continue ;
15021539 synchronized (userAccounts .cacheLock ) {
@@ -2006,6 +2043,7 @@ private static boolean scanArgs(String[] args, String value) {
20062043 return false ;
20072044 }
20082045
2046+ @ Override
20092047 protected void dump (FileDescriptor fd , PrintWriter fout , String [] args ) {
20102048 if (mContext .checkCallingOrSelfPermission (android .Manifest .permission .DUMP )
20112049 != PackageManager .PERMISSION_GRANTED ) {
@@ -2015,17 +2053,15 @@ protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
20152053 return ;
20162054 }
20172055 final boolean isCheckinRequest = scanArgs (args , "--checkin" ) || scanArgs (args , "-c" );
2018-
2019- fout = new IndentingPrintWriter (fout , " " );
2020- int size = mUsers .size ();
2021- for (int i = 0 ; i < size ; i ++) {
2022- fout .println ("User " + mUsers .keyAt (i ) + ":" );
2023- ((IndentingPrintWriter ) fout ).increaseIndent ();
2024- dumpUser (mUsers .valueAt (i ), fd , fout , args , isCheckinRequest );
2025- ((IndentingPrintWriter ) fout ).decreaseIndent ();
2026- if (i < size - 1 ) {
2027- fout .println ();
2028- }
2056+ final IndentingPrintWriter ipw = new IndentingPrintWriter (fout , " " );
2057+
2058+ final List <UserInfo > users = getUserManager ().getUsers ();
2059+ for (UserInfo user : users ) {
2060+ ipw .println ("User " + user + ":" );
2061+ ipw .increaseIndent ();
2062+ dumpUser (getUserAccounts (user .id ), fd , ipw , args , isCheckinRequest );
2063+ ipw .println ();
2064+ ipw .decreaseIndent ();
20292065 }
20302066 }
20312067
0 commit comments