Home | History | Annotate | Download | only in email
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.email;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.Parcel;
     22 import android.os.Parcelable;
     23 
     24 import com.android.email.activity.EmailActivity;
     25 import com.android.emailcommon.provider.Account;
     26 import com.android.emailcommon.provider.Mailbox;
     27 import com.android.emailcommon.service.SearchParams;
     28 import com.google.common.base.Objects;
     29 import com.google.common.base.Preconditions;
     30 
     31 /**
     32  * Information about what is being shown in a message list.
     33  * This encapsulates the meta-data about the list of messages, which can either be the
     34  * {@link Mailbox} ID, or {@link SearchParams}.
     35  */
     36 public class MessageListContext implements Parcelable {
     37 
     38     /**
     39      * The active account. Changing an account is a destructive enough operation that it warrants
     40      * the creation of a new {@link MessageListContext}
     41      */
     42     public final long mAccountId;
     43 
     44     /**
     45      * The mailbox ID containing the messages. Must not be {@link Mailbox#NO_MAILBOX}.
     46      */
     47     private final long mMailboxId;
     48 
     49     /**
     50      * The search parameters, if the user is in a search.
     51      * If non-null, {@link #mMailboxId} will always correspond to the search mailbox for the user.
     52      */
     53     private final SearchParams mSearchParams;
     54 
     55     // Private constructor - use static builder methods to generate a validated instance.
     56     private MessageListContext(long accountId, long searchMailboxId, SearchParams searchParams) {
     57         mAccountId = accountId;
     58         mMailboxId = searchMailboxId;
     59         mSearchParams = searchParams;
     60     }
     61 
     62     /**
     63      * Builds an instance from the information provided in an Intent.
     64      * This method will perform proper validation and throw an {@link IllegalArgumentException}
     65      * if values in the {@link Intent} are inconsistent.
     66      * This will also handle the generation of default values if certain fields are unspecified
     67      * in the {@link Intent}.
     68      */
     69     public static MessageListContext forIntent(Context context, Intent intent) {
     70         long accountId = intent.getLongExtra(EmailActivity.EXTRA_ACCOUNT_ID, Account.NO_ACCOUNT);
     71         long mailboxId = intent.getLongExtra(EmailActivity.EXTRA_MAILBOX_ID, Mailbox.NO_MAILBOX);
     72 
     73         if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
     74             final String queryTerm = intent.getStringExtra(EmailActivity.EXTRA_QUERY_STRING);
     75             final long searchMailboxId =
     76                     Controller.getInstance(context).getSearchMailbox(accountId).mId;
     77             return forSearch(accountId, searchMailboxId, new SearchParams(mailboxId, queryTerm));
     78         } else {
     79             if (accountId == Account.NO_ACCOUNT) {
     80                 accountId = Account.getDefaultAccountId(context);
     81             }
     82             if (mailboxId == Mailbox.NO_MAILBOX) {
     83                 mailboxId = (accountId == Account.ACCOUNT_ID_COMBINED_VIEW)
     84                         ? Mailbox.QUERY_ALL_INBOXES
     85                         : Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_INBOX);
     86             }
     87 
     88             return forMailbox(accountId, mailboxId);
     89         }
     90     }
     91 
     92     /**
     93      * Creates a view context for a given search.
     94      */
     95     public static MessageListContext forSearch(
     96             long accountId, long searchMailboxId, SearchParams searchParams) {
     97         Preconditions.checkArgument(
     98                 Account.isNormalAccount(accountId),
     99                 "Can only search in normal accounts");
    100         return new MessageListContext(accountId, searchMailboxId, searchParams);
    101     }
    102 
    103     /**
    104      * Creates a view context for a given mailbox.
    105      */
    106     public static MessageListContext forMailbox(long accountId, long mailboxId) {
    107         Preconditions.checkArgument(accountId != Account.NO_ACCOUNT, "Must specify an account");
    108         Preconditions.checkArgument(mailboxId != Mailbox.NO_MAILBOX, "Must specify a mailbox");
    109         return new MessageListContext(accountId, mailboxId, null);
    110     }
    111 
    112     public boolean isSearch() {
    113         return mSearchParams != null;
    114     }
    115 
    116     public long getSearchedMailbox() {
    117         return isSearch() ? mSearchParams.mMailboxId : Mailbox.NO_MAILBOX;
    118     }
    119 
    120     public SearchParams getSearchParams() {
    121         return mSearchParams;
    122     }
    123 
    124     public long getMailboxId() {
    125         return mMailboxId;
    126     }
    127 
    128     @Override
    129     public boolean equals(Object o) {
    130         if (o == this) {
    131             return true;
    132         }
    133         if ((o == null) || !(o instanceof MessageListContext)) {
    134             return false;
    135         }
    136 
    137         MessageListContext om = (MessageListContext) o;
    138         return mAccountId == om.mAccountId
    139                 && mMailboxId == om.mMailboxId
    140                 && Objects.equal(mSearchParams, om.mSearchParams);
    141     }
    142 
    143     @Override
    144     public int hashCode() {
    145         return Objects.hashCode(mAccountId, mMailboxId, mSearchParams);
    146     }
    147 
    148     @Override
    149     public String toString() {
    150         return "[MessageListContext " + mAccountId + ":" + mMailboxId + ":" + mSearchParams + "]";
    151     }
    152 
    153 
    154     private MessageListContext(Parcel in) {
    155         mAccountId = in.readLong();
    156         mMailboxId = in.readLong();
    157         mSearchParams = in.readParcelable(SearchParams.class.getClassLoader());
    158     }
    159 
    160     @Override
    161     public int describeContents() {
    162         return 0;
    163     }
    164 
    165     @Override
    166     public void writeToParcel(Parcel dest, int flags) {
    167         dest.writeLong(mAccountId);
    168         dest.writeLong(mMailboxId);
    169         dest.writeParcelable(mSearchParams, flags);
    170     }
    171 
    172     public static Parcelable.Creator<MessageListContext> CREATOR =
    173                 new Parcelable.Creator<MessageListContext>() {
    174         @Override
    175         public MessageListContext createFromParcel(Parcel source) {
    176             return new MessageListContext(source);
    177         }
    178 
    179         @Override
    180         public MessageListContext[] newArray(int size) {
    181             return new MessageListContext[size];
    182         }
    183     };
    184 }
    185