Fix the cache in Controller.

Turned out we can't reuse EmailServiceProxy, so only cache the account type.

Change-Id: I5499c2440082580d6f0739ae85b888f1afb65ba8
This commit is contained in:
Makoto Onuki 2010-06-03 16:25:45 -07:00
parent a00bf4e0d0
commit c7b6145c12

View File

@ -71,9 +71,14 @@ public class Controller {
}; };
private static int MESSAGEID_TO_MAILBOXID_COLUMN_MAILBOXID = 1; private static int MESSAGEID_TO_MAILBOXID_COLUMN_MAILBOXID = 1;
/** Cache used by {@link #getServiceForAccount} */ private static final int ACCOUNT_TYPE_LEGACY = 0;
private final ConcurrentHashMap<Long, Object> mAccountToService private static final int ACCOUNT_TYPE_SERVICE = 1;
= new ConcurrentHashMap<Long, Object>();
/**
* Cache used by {@link #getServiceForAccount}. Map from account-ids to ACCOUNT_TYPE_*.
*/
private final ConcurrentHashMap<Long, Integer> mAccountToType
= new ConcurrentHashMap<Long, Integer>();
protected Controller(Context _context) { protected Controller(Context _context) {
mContext = _context.getApplicationContext(); mContext = _context.getApplicationContext();
@ -787,26 +792,31 @@ public class Controller {
*/ */
private IEmailService getServiceForAccount(long accountId) { private IEmailService getServiceForAccount(long accountId) {
// First, try cache. // First, try cache.
final Object value = mAccountToService.get(accountId); final Integer type = mAccountToType.get(accountId);
if (value != null) { if (type != null) {
if (value instanceof IEmailService) { // Cached
return (IEmailService) value; switch (type) {
} else { case ACCOUNT_TYPE_LEGACY:
return null; return null;
case ACCOUNT_TYPE_SERVICE:
return getExchangeEmailService();
} }
} }
// Not cached
Account account = EmailContent.Account.restoreAccountWithId(mProviderContext, accountId); Account account = EmailContent.Account.restoreAccountWithId(mProviderContext, accountId);
if (account == null || isMessagingController(account)) { if (account == null || isMessagingController(account)) {
// Put any non-IEmailService object to indicate it uses the legacy controller. mAccountToType.put(accountId, ACCOUNT_TYPE_LEGACY);
mAccountToService.put(accountId, "");
return null; return null;
} else { } else {
IEmailService s = ExchangeUtils.getExchangeEmailService(mContext, mServiceCallback); mAccountToType.put(accountId, ACCOUNT_TYPE_SERVICE);
mAccountToService.put(accountId, s); return getExchangeEmailService();
return s;
} }
} }
private IEmailService getExchangeEmailService() {
return ExchangeUtils.getExchangeEmailService(mContext, mServiceCallback);
}
/** /**
* Simple helper to determine if legacy MessagingController should be used * Simple helper to determine if legacy MessagingController should be used
* *
@ -832,7 +842,7 @@ public class Controller {
* TODO: Make it really delete accounts and remove DeleteAccountTask. * TODO: Make it really delete accounts and remove DeleteAccountTask.
*/ */
public void deleteAccount(long accountId) { public void deleteAccount(long accountId) {
mAccountToService.remove(accountId); mAccountToType.remove(accountId);
} }
/** /**