Some code cleanup/refactor.

- In places where we passed the connection manager around,
  don't bother now that we have caching.
- Move mHostAuth to EasServerConnection (since all
  connections need one).

Change-Id: Id501482510e39b85235e812da5cda2d12d900d89
diff --git a/src/com/android/exchange/service/EasAccountSyncHandler.java b/src/com/android/exchange/service/EasAccountSyncHandler.java
index 0eaf857..2d3686e 100644
--- a/src/com/android/exchange/service/EasAccountSyncHandler.java
+++ b/src/com/android/exchange/service/EasAccountSyncHandler.java
@@ -35,7 +35,7 @@
     private boolean tryProvision() {
         try {
             final EasAccountValidator validator = new EasAccountValidator(mContext, mHostAuth);
-            return validator.tryProvision(getClientConnectionManager(mHostAuth), mAccount);
+            return validator.tryProvision(mAccount);
         } catch (final IOException e) {
 
         }
@@ -91,7 +91,12 @@
                 } else if (EasResponse.isAuthError(code)) {
                     return SyncStatus.FAILURE_LOGIN;
                 } else if (EasResponse.isRedirectError(code)) {
-                    if (redirectHostAuth(resp.getRedirectAddress())) {
+                    final String newAddress = resp.getRedirectAddress();
+                    if (newAddress != null) {
+                        redirectHostAuth(newAddress);
+                        final ContentValues haValues = new ContentValues(1);
+                        haValues.put(HostAuthColumns.ADDRESS, newAddress);
+                        mHostAuth.update(mContext, haValues);
                         needsResync = true;
                     } else {
                         // TODO: Perhaps a more severe error here?
@@ -106,20 +111,4 @@
         return SyncStatus.SUCCESS;
     }
 
-    /**
-     * Reset our local HostAuth's address and save the change to the DB.
-     * @param newAddress The new address for the HostAuth.
-     * @return Whether the redirect was processed.
-     */
-    private boolean redirectHostAuth(final String newAddress) {
-        if (newAddress != null) {
-            mHostAuth.mAddress = newAddress;
-            final ContentValues haValues = new ContentValues(1);
-            haValues.put(HostAuthColumns.ADDRESS, newAddress);
-            mHostAuth.update(mContext, haValues);
-            // TODO: When we start caching connections, make sure to uncache here.
-        }
-        return newAddress != null;
-    }
-
 }
diff --git a/src/com/android/exchange/service/EasAccountValidator.java b/src/com/android/exchange/service/EasAccountValidator.java
index 7914c1d..dc972df 100644
--- a/src/com/android/exchange/service/EasAccountValidator.java
+++ b/src/com/android/exchange/service/EasAccountValidator.java
@@ -12,7 +12,6 @@
 import com.android.emailcommon.provider.Policy;
 import com.android.emailcommon.service.EmailServiceProxy;
 import com.android.emailcommon.service.PolicyServiceProxy;
-import com.android.emailcommon.utility.EmailClientConnectionManager;
 import com.android.exchange.CommandStatusException;
 import com.android.exchange.CommandStatusException.CommandStatus;
 import com.android.exchange.Eas;
@@ -47,12 +46,11 @@
     private static final String PROVISION_STATUS_PARTIAL = "2";
 
     // Set of Exchange protocol versions we understand.
-    private static HashSet<String> SUPPORTED_PROTOCOL_VERSIONS = Sets.newHashSet(
+    private static final HashSet<String> SUPPORTED_PROTOCOL_VERSIONS = Sets.newHashSet(
             Eas.SUPPORTED_PROTOCOL_EX2003,
             Eas.SUPPORTED_PROTOCOL_EX2007, Eas.SUPPORTED_PROTOCOL_EX2007_SP1,
             Eas.SUPPORTED_PROTOCOL_EX2010, Eas.SUPPORTED_PROTOCOL_EX2010_SP1);
 
-    private final HostAuth mHostAuth;
     private String mProtocolVersion;
     private Double mProtocolVersionDouble;
     private int mRedirectCount;
@@ -65,9 +63,9 @@
     }
 
     public EasAccountValidator(final Context context, final HostAuth hostAuth) {
-        super(context);
-        mHostAuth = hostAuth;
+        super(context, hostAuth);
         mProtocolVersion = null;
+        mProtocolVersionDouble = null;
         mRedirectCount = 0;
     }
 
@@ -119,9 +117,8 @@
      * @return A status code for getting the protocol version. If NO_ERROR, then mProtocolVersion
      *     will be set correctly.
      */
-    private int doHttpOptions(final EmailClientConnectionManager connectionManager)
-            throws IOException, RedirectException {
-        final EasResponse resp = sendHttpClientOptions(connectionManager, mHostAuth);
+    private int doHttpOptions() throws IOException, RedirectException {
+        final EasResponse resp = sendHttpClientOptions();
         final int resultCode;
         try {
             final int code = resp.getStatus();
@@ -163,15 +160,14 @@
     /**
      * Send a FolderSync request to the server to verify the response -- we aren't actually
      * syncing the account at this point, just want to make sure we get valid output.
-     * @param connectionManager The connection for this request.
      * @param account The account we're verifying.
      * @return A status code indicating the result of this check.
      * @throws IOException
      * @throws CommandStatusException
      * @throws RedirectException
      */
-    private int doFolderSync(final EmailClientConnectionManager connectionManager,
-            final Account account) throws IOException, CommandStatusException, RedirectException {
+    private int doFolderSync(final Account account)
+            throws IOException, CommandStatusException, RedirectException {
         LogUtils.i(TAG, "Try FolderSync for %s, %s, ssl = %s", mHostAuth.mAddress, mHostAuth.mLogin,
                 mHostAuth.shouldUseSsl() ? "1" : "0");
 
@@ -180,8 +176,7 @@
         final Serializer s = new Serializer();
         s.start(Tags.FOLDER_FOLDER_SYNC).start(Tags.FOLDER_SYNC_KEY).text(syncKey)
             .end().end().done();
-        final EasResponse resp = sendHttpClientPost(connectionManager, account, mHostAuth,
-                "FolderSync", s.toByteArray());
+        final EasResponse resp = sendHttpClientPost(account, "FolderSync", s.toByteArray());
         final int resultCode;
         try {
             final int code = resp.getStatus();
@@ -222,8 +217,7 @@
         return resultCode;
     }
 
-    private boolean sendSettings(final EmailClientConnectionManager connectionManager,
-            final Account account) throws IOException {
+    private boolean sendSettings(final Account account) throws IOException {
         final Serializer s = new Serializer();
         s.start(Tags.SETTINGS_SETTINGS);
         s.start(Tags.SETTINGS_DEVICE_INFORMATION).start(Tags.SETTINGS_SET);
@@ -231,8 +225,7 @@
         s.data(Tags.SETTINGS_OS, "Android " + Build.VERSION.RELEASE);
         s.data(Tags.SETTINGS_USER_AGENT, USER_AGENT);
         s.end().end().end().done(); // SETTINGS_SET, SETTINGS_DEVICE_INFORMATION, SETTINGS_SETTINGS
-        final EasResponse resp = sendHttpClientPost(connectionManager, account, mHostAuth,
-                "Settings", s.toByteArray());
+        final EasResponse resp = sendHttpClientPost(account, "Settings", s.toByteArray());
         try {
             if (resp.getStatus() == HttpStatus.SC_OK) {
                 return new SettingsParser(resp.getInputStream()).parse();
@@ -249,21 +242,18 @@
             Eas.SUPPORTED_PROTOCOL_EX2007_DOUBLE) ? EAS_12_POLICY_TYPE : EAS_2_POLICY_TYPE;
     }
 
-    private void acknowledgeRemoteWipe(final EmailClientConnectionManager connectionManager,
-            final Account account, final String tempKey)
+    private void acknowledgeRemoteWipe(final Account account, final String tempKey)
             throws IOException {
-        acknowledgeProvisionImpl(connectionManager, account, tempKey, PROVISION_STATUS_OK, true);
+        acknowledgeProvisionImpl(account, tempKey, PROVISION_STATUS_OK, true);
     }
 
-    private String acknowledgeProvision(final EmailClientConnectionManager connectionManager,
-            final Account account, final String tempKey, final String result)
-            throws IOException {
-        return acknowledgeProvisionImpl(connectionManager, account, tempKey, result, false);
+    private String acknowledgeProvision(final Account account, final String tempKey,
+            final String result) throws IOException {
+        return acknowledgeProvisionImpl(account, tempKey, result, false);
     }
 
-    private String acknowledgeProvisionImpl(final EmailClientConnectionManager connectionManager,
-            final Account account, final String tempKey, final String status,
-            final boolean remoteWipe) throws IOException {
+    private String acknowledgeProvisionImpl(final Account account, final String tempKey,
+            final String status, final boolean remoteWipe) throws IOException {
         final Serializer s = new Serializer();
         s.start(Tags.PROVISION_PROVISION).start(Tags.PROVISION_POLICIES);
         s.start(Tags.PROVISION_POLICY);
@@ -280,8 +270,7 @@
             s.end();
         }
         s.end().done(); // PROVISION_PROVISION
-        EasResponse resp = sendHttpClientPost(connectionManager, account, mHostAuth, "Provision",
-                s.toByteArray());
+        EasResponse resp = sendHttpClientPost(account, "Provision", s.toByteArray());
         try {
             if (resp.getStatus() == HttpStatus.SC_OK) {
                 final ProvisionParser pp = new ProvisionParser(mContext, resp.getInputStream());
@@ -303,8 +292,7 @@
         return null;
     }
 
-    public ProvisionParser canProvision(final EmailClientConnectionManager connectionManager,
-            final Account account) throws IOException {
+    public ProvisionParser canProvision(final Account account) throws IOException {
         final Serializer s = new Serializer();
         s.start(Tags.PROVISION_PROVISION);
         if (mProtocolVersionDouble >= Eas.SUPPORTED_PROTOCOL_EX2010_SP1_DOUBLE) {
@@ -324,8 +312,7 @@
         s.start(Tags.PROVISION_POLICY);
         s.data(Tags.PROVISION_POLICY_TYPE, getPolicyType(mProtocolVersionDouble));
         s.end().end().end().done(); // PROVISION_POLICY, PROVISION_POLICIES, PROVISION_PROVISION
-        final EasResponse resp = sendHttpClientPost(connectionManager, account, mHostAuth,
-                "Provision", s.toByteArray());
+        final EasResponse resp = sendHttpClientPost(account, "Provision", s.toByteArray());
         try {
             int code = resp.getStatus();
             if (code == HttpStatus.SC_OK) {
@@ -338,8 +325,8 @@
                         // In EAS 14.0, we need the final security key in order to use the settings
                         // command
                         final String policyKey =
-                                acknowledgeProvision(connectionManager, account,
-                                        pp.getSecuritySyncKey(), PROVISION_STATUS_OK);
+                                acknowledgeProvision(account, pp.getSecuritySyncKey(),
+                                        PROVISION_STATUS_OK);
                         if (policyKey != null) {
                             pp.setSecuritySyncKey(policyKey);
                         }
@@ -348,8 +335,8 @@
                         // accommodate the required policies).  The server will agree to this if the
                         // "allow non-provisionable devices" setting is enabled on the server
                         LogUtils.i(TAG, "PolicySet is NOT fully supportable");
-                        if (acknowledgeProvision(connectionManager, account,
-                                pp.getSecuritySyncKey(), PROVISION_STATUS_PARTIAL) != null) {
+                        if (acknowledgeProvision(account, pp.getSecuritySyncKey(),
+                                PROVISION_STATUS_PARTIAL) != null) {
                             // The server's ok with our inability to support policies, so we'll
                             // clear them
                             pp.clearUnsupportablePolicies();
@@ -367,13 +354,12 @@
     }
 
 
-    public boolean tryProvision(final EmailClientConnectionManager connectionManager,
-            final Account account) throws IOException {
+    public boolean tryProvision(final Account account) throws IOException {
         mProtocolVersion = account.mProtocolVersion;
         mProtocolVersionDouble = Eas.getProtocolVersionDouble(mProtocolVersion);
         // First, see if provisioning is even possible, i.e. do we support the policies required
         // by the server
-        ProvisionParser pp = canProvision(connectionManager, account);
+        ProvisionParser pp = canProvision(account);
         if (pp == null) return false;
         // Get the policies from ProvisionParser
         Policy policy = pp.getPolicy();
@@ -397,7 +383,7 @@
             // we wipe the device regardless of any errors in acknowledgment
             try {
                 LogUtils.i(TAG, "!!! Acknowledging remote wipe to server");
-                acknowledgeRemoteWipe(connectionManager, account, pp.getSecuritySyncKey());
+                acknowledgeRemoteWipe(account, pp.getSecuritySyncKey());
             } catch (Exception e) {
                 // Because remote wipe is such a high priority task, we don't want to
                 // circumvent it if there's an exception in acknowledgment
@@ -414,8 +400,8 @@
             if (mProtocolVersionDouble == Eas.SUPPORTED_PROTOCOL_EX2010_DOUBLE) {
                 securitySyncKey = pp.getSecuritySyncKey();
             } else {
-                securitySyncKey = acknowledgeProvision(connectionManager, account,
-                        pp.getSecuritySyncKey(), PROVISION_STATUS_OK);
+                securitySyncKey = acknowledgeProvision(account, pp.getSecuritySyncKey(),
+                        PROVISION_STATUS_OK);
             }
             if (securitySyncKey != null) {
                 // If attachment policies have changed, fix up any affected attachment records
@@ -440,12 +426,10 @@
     public Bundle validate() {
         LogUtils.i(TAG, "Testing EAS: %s, %s, ssl = %s", mHostAuth.mAddress, mHostAuth.mLogin,
                 mHostAuth.shouldUseSsl() ? "1" : "0");
-        EmailClientConnectionManager connectionManager = getClientConnectionManager(mHostAuth);
-
         final Bundle bundle = new Bundle();
         if (mHostAuth.mClientCertAlias != null) {
             try {
-                connectionManager.registerClientCert(mContext, mHostAuth);
+                getClientConnectionManager().registerClientCert(mContext, mHostAuth);
             } catch (final CertificateException e) {
                 // The client certificate the user specified is invalid/inaccessible.
                 bundle.putInt(EmailServiceProxy.VALIDATE_BUNDLE_RESULT_CODE,
@@ -460,7 +444,7 @@
         // Need a nested try here because the provisioning exception handler can throw IOException.
         try {
             try {
-                final int optionsResult = doHttpOptions(connectionManager);
+                final int optionsResult = doHttpOptions();
                 if (optionsResult != MessagingException.NO_ERROR) {
                     bundle.putInt(EmailServiceProxy.VALIDATE_BUNDLE_RESULT_CODE, optionsResult);
                     return bundle;
@@ -468,12 +452,12 @@
                 account.mProtocolVersion = mProtocolVersion;
                 bundle.putString(EmailServiceProxy.VALIDATE_BUNDLE_PROTOCOL_VERSION,
                         mProtocolVersion);
-                resultCode = doFolderSync(connectionManager, account);
+                resultCode = doFolderSync(account);
             } catch (final CommandStatusException e) {
                 final int status = e.mStatus;
                 if (CommandStatus.isNeedsProvisioning(status)) {
                     // Get the policies and see if we are able to support them
-                    final ProvisionParser pp = canProvision(connectionManager, account);
+                    final ProvisionParser pp = canProvision(account);
                     if (pp != null && pp.hasSupportablePolicySet()) {
                         // Set the proper result code and save the PolicySet in our Bundle
                         resultCode = MessagingException.SECURITY_POLICIES_REQUIRED;
@@ -481,7 +465,7 @@
                                 pp.getPolicy());
                         if (mProtocolVersionDouble == Eas.SUPPORTED_PROTOCOL_EX2010_DOUBLE) {
                             account.mSecuritySyncKey = pp.getSecuritySyncKey();
-                            if (!sendSettings(connectionManager, account)) {
+                            if (!sendSettings(account)) {
                                 LogUtils.i(TAG, "Denied access: %s",
                                         CommandStatus.toString(status));
                                 resultCode = MessagingException.ACCESS_DENIED;
@@ -507,8 +491,7 @@
                 // We handle a limited number of redirects by recursively calling ourself.
                 if (mRedirectCount < MAX_REDIRECTS && e.mRedirectAddress != null) {
                     ++mRedirectCount;
-                    mHostAuth.mAddress = e.mRedirectAddress;
-                    uncacheConnectionManager(mHostAuth);
+                    redirectHostAuth(e.mRedirectAddress);
                     return validate();
                 } else {
                     resultCode = MessagingException.UNSPECIFIED_EXCEPTION;
diff --git a/src/com/android/exchange/service/EasPingSyncHandler.java b/src/com/android/exchange/service/EasPingSyncHandler.java
index da72797..a63fae8 100644
--- a/src/com/android/exchange/service/EasPingSyncHandler.java
+++ b/src/com/android/exchange/service/EasPingSyncHandler.java
@@ -34,7 +34,6 @@
 public class EasPingSyncHandler extends EasServerConnection {
     private final ContentResolver mContentResolver;
     private final Account mAccount;
-    private final HostAuth mHostAuth;
     private final PingTask mPingTask;
 
     private class PingTask extends AsyncTask<Void, Void, Void> {
@@ -87,7 +86,7 @@
                         // in handleOneMailbox when the Serializer is first created.
                         // If either side changes, the other must be kept in sync.
                         s.end().end().done();
-                        final EasResponse resp = sendHttpClientPost(mAccount, mHostAuth, "Ping",
+                        final EasResponse resp = sendHttpClientPost(mAccount, "Ping",
                                 s.toByteArray(), PING_HEARTBEAT);
                         try {
                             continuePing = handleResponse(resp, amAccount);
@@ -289,10 +288,9 @@
 
     public EasPingSyncHandler(final Context context, final Account account,
             final EmailSyncAdapterService.SyncHandlerSychronizer syncHandlerMap) {
-        super(context);
+        super(context, HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
         mContentResolver = context.getContentResolver();
         mAccount = account;
-        mHostAuth = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv);
         mPingTask = new PingTask(syncHandlerMap);
         mPingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     }
diff --git a/src/com/android/exchange/service/EasServerConnection.java b/src/com/android/exchange/service/EasServerConnection.java
index 71d7a55..78dc9ba 100644
--- a/src/com/android/exchange/service/EasServerConnection.java
+++ b/src/com/android/exchange/service/EasServerConnection.java
@@ -60,6 +60,7 @@
     };
 
     protected final Context mContext;
+    protected final HostAuth mHostAuth;
 
     // Bookkeeping for interrupting a POST. This is primarily for use by Ping (there's currently
     // no mechanism for stopping a sync).
@@ -114,16 +115,18 @@
     }
     private static final ConnectionManagerCache sConnectionManagers = new ConnectionManagerCache();
 
-    protected EasServerConnection(final Context context) {
+    protected EasServerConnection(final Context context, final HostAuth hostAuth) {
         mContext = context;
+        mHostAuth = hostAuth;
     }
 
-    protected EmailClientConnectionManager getClientConnectionManager(final HostAuth hostAuth) {
-        return sConnectionManagers.getConnectionManager(mContext, hostAuth);
+    protected EmailClientConnectionManager getClientConnectionManager() {
+        return sConnectionManagers.getConnectionManager(mContext, mHostAuth);
     }
 
-    protected void uncacheConnectionManager(final HostAuth hostAuth) {
-        sConnectionManagers.uncacheConnectionManager(hostAuth);
+    protected void redirectHostAuth(final String newAddress) {
+        mHostAuth.mAddress = newAddress;
+        sConnectionManagers.uncacheConnectionManager(mHostAuth);
     }
 
     private HttpClient getHttpClient(final EmailClientConnectionManager connectionManager,
@@ -135,12 +138,12 @@
         return new DefaultHttpClient(connectionManager, params);
     }
 
-    private String makeAuthString(final HostAuth hostAuth) {
-        final String cs = hostAuth.mLogin + ":" + hostAuth.mPassword;
+    private String makeAuthString() {
+        final String cs = mHostAuth.mLogin + ":" + mHostAuth.mPassword;
         return "Basic " + Base64.encodeToString(cs.getBytes(), Base64.NO_WRAP);
     }
 
-    private String makeUserString(final HostAuth hostAuth) {
+    private String makeUserString() {
         String deviceId = "";
         try {
             deviceId = Device.getDeviceId(mContext);
@@ -149,20 +152,20 @@
             // Otherwise use a better deviceId default.
             deviceId = "0";
         }
-        return "&User=" + Uri.encode(hostAuth.mLogin) + "&DeviceId=" +
+        return "&User=" + Uri.encode(mHostAuth.mLogin) + "&DeviceId=" +
                 deviceId + "&DeviceType=" + DEVICE_TYPE;
     }
 
-    private String makeBaseUriString(final HostAuth hostAuth) {
-        return EmailClientConnectionManager.makeScheme(hostAuth.shouldUseSsl(),
-                hostAuth.shouldTrustAllServerCerts(), hostAuth.mClientCertAlias) +
-                "://" + hostAuth.mAddress + "/Microsoft-Server-ActiveSync";
+    private String makeBaseUriString() {
+        return EmailClientConnectionManager.makeScheme(mHostAuth.shouldUseSsl(),
+                mHostAuth.shouldTrustAllServerCerts(), mHostAuth.mClientCertAlias) +
+                "://" + mHostAuth.mAddress + "/Microsoft-Server-ActiveSync";
     }
 
-    private String makeUriString(final HostAuth hostAuth, final String cmd, final String extra) {
-        String uriString = makeBaseUriString(hostAuth);
+    private String makeUriString(final String cmd, final String extra) {
+        String uriString = makeBaseUriString();
         if (cmd != null) {
-            uriString += "?Cmd=" + cmd + makeUserString(hostAuth);
+            uriString += "?Cmd=" + cmd + makeUserString();
         }
         if (extra != null) {
             uriString += extra;
@@ -185,13 +188,12 @@
     /**
      * Set standard HTTP headers, using a policy key if required
      * @param account The Account for which we are communicating.
-     * @param hostAuth the HostAuth for account.
      * @param method the method we are going to send
      * @param usePolicyKey whether or not a policy key should be sent in the headers
      */
-    private void setHeaders(final Account account, final HostAuth hostAuth,
-            final HttpRequestBase method, final boolean usePolicyKey) {
-        method.setHeader("Authorization", makeAuthString(hostAuth));
+    private void setHeaders(final Account account, final HttpRequestBase method,
+            final boolean usePolicyKey) {
+        method.setHeader("Authorization", makeAuthString());
         method.setHeader("MS-ASProtocolVersion", getProtocolVersion(account));
         method.setHeader("User-Agent", USER_AGENT);
         method.setHeader("Accept-Encoding", "gzip");
@@ -212,40 +214,31 @@
 
     /**
      * Send an http OPTIONS request to server.
-     * @param connectionManager The {@link EmailClientConnectionManager} to use for this request.
-     * @param hostAuth The {@link HostAuth} for the server we're getting options for.
      * @return The {@link EasResponse} from the Exchange server.
      * @throws IOException
      */
-    protected EasResponse sendHttpClientOptions(
-            final EmailClientConnectionManager connectionManager,
-            final HostAuth hostAuth) throws IOException {
+    protected EasResponse sendHttpClientOptions() throws IOException {
+        final EmailClientConnectionManager connectionManager = getClientConnectionManager();
         // For OPTIONS, just use the base string and the single header
-        final HttpOptions method = new HttpOptions(URI.create(makeBaseUriString(hostAuth)));
-        method.setHeader("Authorization", makeAuthString(hostAuth));
+        final HttpOptions method = new HttpOptions(URI.create(makeBaseUriString()));
+        method.setHeader("Authorization", makeAuthString());
         method.setHeader("User-Agent", USER_AGENT);
         final HttpClient client = getHttpClient(connectionManager, COMMAND_TIMEOUT);
         return EasResponse.fromHttpRequest(connectionManager, client, method);
     }
 
-    protected EasResponse sendHttpClientOptions(final HostAuth hostAuth) throws IOException {
-        return sendHttpClientOptions(getClientConnectionManager(hostAuth), hostAuth);
-    }
-
     /**
      * Send a POST request to the server.
-     * @param connectionManager The {@link EmailClientConnectionManager} to use for this request.
      * @param account The {@link Account} for which we're sending the POST.
-     * @param hostAuth The {@link HostAuth} for account.
      * @param cmd The command we're sending to the server.
      * @param entity The {@link HttpEntity} containing the payload of the message.
      * @param timeout The timeout for this POST.
      * @return The response from the Exchange server.
      * @throws IOException
      */
-    protected EasResponse sendHttpClientPost(final EmailClientConnectionManager connectionManager,
-            final Account account, final HostAuth hostAuth, String cmd, final HttpEntity entity,
-            final long timeout) throws IOException {
+    protected EasResponse sendHttpClientPost(final Account account,
+            String cmd, final HttpEntity entity, final long timeout) throws IOException {
+        final EmailClientConnectionManager connectionManager = getClientConnectionManager();
         final HttpClient client = getHttpClient(connectionManager, timeout);
         final boolean isPingCommand = cmd.equals("Ping");
 
@@ -261,7 +254,7 @@
             msg = true;
         }
 
-        final String us = makeUriString(hostAuth, cmd, extra);
+        final String us = makeUriString(cmd, extra);
         final HttpPost method = new HttpPost(URI.create(us));
         // Send the proper Content-Type header; it's always wbxml except for messages when
         // the EAS protocol version is < 14.0
@@ -273,7 +266,7 @@
         } else if (entity != null) {
             method.setHeader("Content-Type", "application/vnd.ms-sync.wbxml");
         }
-        setHeaders(account, hostAuth, method, !isPingCommand);
+        setHeaders(account, method, !isPingCommand);
         // NOTE
         // The next lines are added at the insistence of $VENDOR, who is seeing inappropriate
         // network activity related to the Ping command on some networks with some servers.
@@ -303,22 +296,14 @@
         }
     }
 
-    protected EasResponse sendHttpClientPost(final EmailClientConnectionManager connectionManager,
-            final Account account, final HostAuth hostAuth, final String cmd, final byte[] bytes)
-                    throws IOException {
-        return sendHttpClientPost(connectionManager, account, hostAuth, cmd,
-                new ByteArrayEntity(bytes), COMMAND_TIMEOUT);
+    protected EasResponse sendHttpClientPost(final Account account, final String cmd,
+            final byte[] bytes, final long timeout) throws IOException {
+        return sendHttpClientPost(account, cmd, new ByteArrayEntity(bytes), timeout);
     }
 
-    protected EasResponse sendHttpClientPost(final Account account, final HostAuth hostAuth,
-            String cmd, final HttpEntity entity, final long timeout) throws IOException {
-        return sendHttpClientPost(getClientConnectionManager(hostAuth), account, hostAuth, cmd,
-                entity, timeout);
-    }
-
-    protected EasResponse sendHttpClientPost(final Account account, final HostAuth hostAuth,
-            final String cmd, final byte[] bytes, final long timeout) throws IOException {
-        return sendHttpClientPost(account, hostAuth, cmd, new ByteArrayEntity(bytes), timeout);
+    protected EasResponse sendHttpClientPost(final Account account, final String cmd,
+            final byte[] bytes) throws IOException {
+        return sendHttpClientPost(account, cmd, bytes, COMMAND_TIMEOUT);
     }
 
     /**
diff --git a/src/com/android/exchange/service/EasSyncHandler.java b/src/com/android/exchange/service/EasSyncHandler.java
index 1404258..4496c1d 100644
--- a/src/com/android/exchange/service/EasSyncHandler.java
+++ b/src/com/android/exchange/service/EasSyncHandler.java
@@ -24,7 +24,6 @@
 public abstract class EasSyncHandler extends EasServerConnection {
     protected final ContentResolver mContentResolver;
     protected final Account mAccount;
-    protected final HostAuth mHostAuth;
     protected final Mailbox mMailbox;
     protected final Bundle mSyncExtras;
     protected final SyncResult mSyncResult;
@@ -50,10 +49,9 @@
     protected EasSyncHandler(final Context context, final ContentResolver contentResolver,
             final Account account, final Mailbox mailbox, final Bundle syncExtras,
             final SyncResult syncResult) {
-        super(context);
+        super(context, HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
         mContentResolver = contentResolver;
         mAccount = account;
-        mHostAuth = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv);
         mMailbox = mailbox;
         mSyncExtras = syncExtras;
         mSyncResult = syncResult;
@@ -111,12 +109,12 @@
 
     protected EasResponse sendHttpClientPost(final String cmd, final HttpEntity entity,
             final long timeout) throws IOException {
-        return sendHttpClientPost(mAccount, mHostAuth, cmd, entity, timeout);
+        return sendHttpClientPost(mAccount, cmd, entity, timeout);
     }
 
     protected EasResponse sendHttpClientPost(final String cmd, final byte[] bytes)
             throws IOException {
-        return sendHttpClientPost(mAccount, mHostAuth, cmd, bytes, COMMAND_TIMEOUT);
+        return sendHttpClientPost(mAccount, cmd, bytes, COMMAND_TIMEOUT);
     }
 
     // Communication with the application.