From 87c84a6d96a360b2310f44ff4ae2e181c7a4479a Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Wed, 22 Feb 2012 16:08:23 -0800 Subject: [PATCH] Send sync status updates from POP3/IMAP Change-Id: Ia1bb5d1a302d6dde8b8979e03a762d6d45ee8cd3 --- .../android/email/provider/EmailProvider.java | 3 + .../email/service/EmailServiceStub.java | 2 +- .../service/PopImapSyncAdapterService.java | 59 +++++++++++++------ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index 8ebcf5c03..de25a5f5a 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -1575,6 +1575,9 @@ outer: Uri notifyUri = UIPROVIDER_MAILBOX_NOTIFIER.buildUpon().appendPath(id).build(); resolver.notifyChange(notifyUri, null); + // TODO: Remove logging + Log.d(TAG, "Notifying mailbox " + id + " status: " + + values.getAsInteger(Mailbox.UI_SYNC_STATUS)); } break; case BODY: diff --git a/src/com/android/email/service/EmailServiceStub.java b/src/com/android/email/service/EmailServiceStub.java index 190618132..aec24758e 100644 --- a/src/com/android/email/service/EmailServiceStub.java +++ b/src/com/android/email/service/EmailServiceStub.java @@ -107,7 +107,7 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm android.accounts.Account acct = new android.accounts.Account(account.mEmailAddress, AccountManagerTypes.TYPE_POP_IMAP); Bundle extras = new Bundle(); - extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); ContentResolver.requestSync(acct, EmailContent.AUTHORITY, extras); } diff --git a/src/com/android/email/service/PopImapSyncAdapterService.java b/src/com/android/email/service/PopImapSyncAdapterService.java index c28f0296a..47d5250f7 100644 --- a/src/com/android/email/service/PopImapSyncAdapterService.java +++ b/src/com/android/email/service/PopImapSyncAdapterService.java @@ -21,16 +21,20 @@ import android.app.Service; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SyncResult; import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import com.android.emailcommon.mail.MessagingException; import com.android.emailcommon.provider.Account; +import com.android.emailcommon.provider.EmailContent; import com.android.emailcommon.provider.EmailContent.AccountColumns; import com.android.emailcommon.provider.EmailContent.Message; import com.android.emailcommon.provider.HostAuth; @@ -82,28 +86,43 @@ public class PopImapSyncAdapterService extends Service { return sSyncAdapter.getSyncAdapterBinder(); } - private static void sync(Context context, long mailboxId, SyncResult syncResult) { + private static void sync(Context context, long mailboxId, SyncResult syncResult, + boolean uiRefresh) { Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId); if (mailbox == null) return; Log.d(TAG, "Mailbox: " + mailbox.mDisplayName); - Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey); - if (account == null) return; + + ContentResolver resolver = context.getContentResolver(); + Uri mailboxUri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId); + ContentValues values = new ContentValues(); + // Set mailbox sync state + values.put(Mailbox.UI_SYNC_STATUS, + uiRefresh ? EmailContent.SYNC_STATUS_USER : EmailContent.SYNC_STATUS_BACKGROUND); + resolver.update(mailboxUri, values, null, null); try { - if (account.getProtocol(context).equals(HostAuth.SCHEME_IMAP)) { - ImapService.synchronizeMailboxSynchronous(context, account, mailbox); - } else { - Pop3Service.synchronizeMailboxSynchronous(context, account, mailbox); - } - } catch (MessagingException e) { - int cause = e.getExceptionType(); - switch(cause) { - case MessagingException.IOERROR: - syncResult.stats.numIoExceptions++; - break; - case MessagingException.AUTHENTICATION_FAILED: - syncResult.stats.numAuthExceptions++; - break; + Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey); + if (account == null) return; + try { + if (account.getProtocol(context).equals(HostAuth.SCHEME_IMAP)) { + ImapService.synchronizeMailboxSynchronous(context, account, mailbox); + } else { + Pop3Service.synchronizeMailboxSynchronous(context, account, mailbox); + } + } catch (MessagingException e) { + int cause = e.getExceptionType(); + switch(cause) { + case MessagingException.IOERROR: + syncResult.stats.numIoExceptions++; + break; + case MessagingException.AUTHENTICATION_FAILED: + syncResult.stats.numAuthExceptions++; + break; + } } + } finally { + // Always clear our sync state + values.put(Mailbox.UI_SYNC_STATUS, EmailContent.SYNC_STATUS_NONE); + resolver.update(mailboxUri, values, null, null); } } @@ -139,7 +158,7 @@ public class PopImapSyncAdapterService extends Service { } } for (long mailboxId: mailboxesToUpdate) { - sync(context, mailboxId, syncResult); + sync(context, mailboxId, syncResult, false); } } else { Log.d(TAG, "Sync request for " + acct.mDisplayName); @@ -158,7 +177,9 @@ public class PopImapSyncAdapterService extends Service { isInbox = true; } if (mailboxId == Mailbox.NO_MAILBOX) return; - sync(context, mailboxId, syncResult); + boolean uiRefresh = + extras.getBoolean(ContentResolver.SYNC_EXTRAS_FORCE, false); + sync(context, mailboxId, syncResult, uiRefresh); // Convert from minutes to seconds int syncFrequency = acct.mSyncInterval * 60;