Avoid ANR due to looking up account info on the main thread

b/10987175

Change-Id: I7344e2717ab1b9557385eb6eb9e7962461b10c9d
This commit is contained in:
Tony Mantler 2013-09-30 11:51:14 -07:00
parent 125cbdf9d4
commit b38c7d1c37

View File

@ -1716,15 +1716,23 @@ public class EmailProvider extends ContentProvider {
// purpose of this is not to spam syncs when making frequent // purpose of this is not to spam syncs when making frequent
// modifications. // modifications.
final Handler handler = getDelayedSyncHandler(); final Handler handler = getDelayedSyncHandler();
final SyncRequestMessage request = new SyncRequestMessage( final android.accounts.Account amAccount =
uri.getAuthority(), accountId, mailboxId); getAccountManagerAccount(accountId);
synchronized (mDelayedSyncRequests) { if (amAccount != null) {
if (!mDelayedSyncRequests.contains(request)) { final SyncRequestMessage request = new SyncRequestMessage(
mDelayedSyncRequests.add(request); uri.getAuthority(), amAccount, mailboxId);
final android.os.Message message = synchronized (mDelayedSyncRequests) {
handler.obtainMessage(0, request); if (!mDelayedSyncRequests.contains(request)) {
handler.sendMessageDelayed(message, SYNC_DELAY_MILLIS); mDelayedSyncRequests.add(request);
final android.os.Message message =
handler.obtainMessage(0, request);
handler.sendMessageDelayed(message, SYNC_DELAY_MILLIS);
}
} }
} else {
LogUtils.d(TAG,
"Attempted to start delayed sync for invalid account %d",
accountId);
} }
} else { } else {
// Old way of doing upsync. // Old way of doing upsync.
@ -5249,13 +5257,12 @@ public class EmailProvider extends ContentProvider {
public boolean handleMessage(android.os.Message msg) { public boolean handleMessage(android.os.Message msg) {
synchronized (mDelayedSyncRequests) { synchronized (mDelayedSyncRequests) {
final SyncRequestMessage request = (SyncRequestMessage) msg.obj; final SyncRequestMessage request = (SyncRequestMessage) msg.obj;
final android.accounts.Account account = // TODO: It's possible that the account is deleted by the time we get here
getAccountManagerAccount(request.mAccountId); // It would be nice if we could validate it before trying to sync
if (account != null) { final android.accounts.Account account = request.mAccount;
final Bundle extras = new Bundle(); final Bundle extras = new Bundle();
extras.putLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, request.mMailboxId); extras.putLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, request.mMailboxId);
ContentResolver.requestSync(account, request.mAuthority, extras); ContentResolver.requestSync(account, request.mAuthority, extras);
}
mDelayedSyncRequests.remove(request); mDelayedSyncRequests.remove(request);
return true; return true;
} }
@ -5267,12 +5274,13 @@ public class EmailProvider extends ContentProvider {
private class SyncRequestMessage { private class SyncRequestMessage {
private final String mAuthority; private final String mAuthority;
private final long mAccountId; private final android.accounts.Account mAccount;
private final long mMailboxId; private final long mMailboxId;
private SyncRequestMessage(String authority, long accountId, long mailboxId) { private SyncRequestMessage(final String authority, final android.accounts.Account account,
final long mailboxId) {
mAuthority = authority; mAuthority = authority;
mAccountId = accountId; mAccount = account;
mMailboxId = mailboxId; mMailboxId = mailboxId;
} }
@ -5287,24 +5295,15 @@ public class EmailProvider extends ContentProvider {
SyncRequestMessage that = (SyncRequestMessage) o; SyncRequestMessage that = (SyncRequestMessage) o;
if (mAccountId != that.mAccountId) { return mAccount.equals(that.mAccount)
return false; && mMailboxId == that.mMailboxId
} && mAuthority.equals(that.mAuthority);
if (mMailboxId != that.mMailboxId) {
return false;
}
//noinspection RedundantIfStatement
if (!mAuthority.equals(that.mAuthority)) {
return false;
}
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = mAuthority.hashCode(); int result = mAuthority.hashCode();
result = 31 * result + (int) (mAccountId ^ (mAccountId >>> 32)); result = 31 * result + mAccount.hashCode();
result = 31 * result + (int) (mMailboxId ^ (mMailboxId >>> 32)); result = 31 * result + (int) (mMailboxId ^ (mMailboxId >>> 32));
return result; return result;
} }