diff --git a/src/com/android/email/provider/EmailContent.java b/src/com/android/email/provider/EmailContent.java index 3ed19c48b..649de4382 100644 --- a/src/com/android/email/provider/EmailContent.java +++ b/src/com/android/email/provider/EmailContent.java @@ -793,6 +793,15 @@ public abstract class EmailContent { public static int getFavoriteMessageCount(Context context) { return count(context, Message.CONTENT_URI, FAVORITE_COUNT_SELECTION, null); } + + public static long getKeyColumnLong(Context context, long messageId, String column) { + String[] columns = + Utility.getRowColumns(context, Message.CONTENT_URI, messageId, column); + if (columns != null && columns[0] != null) { + return Long.parseLong(columns[0]); + } + return -1; + } } public interface AccountColumns { @@ -1392,6 +1401,21 @@ public abstract class EmailContent { return null; } + /** + * Return the account for a message with a given id + * @param context the caller's context + * @param messageId the id of the message + * @return the account, or null if the account doesn't exist + */ + public static Account getAccountForMessageId(Context context, long messageId) { + long accountId = Message.getKeyColumnLong(context, messageId, + MessageColumns.ACCOUNT_KEY); + if (accountId != -1) { + return Account.restoreAccountWithId(context, accountId); + } + return null; + } + /** * @return true if an {@code accountId} is assigned to any existing account. */ @@ -2228,6 +2252,21 @@ public abstract class EmailContent { new String[] { String.valueOf(type) }, null, MESSAGE_COUNT_COUNT_COLUMN) .intValue(); } + + /** + * Return the mailbox for a message with a given id + * @param context the caller's context + * @param messageId the id of the message + * @return the mailbox, or null if the mailbox doesn't exist + */ + public static Mailbox getMailboxForMessageId(Context context, long messageId) { + long mailboxId = Message.getKeyColumnLong(context, messageId, + MessageColumns.MAILBOX_KEY); + if (mailboxId != -1) { + return Mailbox.restoreMailboxWithId(context, mailboxId); + } + return null; + } } public interface HostAuthColumns { diff --git a/tests/src/com/android/email/provider/ProviderTests.java b/tests/src/com/android/email/provider/ProviderTests.java index 683dac7a0..ea3e4c359 100644 --- a/tests/src/com/android/email/provider/ProviderTests.java +++ b/tests/src/com/android/email/provider/ProviderTests.java @@ -1959,4 +1959,33 @@ public class ProviderTests extends ProviderTestCase2 { account.mHostAuthRecv.mProtocol = "eas"; assertTrue(account.isEasAccount()); } + + public void testGetKeyColumnLong() { + final Context c = mMockContext; + Account a = ProviderTestUtils.setupAccount("acct", true, c); + Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a.mId, true, c, Mailbox.TYPE_MAIL); + Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a.mId, true, c, Mailbox.TYPE_MAIL); + Message m1 = createMessage(c, b1, false, false); + Message m2 = createMessage(c, b2, false, false); + assertEquals(a.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.ACCOUNT_KEY)); + assertEquals(a.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.ACCOUNT_KEY)); + assertEquals(b1.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.MAILBOX_KEY)); + assertEquals(b2.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.MAILBOX_KEY)); + } + + public void testGetAccountMailboxFromMessageId() { + final Context c = mMockContext; + Account a = ProviderTestUtils.setupAccount("acct", true, c); + Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a.mId, true, c, Mailbox.TYPE_MAIL); + Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a.mId, true, c, Mailbox.TYPE_MAIL); + Message m1 = createMessage(c, b1, false, false); + Message m2 = createMessage(c, b2, false, false); + ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m1.mId)); + ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m2.mId)); + // Restore the mailboxes, since the unread & total counts will have changed + b1 = Mailbox.restoreMailboxWithId(c, b1.mId); + b2 = Mailbox.restoreMailboxWithId(c, b2.mId); + ProviderTestUtils.assertMailboxEqual("x", b1, Mailbox.getMailboxForMessageId(c, m1.mId)); + ProviderTestUtils.assertMailboxEqual("x", b2, Mailbox.getMailboxForMessageId(c, m2.mId)); + } }