From f65bdbdaf5960951452b148f801c51feca864bfe Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Thu, 16 Jun 2011 08:59:19 -0700 Subject: [PATCH] Fix Store caching Bug: 4671090 Change-Id: I07413eb713c564f6dbcac45091357153cfe19a5d --- .../emailcommon/provider/HostAuth.java | 24 +++++++++++++++---- src/com/android/email/mail/Store.java | 19 +++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java index 6720b1dc6..8729418bc 100644 --- a/emailcommon/src/com/android/emailcommon/provider/HostAuth.java +++ b/emailcommon/src/com/android/emailcommon/provider/HostAuth.java @@ -17,10 +17,6 @@ package com.android.emailcommon.provider; -import com.android.emailcommon.provider.EmailContent.HostAuthColumns; -import com.android.emailcommon.utility.SSLUtils; -import com.android.emailcommon.utility.Utility; - import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -29,6 +25,10 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; +import com.android.emailcommon.provider.EmailContent.HostAuthColumns; +import com.android.emailcommon.utility.SSLUtils; +import com.android.emailcommon.utility.Utility; + import java.net.URI; import java.net.URISyntaxException; @@ -377,6 +377,22 @@ public final class HostAuth extends EmailContent implements HostAuthColumns, Par && Utility.areStringsEqual(mClientCertAlias, that.mClientCertAlias); } + /** + * The flag, password, and client cert alias are the only items likely to change after a + * HostAuth is created + */ + @Override + public int hashCode() { + int hashCode = 29; + if (mPassword != null) { + hashCode += mPassword.hashCode(); + } + if (mClientCertAlias != null) { + hashCode += (mClientCertAlias.hashCode() << 8); + } + return (hashCode << 8) + mFlags; + } + /** * Legacy URI parser. Used in parsing template from provider.xml * Example string: diff --git a/src/com/android/email/mail/Store.java b/src/com/android/email/mail/Store.java index 8a8ea5952..ef865ac3c 100644 --- a/src/com/android/email/mail/Store.java +++ b/src/com/android/email/mail/Store.java @@ -66,7 +66,7 @@ public abstract class Store { public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024); @VisibleForTesting - static final HashMap sStores = new HashMap(); + static final HashMap sStores = new HashMap(); protected Context mContext; protected Account mAccount; @@ -167,8 +167,8 @@ public abstract class Store { * Get an instance of a mail store for the given account. The account must be valid (i.e. has * at least an incoming server name). * - * NOTE: The internal algorithm used to find a cached store depends upon the id of the - * account's HostAuth row. If this ever changes (e.g. such as the user updating the + * NOTE: The internal algorithm used to find a cached store depends upon the account's + * HostAuth row. If this ever changes (e.g. such as the user updating the * host name or port), we will leak entries. This should not be typical, so, it is not * a critical problem. However, it is something we should consider fixing. * @@ -178,18 +178,17 @@ public abstract class Store { */ public synchronized static Store getInstance(Account account, Context context, PersistentDataCallbacks callbacks) throws MessagingException { - HostAuth recvAuth = account.getOrCreateHostAuthRecv(context); - long storeKey = recvAuth.mId; - Store store = sStores.get(storeKey); + HostAuth hostAuth = account.getOrCreateHostAuthRecv(context); + Store store = sStores.get(hostAuth); if (store == null) { Context appContext = context.getApplicationContext(); - StoreInfo info = StoreInfo.getStoreInfo(recvAuth.mProtocol, context); + StoreInfo info = StoreInfo.getStoreInfo(hostAuth.mProtocol, context); if (info != null) { store = instantiateStore(info.mClassName, account, appContext, callbacks); } // Don't cache this unless it's we've got a saved HostAUth - if (store != null && (storeKey != EmailContent.NOT_SAVED)) { - sStores.put(storeKey, store); + if (store != null && (hostAuth.mId != EmailContent.NOT_SAVED)) { + sStores.put(hostAuth, store); } } else { // update the callbacks, which may have been null at creation time. @@ -214,7 +213,7 @@ public abstract class Store { */ public synchronized static Store removeInstance(Account account, Context context) throws MessagingException { - return sStores.remove(account.mId); + return sStores.remove(HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv)); } /**