diff --git a/src/com/android/email/Utility.java b/src/com/android/email/Utility.java index 532cb1383..df310852b 100644 --- a/src/com/android/email/Utility.java +++ b/src/com/android/email/Utility.java @@ -831,6 +831,12 @@ public class Utility { } }; + private static final CursorGetter STRING_GETTER = new CursorGetter() { + public String get(Cursor cursor, int column) { + return cursor.getString(column); + } + }; + private static final CursorGetter BLOB_GETTER = new CursorGetter() { public byte[] get(Cursor cursor, int column) { return cursor.getBlob(column); @@ -899,7 +905,26 @@ public class Utility { } /** - * {@link #getFirstRowColumn} for a byte array with null as a default value. + * {@link #getFirstRowColumn} for a String with null as a default value. + */ + public static String getFirstRowString(Context context, Uri uri, String[] projection, + String selection, String[] selectionArgs, String sortOrder, int column) { + return getFirstRowColumn(context, uri, projection, selection, selectionArgs, + sortOrder, column, null, STRING_GETTER); + } + + /** + * {@link #getFirstRowColumn} for a String with a provided default value. + */ + public static String getFirstRowString(Context context, Uri uri, String[] projection, + String selection, String[] selectionArgs, String sortOrder, int column, + String defaultValue) { + return getFirstRowColumn(context, uri, projection, selection, selectionArgs, + sortOrder, column, defaultValue, STRING_GETTER); + } + + /** + * {@link #getFirstRowColumn} for a byte array with a provided default value. */ public static byte[] getFirstRowBlob(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int column, diff --git a/src/com/android/email/activity/ActivityHelper.java b/src/com/android/email/activity/ActivityHelper.java index 7a3345c87..597598ef7 100644 --- a/src/com/android/email/activity/ActivityHelper.java +++ b/src/com/android/email/activity/ActivityHelper.java @@ -19,6 +19,7 @@ package com.android.email.activity; import com.android.email.Controller; import com.android.email.R; import com.android.email.Utility; +import com.android.email.provider.EmailContent.Mailbox; import android.app.Activity; import android.content.ActivityNotFoundException; @@ -100,11 +101,21 @@ public final class ActivityHelper { activity.getResources().getQuantityString(R.plurals.message_deleted_toast, 1)); } - public static void moveMessages(Activity activity, long newMailboxId, long[] messageIds) { - // TODO Support moving multiple messages + public static void moveMessages(final Activity activity, final long newMailboxId, + final long[] messageIds) { Controller.getInstance(activity).moveMessage(messageIds, newMailboxId); - String message = activity.getResources().getQuantityString(R.plurals.message_moved_toast, - messageIds.length, messageIds.length , "a mailbox"); // STOPSHIP get mailbox name - Utility.showToast(activity, message); + Utility.runAsync(new Runnable() { + @Override + public void run() { + String mailboxName = Mailbox.getDisplayName(activity, newMailboxId); + if (mailboxName == null) { + return; // Mailbox gone?? + } + String message = activity.getResources().getQuantityString( + R.plurals.message_moved_toast, messageIds.length, messageIds.length , + mailboxName); + Utility.showToast(activity, message); + } + }); } } diff --git a/src/com/android/email/provider/EmailContent.java b/src/com/android/email/provider/EmailContent.java index 823440a5f..9578f8d3a 100644 --- a/src/com/android/email/provider/EmailContent.java +++ b/src/com/android/email/provider/EmailContent.java @@ -2165,6 +2165,11 @@ public abstract class EmailContent { }; private static final int MAILBOX_TYPE_TYPE_COLUMN = 0; + private static final String[] MAILBOX_DISPLAY_NAME_PROJECTION = new String [] { + MailboxColumns.DISPLAY_NAME + }; + private static final int MAILBOX_DISPLAY_NAME_COLUMN = 0; + public static final long NO_MAILBOX = -1; // Sentinel values for the mSyncInterval field of both Mailbox records @@ -2366,6 +2371,15 @@ public abstract class EmailContent { null, null, null, MAILBOX_TYPE_TYPE_COLUMN, -1); } + /** + * @return mailbox display name, or null if mailbox not found. + */ + public static String getDisplayName(Context context, long mailboxId) { + Uri url = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId); + return Utility.getFirstRowString(context, url, MAILBOX_DISPLAY_NAME_PROJECTION, + null, null, null, MAILBOX_DISPLAY_NAME_COLUMN, null); + } + /** * @param mailboxId ID of a mailbox. This method accepts magic mailbox IDs, such as * {@link #QUERY_ALL_INBOXES}. (They're all non-refreshable.) diff --git a/tests/src/com/android/email/UtilityMediumTests.java b/tests/src/com/android/email/UtilityMediumTests.java index 39f9dc731..0c33391d4 100644 --- a/tests/src/com/android/email/UtilityMediumTests.java +++ b/tests/src/com/android/email/UtilityMediumTests.java @@ -187,6 +187,38 @@ public class UtilityMediumTests extends ProviderTestCase2 { EmailContent.ID_PROJECTION_COLUMN, -1)); } + public void testGetFirstRowString() { + final String[] DISPLAY_NAME_PROJECTION = new String[] {Account.DISPLAY_NAME}; + + Account account1 = ProviderTestUtils.setupAccount("1", true, mMockContext); + Account account2 = ProviderTestUtils.setupAccount("X1", true, mMockContext); + Account account3 = ProviderTestUtils.setupAccount("X2", true, mMockContext); + + // case 1. Account found + assertEquals(account2.mDisplayName, Utility.getFirstRowString( + mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION, + Account.DISPLAY_NAME + " like :1", new String[] {"X%"}, + Account.DISPLAY_NAME, 0)); + + // different sort order + assertEquals(account3.mDisplayName, Utility.getFirstRowString( + mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION, + Account.DISPLAY_NAME + " like :1", new String[] {"X%"}, + Account.DISPLAY_NAME + " desc", 0)); + + // case 2. no row found + assertEquals(null, Utility.getFirstRowString( + mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION, + Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"}, + null, 0)); + + // case 3. no row found with default value + assertEquals("-", Utility.getFirstRowString( + mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION, + Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"}, + null, 0, "-")); + } + public void testBuildMailboxIdSelection() { // Create dummy data... Context c = mMockContext; diff --git a/tests/src/com/android/email/provider/ProviderTests.java b/tests/src/com/android/email/provider/ProviderTests.java index 6ec8d9a38..00b53fb2d 100644 --- a/tests/src/com/android/email/provider/ProviderTests.java +++ b/tests/src/com/android/email/provider/ProviderTests.java @@ -2114,6 +2114,18 @@ public class ProviderTests extends ProviderTestCase2 { assertEquals(-1, Mailbox.getMailboxType(c, 999999)); // mailbox not found } + public void testGetDisplayName() { + 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("b2", a.mId, true, c, Mailbox.TYPE_MAIL); + + assertEquals("b1", Mailbox.getDisplayName(c, bi.mId)); + assertEquals("b2", Mailbox.getDisplayName(c, bm.mId)); + assertEquals(null, Mailbox.getDisplayName(c, 999999)); // mailbox not found + } + public void testMailboxIsRefreshable() { final Context c = mMockContext;