Fix GAL search in EAS 12.1

* We weren't sending the proper protocol version to GAL search, which
  causes errors when using 12.1
* The simple fix is to send the agreed upon protocol version, instead
  of the default (which is 2.5)
* Replace parsing of protocol version with lookup

Bug: 2793588
Change-Id: Ib2a255d8467004ce985d2d688b37066e1e09d78a
diff --git a/src/com/android/exchange/Eas.java b/src/com/android/exchange/Eas.java
index 2fa162b..14dc537 100644
--- a/src/com/android/exchange/Eas.java
+++ b/src/com/android/exchange/Eas.java
@@ -93,5 +93,16 @@
             Log.d("Eas Debug", "Logging: " + (USER_LOG ? "User " : "") +
                     (PARSER_LOG ? "Parser " : "") + (FILE_LOG ? "File" : ""));
         }
-     }
+    }
+
+    static public Double getProtocolVersionDouble(String version) {
+        if (SUPPORTED_PROTOCOL_EX2003.equals(version)) {
+            return SUPPORTED_PROTOCOL_EX2003_DOUBLE;
+        } else if (SUPPORTED_PROTOCOL_EX2007.equals(version)) {
+            return SUPPORTED_PROTOCOL_EX2007_DOUBLE;
+        } if (SUPPORTED_PROTOCOL_EX2007_SP1.equals(version)) {
+            return SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE;
+        }
+        throw new IllegalArgumentException("illegal protocol version");
+    }
 }
diff --git a/src/com/android/exchange/EasSyncService.java b/src/com/android/exchange/EasSyncService.java
index 70d6403..c79e8d1 100644
--- a/src/com/android/exchange/EasSyncService.java
+++ b/src/com/android/exchange/EasSyncService.java
@@ -369,7 +369,7 @@
             throw new MessagingException(MessagingException.PROTOCOL_VERSION_UNSUPPORTED);
         } else {
             service.mProtocolVersion = ourVersion;
-            service.mProtocolVersionDouble = Double.parseDouble(ourVersion);
+            service.mProtocolVersionDouble = Eas.getProtocolVersionDouble(ourVersion);
             if (service.mAccount != null) {
                 service.mAccount.mProtocolVersion = ourVersion;
             }
@@ -779,7 +779,7 @@
      * @param context caller's context
      * @param accountId the account Id to search
      * @param filter the characters entered so far
-     * @return a result record
+     * @return a result record or null
      *
      * TODO: shorter timeout for interactive lookup
      * TODO: make watchdog actually work (it doesn't understand our service w/Mailbox == 0)
@@ -791,6 +791,15 @@
             HostAuth ha = HostAuth.restoreHostAuthWithId(context, acct.mHostAuthKeyRecv);
             EasSyncService svc = new EasSyncService("%GalLookupk%");
             try {
+                // If there's no protocol version set up, we haven't successfully started syncing
+                // so we can't use GAL yet
+                String protocolVersion = acct.mProtocolVersion;
+                if (protocolVersion == null) {
+                    return null;
+                } else {
+                    svc.mProtocolVersion = protocolVersion;
+                    svc.mProtocolVersionDouble = Eas.getProtocolVersionDouble(protocolVersion);
+                }
                 svc.mContext = context;
                 svc.mHostAddress = ha.mAddress;
                 svc.mUserName = ha.mLogin;
@@ -2126,7 +2135,7 @@
         if (mProtocolVersion == null) {
             mProtocolVersion = Eas.DEFAULT_PROTOCOL_VERSION;
         }
-        mProtocolVersionDouble = Double.parseDouble(mProtocolVersion);
+        mProtocolVersionDouble = Eas.getProtocolVersionDouble(mProtocolVersion);
         return true;
     }
 
diff --git a/tests/src/com/android/exchange/EasSyncServiceTests.java b/tests/src/com/android/exchange/EasSyncServiceTests.java
index dbb9bbb..2f070d5 100644
--- a/tests/src/com/android/exchange/EasSyncServiceTests.java
+++ b/tests/src/com/android/exchange/EasSyncServiceTests.java
@@ -121,4 +121,13 @@
         assertEquals(1, headers.length);
         assertEquals("key", headers[0].getValue());
     }
+
+    public void testGetProtocolVersionDouble() {
+        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2003_DOUBLE,
+                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2003));
+        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE,
+                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007));
+        assertEquals(Eas.SUPPORTED_PROTOCOL_EX2007_SP1_DOUBLE,
+                Eas.getProtocolVersionDouble(Eas.SUPPORTED_PROTOCOL_EX2007_SP1));
+    }
 }