Autosync stale mailboxes when switching to them.

"Stale" is currently defined as "it's been 5 minutes since
my last sync".

Also includes a small refactor so that uiloadmore doesn't
load the mailbox from DB twice.

Bug: 7100372
Change-Id: Ib31aa211fe2ab790044beb7bc7d2fdcccf595171
This commit is contained in:
Yu Ping Hu 2013-04-11 17:11:34 -07:00
parent 4b0e25898e
commit 64cc777f8b

View File

@ -44,6 +44,7 @@ import android.os.ParcelFileDescriptor;
import android.os.RemoteException; import android.os.RemoteException;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import com.android.common.content.ProjectionMap; import com.android.common.content.ProjectionMap;
@ -149,6 +150,9 @@ public class EmailProvider extends ContentProvider {
/** Appended to the notification URI for update operations */ /** Appended to the notification URI for update operations */
private static final String NOTIFICATION_OP_UPDATE = "update"; private static final String NOTIFICATION_OP_UPDATE = "update";
/** The query string to trigger a folder refresh. */
private static String QUERY_UIREFRESH = "uirefresh";
// Definitions for our queries looking for orphaned messages // Definitions for our queries looking for orphaned messages
private static final String[] ORPHANS_PROJECTION private static final String[] ORPHANS_PROJECTION
= new String[] {MessageColumns.ID, MessageColumns.MAILBOX_KEY}; = new String[] {MessageColumns.ID, MessageColumns.MAILBOX_KEY};
@ -1158,7 +1162,7 @@ public class EmailProvider extends ContentProvider {
matcher.addURI(EmailContent.AUTHORITY, "uimessages/#", UI_MESSAGES); matcher.addURI(EmailContent.AUTHORITY, "uimessages/#", UI_MESSAGES);
matcher.addURI(EmailContent.AUTHORITY, "uimessage/#", UI_MESSAGE); matcher.addURI(EmailContent.AUTHORITY, "uimessage/#", UI_MESSAGE);
matcher.addURI(EmailContent.AUTHORITY, "uiundo", UI_UNDO); matcher.addURI(EmailContent.AUTHORITY, "uiundo", UI_UNDO);
matcher.addURI(EmailContent.AUTHORITY, "uirefresh/#", UI_FOLDER_REFRESH); matcher.addURI(EmailContent.AUTHORITY, QUERY_UIREFRESH + "/#", UI_FOLDER_REFRESH);
// We listen to everything trailing uifolder/ since there might be an appVersion // We listen to everything trailing uifolder/ since there might be an appVersion
// as in Utils.appendVersionQueryParameter(). // as in Utils.appendVersionQueryParameter().
matcher.addURI(EmailContent.AUTHORITY, "uifolder/*", UI_FOLDER); matcher.addURI(EmailContent.AUTHORITY, "uifolder/*", UI_FOLDER);
@ -1302,10 +1306,10 @@ public class EmailProvider extends ContentProvider {
c = uiFolders(uri, projection); c = uiFolders(uri, projection);
return c; return c;
case UI_FOLDER_LOAD_MORE: case UI_FOLDER_LOAD_MORE:
c = uiFolderLoadMore(uri); c = uiFolderLoadMore(getMailbox(uri));
return c; return c;
case UI_FOLDER_REFRESH: case UI_FOLDER_REFRESH:
c = uiFolderRefresh(uri, 0); c = uiFolderRefresh(getMailbox(uri), 0);
return c; return c;
case MAILBOX_NOTIFICATION: case MAILBOX_NOTIFICATION:
c = notificationQuery(uri); c = notificationQuery(uri);
@ -2276,7 +2280,7 @@ outer:
.add(UIProvider.FolderColumns.CHILD_FOLDERS_LIST_URI, uriWithId("uisubfolders")) .add(UIProvider.FolderColumns.CHILD_FOLDERS_LIST_URI, uriWithId("uisubfolders"))
.add(UIProvider.FolderColumns.UNREAD_COUNT, MailboxColumns.UNREAD_COUNT) .add(UIProvider.FolderColumns.UNREAD_COUNT, MailboxColumns.UNREAD_COUNT)
.add(UIProvider.FolderColumns.TOTAL_COUNT, MailboxColumns.TOTAL_COUNT) .add(UIProvider.FolderColumns.TOTAL_COUNT, MailboxColumns.TOTAL_COUNT)
.add(UIProvider.FolderColumns.REFRESH_URI, uriWithId("uirefresh")) .add(UIProvider.FolderColumns.REFRESH_URI, uriWithId(QUERY_UIREFRESH))
.add(UIProvider.FolderColumns.SYNC_STATUS, MailboxColumns.UI_SYNC_STATUS) .add(UIProvider.FolderColumns.SYNC_STATUS, MailboxColumns.UI_SYNC_STATUS)
.add(UIProvider.FolderColumns.LAST_SYNC_RESULT, MailboxColumns.UI_LAST_SYNC_RESULT) .add(UIProvider.FolderColumns.LAST_SYNC_RESULT, MailboxColumns.UI_LAST_SYNC_RESULT)
.add(UIProvider.FolderColumns.TYPE, FOLDER_TYPE) .add(UIProvider.FolderColumns.TYPE, FOLDER_TYPE)
@ -3355,6 +3359,12 @@ outer:
return mExtras; return mExtras;
} }
/**
* When showing a folder, if it's been at least this long since the last sync,
* force a folder refresh.
*/
private static final long AUTO_REFRESH_INTERVAL_MS = 5 * DateUtils.MINUTE_IN_MILLIS;
@Override @Override
public Bundle respond(Bundle params) { public Bundle respond(Bundle params) {
final String setVisibilityKey = final String setVisibilityKey =
@ -3368,14 +3378,22 @@ outer:
UIProvider.ConversationCursorCommand.COMMAND_KEY_ENTERED_FOLDER)) { UIProvider.ConversationCursorCommand.COMMAND_KEY_ENTERED_FOLDER)) {
Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId); Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
if (mailbox != null) { if (mailbox != null) {
final ContentResolver resolver = mContext.getContentResolver();
// Mark all messages as seen // Mark all messages as seen
// TODO: should this happen even if the mailbox couldn't be restored? // TODO: should this happen even if the mailbox couldn't be restored?
final ContentValues contentValues = new ContentValues(1); final ContentValues contentValues = new ContentValues(1);
contentValues.put(MessageColumns.FLAG_SEEN, true); contentValues.put(MessageColumns.FLAG_SEEN, true);
final Uri uri = EmailContent.Message.CONTENT_URI; final Uri uri = EmailContent.Message.CONTENT_URI;
mContext.getContentResolver().update(uri, contentValues, resolver.update(uri, contentValues, MessageColumns.MAILBOX_KEY + " = ?",
MessageColumns.MAILBOX_KEY + " = ?",
new String[] {String.valueOf(mailbox.mId)}); new String[] {String.valueOf(mailbox.mId)});
// If it's been long enough, force sync this mailbox.
final long timeSinceLastSync =
System.currentTimeMillis() - mailbox.mSyncTime;
if (timeSinceLastSync > AUTO_REFRESH_INTERVAL_MS) {
final Uri refreshUri = Uri.parse(EmailContent.CONTENT_URI + "/" +
QUERY_UIREFRESH + "/" + mailbox.mId);
resolver.query(refreshUri, null, null, null, null);
}
} }
} }
} }
@ -4364,16 +4382,17 @@ outer:
} }
}; };
private Cursor uiFolderRefresh(Uri uri, int deltaMessageCount) { private Mailbox getMailbox(final Uri uri) {
Context context = getContext(); final long id = Long.parseLong(uri.getLastPathSegment());
String idString = uri.getLastPathSegment(); return Mailbox.restoreMailboxWithId(getContext(), id);
long id = Long.parseLong(idString); }
Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
private Cursor uiFolderRefresh(final Mailbox mailbox, final int deltaMessageCount) {
if (mailbox == null) return null; if (mailbox == null) return null;
EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context, EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(getContext(),
mServiceCallback, mailbox.mAccountKey); mServiceCallback, mailbox.mAccountKey);
try { try {
service.startSync(id, true, deltaMessageCount); service.startSync(mailbox.mId, true, deltaMessageCount);
} catch (RemoteException e) { } catch (RemoteException e) {
} }
return null; return null;
@ -4384,18 +4403,14 @@ outer:
//Number of additional messages to load when a user selects "Load more..." in a search //Number of additional messages to load when a user selects "Load more..." in a search
public static final int SEARCH_MORE_INCREMENT = 10; public static final int SEARCH_MORE_INCREMENT = 10;
private Cursor uiFolderLoadMore(Uri uri) { private Cursor uiFolderLoadMore(final Mailbox mailbox) {
Context context = getContext();
String idString = uri.getLastPathSegment();
long id = Long.parseLong(idString);
Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
if (mailbox == null) return null; if (mailbox == null) return null;
if (mailbox.mType == Mailbox.TYPE_SEARCH) { if (mailbox.mType == Mailbox.TYPE_SEARCH) {
// Ask for 10 more messages // Ask for 10 more messages
mSearchParams.mOffset += SEARCH_MORE_INCREMENT; mSearchParams.mOffset += SEARCH_MORE_INCREMENT;
runSearchQuery(context, mailbox.mAccountKey, id); runSearchQuery(getContext(), mailbox.mAccountKey, mailbox.mId);
} else { } else {
uiFolderRefresh(uri, VISIBLE_LIMIT_INCREMENT); uiFolderRefresh(mailbox, VISIBLE_LIMIT_INCREMENT);
} }
return null; return null;
} }