Merge "Fix account deletion when removing security policies." into ub-mail-master
diff --git a/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java b/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java
index f2173c3..8e3575e 100644
--- a/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java
+++ b/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java
@@ -375,6 +375,10 @@
                 mService.deleteAccountPIMData(emailAddress);
             }
         }, "deleteAccountPIMData");
+        // This can be called when deleting accounts. After making this call, the caller will
+        // ask for account reconciliation, which will kill the processes. We wait for completion
+        // to avoid the race.
+        waitForCompletion();
     }
 
     /**
diff --git a/src/com/android/email/SecurityPolicy.java b/src/com/android/email/SecurityPolicy.java
index fa63c84..de1b31d 100644
--- a/src/com/android/email/SecurityPolicy.java
+++ b/src/com/android/email/SecurityPolicy.java
@@ -32,6 +32,7 @@
 import android.os.Bundle;
 import android.os.RemoteException;
 
+import com.android.email.provider.AccountReconciler;
 import com.android.email.provider.EmailProvider;
 import com.android.email.service.EmailBroadcastProcessorService;
 import com.android.email.service.EmailServiceUtils;
@@ -699,13 +700,14 @@
                     " secured account(s)");
             while (c.moveToNext()) {
                 long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
-                Uri uri = EmailProvider.uiUri("uiaccountdata", accountId);
+                Uri uri = EmailProvider.uiUri("uiaccount", accountId);
                 cr.delete(uri, null, null);
             }
         } finally {
             c.close();
         }
         policiesUpdated();
+        AccountReconciler.reconcileAccounts(context);
     }
 
     /**
diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java
index b71a1e3..d1be0b2 100644
--- a/src/com/android/email/provider/EmailProvider.java
+++ b/src/com/android/email/provider/EmailProvider.java
@@ -5501,6 +5501,8 @@
             AccountBackupRestore.backup(context);
             SecurityPolicy.getInstance(context).reducePolicies();
             MailActivityEmail.setServicesEnabledSync(context);
+            // TODO: We ought to reconcile accounts here, but some callers do this in a loop,
+            // which would be a problem when the first account reconciliation shuts us down.
             return 1;
         } catch (Exception e) {
             LogUtils.w(Logging.LOG_TAG, "Exception while deleting account", e);