Fix for "Unknown uri: .../mailbox/-4

It was caused by the fact that we show the combined starred mailbox
on each account's mailbox list.

Beacuse of this, when you open the starred mailbox on non-combined view,
we pass a normal account ID + the combined starred mailbox ID to the
AccountsLoader, which then thinks that because the account ID is not
ACCOUNT_ID_COMBINED_VIEW the mailbox ID must be of a regular mailbox,
issues a query to get mailbox info, and crashes because the mailbox
is virtual.

- Also fixed the issue that we don't show message count on the account
spinner for combined mailboxes.

bug 4971181

Change-Id: Iaa13b362505b8babc7f7ea8a03ddf5494736dc2d
This commit is contained in:
Makoto Onuki 2011-06-29 14:42:56 -07:00
parent 3dbab0e26d
commit 4b39d7ccff
3 changed files with 51 additions and 22 deletions

View File

@ -16,14 +16,16 @@
package com.android.email;
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.provider.Mailbox;
import com.google.common.base.Preconditions;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
import com.android.emailcommon.provider.Mailbox;
// TODO When the UI is settled, cache all strings/drawables
/**
@ -174,6 +176,24 @@ public class FolderProperties {
);
}
public int getMessageCountForCombinedMailbox(long mailboxId) {
Preconditions.checkState(mailboxId < -1L);
if ((mailboxId == Mailbox.QUERY_ALL_INBOXES)
|| (mailboxId == Mailbox.QUERY_ALL_UNREAD)) {
return Mailbox.getUnreadCountByMailboxType(mContext, Mailbox.TYPE_INBOX);
} else if (mailboxId == Mailbox.QUERY_ALL_FAVORITES) {
return Message.getFavoriteMessageCount(mContext);
} else if (mailboxId == Mailbox.QUERY_ALL_DRAFTS) {
return Mailbox.getMessageCountByMailboxType(mContext, Mailbox.TYPE_DRAFTS);
} else if (mailboxId == Mailbox.QUERY_ALL_OUTBOX) {
return Mailbox.getMessageCountByMailboxType(mContext, Mailbox.TYPE_OUTBOX);
}
throw new IllegalStateException("Invalid mailbox ID");
}
/**
* Lookup icons of special mailboxes
*/

View File

