EasService fixes & improvements.

- Make sure to call EmailContent.init.
- Add function to determine if an account needs to ping.
  This is currently unused but will be necessary later.
- Track the last ping/not ping status in PingSyncSynchronizer.
  This is used to know whether to restart a ping, rather than
  expecting the EasService to pass this in.

(NOTE: This CP was missed from exchange branch initially, but
some of the diffs got applied in another way.)

Change-Id: Ibb0aa45cf5174ea5dd3ba6210104a25e9060dcf8
diff --git a/src/com/android/exchange/service/EasService.java b/src/com/android/exchange/service/EasService.java
index 45c93f2..41aa48c 100644
--- a/src/com/android/exchange/service/EasService.java
+++ b/src/com/android/exchange/service/EasService.java
@@ -242,8 +242,7 @@
         try {
             return operation.performOperation();
         } finally {
-            // TODO: Fix pushEnabled param
-            mSynchronizer.syncEnd(accountId, false);
+            mSynchronizer.syncEnd(accountId);
         }
     }
 
diff --git a/src/com/android/exchange/service/PingSyncSynchronizer.java b/src/com/android/exchange/service/PingSyncSynchronizer.java
index 461e05e..7871f0f 100644
--- a/src/com/android/exchange/service/PingSyncSynchronizer.java
+++ b/src/com/android/exchange/service/PingSyncSynchronizer.java
@@ -79,6 +79,12 @@
         private PingTask mPingTask;
 
         /**
+         * Tracks whether this account wants to get push notifications, based on calls to
+         * {@link #pushModify} and {@link #pushStop} (i.e. it tracks the last requested push state).
+         */
+        private boolean mPushEnabled;
+
+        /**
          * The number of syncs that are blocked waiting for the current operation to complete.
          * Unlike Pings, sync operations do not start their own tasks and are assumed to run in
          * whatever thread calls into this class.
@@ -94,6 +100,7 @@
          */
         public AccountSyncState(final Lock lock) {
             mPingTask = null;
+            mPushEnabled = false;
             mSyncCount = 0;
             mCondition = lock.newCondition();
         }
@@ -125,17 +132,16 @@
         /**
          * Update bookkeeping when a sync completes. This includes signaling pending ops to
          * go ahead, or starting the ping if appropriate and there are no waiting ops.
-         * @param pushEnabled Whether this account is configured for push.
          * @return Whether this account is now idle.
          */
-        public boolean syncEnd(final boolean pushEnabled) {
+        public boolean syncEnd() {
             --mSyncCount;
             if (mSyncCount > 0) {
                 LogUtils.d(TAG, "Signalling a pending sync to proceed.");
                 mCondition.signal();
                 return false;
             } else {
-                if (pushEnabled) {
+                if (mPushEnabled) {
                     // TODO: Start the ping task
                     return false;
                 }
@@ -145,16 +151,15 @@
 
         /**
          * Update bookkeeping when the ping task terminates, including signaling any waiting ops.
-         * @param pushEnabled Whether this account is configured for push.
          * @return Whether this account is now idle.
          */
-        public boolean pingEnd(final boolean pushEnabled) {
+        public boolean pingEnd() {
             mPingTask = null;
             if (mSyncCount > 0) {
                 mCondition.signal();
                 return false;
             } else {
-                if (pushEnabled) {
+                if (mPushEnabled) {
                     // TODO: request a push-only sync.
                     return false;
                 }
@@ -166,6 +171,7 @@
          * Modifies or starts a ping for this account if no syncs are running.
          */
         public void pushModify() {
+            mPushEnabled = true;
             if (mSyncCount == 0) {
                 if (mPingTask == null) {
                     // No ping, no running syncs -- start a new ping.
@@ -183,6 +189,7 @@
          * Stop the currently running ping.
          */
         public void pushStop() {
+            mPushEnabled = false;
             if (mPingTask != null) {
                 mPingTask.stop();
             }
@@ -263,7 +270,7 @@
         }
     }
 
-    public void syncEnd(final long accountId, final boolean pushEnabled) {
+    public void syncEnd(final long accountId) {
         mLock.lock();
         try {
             LogUtils.d(TAG, "PSS syncEnd for account %d", accountId);
@@ -272,7 +279,7 @@
                 LogUtils.w(TAG, "PSS syncEnd for account %d but no state found", accountId);
                 return;
             }
-            if (accountState.syncEnd(pushEnabled)) {
+            if (accountState.syncEnd()) {
                 removeAccount(accountId);
             }
         } finally {
@@ -280,7 +287,7 @@
         }
     }
 
-    public void pingEnd(final long accountId, final boolean pushEnabled) {
+    public void pingEnd(final long accountId) {
         mLock.lock();
         try {
             LogUtils.d(TAG, "PSS pingEnd for account %d", accountId);
@@ -289,7 +296,7 @@
                 LogUtils.w(TAG, "PSS pingEnd for account %d but no state found", accountId);
                 return;
             }
-            if (accountState.pingEnd(pushEnabled)) {
+            if (accountState.pingEnd()) {
                 removeAccount(accountId);
             }
         } finally {
diff --git a/src/com/android/exchange/service/PingTask.java b/src/com/android/exchange/service/PingTask.java
index a016556..98b71f4 100644
--- a/src/com/android/exchange/service/PingTask.java
+++ b/src/com/android/exchange/service/PingTask.java
@@ -91,8 +91,7 @@
             mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
                     pingStatus);
         } else {
-            // TODO: Fix the pushEnabled param.
-            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), false);
+            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId());
         }
         return null;
     }
@@ -106,8 +105,7 @@
             mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
                     EasOperation.RESULT_REQUEST_FAILURE);
         } else {
-            // TODO: Fix the pushEnabled param.
-            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), false);
+            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId());
         }
     }
 }