From 02ba344cf2489dba81058e25e3202f2898360083 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Thu, 20 Jan 2011 12:39:13 -0800 Subject: [PATCH] Only show messages from inbox for account & unread views Use a sub-selection to limit the messages from the inbox. Also add a unit test to ensure the selections are working correctly. bug 3368613 Change-Id: Ia24ef6028ded27c69f982ecbc6b67f35f84d1b6d --- .../android/email/provider/EmailContent.java | 18 ++++- .../email/provider/WidgetProvider.java | 23 ++---- .../email/provider/WidgetProviderTests.java | 71 +++++++++++++++++++ 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/com/android/email/provider/EmailContent.java b/src/com/android/email/provider/EmailContent.java index c7a906e72..9f0be5e6b 100644 --- a/src/com/android/email/provider/EmailContent.java +++ b/src/com/android/email/provider/EmailContent.java @@ -541,15 +541,27 @@ public abstract class EmailContent { public static final String[] ID_COLUMN_PROJECTION = new String[] { RECORD_ID }; + private static final String ACCOUNT_KEY_SELECTION = + MessageColumns.ACCOUNT_KEY + "=?"; + public static final String ALL_FAVORITE_SELECTION = MessageColumns.FLAG_FAVORITE + "=1 AND " + MessageColumns.MAILBOX_KEY + " NOT IN (" + "SELECT " + MailboxColumns.ID + " FROM " + Mailbox.TABLE_NAME + "" + " WHERE " + MailboxColumns.TYPE + " = " + Mailbox.TYPE_TRASH + ")"; - - private static final String ACCOUNT_KEY_SELECTION = - MessageColumns.ACCOUNT_KEY + "=?"; + /** Selection to retrieve all messages in "inbox" for any account */ + public static final String INBOX_SELECTION = + MessageColumns.MAILBOX_KEY + " IN (" + + "SELECT " + MailboxColumns.ID + " FROM " + Mailbox.TABLE_NAME + + " WHERE " + MailboxColumns.TYPE + " = " + Mailbox.TYPE_INBOX + + ")"; + /** Selection to retrieve unread messages in "inbox" for any account */ + public static final String UNREAD_SELECTION = + MessageColumns.FLAG_READ + "=0 AND " + INBOX_SELECTION; + /** Selection to retrieve all messages in "inbox" for one account */ + public static final String PER_ACCOUNT_INBOX_SELECTION = + ACCOUNT_KEY_SELECTION + " AND " + INBOX_SELECTION; private static final String ACCOUNT_FAVORITE_SELECTION = ACCOUNT_KEY_SELECTION + " AND " + ALL_FAVORITE_SELECTION; diff --git a/src/com/android/email/provider/WidgetProvider.java b/src/com/android/email/provider/WidgetProvider.java index cbc35f07f..cbecc3b3d 100644 --- a/src/com/android/email/provider/WidgetProvider.java +++ b/src/com/android/email/provider/WidgetProvider.java @@ -127,14 +127,14 @@ public class WidgetProvider extends AppWidgetProvider { * Types of views that we're prepared to show in the widget - all mail, unread mail, and starred * mail; we rotate between them. Each ViewType is composed of a selection string and a title. */ - public enum ViewType { - ALL_INBOX(null, NO_ARGUMENTS, R.string.widget_all_mail), - UNREAD(MessageColumns.FLAG_READ + "=0", NO_ARGUMENTS, R.string.widget_unread), + /* package */ enum ViewType { + ALL_INBOX(Message.INBOX_SELECTION, NO_ARGUMENTS, R.string.widget_all_mail), + UNREAD(Message.UNREAD_SELECTION, NO_ARGUMENTS, R.string.widget_unread), STARRED(Message.ALL_FAVORITE_SELECTION, NO_ARGUMENTS, R.string.widget_starred), - ACCOUNT(MessageColumns.ACCOUNT_KEY + "=?", new String[1], 0); + ACCOUNT(Message.PER_ACCOUNT_INBOX_SELECTION, new String[1], 0); - private final String selection; - private final String[] selectionArgs; + /* package */ final String selection; + /* package */ final String[] selectionArgs; private final int titleResource; private String title; @@ -168,15 +168,6 @@ public class WidgetProvider extends AppWidgetProvider { return sb.toString(); } - - public String getSelection(Context context) { - // For "all inbox", we define a special selection - if (this == ViewType.ALL_INBOX) { - // Rebuild selection every time in case accounts have been added or removed - return Utility.buildMailboxIdSelection(context, Mailbox.QUERY_ALL_INBOXES); - } - return selection; - } } static class EmailWidget implements RemoteViewsService.RemoteViewsFactory { @@ -279,7 +270,7 @@ public class WidgetProvider extends AppWidgetProvider { */ private void load(ViewType viewType) { reset(); - setSelection(viewType.getSelection(sContext)); + setSelection(viewType.selection); setSelectionArgs(viewType.selectionArgs); startLoading(); } diff --git a/tests/src/com/android/email/provider/WidgetProviderTests.java b/tests/src/com/android/email/provider/WidgetProviderTests.java index b2650995f..b4f9da825 100644 --- a/tests/src/com/android/email/provider/WidgetProviderTests.java +++ b/tests/src/com/android/email/provider/WidgetProviderTests.java @@ -15,10 +15,17 @@ package com.android.email.provider; +import com.android.email.provider.EmailContent.Account; +import com.android.email.provider.EmailContent.Mailbox; +import com.android.email.provider.EmailContent.Message; +import com.android.email.provider.WidgetProvider; import com.android.email.provider.WidgetProvider.EmailWidget; +import com.android.email.provider.WidgetProvider.ViewType; import com.android.email.provider.WidgetProvider.WidgetViewSwitcher; +import android.content.ContentResolver; import android.content.Context; +import android.database.Cursor; import android.test.ProviderTestCase2; import java.util.concurrent.ExecutionException; @@ -60,6 +67,24 @@ public class WidgetProviderTests extends ProviderTestCase2 { } } + private int getMessageCount(ViewType view) { + int messageCount = 0; + ContentResolver cr = mMockContext.getContentResolver(); + Cursor c = cr.query(Message.CONTENT_URI, WidgetProvider.EmailWidget.WIDGET_PROJECTION, + view.selection, view.selectionArgs, null); + try { + messageCount = c.getCount(); + } finally { + c.close(); + } + return messageCount; + } + + private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read) { + return ProviderTestUtils.setupMessage("1", b.mAccountKey, b.mId, true, true, c, starred, + read); + } + public void testWidgetSwitcher() { // Create account ProviderTestUtils.setupAccount("account1", true, mMockContext); @@ -89,4 +114,50 @@ public class WidgetProviderTests extends ProviderTestCase2 { switchSync(widget); assertEquals(WidgetProvider.ViewType.STARRED, widget.mViewType); } + + /** + * Test the message counts returned by the ViewType selectors. + */ + public void testCursorCount() { + // Create 2 accounts + Account a1 = ProviderTestUtils.setupAccount("account1", true, mMockContext); + Account a2 = ProviderTestUtils.setupAccount("account2", true, mMockContext); + + // Create 2 mailboxes for each account + Mailbox b1 = ProviderTestUtils.setupMailbox( + "box1", a1.mId, true, mMockContext, Mailbox.TYPE_INBOX); + Mailbox b2 = ProviderTestUtils.setupMailbox( + "box2", a1.mId, true, mMockContext, Mailbox.TYPE_OUTBOX); + Mailbox b3 = ProviderTestUtils.setupMailbox( + "box3", a2.mId, true, mMockContext, Mailbox.TYPE_INBOX); + Mailbox b4 = ProviderTestUtils.setupMailbox( + "box4", a2.mId, true, mMockContext, Mailbox.TYPE_OUTBOX); + Mailbox bt = ProviderTestUtils.setupMailbox( + "boxT", a2.mId, true, mMockContext, Mailbox.TYPE_TRASH); + + // Create some messages + // b1 (account 1, inbox): 1 message, including 1 starred + Message m11 = createMessage(mMockContext, b1, true, false); + + // b2 (account 1, outbox): 2 message, including 1 starred + Message m21 = createMessage(mMockContext, b2, false, false); + Message m22 = createMessage(mMockContext, b2, true, true); + + // b3 (account 2, inbox): 3 message, including 1 starred + Message m31 = createMessage(mMockContext, b3, false, false); + Message m32 = createMessage(mMockContext, b3, false, true); + Message m33 = createMessage(mMockContext, b3, true, true); + + // b4 (account 2, outbox) has no messages. + + // bt (account 2, trash) has 3 messages, including 2 starred + Message mt1 = createMessage(mMockContext, bt, true, false); + Message mt2 = createMessage(mMockContext, bt, true, true); + Message mt3 = createMessage(mMockContext, bt, false, false); + + assertEquals(4, getMessageCount(ViewType.ALL_INBOX)); + assertEquals(3, getMessageCount(ViewType.STARRED)); + assertEquals(2, getMessageCount(ViewType.UNREAD)); + } + }