Ping fixes.

- Clear the EasServerConnection's client whenever the ConnMan
  changes.
- Restart ping on generic errors as well.

Bug: 11249423
Change-Id: Ie6b661bec0f5d6176b01050201c514dca463aebf
diff --git a/src/com/android/exchange/eas/EasConnectionCache.java b/src/com/android/exchange/eas/EasConnectionCache.java
index 847ca9f..e4ce7b2 100644
--- a/src/com/android/exchange/eas/EasConnectionCache.java
+++ b/src/com/android/exchange/eas/EasConnectionCache.java
@@ -85,7 +85,7 @@
      */
     private EmailClientConnectionManager createConnectionManager(final Context context,
             final HostAuth hostAuth) {
-        LogUtils.d(Eas.LOG_TAG, "Creating connection for HostAuth %d", hostAuth.mId);
+        LogUtils.d(Eas.LOG_TAG, "Creating new connection manager for HostAuth %d", hostAuth.mId);
         final HttpParams params = new BasicHttpParams();
         params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 25);
         params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, sConnPerRoute);
@@ -107,18 +107,19 @@
         if (connectionManager != null) {
             final long lifetime = now - mConnectionCreationTimes.get(hostAuth.mId);
             if (lifetime > MAX_LIFETIME) {
-                LogUtils.d(Eas.LOG_TAG, "Aging out connection for HostAuth %d", hostAuth.mId);
+                LogUtils.d(Eas.LOG_TAG, "Aging out connection manager for HostAuth %d",
+                        hostAuth.mId);
                 uncacheConnectionManager(hostAuth);
                 connectionManager = null;
             }
         }
         if (connectionManager == null) {
-            LogUtils.d(Eas.LOG_TAG, "Creating new connection for HostAuth %d", hostAuth.mId);
             connectionManager = createConnectionManager(context, hostAuth);
             mConnectionMap.put(hostAuth.mId, connectionManager);
             mConnectionCreationTimes.put(hostAuth.mId, now);
         } else {
-            LogUtils.d(Eas.LOG_TAG, "Reusing cached connection for HostAuth %d", hostAuth.mId);
+            LogUtils.d(Eas.LOG_TAG, "Reusing cached connection manager for HostAuth %d",
+                    hostAuth.mId);
         }
         return connectionManager;
     }
@@ -150,7 +151,7 @@
      * @param hostAuth The {@link HostAuth} whose connection manager should be deleted.
      */
     public synchronized void uncacheConnectionManager(final HostAuth hostAuth) {
-        LogUtils.d(Eas.LOG_TAG, "Uncaching connection for HostAuth %d", hostAuth.mId);
+        LogUtils.d(Eas.LOG_TAG, "Uncaching connection manager for HostAuth %d", hostAuth.mId);
         EmailClientConnectionManager connectionManager = mConnectionMap.get(hostAuth.mId);
         if (connectionManager != null) {
             connectionManager.shutdown();
diff --git a/src/com/android/exchange/service/EasServerConnection.java b/src/com/android/exchange/service/EasServerConnection.java
index 29a84cb..93faba2 100644
--- a/src/com/android/exchange/service/EasServerConnection.java
+++ b/src/com/android/exchange/service/EasServerConnection.java
@@ -130,6 +130,11 @@
      */
     private HttpClient mClient;
 
+    /**
+     * This is used only to check when our client needs to be refreshed.
+     */
+    private EmailClientConnectionManager mClientConnectionManager;
+
     public EasServerConnection(final Context context, final Account account,
             final HostAuth hostAuth) {
         mContext = context;
@@ -144,7 +149,13 @@
     }
 
     protected EmailClientConnectionManager getClientConnectionManager() {
-        return EasConnectionCache.instance().getConnectionManager(mContext, mHostAuth);
+        final EmailClientConnectionManager connManager =
+                EasConnectionCache.instance().getConnectionManager(mContext, mHostAuth);
+        if (mClientConnectionManager != connManager) {
+            mClientConnectionManager = connManager;
+            mClient = null;
+        }
+        return connManager;
     }
 
     public void redirectHostAuth(final String newAddress) {
diff --git a/src/com/android/exchange/service/EmailSyncAdapterService.java b/src/com/android/exchange/service/EmailSyncAdapterService.java
index c6e57a6..6bbb10a 100644
--- a/src/com/android/exchange/service/EmailSyncAdapterService.java
+++ b/src/com/android/exchange/service/EmailSyncAdapterService.java
@@ -295,7 +295,10 @@
             // TODO: if (pingStatus == PingParser.STATUS_FAILED), notify UI.
             // TODO: if (pingStatus == PingParser.STATUS_REQUEST_TOO_MANY_FOLDERS), notify UI.
 
-            if (pingStatus == EasOperation.RESULT_REQUEST_FAILURE) {
+            // TODO: Should this just re-request ping if status < 0? This would do the wrong thing
+            // for e.g. auth errors, though.
+            if (pingStatus == EasOperation.RESULT_REQUEST_FAILURE ||
+                    pingStatus == EasOperation.RESULT_OTHER_FAILURE) {
                 // Request a new ping through the SyncManager. This will do the right thing if the
                 // exception was due to loss of network connectivity, etc. (i.e. it will wait for
                 // network to restore and then request it).