Delete account from DE and CE databases

When authenticator no longer exists and the user is unlocked, we should
remove accounts from both tables in a single transaction.

Bug: 28910995
Change-Id: Ibabf5d3e5ba561ffb3dda03aef99e358e2f71c58
diff --git a/services/core/java/com/android/server/accounts/AccountManagerService.java b/services/core/java/com/android/server/accounts/AccountManagerService.java
index a9a53a2..4084542 100644
--- a/services/core/java/com/android/server/accounts/AccountManagerService.java
+++ b/services/core/java/com/android/server/accounts/AccountManagerService.java
@@ -432,6 +432,7 @@
 
         final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser(
                 mAuthenticatorCache, accounts.userId);
+        boolean userUnlocked = isLocalUnlockedUser(accounts.userId);
 
         synchronized (accounts.cacheLock) {
             final SQLiteDatabase db = accounts.openHelper.getWritableDatabase();
@@ -530,7 +531,18 @@
                     if (obsoleteAuthType.contains(accountType)) {
                         Slog.w(TAG, "deleting account " + accountName + " because type "
                                 + accountType + "'s registered authenticator no longer exist.");
-                        db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null);
+                        db.beginTransaction();
+                        try {
+                            db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null);
+                            // Also delete from CE table if user is unlocked; if user is currently
+                            // locked the account will be removed later by syncDeCeAccountsLocked
+                            if (userUnlocked) {
+                                db.delete(CE_TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null);
+                            }
+                            db.setTransactionSuccessful();
+                        } finally {
+                            db.endTransaction();
+                        }
                         accountDeleted = true;
 
                         logRecord(db, DebugDbHelper.ACTION_AUTHENTICATOR_REMOVE, TABLE_ACCOUNTS,