Handle all possible exits of the PingTask.

- If we get an exception, reschedule the ping instead of just dying.
- If the task was cancelled, also reschedule.

Bug: 11249423
Change-Id: I74a39e04de73659d58f1c53b238f8a40f15d6849
diff --git a/src/com/android/exchange/service/PingTask.java b/src/com/android/exchange/service/PingTask.java
index 87a7b8e..768727c 100644
--- a/src/com/android/exchange/service/PingTask.java
+++ b/src/com/android/exchange/service/PingTask.java
@@ -18,12 +18,13 @@
 
 import android.content.Context;
 import android.os.AsyncTask;
-import android.util.Log;
 
 import com.android.emailcommon.provider.Account;
 import com.android.exchange.Eas;
 import com.android.exchange.adapter.PingParser;
+import com.android.exchange.eas.EasOperation;
 import com.android.exchange.eas.EasPing;
+import com.android.mail.utils.LogUtils;
 
 /**
  * Thread management class for Ping operations.
@@ -58,15 +59,32 @@
 
     @Override
     protected Void doInBackground(Void... params) {
-        Log.i(TAG, "Ping task starting");
+        LogUtils.i(TAG, "Ping task starting for %d", mOperation.getAccountId());
         int pingStatus;
-        do {
-            pingStatus = mOperation.doPing();
-        } while (PingParser.shouldPingAgain(pingStatus));
-        Log.i(TAG, "Ping task ending with status: " + pingStatus);
+        try {
+            do {
+                pingStatus = mOperation.doPing();
+            } while (PingParser.shouldPingAgain(pingStatus));
+        } catch (final Exception e) {
+            // TODO: This is hacky, try to be cleaner.
+            // If we get any sort of exception here, treat it like the ping returned a connection
+            // failure.
+            LogUtils.e(TAG, e, "Ping exception for account %d", mOperation.getAccountId());
+            pingStatus = EasOperation.RESULT_REQUEST_FAILURE;
+        }
+        LogUtils.i(TAG, "Ping task ending with status: %d", pingStatus);
 
         mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
                 pingStatus);
         return null;
     }
+
+    @Override
+    protected void onCancelled (Void result) {
+        // TODO: This is also hacky, should have a separate result code at minimum.
+        // If the ping is cancelled, make sure it reports something to the sync adapter.
+        LogUtils.w(TAG, "Ping cancelled for %d", mOperation.getAccountId());
+        mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
+                EasOperation.RESULT_REQUEST_FAILURE);
+    }
 }