am bc8bc050: During upgrade, ignore protocols whose strings don\'t change.

* commit 'bc8bc050b080287c35241f5ab0c310bbbdb6e805':
  During upgrade, ignore protocols whose strings don't change.
diff --git a/src/com/android/email/service/EmailBroadcastProcessorService.java b/src/com/android/email/service/EmailBroadcastProcessorService.java
index 4e54214..ffad8a0 100644
--- a/src/com/android/email/service/EmailBroadcastProcessorService.java
+++ b/src/com/android/email/service/EmailBroadcastProcessorService.java
@@ -31,6 +31,7 @@
 import android.os.Bundle;
 import android.provider.CalendarContract;
 import android.provider.ContactsContract;
+import android.text.TextUtils;
 import android.text.format.DateUtils;
 
 import com.android.email.Preferences;
@@ -45,11 +46,14 @@
 import com.android.emailcommon.provider.EmailContent.AccountColumns;
 import com.android.emailcommon.provider.HostAuth;
 import com.android.mail.utils.LogUtils;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Maps;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * The service that really handles broadcast intents on a worker thread.
@@ -231,13 +235,23 @@
         return Collections.emptyMap();
     }
 
+    @VisibleForTesting
+    protected static void removeNoopUpgrades(final Map<String, String> protocolMap) {
+        final Set<String> keySet = new HashSet<String>(protocolMap.keySet());
+        for (final String key : keySet) {
+            if (TextUtils.equals(key, protocolMap.get(key))) {
+                protocolMap.remove(key);
+            }
+        }
+    }
+
     private void onAppUpgrade() {
         if (isComponentDisabled(EmailUpgradeBroadcastReceiver.class)) {
             return;
         }
-        // TODO: Only do this for Email2Google.
-        // When upgrading to Email2Google, we need to essentially rename the account manager
-        // type for all existing accounts, so we add new ones and delete the old.
+        // When upgrading to a version that changes the protocol strings, we need to essentially
+        // rename the account manager type for all existing accounts, so we add new ones and delete
+        // the old.
         // We specify the translations in this map. We map from old protocol name to new protocol
         // name, and from protocol name + "_type" to new account manager type name. (Email1 did
         // not use distinct account manager types for POP and IMAP, but Email2 does, hence this
@@ -245,14 +259,20 @@
         final Map<String, String> protocolMap = Maps.newHashMapWithExpectedSize(4);
         protocolMap.put("imap", getString(R.string.protocol_legacy_imap));
         protocolMap.put("pop3", getString(R.string.protocol_pop3));
-        protocolMap.put("imap_type", getString(R.string.account_manager_type_legacy_imap));
-        protocolMap.put("pop3_type", getString(R.string.account_manager_type_pop3));
-        updateAccountManagerAccountsOfType("com.android.email", protocolMap);
+        removeNoopUpgrades(protocolMap);
+        if (!protocolMap.isEmpty()) {
+            protocolMap.put("imap_type", getString(R.string.account_manager_type_legacy_imap));
+            protocolMap.put("pop3_type", getString(R.string.account_manager_type_pop3));
+            updateAccountManagerAccountsOfType("com.android.email", protocolMap);
+        }
 
         protocolMap.clear();
         protocolMap.put("eas", getString(R.string.protocol_eas));
-        protocolMap.put("eas_type", getString(R.string.account_manager_type_exchange));
-        updateAccountManagerAccountsOfType("com.android.exchange", protocolMap);
+        removeNoopUpgrades(protocolMap);
+        if (!protocolMap.isEmpty()) {
+            protocolMap.put("eas_type", getString(R.string.account_manager_type_exchange));
+            updateAccountManagerAccountsOfType("com.android.exchange", protocolMap);
+        }
 
         // Disable the old authenticators.
         disableComponent(LegacyEmailAuthenticatorService.class);
diff --git a/tests/src/com/android/email/service/EmailBroadcastProcessorServiceTests.java b/tests/src/com/android/email/service/EmailBroadcastProcessorServiceTests.java
index 29c61fd..d171335 100644
--- a/tests/src/com/android/email/service/EmailBroadcastProcessorServiceTests.java
+++ b/tests/src/com/android/email/service/EmailBroadcastProcessorServiceTests.java
@@ -135,4 +135,17 @@
         assertEquals(0x00000008, accountFlags6);
     }
 
+    public void testNoopRemover() {
+        final Map<String, String> protocolMap = Maps.newHashMap();
+        protocolMap.put("imap", "imap");
+        protocolMap.put("pop3", "gPop3");
+
+        EmailBroadcastProcessorService.removeNoopUpgrades(protocolMap);
+
+        final Map<String, String> protocolMapExpected = Maps.newHashMap();
+        protocolMapExpected.put("pop3", "gPop3");
+
+        assertEquals(protocolMap, protocolMapExpected);
+    }
+
 }