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,8 +1716,11 @@ 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 android.accounts.Account amAccount =
getAccountManagerAccount(accountId);
if (amAccount != null) {
final SyncRequestMessage request = new SyncRequestMessage( final SyncRequestMessage request = new SyncRequestMessage(
uri.getAuthority(), accountId, mailboxId); uri.getAuthority(), amAccount, mailboxId);
synchronized (mDelayedSyncRequests) { synchronized (mDelayedSyncRequests) {
if (!mDelayedSyncRequests.contains(request)) { if (!mDelayedSyncRequests.contains(request)) {
mDelayedSyncRequests.add(request); mDelayedSyncRequests.add(request);
@ -1726,6 +1729,11 @@ public class EmailProvider extends ContentProvider {
handler.sendMessageDelayed(message, SYNC_DELAY_MILLIS); 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.
// For synced messages, first copy the old message to the updated table // For synced messages, first copy the old message to the updated table
@ -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;
} }