Add hostChanged service call; use it from UI (fixes #2148572)

* After a HostAuth has been changed for an EAS account, the SyncManager needs
  to be alerted so that it can take appropriate action
* Added hostChanged service call
* Send service call from AccountSetupExchange after a HostAuth is edited
* Stop running syncs and clear error states in hostChanged

Change-Id: I2311e2d00be81ea7829f5f4e38b2377f18c63f30
diff --git a/src/com/android/exchange/IEmailService.aidl b/src/com/android/exchange/IEmailService.aidl
index a07fab0..ec0fd5e 100644
--- a/src/com/android/exchange/IEmailService.aidl
+++ b/src/com/android/exchange/IEmailService.aidl
@@ -38,4 +38,6 @@
     void setCallback(IEmailServiceCallback cb);
 
     void setLogging(int on);
+
+    void hostChanged(long accountId);
 }
\ No newline at end of file
diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java
index 8d603c3..429de09 100644
--- a/src/com/android/exchange/SyncManager.java
+++ b/src/com/android/exchange/SyncManager.java
@@ -310,6 +310,28 @@
             reloadFolderList(SyncManager.this, accountId, false);
         }
 
+        public void hostChanged(long accountId) throws RemoteException {
+            if (INSTANCE != null) {
+                synchronized (INSTANCE.mSyncErrorMap) {
+                    // Go through the various error mailboxes
+                    for (long mailboxId: INSTANCE.mSyncErrorMap.keySet()) {
+                        SyncError error = INSTANCE.mSyncErrorMap.get(mailboxId);
+                        // If it's a login failure, look a little harder
+                        Mailbox m = Mailbox.restoreMailboxWithId(INSTANCE, mailboxId);
+                        // If it's for the account whose host has changed, clear the error
+                        if (m.mAccountKey == accountId) {
+                            error.fatal = false;
+                            error.holdEndTime = 0;
+                        }
+                    }
+                }
+                // Stop any running syncs
+                INSTANCE.stopAccountSyncs(accountId, true);
+                // Kick SyncManager
+                kick("host changed");
+            }
+        }
+
         public void setLogging(int on) throws RemoteException {
             Eas.setUserDebug(on);
         }
@@ -547,38 +569,6 @@
             INSTANCE.log("Initializing account: " + acct.mDisplayName);
         }
 
-        private void stopAccountSyncs(long acctId, boolean includeAccountMailbox) {
-            synchronized (sSyncToken) {
-                List<Long> deletedBoxes = new ArrayList<Long>();
-                for (Long mid : INSTANCE.mServiceMap.keySet()) {
-                    Mailbox box = Mailbox.restoreMailboxWithId(INSTANCE, mid);
-                    if (box != null) {
-                        if (box.mAccountKey == acctId) {
-                            if (!includeAccountMailbox &&
-                                    box.mType == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) {
-                                AbstractSyncService svc = INSTANCE.mServiceMap.get(mid);
-                                if (svc != null) {
-                                    svc.stop();
-                                }
-                                continue;
-                            }
-                            AbstractSyncService svc = INSTANCE.mServiceMap.get(mid);
-                            if (svc != null) {
-                                svc.stop();
-                                Thread t = svc.mThread;
-                                if (t != null) {
-                                    t.interrupt();
-                                }
-                            }
-                            deletedBoxes.add(mid);
-                        }
-                    }
-                }
-                for (Long mid : deletedBoxes) {
-                    releaseMailbox(mid);
-                }
-            }
-        }
     }
 
     class MailboxObserver extends ContentObserver {
@@ -913,6 +903,39 @@
         return sClientConnectionManager;
     }
 
+    private void stopAccountSyncs(long acctId, boolean includeAccountMailbox) {
+        synchronized (sSyncToken) {
+            List<Long> deletedBoxes = new ArrayList<Long>();
+            for (Long mid : INSTANCE.mServiceMap.keySet()) {
+                Mailbox box = Mailbox.restoreMailboxWithId(INSTANCE, mid);
+                if (box != null) {
+                    if (box.mAccountKey == acctId) {
+                        if (!includeAccountMailbox &&
+                                box.mType == Mailbox.TYPE_EAS_ACCOUNT_MAILBOX) {
+                            AbstractSyncService svc = INSTANCE.mServiceMap.get(mid);
+                            if (svc != null) {
+                                svc.stop();
+                            }
+                            continue;
+                        }
+                        AbstractSyncService svc = INSTANCE.mServiceMap.get(mid);
+                        if (svc != null) {
+                            svc.stop();
+                            Thread t = svc.mThread;
+                            if (t != null) {
+                                t.interrupt();
+                            }
+                        }
+                        deletedBoxes.add(mid);
+                    }
+                }
+            }
+            for (Long mid : deletedBoxes) {
+                releaseMailbox(mid);
+            }
+        }
+    }
+
     static public void reloadFolderList(Context context, long accountId, boolean force) {
         if (INSTANCE == null) return;
         Cursor c = context.getContentResolver().query(Mailbox.CONTENT_URI,
@@ -975,7 +998,7 @@
     static public void folderListReloaded(long acctId) {
         if (INSTANCE != null) {
             AccountObserver obs = INSTANCE.mAccountObserver;
-            obs.stopAccountSyncs(acctId, false);
+            INSTANCE.stopAccountSyncs(acctId, false);
             kick("reload folder list");
         }
     }