From 18410ed346e9969054797c9dca2dce48074008c5 Mon Sep 17 00:00:00 2001 From: Ben Komalo Date: Mon, 20 Jun 2011 13:43:02 -0700 Subject: [PATCH] Introduce MessageListContext. This will encapsulate meta information about a message list. Notably the mailbox it's showing OR the search parameters that were used to build its contents. Change-Id: Ibe078a700860e7b9426c865e843e899f82306a96 --- src/com/android/email/MessageListContext.java | 124 ++++++++++++++++++ .../android/email/activity/EmailActivity.java | 8 +- 2 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 src/com/android/email/MessageListContext.java diff --git a/src/com/android/email/MessageListContext.java b/src/com/android/email/MessageListContext.java new file mode 100644 index 000000000..762016551 --- /dev/null +++ b/src/com/android/email/MessageListContext.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.email; + +import android.content.Context; +import android.content.Intent; + +import com.android.email.activity.EmailActivity; +import com.android.emailcommon.provider.Account; +import com.android.emailcommon.provider.Mailbox; +import com.android.emailcommon.service.SearchParams; +import com.google.common.base.Preconditions; + +/** + * Information about what is being shown in a message list. + * This encapsulates the meta-data about the list of messages, which can either be the + * {@link Mailbox} ID, or {@link SearchParams}. + */ +public class MessageListContext { + + /** + * The active account. Changing an account is a destructive enough operation that it warrants + * the creation of a new {@link MessageListContext} + */ + public final long mAccountId; + + /** + * The mailbox ID containing the messages. Must not be {@link Mailbox#NO_MAILBOX}. + */ + private final long mMailboxId; + + /** + * The search parameters, if the user is in a search. + * If non-null, {@link #mMailboxId} will always correspond to the search mailbox for the user. + */ + private final SearchParams mSearchParams; + + // Private constructor - use static builder methods to generate a validated instance. + private MessageListContext(long accountId, long searchMailboxId, SearchParams searchParams) { + mAccountId = accountId; + mMailboxId = searchMailboxId; + mSearchParams = searchParams; + } + + /** + * Builds an instance from the information provided in an Intent. + * This method will perform proper validation and throw an {@link IllegalArgumentException} + * if values in the {@link Intent} are inconsistent. + * This will also handle the generation of default values if certain fields are unspecified + * in the {@link Intent}. + */ + public static MessageListContext forIntent(Context context, Intent intent) { + long accountId = intent.getLongExtra(EmailActivity.EXTRA_ACCOUNT_ID, Account.NO_ACCOUNT); + long mailboxId = intent.getLongExtra(EmailActivity.EXTRA_MAILBOX_ID, Mailbox.NO_MAILBOX); + + if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + final String queryTerm = intent.getStringExtra(EmailActivity.EXTRA_QUERY_STRING); + final long searchMailboxId = + Controller.getInstance(context).getSearchMailbox(accountId).mId; + return forSearch(accountId, searchMailboxId, new SearchParams(mailboxId, queryTerm)); + } else { + if (accountId == Account.NO_ACCOUNT) { + accountId = Account.getDefaultAccountId(context); + } + if (mailboxId == Mailbox.NO_MAILBOX) { + mailboxId = (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) + ? Mailbox.QUERY_ALL_INBOXES + : Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_INBOX); + } + + return forMailbox(accountId, mailboxId); + } + } + + /** + * Creates a view context for a given search. + */ + public static MessageListContext forSearch( + long accountId, long searchMailboxId, SearchParams searchParams) { + Preconditions.checkArgument( + Account.isNormalAccount(accountId), + "Can only search in normal accounts"); + return new MessageListContext(accountId, searchMailboxId, searchParams); + } + + /** + * Creates a view context for a given mailbox. + */ + public static MessageListContext forMailbox(long accountId, long mailboxId) { + Preconditions.checkArgument(accountId != Account.NO_ACCOUNT, "Must specify an account"); + Preconditions.checkArgument(mailboxId != Mailbox.NO_MAILBOX, "Must specify a mailbox"); + return new MessageListContext(accountId, mailboxId, null); + } + + public boolean isSearch() { + return mSearchParams != null; + } + + public long getSearchedMailbox() { + return isSearch() ? mSearchParams.mMailboxId : Mailbox.NO_MAILBOX; + } + + public SearchParams getSearchParams() { + return mSearchParams; + } + + public long getMailboxId() { + return mMailboxId; + } +} diff --git a/src/com/android/email/activity/EmailActivity.java b/src/com/android/email/activity/EmailActivity.java index 12d251603..15114470f 100644 --- a/src/com/android/email/activity/EmailActivity.java +++ b/src/com/android/email/activity/EmailActivity.java @@ -57,10 +57,10 @@ import java.util.ArrayList; * the UIController. */ public class EmailActivity extends Activity implements View.OnClickListener, FragmentInstallable { - private static final String EXTRA_ACCOUNT_ID = "ACCOUNT_ID"; - private static final String EXTRA_MAILBOX_ID = "MAILBOX_ID"; - private static final String EXTRA_MESSAGE_ID = "MESSAGE_ID"; - private static final String EXTRA_QUERY_STRING = "QUERY_STRING"; + public static final String EXTRA_ACCOUNT_ID = "ACCOUNT_ID"; + public static final String EXTRA_MAILBOX_ID = "MAILBOX_ID"; + public static final String EXTRA_MESSAGE_ID = "MESSAGE_ID"; + public static final String EXTRA_QUERY_STRING = "QUERY_STRING"; /** Loader IDs starting with this is safe to use from UIControllers. */ static final int UI_CONTROLLER_LOADER_ID_BASE = 100;