Async code for read/unread and starred

* Move each worker into async (combining common code)
* Update unit tests to match
* Make the message->account->controller lookups more efficient

Bug: 3134653
Change-Id: Icc82998a5d8eb07c7ebc7edbd28cd9308378d866
This commit is contained in:
Andy Stadler 2010-12-23 09:55:02 -08:00
parent 8d22e85e45
commit 3b1cccf234
2 changed files with 55 additions and 65 deletions

View File

@ -741,7 +741,7 @@ public class Controller {
*
* @param messageIds The IDs of the messages to move
* @param newMailboxId The id of the folder we're supposed to move the folder to
* @return the AsyncTask that will execute the move
* @return the AsyncTask that will execute the move (for testing only)
*/
public AsyncTask<Void, Void, Void> moveMessage(final long[] messageIds,
final long newMailboxId) {
@ -771,67 +771,54 @@ public class Controller {
/**
* Set/clear the unread status of a message
*
* TODO db ops should not be in this thread. queue it up.
*
* @param messageId the message to update
* @param isRead the new value for the isRead flag
* @return the AsyncTask that will execute the changes (for testing only)
*/
public void setMessageRead(final long messageId, boolean isRead) {
ContentValues cv = new ContentValues();
cv.put(EmailContent.MessageColumns.FLAG_READ, isRead);
Uri uri = ContentUris.withAppendedId(
EmailContent.Message.SYNCED_CONTENT_URI, messageId);
mProviderContext.getContentResolver().update(uri, cv, null, null);
// Service runs automatically, MessagingController needs a kick
final Message message = Message.restoreMessageWithId(mProviderContext, messageId);
if (message == null) {
return;
}
Account account = Account.restoreAccountWithId(mProviderContext, message.mAccountKey);
if (account == null) {
return; // isMessagingController returns false for null, but let's make it clear.
}
if (isMessagingController(account)) {
Utility.runAsync(new Runnable() {
public void run() {
mLegacyController.processPendingActions(message.mAccountKey);
}
});
}
public AsyncTask<Void, Void, Void> setMessageRead(final long messageId, final boolean isRead) {
return setMessageBoolean(messageId, EmailContent.MessageColumns.FLAG_READ, isRead);
}
/**
* Set/clear the favorite status of a message
*
* TODO db ops should not be in this thread. queue it up.
*
* @param messageId the message to update
* @param isFavorite the new value for the isFavorite flag
* @return the AsyncTask that will execute the changes (for testing only)
*/
public void setMessageFavorite(final long messageId, boolean isFavorite) {
ContentValues cv = new ContentValues();
cv.put(EmailContent.MessageColumns.FLAG_FAVORITE, isFavorite);
Uri uri = ContentUris.withAppendedId(
EmailContent.Message.SYNCED_CONTENT_URI, messageId);
mProviderContext.getContentResolver().update(uri, cv, null, null);
public AsyncTask<Void, Void, Void> setMessageFavorite(final long messageId,
final boolean isFavorite) {
return setMessageBoolean(messageId, EmailContent.MessageColumns.FLAG_FAVORITE, isFavorite);
}
// Service runs automatically, MessagingController needs a kick
final Message message = Message.restoreMessageWithId(mProviderContext, messageId);
if (message == null) {
return;
}
Account account = Account.restoreAccountWithId(mProviderContext, message.mAccountKey);
if (account == null) {
return; // isMessagingController returns false for null, but let's make it clear.
}
if (isMessagingController(account)) {
Utility.runAsync(new Runnable() {
public void run() {
mLegacyController.processPendingActions(message.mAccountKey);
/**
* Set/clear boolean columns of a message
*
* @param messageId the message to update
* @param columnName the column to update
* @param columnValue the new value for the column
* @return the AsyncTask that will execute the changes (for testing only)
*/
private AsyncTask<Void, Void, Void> setMessageBoolean(final long messageId,
final String columnName, final boolean columnValue) {
return Utility.runAsync(new Runnable() {
public void run() {
ContentValues cv = new ContentValues();
cv.put(columnName, columnValue);
Uri uri = ContentUris.withAppendedId(
EmailContent.Message.SYNCED_CONTENT_URI, messageId);
mProviderContext.getContentResolver().update(uri, cv, null, null);
// Service runs automatically, MessagingController needs a kick
long accountId = Account.getAccountIdForMessageId(mProviderContext, messageId);
if (accountId == -1) {
return;
}
});
}
if (isMessagingController(accountId)) {
mLegacyController.processPendingActions(accountId);
}
}
});
}
/**
@ -1579,8 +1566,8 @@ public class Controller {
public static class ControllerService extends Service {
/**
* Create our EmailService implementation here. For now, only loadAttachment is supported; the
* intention, however, is to move more functionality to the service interface
* Create our EmailService implementation here. For now, only loadAttachment is supported;
* the intention, however, is to move more functionality to the service interface
*/
private final IEmailService.Stub mBinder = new IEmailService.Stub() {
@ -1621,7 +1608,7 @@ public class Controller {
msg = Message.restoreMessageWithId(ControllerService.this,
Long.parseLong(cols[BODY_SOURCE_KEY_COLUMN]));
if (msg == null) {
// TODO: We can try restoring from the deleted table at this point...
// TODO: We can try restoring from the deleted table here...
return;
}
}

View File

@ -161,9 +161,6 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
/**
* Test the "move message" function.
*
* @throws ExecutionException
* @throws InterruptedException
*/
public void testMoveMessage() throws InterruptedException, ExecutionException {
Account account1 = ProviderTestUtils.setupAccount("message-move", true, mProviderContext);
@ -196,8 +193,6 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
* Test the "delete message" function. Sunny day:
* - message/mailbox/account all exist
* - trash mailbox exists
* @throws ExecutionException
* @throws InterruptedException
*/
public void testDeleteMessage() throws InterruptedException, ExecutionException {
Account account1 = ProviderTestUtils.setupAccount("message-delete", true, mProviderContext);
@ -272,8 +267,6 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
/**
* Test deleting message when there is no trash mailbox
* @throws ExecutionException
* @throws InterruptedException
*/
public void testDeleteMessageNoTrash() throws InterruptedException, ExecutionException {
Account account1 =
@ -304,7 +297,7 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
/**
* Test read/unread flag
*/
public void testReadUnread() {
public void testReadUnread() throws InterruptedException, ExecutionException {
Account account1 = ProviderTestUtils.setupAccount("read-unread", false, mProviderContext);
account1.mHostAuthRecv
= ProviderTestUtils.setupHostAuth("read-unread", 0, false, mProviderContext);
@ -318,20 +311,25 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
long message1Id = message1.mId;
// test setting to "read"
mTestController.setMessageRead(message1Id, true);
mTestController.setMessageRead(message1Id, true).get();
Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertTrue(message1get.mFlagRead);
// test setting to "unread"
mTestController.setMessageRead(message1Id, false);
mTestController.setMessageRead(message1Id, false).get();
message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertFalse(message1get.mFlagRead);
// test setting to "read"
mTestController.setMessageRead(message1Id, true).get();
message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertTrue(message1get.mFlagRead);
}
/**
* Test favorites flag
*/
public void testFavorites() {
public void testFavorites() throws InterruptedException, ExecutionException {
Account account1 = ProviderTestUtils.setupAccount("favorites", false, mProviderContext);
account1.mHostAuthRecv
= ProviderTestUtils.setupHostAuth("favorites", 0, false, mProviderContext);
@ -345,14 +343,19 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
long message1Id = message1.mId;
// test setting to "favorite"
mTestController.setMessageFavorite(message1Id, true);
mTestController.setMessageFavorite(message1Id, true).get();
Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertTrue(message1get.mFlagFavorite);
// test setting to "not favorite"
mTestController.setMessageFavorite(message1Id, false);
mTestController.setMessageFavorite(message1Id, false).get();
message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertFalse(message1get.mFlagFavorite);
// test setting to "favorite"
mTestController.setMessageFavorite(message1Id, true).get();
message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
assertTrue(message1get.mFlagFavorite);
}
public void testGetAndDeleteAttachmentMailbox() {