diff --git a/src/com/android/email/RefreshManager.java b/src/com/android/email/RefreshManager.java index ce4d0a1b4..5b4e8b64b 100644 --- a/src/com/android/email/RefreshManager.java +++ b/src/com/android/email/RefreshManager.java @@ -17,10 +17,8 @@ package com.android.email; import com.android.email.mail.MessagingException; -import com.android.email.provider.EmailContent; import android.content.Context; -import android.database.Cursor; import android.os.Handler; import android.util.Log; @@ -45,7 +43,7 @@ import java.util.HashMap; * *
Conceptually it can be a part of {@link Controller}, but extracted for easy testing. * - * (All public method musb be called on the UI thread. All callbacks will be called on the UI + * (All public methods must be called on the UI thread. All callbacks will be called on the UI * thread.) */ public class RefreshManager { @@ -159,7 +157,7 @@ public class RefreshManager { return sInstance; } - /* package */ RefreshManager(Context context, Controller controller, Clock clock, + protected RefreshManager(Context context, Controller controller, Clock clock, Handler handler) { mClock = clock; mContext = context.getApplicationContext(); @@ -169,6 +167,14 @@ public class RefreshManager { mController.addResultCallback(mControllerResult); } + /** + * MUST be called for mock instances. (The actual instance is a singleton, so no cleanup + * is necessary.) + */ + public void cleanUpForTest() { + mController.removeResultCallback(mControllerResult); + } + public void registerListener(Listener listener) { if (listener == null) { throw new InvalidParameterException(); @@ -261,6 +267,18 @@ public class RefreshManager { } } + public long getLastMailboxListRefreshTime(long accountId) { + return mMailboxListStatus.get(accountId).getLastRefreshTime(); + } + + public long getLastMessageListRefreshTime(long mailboxId) { + return mMessageListStatus.get(mailboxId).getLastRefreshTime(); + } + + public long getLastSendMessageTime(long accountId) { + return mOutboxStatus.get(accountId).getLastRefreshTime(); + } + public boolean isMailboxListRefreshing(long accountId) { return mMailboxListStatus.get(accountId).isRefreshing(); } diff --git a/src/com/android/email/activity/MessageListFragment.java b/src/com/android/email/activity/MessageListFragment.java index 569c105bc..c64dc7979 100644 --- a/src/com/android/email/activity/MessageListFragment.java +++ b/src/com/android/email/activity/MessageListFragment.java @@ -98,6 +98,7 @@ public class MessageListFragment extends ListFragment private Account mAccount; private Mailbox mMailbox; private boolean mIsEasAccount; + private boolean mIsRefreshable; // Controller access private Controller mController; @@ -306,7 +307,7 @@ public class MessageListFragment extends ListFragment } /** - * @return the mailbox id, which is the value set to {@link #openMailbox(long, long)}. + * @return the mailbox id, which is the value set to {@link #openMailbox}. * (Meaning it will never return -1, but may return special values, * eg {@link Mailbox#QUERY_ALL_INBOXES}). */ @@ -414,8 +415,13 @@ public class MessageListFragment extends ListFragment /** * Refresh the list. NOOP for special mailboxes (e.g. combined inbox). + * + * Note: Manual refresh is enabled even for push accounts. */ public void onRefresh() { + if (!mIsRefreshable) { + return; + } long accountId = getAccountId(); if (accountId != -1) { mRefreshManager.refreshMessageList(accountId, mMailboxId); @@ -646,13 +652,14 @@ public class MessageListFragment extends ListFragment /** * Implements a timed refresh of "stale" mailboxes. This should only happen when * multiple conditions are true, including: + * Only refreshable mailboxes. * Only when the user explicitly opens the mailbox (not onResume, for example) - * Only for real, non-push mailboxes + * Only for real, non-push mailboxes (c.f. manual refresh is still enabled for push accounts) * Only when the mailbox is "stale" (currently set to 5 minutes since last refresh) */ private void autoRefreshStaleMailbox() { if (!mDoAutoRefresh // Not explicitly open - || (mMailbox == null) // Magic inbox + || mIsRefreshable // Not refreshable (special box such as drafts, or magic boxes) || (mAccount.mSyncInterval == Account.CHECK_INTERVAL_PUSH) // Not push ) { return; @@ -788,7 +795,7 @@ public class MessageListFragment extends ListFragment Log.d(Email.LOG_TAG, "MessageListFragment onLoadFinished(mailbox) mailboxId=" + mMailboxId); } - if (!isMagicMailbox() && !result.isFound()) { + if (!result.mIsFound) { mCallback.onMailboxNotFound(); return; } @@ -797,6 +804,7 @@ public class MessageListFragment extends ListFragment mAccount = result.mAccount; mMailbox = result.mMailbox; mIsEasAccount = result.mIsEasAccount; + mIsRefreshable = result.mIsRefreshable; getLoaderManager().initLoader(LOADER_ID_MESSAGES_LOADER, null, new MessagesLoaderCallback()); } diff --git a/src/com/android/email/activity/MessageListXL.java b/src/com/android/email/activity/MessageListXL.java index 39f872fab..18167611c 100644 --- a/src/com/android/email/activity/MessageListXL.java +++ b/src/com/android/email/activity/MessageListXL.java @@ -16,12 +16,14 @@ package com.android.email.activity; +import com.android.email.Clock; import com.android.email.Email; import com.android.email.R; import com.android.email.RefreshManager; import com.android.email.Utility; import com.android.email.activity.setup.AccountSettingsXL; import com.android.email.activity.setup.AccountSetupBasics; +import com.android.email.provider.EmailContent.Account; import com.android.email.provider.EmailContent.Mailbox; import com.android.email.service.MailService; @@ -33,6 +35,7 @@ import android.content.Context; import android.content.Intent; import android.content.Loader; import android.database.Cursor; +import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; @@ -58,6 +61,8 @@ public class MessageListXL extends Activity implements View.OnClickListener, private static final String EXTRA_ACCOUNT_ID = "ACCOUNT_ID"; private static final String EXTRA_MAILBOX_ID = "MAILBOX_ID"; private static final int LOADER_ID_ACCOUNT_LIST = 0; + /* package */ static final int MAILBOX_REFRESH_MIN_INTERVAL = 30 * 1000; // in milliseconds + /* package */ static final int INBOX_AUTO_REFRESH_MIN_INTERVAL = 10 * 1000; // in milliseconds private Context mContext; private RefreshManager mRefreshManager; @@ -80,6 +85,8 @@ public class MessageListXL extends Activity implements View.OnClickListener, private final MessageOrderManagerCallback mMessageOrderManagerCallback = new MessageOrderManagerCallback(); + private RefreshTask mRefreshTask; + /** * Launch and open account's inbox. * @@ -210,6 +217,7 @@ public class MessageListXL extends Activity implements View.OnClickListener, @Override protected void onDestroy() { if (Email.DEBUG_LIFECYCLE && Email.DEBUG) Log.d(Email.LOG_TAG, "MessageListXL onDestroy"); + Utility.cancelTaskInterrupt(mRefreshTask); mRefreshManager.unregisterListener(mMailRefreshManagerListener); super.onDestroy(); } @@ -543,7 +551,6 @@ public class MessageListXL extends Activity implements View.OnClickListener, private boolean isProgressActive() { final long mailboxId = mFragmentManager.getMailboxId(); return (mailboxId >= 0) && mRefreshManager.isMessageListRefreshing(mailboxId); - } @Override @@ -604,29 +611,114 @@ public class MessageListXL extends Activity implements View.OnClickListener, } private void onRefresh() { - // Temporary implementation - if (mFragmentManager.isMailboxSelected()) { - long mailboxId = mFragmentManager.getMailboxId(); - // TODO This class here shouldn't really know what can be refreshable. - // (The test below is only to prevent a crash... It's not enough. e.g. no refresh - // for outboxes.) - if (mailboxId >= 0) { - mRefreshManager.refreshMessageList(mFragmentManager.getAccountId(), mailboxId); + // Cancel previously running instance if any. + Utility.cancelTaskInterrupt(mRefreshTask); + mRefreshTask = new RefreshTask(this, mFragmentManager.getAccountId(), + mFragmentManager.getMailboxId()); + mRefreshTask.execute(); + } + + /** + * Class to handle refresh. + * + * When the user press "refresh", + *