@ -17,6 +17,7 @@
package com.android.email.activity;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.android.email.FolderProperties;
import com.android.email.R;
@ -495,11 +496,7 @@ public class AccountSelectorAdapter extends CursorAdapter {
// We need to treat ACCOUNT_ID_COMBINED_VIEW specially...
mAccountExists = true;
mAccountDisplayName = getCombinedViewDisplayName(context);
mMailboxDisplayName = FolderProperties.getInstance(context)
.getCombinedMailboxName(mMailboxId);
// TODO Would be nicer to show message count for combined mailboxes too..
mMailboxMessageCount = 0;
setCombinedMailboxInfo(context, mailboxId);
return;
}
@ -517,6 +514,13 @@ public class AccountSelectorAdapter extends CursorAdapter {
if (mMailboxId == Mailbox.NO_MAILBOX) {
return;
}
// Combined mailbox?
// Unfortunately this can happen even when account != ACCOUNT_ID_COMBINED_VIEW,
// when you open "starred" on 2-pane on non-combined view.
if (mMailboxId < 0) {
setCombinedMailboxInfo(context, mailboxId);
return;
}
// Get mailbox info
final ContentResolver r = context.getContentResolver();
@ -534,6 +538,15 @@ public class AccountSelectorAdapter extends CursorAdapter {
}
}
private void setCombinedMailboxInfo(Context context, long mailboxId) {
Preconditions.checkState(mailboxId < -1, "Not combined mailbox");
mMailboxDisplayName = FolderProperties.getInstance(context)
.getCombinedMailboxName(mMailboxId);
mMailboxMessageCount = FolderProperties.getInstance(context)
.getMessageCountForCombinedMailbox(mailboxId);
}
/**
* Returns the cursor position of the item with the given ID. Or {@link #UNKNOWN_POSITION}
* if the given ID does not exist.

View File

@ -467,11 +467,12 @@ class MailboxFragmentAdapter extends CursorAdapter {
row.add(accountId);
}
private static void addCombinedMailboxRow(MatrixCursor cursor, long id, int mailboxType,
int count, boolean showAlways) {
private static void addCombinedMailboxRow(Context context, MatrixCursor cursor, long id,
int mailboxType, boolean showAlways) {
if (id >= 0) {
throw new IllegalArgumentException(); // Must be QUERY_ALL_*, which are all negative
}
int count = FolderProperties.getInstance(context).getMessageCountForCombinedMailbox(id);
if (showAlways || (count > 0)) {
addMailboxRow(
cursor, id, "", mailboxType, count, count, ROW_TYPE_MAILBOX, Mailbox.FLAG_NONE,
@ -563,9 +564,8 @@ class MailboxFragmentAdapter extends CursorAdapter {
int accountStarredCount = Message.getFavoriteMessageCount(mContext, mAccountId);
if (accountStarredCount > 0) {
// Only add "Starred", if there is at least one starred message
final int totalStarredCount = Message.getFavoriteMessageCount(mContext);
addCombinedMailboxRow(starredCursor, Mailbox.QUERY_ALL_FAVORITES,
Mailbox.TYPE_MAIL, totalStarredCount, true);
addCombinedMailboxRow(mContext, starredCursor, Mailbox.QUERY_ALL_FAVORITES,
Mailbox.TYPE_MAIL, true);
}
returnCursor = new MergeCursor(new Cursor[] {
starredCursor, systemMailboxCursor, recentCursor, headerCursor,
@ -615,23 +615,19 @@ class MailboxFragmentAdapter extends CursorAdapter {
}
@VisibleForTesting
static MatrixCursor buildCombinedMailboxes(Context context, Cursor innerCursor) {
static MatrixCursor buildCombinedMailboxes(Context c, Cursor innerCursor) {
MatrixCursor cursor = new ClosingMatrixCursor(MATRIX_PROJECTION, innerCursor);
// Combined inbox -- show unread count
addCombinedMailboxRow(cursor, Mailbox.QUERY_ALL_INBOXES, Mailbox.TYPE_INBOX,
Mailbox.getUnreadCountByMailboxType(context, Mailbox.TYPE_INBOX), true);
addCombinedMailboxRow(c, cursor, Mailbox.QUERY_ALL_INBOXES, Mailbox.TYPE_INBOX, true);
// Favorite (starred) -- show # of favorites
addCombinedMailboxRow(cursor, Mailbox.QUERY_ALL_FAVORITES, Mailbox.TYPE_MAIL,
Message.getFavoriteMessageCount(context), false);
addCombinedMailboxRow(c, cursor, Mailbox.QUERY_ALL_FAVORITES, Mailbox.TYPE_MAIL, false);
// Drafts -- show # of drafts
addCombinedMailboxRow(cursor, Mailbox.QUERY_ALL_DRAFTS, Mailbox.TYPE_DRAFTS,
Mailbox.getMessageCountByMailboxType(context, Mailbox.TYPE_DRAFTS), false);
addCombinedMailboxRow(c, cursor, Mailbox.QUERY_ALL_DRAFTS, Mailbox.TYPE_DRAFTS, false);
// Outbox -- # of outstanding messages
addCombinedMailboxRow(cursor, Mailbox.QUERY_ALL_OUTBOX, Mailbox.TYPE_OUTBOX,
Mailbox.getMessageCountByMailboxType(context, Mailbox.TYPE_OUTBOX), false);
addCombinedMailboxRow(c, cursor, Mailbox.QUERY_ALL_OUTBOX, Mailbox.TYPE_OUTBOX, false);
return cursor;
}