Fix a merge error I made

Change-Id: I3c022d06e4f036962f12d167af21d73eeb59922f
diff --git a/src/com/android/exchange/service/EasService.java b/src/com/android/exchange/service/EasService.java
index dd8c8f0..82d8318 100644
--- a/src/com/android/exchange/service/EasService.java
+++ b/src/com/android/exchange/service/EasService.java
@@ -17,6 +17,7 @@
 package com.android.exchange.service;
 
 import android.app.Service;
+import android.content.ContentResolver;
 import android.content.Intent;
 import android.content.SyncResult;
 import android.database.Cursor;
@@ -27,7 +28,10 @@
 import android.provider.ContactsContract;
 import android.text.TextUtils;
 
+import com.android.emailcommon.provider.Account;
+import com.android.emailcommon.provider.EmailContent;
 import com.android.emailcommon.provider.HostAuth;
+import com.android.emailcommon.provider.Mailbox;
 import com.android.emailcommon.service.IEmailService;
 import com.android.emailcommon.service.IEmailServiceCallback;
 import com.android.emailcommon.service.SearchParams;
@@ -37,6 +41,9 @@
 import com.android.exchange.eas.EasOperation;
 import com.android.mail.utils.LogUtils;
 
+import java.util.HashSet;
+import java.util.Set;
+
 /**
  * Service to handle all communication with the EAS server. Note that this is completely decoupled
  * from the sync adapters; sync adapters should make blocking calls on this service to actually
@@ -174,9 +181,6 @@
 
     @Override
     public void onCreate() {
-<<<<<<< HEAD
-        // TODO: Restart all pings that are needed.
-=======
         super.onCreate();
         EmailContent.init(this);
         AUTHORITIES_TO_SYNC = new String[] {
@@ -190,7 +194,6 @@
         // task to complete. The task will stop the service if necessary after it's done.
         startService(new Intent(this, EasService.class));
         new RestartPingsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
->>>>>>> c3ad5a3... Flesh out EasService some, and start using it.
     }
 
     @Override
@@ -237,4 +240,79 @@
             mSynchronizer.syncEnd(accountId, false);
         }
     }
+
+    /**
+     * Determine whether this account is configured with folders that are ready for push
+     * notifications.
+     * @param account The {@link Account} that we're interested in.
+     * @return Whether this account needs to ping.
+     */
+    public boolean pingNeededForAccount(final Account account) {
+        // Check account existence.
+        if (account == null || account.mId == Account.NO_ACCOUNT) {
+            LogUtils.d(TAG, "Do not ping: Account not found or not valid");
+            return false;
+        }
+
+        // Check if account is configured for a push sync interval.
+        if (account.mSyncInterval != Account.CHECK_INTERVAL_PUSH) {
+            LogUtils.d(TAG, "Do not ping: Account %d not configured for push", account.mId);
+            return false;
+        }
+
+        // Check security hold status of the account.
+        if ((account.mFlags & Account.FLAGS_SECURITY_HOLD) != 0) {
+            LogUtils.d(TAG, "Do not ping: Account %d is on security hold", account.mId);
+            return false;
+        }
+
+        // Check if the account has performed at least one sync so far (accounts must perform
+        // the initial sync before push is possible).
+        if (EmailContent.isInitialSyncKey(account.mSyncKey)) {
+            LogUtils.d(TAG, "Do not ping: Account %d has not done initial sync", account.mId);
+            return false;
+        }
+
+        // Check that there's at least one mailbox that is both configured for push notifications,
+        // and whose content type is enabled for sync in the account manager.
+        final android.accounts.Account amAccount = new android.accounts.Account(
+                        account.mEmailAddress, Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE);
+
+        final Set<String> authsToSync = getAuthoritiesToSync(amAccount, AUTHORITIES_TO_SYNC);
+        // If we have at least one sync-enabled content type, check for syncing mailboxes.
+        if (!authsToSync.isEmpty()) {
+            final Cursor c = Mailbox.getMailboxesForPush(getContentResolver(), account.mId);
+            if (c != null) {
+                try {
+                    while (c.moveToNext()) {
+                        final int mailboxType = c.getInt(Mailbox.CONTENT_TYPE_COLUMN);
+                        if (authsToSync.contains(Mailbox.getAuthority(mailboxType))) {
+                            return true;
+                        }
+                    }
+                } finally {
+                    c.close();
+                }
+            }
+        }
+        LogUtils.d(TAG, "Do not ping: Account %d has no folders configured for push", account.mId);
+        return false;
+    }
+
+    /**
+     * Determine which content types are set to sync for an account.
+     * @param account The account whose sync settings we're looking for.
+     * @param authorities All possible authorities we could care about.
+     * @return The authorities for the content types we want to sync for account.
+     */
+    private static Set<String> getAuthoritiesToSync(final android.accounts.Account account,
+            final String[] authorities) {
+        final HashSet<String> authsToSync = new HashSet();
+        for (final String authority : authorities) {
+            if (ContentResolver.getSyncAutomatically(account, authority)) {
+                authsToSync.add(authority);
+            }
+        }
+        return authsToSync;
+    }
 }