Do alert work in background thread

* An ANR was reported in MailboxAlarmReceiver
* The reciever calls into SyncManager, which does some database
  operations, and may abort an I/O operation in a sync service
  thread
* Move this potentially long-running code into a background
  thread

Bug: 2215045
Change-Id: Id65c51f706b212d6b50af3921f3ba3dc2d014ce0
This commit is contained in:
Marc Blank 2010-03-19 09:06:50 -07:00
parent ad33c5acf4
commit bb2f25a23a

View File

@ -1321,31 +1321,38 @@ public class SyncManager extends Service implements Runnable {
} }
} }
static public void alert(Context context, long id) { static public void alert(Context context, final long id) {
SyncManager syncManager = INSTANCE; final SyncManager syncManager = INSTANCE;
checkSyncManagerServiceRunning(); checkSyncManagerServiceRunning();
if (id < 0) { if (id < 0) {
kick("ping SyncManager"); kick("ping SyncManager");
} else if (syncManager == null) { } else if (syncManager == null) {
context.startService(new Intent(context, SyncManager.class)); context.startService(new Intent(context, SyncManager.class));
} else { } else {
AbstractSyncService service = syncManager.mServiceMap.get(id); final AbstractSyncService service = syncManager.mServiceMap.get(id);
if (service != null) { if (service != null) {
Mailbox m = Mailbox.restoreMailboxWithId(syncManager, id); // Handle alerts in a background thread, as we are typically called from a
if (m != null) { // broadcast receiver, and are therefore running in the UI thread
// We ignore drafts completely (doesn't sync). Changes in Outbox are handled new Thread(new Runnable() {
// in the checkMailboxes loop, so we can ignore these pings. public void run() {
if (m.mType == Mailbox.TYPE_DRAFTS || m.mType == Mailbox.TYPE_OUTBOX) { Mailbox m = Mailbox.restoreMailboxWithId(syncManager, id);
String[] args = new String[] {Long.toString(m.mId)}; if (m != null) {
ContentResolver resolver = INSTANCE.mResolver; // We ignore drafts completely (doesn't sync). Changes in Outbox are
resolver.delete(Message.DELETED_CONTENT_URI, WHERE_MAILBOX_KEY, args); // handled in the checkMailboxes loop, so we can ignore these pings.
resolver.delete(Message.UPDATED_CONTENT_URI, WHERE_MAILBOX_KEY, args); if (m.mType == Mailbox.TYPE_DRAFTS || m.mType == Mailbox.TYPE_OUTBOX) {
return; String[] args = new String[] {Long.toString(m.mId)};
} ContentResolver resolver = INSTANCE.mResolver;
service.mAccount = Account.restoreAccountWithId(INSTANCE, m.mAccountKey); resolver.delete(Message.DELETED_CONTENT_URI, WHERE_MAILBOX_KEY,
service.mMailbox = m; args);
service.alarm(); resolver.delete(Message.UPDATED_CONTENT_URI, WHERE_MAILBOX_KEY,
} args);
return;
}
service.mAccount = Account.restoreAccountWithId(INSTANCE, m.mAccountKey);
service.mMailbox = m;
service.alarm();
}
}}).start();
} }
} }
} }