Fully implement "refresh" action bar button
* Now it'll refresh mailbox list (left pane) as well. (With the minimal interval of 30 seconds) * Always refresh inbox. (also with the minimal interval of 10 seconds) * Also make sure the "auto-refresh" won't refresh non-refreshable mailboxes. (drafts, etc) Bug 2929889 Bug 2930018 Change-Id: I09452d40aad6008a721cfbc3f491617224d7048f
This commit is contained in:
parent
086aac2386
commit
e357f58791
@ -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;
|
||||
*
|
||||
* <p>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();
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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",
|
||||
* <ul>
|
||||
* <li>Refresh the current mailbox, if it's refreshable. (e.g. don't refresh combined inbox,
|
||||
* drafts, etc.
|
||||
* <li>Refresh the mailbox list, if it hasn't been refreshed in the last
|
||||
* {@link #MAILBOX_REFRESH_MIN_INTERVAL}.
|
||||
* <li>Refresh inbox, if it's not the current mailbox and it hasn't been refreshed in the last
|
||||
* {@link #INBOX_AUTO_REFRESH_MIN_INTERVAL}.
|
||||
* </ul>
|
||||
*/
|
||||
/* package */ static class RefreshTask extends AsyncTask<Void, Void, Boolean> {
|
||||
private final Clock mClock;
|
||||
private final Context mContext;
|
||||
private final long mAccountId;
|
||||
private final long mMailboxId;
|
||||
private final RefreshManager mRefreshManager;
|
||||
/* package */ long mInboxId;
|
||||
|
||||
public RefreshTask(Context context, long accountId, long mailboxId) {
|
||||
this(context, accountId, mailboxId, Clock.INSTANCE,
|
||||
RefreshManager.getInstance(context));
|
||||
}
|
||||
|
||||
/* package */ RefreshTask(Context context, long accountId, long mailboxId, Clock clock,
|
||||
RefreshManager refreshManager) {
|
||||
mClock = clock;
|
||||
mContext = context;
|
||||
mRefreshManager = refreshManager;
|
||||
mAccountId = accountId;
|
||||
mMailboxId = mailboxId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do DB access on a worker thread.
|
||||
*/
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
mInboxId = Account.getInboxId(mContext, mAccountId);
|
||||
return Mailbox.isRefreshable(mContext, mMailboxId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the actual refresh.
|
||||
*/
|
||||
@Override
|
||||
protected void onPostExecute(Boolean isCurrentMailboxRefreshable) {
|
||||
if (isCancelled() || isCurrentMailboxRefreshable == null) {
|
||||
return;
|
||||
}
|
||||
if (isCurrentMailboxRefreshable) {
|
||||
mRefreshManager.refreshMessageList(mAccountId, mMailboxId);
|
||||
}
|
||||
// Refresh mailbox list
|
||||
if (mAccountId != -1) {
|
||||
if (shouldRefreshMailboxList()) {
|
||||
mRefreshManager.refreshMailboxList(mAccountId);
|
||||
}
|
||||
}
|
||||
// Refresh inbox
|
||||
if (shouldAutoRefreshInbox()) {
|
||||
mRefreshManager.refreshMessageList(mAccountId, mInboxId);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO implement this
|
||||
// - Refresh mailbox list. But don't do that always; implement a min interval.
|
||||
//
|
||||
// - Refresh the selected mailbox, if it's supported.
|
||||
// (regardless if the right-pane is MessageList or MessageView)
|
||||
// - If not suppoted (e.g. outbox, draft, or push mailboxes), refresh the inbox of the
|
||||
// current account.
|
||||
/**
|
||||
* @return true if the mailbox list of the current account hasn't been refreshed
|
||||
* in the last {@link #MAILBOX_REFRESH_MIN_INTERVAL}.
|
||||
*/
|
||||
/* package */ boolean shouldRefreshMailboxList() {
|
||||
if (mRefreshManager.isMailboxListRefreshing(mAccountId)) {
|
||||
return false;
|
||||
}
|
||||
final long nextRefreshTime = mRefreshManager.getLastMailboxListRefreshTime(mAccountId)
|
||||
+ MAILBOX_REFRESH_MIN_INTERVAL;
|
||||
if (nextRefreshTime > mClock.getTime()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// To do that, we need a way to tell the type of the currently selected mailbox.
|
||||
// We can do this with MessageListFragment, but it's gone it if a message is being viewed.
|
||||
// Maybe we should always have a MessageListFragment instance?
|
||||
// That way it'll be easier to restore the scroll position.
|
||||
/**
|
||||
* @return true if the inbox of the current account hasn't been refreshed
|
||||
* in the last {@link #INBOX_AUTO_REFRESH_MIN_INTERVAL}.
|
||||
*/
|
||||
/* package */ boolean shouldAutoRefreshInbox() {
|
||||
if (mInboxId == mMailboxId) {
|
||||
return false; // Current ID == inbox. No need to auto-refresh.
|
||||
}
|
||||
if (mRefreshManager.isMessageListRefreshing(mInboxId)) {
|
||||
return false;
|
||||
}
|
||||
final long nextRefreshTime = mRefreshManager.getLastMessageListRefreshTime(mInboxId)
|
||||
+ INBOX_AUTO_REFRESH_MIN_INTERVAL;
|
||||
if (nextRefreshTime > mClock.getTime()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,18 +22,27 @@ import com.android.email.provider.EmailContent.Mailbox;
|
||||
import android.content.AsyncTaskLoader;
|
||||
import android.content.Context;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
|
||||
/**
|
||||
* Loader to load {@link Mailbox} and {@link Account}.
|
||||
*/
|
||||
public class MailboxAccountLoader extends AsyncTaskLoader<MailboxAccountLoader.Result> {
|
||||
public static class Result {
|
||||
public Account mAccount;
|
||||
public Mailbox mMailbox;
|
||||
public boolean mIsEasAccount;
|
||||
public final boolean mIsFound;
|
||||
public final Account mAccount;
|
||||
public final Mailbox mMailbox;
|
||||
public final boolean mIsEasAccount;
|
||||
public final boolean mIsRefreshable;
|
||||
|
||||
public boolean isFound() {
|
||||
return (mAccount != null) && (mMailbox != null);
|
||||
private Result(boolean found, Account account, Mailbox mailbox, boolean isEasAccount,
|
||||
boolean isRefreshable) {
|
||||
mIsFound = found;
|
||||
mAccount = account;
|
||||
mMailbox = mailbox;
|
||||
mIsEasAccount = isEasAccount;
|
||||
mIsRefreshable = isRefreshable;
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,28 +51,38 @@ public class MailboxAccountLoader extends AsyncTaskLoader<MailboxAccountLoader.R
|
||||
|
||||
public MailboxAccountLoader(Context context, long mailboxId) {
|
||||
super(context);
|
||||
if (mailboxId == -1) {
|
||||
throw new InvalidParameterException();
|
||||
}
|
||||
mContext = context;
|
||||
mMailboxId = mailboxId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result loadInBackground() {
|
||||
Result result = new Result();
|
||||
boolean found = false;
|
||||
Account account = null;
|
||||
Mailbox mailbox = null;
|
||||
boolean isEasAccount = false;
|
||||
boolean isRefreshable = false;
|
||||
|
||||
if (mMailboxId < 0) {
|
||||
// Magic mailbox.
|
||||
found = true;
|
||||
} else {
|
||||
result.mMailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
|
||||
if (result.mMailbox != null) {
|
||||
result.mAccount = Account.restoreAccountWithId(mContext,
|
||||
result.mMailbox.mAccountKey);
|
||||
if (result.mAccount != null) {
|
||||
result.mIsEasAccount = result.mAccount.isEasAccount(mContext) ;
|
||||
mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
|
||||
if (mailbox != null) {
|
||||
account = Account.restoreAccountWithId(mContext, mailbox.mAccountKey);
|
||||
if (account != null) {
|
||||
found = true;
|
||||
isEasAccount = account.isEasAccount(mContext) ;
|
||||
isRefreshable = Mailbox.isRefreshable(mContext, mMailboxId);
|
||||
} else { // Account removed?
|
||||
mailbox = null;
|
||||
}
|
||||
}
|
||||
if (result.mAccount == null) { // account removed??
|
||||
result.mMailbox = null;
|
||||
}
|
||||
}
|
||||
Result result = new Result(found, account, mailbox, isEasAccount, isRefreshable);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -943,6 +943,9 @@ public abstract class EmailContent {
|
||||
public static final String SECURITY_NONZERO_SELECTION =
|
||||
Account.SECURITY_FLAGS + " IS NOT NULL AND " + Account.SECURITY_FLAGS + "!=0";
|
||||
|
||||
private static final String FIND_INBOX_SELECTION =
|
||||
MailboxColumns.TYPE + " = " + Mailbox.TYPE_INBOX +
|
||||
" AND " + MailboxColumns.ACCOUNT_KEY + " =?";
|
||||
|
||||
/**
|
||||
* This projection is for searching for the default account
|
||||
@ -1434,6 +1437,15 @@ public abstract class EmailContent {
|
||||
return (flags != null) && ((flags & Account.FLAGS_SECURITY_HOLD) != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return id of the "inbox" mailbox, or -1 if not found.
|
||||
*/
|
||||
public static long getInboxId(Context context, long accountId) {
|
||||
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI, ID_PROJECTION,
|
||||
FIND_INBOX_SELECTION, new String[] {Long.toString(accountId)}, null,
|
||||
ID_PROJECTION_COLUMN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all account hold flags that are set.
|
||||
*
|
||||
@ -2079,6 +2091,11 @@ public abstract class EmailContent {
|
||||
};
|
||||
private static final int MESSAGE_COUNT_COUNT_COLUMN = 0;
|
||||
|
||||
private static final String[] MAILBOX_TYPE_PROJECTION = new String [] {
|
||||
MailboxColumns.TYPE
|
||||
};
|
||||
private static final int MAILBOX_TYPE_TYPE_COLUMN = 0;
|
||||
|
||||
public static final long NO_MAILBOX = -1;
|
||||
|
||||
// Sentinel values for the mSyncInterval field of both Mailbox records
|
||||
@ -2267,6 +2284,25 @@ public abstract class EmailContent {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if a mailbox is refreshable.
|
||||
*/
|
||||
public static boolean isRefreshable(Context context, long mailboxId) {
|
||||
if (mailboxId < 0) {
|
||||
return false; // magic mailboxes
|
||||
}
|
||||
Uri url = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
|
||||
int type = Utility.getFirstRowInt(context, url, MAILBOX_TYPE_PROJECTION,
|
||||
null, null, null, MAILBOX_TYPE_TYPE_COLUMN);
|
||||
Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
|
||||
switch (mailbox.mType) {
|
||||
case TYPE_DRAFTS:
|
||||
case TYPE_OUTBOX:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public interface HostAuthColumns {
|
||||
|
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.email.activity;
|
||||
|
||||
import com.android.email.Clock;
|
||||
import com.android.email.Controller;
|
||||
import com.android.email.MockClock;
|
||||
import com.android.email.RefreshManager;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
/**
|
||||
* Tests for {@link MessageListXL.RefreshTask}.
|
||||
*
|
||||
* TOOD Add more tests.
|
||||
* Right now, it only has tests for the "shouldXxx" methods, because it's hard to notice when
|
||||
* they're subtly broken. (And the spec may change.)
|
||||
*/
|
||||
@SmallTest
|
||||
public class MessageListXLRefreshTaskTest extends AndroidTestCase {
|
||||
private MockClock mClock = new MockClock();
|
||||
private MockRefreshManager mRefreshManager;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
mRefreshManager = new MockRefreshManager(getContext(), Controller.getInstance(getContext()),
|
||||
mClock, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
mRefreshManager.cleanUpForTest();
|
||||
}
|
||||
|
||||
public void testShouldRefreshMailboxList() {
|
||||
final long ACCOUNT_ID = 5;
|
||||
final long MAILBOX_ID = 10;
|
||||
|
||||
MessageListXL.RefreshTask task = new MessageListXL.RefreshTask(getContext(), ACCOUNT_ID,
|
||||
MAILBOX_ID, mClock, mRefreshManager);
|
||||
|
||||
mRefreshManager.mExpectedAccountId = ACCOUNT_ID;
|
||||
|
||||
mClock.mTime = 123456789;
|
||||
|
||||
// Not refreshing, never refreshed == should sync
|
||||
mRefreshManager.mIsMailboxListRefreshing = false;
|
||||
mRefreshManager.mLastMailboxListRefresTime = 0;
|
||||
assertTrue(task.shouldRefreshMailboxList());
|
||||
|
||||
// IS refreshing, never refreshed == should NOT sync
|
||||
mRefreshManager.mIsMailboxListRefreshing = true;
|
||||
mRefreshManager.mLastMailboxListRefresTime = 0;
|
||||
assertFalse(task.shouldRefreshMailboxList());
|
||||
|
||||
// Not refreshing, just refreshed == should NOT sync
|
||||
mRefreshManager.mIsMailboxListRefreshing = false;
|
||||
mRefreshManager.mLastMailboxListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMailboxListRefresTime;
|
||||
assertFalse(task.shouldRefreshMailboxList());
|
||||
|
||||
// Not refreshing, refreshed 1 ms ago == should NOT sync
|
||||
mRefreshManager.mLastMailboxListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMailboxListRefresTime + 1;
|
||||
assertFalse(task.shouldRefreshMailboxList());
|
||||
|
||||
// Not refreshing, refreshed TIMEOUT-1 ago == should NOT sync
|
||||
mRefreshManager.mLastMailboxListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMailboxListRefresTime
|
||||
+ MessageListXL.MAILBOX_REFRESH_MIN_INTERVAL - 1;
|
||||
assertFalse(task.shouldRefreshMailboxList());
|
||||
|
||||
// 1 ms laster... should sync.
|
||||
mClock.advance();
|
||||
assertTrue(task.shouldRefreshMailboxList());
|
||||
}
|
||||
|
||||
public void testShouldAutoRefreshInbox() {
|
||||
final long ACCOUNT_ID = 5;
|
||||
final long MAILBOX_ID = 10;
|
||||
|
||||
MessageListXL.RefreshTask task = new MessageListXL.RefreshTask(getContext(), ACCOUNT_ID,
|
||||
MAILBOX_ID, mClock, mRefreshManager);
|
||||
|
||||
mRefreshManager.mExpectedAccountId = ACCOUNT_ID;
|
||||
|
||||
mClock.mTime = 123456789;
|
||||
|
||||
// Current mailbox != inbox, not refreshing, never refreshed == should sync
|
||||
mRefreshManager.mIsMessageListRefreshing = false;
|
||||
mRefreshManager.mLastMessageListRefresTime = 0;
|
||||
task.mInboxId = MAILBOX_ID + 1;
|
||||
mRefreshManager.mExpectedMailboxId = MAILBOX_ID + 1;
|
||||
assertTrue(task.shouldAutoRefreshInbox());
|
||||
|
||||
// Current mailbox == inbox should NOT sync.
|
||||
task.mInboxId = MAILBOX_ID;
|
||||
mRefreshManager.mExpectedMailboxId = MAILBOX_ID;
|
||||
assertFalse(task.shouldAutoRefreshInbox());
|
||||
|
||||
// Fron here, Current mailbox != inbox
|
||||
task.mInboxId = MAILBOX_ID + 1;
|
||||
mRefreshManager.mExpectedMailboxId = MAILBOX_ID + 1;
|
||||
|
||||
// IS refreshing, never refreshed == should NOT sync
|
||||
mRefreshManager.mIsMessageListRefreshing = true;
|
||||
mRefreshManager.mLastMessageListRefresTime = 0;
|
||||
assertFalse(task.shouldAutoRefreshInbox());
|
||||
|
||||
// Not refreshing, just refreshed == should NOT sync
|
||||
mRefreshManager.mIsMessageListRefreshing = false;
|
||||
mRefreshManager.mLastMessageListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMessageListRefresTime;
|
||||
assertFalse(task.shouldAutoRefreshInbox());
|
||||
|
||||
// Not refreshing, refreshed 1 ms ago == should NOT sync
|
||||
mRefreshManager.mLastMessageListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMessageListRefresTime + 1;
|
||||
assertFalse(task.shouldAutoRefreshInbox());
|
||||
|
||||
// Not refreshing, refreshed TIMEOUT-1 ago == should NOT sync
|
||||
mRefreshManager.mLastMessageListRefresTime = 1234567890;
|
||||
mClock.mTime = mRefreshManager.mLastMessageListRefresTime
|
||||
+ MessageListXL.INBOX_AUTO_REFRESH_MIN_INTERVAL - 1;
|
||||
assertFalse(task.shouldAutoRefreshInbox());
|
||||
|
||||
// 1 ms laster... should sync.
|
||||
mClock.advance();
|
||||
assertTrue(task.shouldAutoRefreshInbox());
|
||||
}
|
||||
|
||||
private static class MockRefreshManager extends RefreshManager {
|
||||
public long mExpectedAccountId;
|
||||
public long mExpectedMailboxId;
|
||||
public boolean mIsMailboxListRefreshing;
|
||||
public long mLastMailboxListRefresTime;
|
||||
public boolean mIsMessageListRefreshing;
|
||||
public long mLastMessageListRefresTime;
|
||||
|
||||
protected MockRefreshManager(
|
||||
Context context, Controller controller, Clock clock, Handler handler) {
|
||||
super(context, controller, clock, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMailboxListRefreshing(long accountId) {
|
||||
Assert.assertEquals(mExpectedAccountId, accountId);
|
||||
return mIsMailboxListRefreshing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastMailboxListRefreshTime(long accountId) {
|
||||
Assert.assertEquals(mExpectedAccountId, accountId);
|
||||
return mLastMailboxListRefresTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMessageListRefreshing(long mailboxId) {
|
||||
Assert.assertEquals(mExpectedMailboxId, mailboxId);
|
||||
return mIsMessageListRefreshing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastMessageListRefreshTime(long mailboxId) {
|
||||
Assert.assertEquals(mExpectedMailboxId, mailboxId);
|
||||
return mLastMessageListRefresTime;
|
||||
}
|
||||
}
|
||||
}
|
@ -35,45 +35,68 @@ public class MailboxAccountLoaderTestCase extends LoaderTestCase {
|
||||
getContext(), EmailProvider.class);
|
||||
}
|
||||
|
||||
private long createAccount() {
|
||||
Account acct = ProviderTestUtils.setupAccount("acct1", true, mProviderContext);
|
||||
private long createAccount(boolean isEas) {
|
||||
Account acct = ProviderTestUtils.setupAccount("acct1", false, mProviderContext);
|
||||
String proto = isEas ? "eas" : "non-eas";
|
||||
acct.mHostAuthRecv = ProviderTestUtils.setupHostAuth(proto, "hostauth", -1, false,
|
||||
mProviderContext);
|
||||
acct.save(mProviderContext);
|
||||
return acct.mId;
|
||||
}
|
||||
|
||||
private long createMailbox(long accountId) {
|
||||
Mailbox box = ProviderTestUtils.setupMailbox("name", accountId, true, mProviderContext);
|
||||
private long createMailbox(long accountId, int type) {
|
||||
Mailbox box = ProviderTestUtils.setupMailbox("name", accountId, false, mProviderContext);
|
||||
box.mType = type;
|
||||
box.save(mProviderContext);
|
||||
return box.mId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link MailboxAccountLoader.Result#isFound()}
|
||||
*/
|
||||
public void testIsFound() {
|
||||
MailboxAccountLoader.Result result = new MailboxAccountLoader.Result();
|
||||
assertFalse(result.isFound());
|
||||
|
||||
result.mAccount = new Account();
|
||||
assertFalse(result.isFound());
|
||||
|
||||
result.mMailbox = new Mailbox();
|
||||
assertTrue(result.isFound());
|
||||
|
||||
result.mAccount = null;
|
||||
assertFalse(result.isFound());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for normal case. (account, mailbox found)
|
||||
*/
|
||||
public void testLoad() {
|
||||
final long accountId = createAccount();
|
||||
final long mailboxId = createMailbox(accountId);
|
||||
final long accountId = createAccount(false);
|
||||
final long mailboxId = createMailbox(accountId, Mailbox.TYPE_MAIL);
|
||||
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, mailboxId));
|
||||
assertTrue(result.isFound());
|
||||
assertTrue(result.mIsFound);
|
||||
assertEquals(accountId, result.mAccount.mId);
|
||||
assertEquals(mailboxId, result.mMailbox.mId);
|
||||
assertFalse(result.mIsEasAccount);
|
||||
assertTrue(result.mIsRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load - isEas = true
|
||||
*/
|
||||
public void testLoadEas() {
|
||||
final long accountId = createAccount(true);
|
||||
final long mailboxId = createMailbox(accountId, Mailbox.TYPE_MAIL);
|
||||
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, mailboxId));
|
||||
assertTrue(result.mIsFound);
|
||||
assertEquals(accountId, result.mAccount.mId);
|
||||
assertEquals(mailboxId, result.mMailbox.mId);
|
||||
assertTrue(result.mIsEasAccount);
|
||||
assertTrue(result.mIsRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load -- drafts, not refreshable.
|
||||
*/
|
||||
public void testLoadNotRefreshable() {
|
||||
final long accountId = createAccount(false);
|
||||
final long mailboxId = createMailbox(accountId, Mailbox.TYPE_DRAFTS);
|
||||
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, mailboxId));
|
||||
assertTrue(result.mIsFound);
|
||||
assertEquals(accountId, result.mAccount.mId);
|
||||
assertEquals(mailboxId, result.mMailbox.mId);
|
||||
assertFalse(result.mIsEasAccount);
|
||||
assertFalse(result.mIsRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,21 +105,38 @@ public class MailboxAccountLoaderTestCase extends LoaderTestCase {
|
||||
public void testMailboxNotFound() {
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, 123));
|
||||
assertFalse(result.isFound());
|
||||
assertFalse(result.mIsFound);
|
||||
assertNull(result.mAccount);
|
||||
assertNull(result.mMailbox);
|
||||
assertFalse(result.mIsEasAccount);
|
||||
assertFalse(result.mIsRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Account not found.
|
||||
*/
|
||||
public void testAccountNotFound() {
|
||||
final long mailboxId = createMailbox(1);
|
||||
final long mailboxId = createMailbox(1, Mailbox.TYPE_MAIL);
|
||||
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, mailboxId));
|
||||
assertFalse(result.isFound());
|
||||
assertFalse(result.mIsFound);
|
||||
assertNull(result.mAccount);
|
||||
assertNull(result.mMailbox);
|
||||
assertFalse(result.mIsEasAccount);
|
||||
assertFalse(result.mIsRefreshable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic mailbox. (always found)
|
||||
*/
|
||||
public void testMagicMailbox() {
|
||||
MailboxAccountLoader.Result result = getLoaderResultSynchronously(
|
||||
new MailboxAccountLoader(mProviderContext, Mailbox.QUERY_ALL_INBOXES));
|
||||
assertTrue(result.mIsFound);
|
||||
assertNull(result.mAccount);
|
||||
assertNull(result.mMailbox);
|
||||
assertFalse(result.mIsEasAccount);
|
||||
assertFalse(result.mIsRefreshable);
|
||||
}
|
||||
}
|
||||
|
@ -1990,4 +1990,39 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
|
||||
ProviderTestUtils.assertMailboxEqual("x", b1, Mailbox.getMailboxForMessageId(c, m1.mId));
|
||||
ProviderTestUtils.assertMailboxEqual("x", b2, Mailbox.getMailboxForMessageId(c, m2.mId));
|
||||
}
|
||||
|
||||
public void testGetAccountGetInboxIdTest() {
|
||||
final Context c = mMockContext;
|
||||
|
||||
// Prepare some data with red-herrings.
|
||||
Account a1 = ProviderTestUtils.setupAccount("acct1", true, c);
|
||||
Account a2 = ProviderTestUtils.setupAccount("acct2", true, c);
|
||||
Mailbox b1i = ProviderTestUtils.setupMailbox("b1i", a1.mId, true, c, Mailbox.TYPE_INBOX);
|
||||
Mailbox b2a = ProviderTestUtils.setupMailbox("b2a", a2.mId, true, c, Mailbox.TYPE_MAIL);
|
||||
Mailbox b2i = ProviderTestUtils.setupMailbox("b2b", a2.mId, true, c, Mailbox.TYPE_INBOX);
|
||||
|
||||
assertEquals(b2i.mId, Account.getInboxId(c, a2.mId));
|
||||
}
|
||||
|
||||
public void testMailboxIsRefreshable() {
|
||||
final Context c = mMockContext;
|
||||
|
||||
Account a = ProviderTestUtils.setupAccount("acct1", true, c);
|
||||
Mailbox bi = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_INBOX);
|
||||
Mailbox bm = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_MAIL);
|
||||
Mailbox bd = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_DRAFTS);
|
||||
Mailbox bo = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_OUTBOX);
|
||||
|
||||
assertTrue(Mailbox.isRefreshable(c, bi.mId));
|
||||
assertTrue(Mailbox.isRefreshable(c, bm.mId));
|
||||
assertFalse(Mailbox.isRefreshable(c, bd.mId));
|
||||
assertFalse(Mailbox.isRefreshable(c, bo.mId));
|
||||
|
||||
// No such mailbox
|
||||
assertFalse(Mailbox.isRefreshable(c, -1));
|
||||
|
||||
// Magic mailboxes can't be refreshed.
|
||||
assertFalse(Mailbox.isRefreshable(c, Mailbox.QUERY_ALL_DRAFTS));
|
||||
assertFalse(Mailbox.isRefreshable(c, Mailbox.QUERY_ALL_INBOXES));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user