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
This commit is contained in:
parent
b41602ed94
commit
18410ed346
124
src/com/android/email/MessageListContext.java
Normal file
124
src/com/android/email/MessageListContext.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user