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                 if (accountId == Account.NO_ACCOUNT) {
     82                     return null;
     83                 }
     84             }
     85             if (mailboxId == Mailbox.NO_MAILBOX) {
     86                 mailboxId = (accountId == Account.ACCOUNT_ID_COMBINED_VIEW)
     87                         ? Mailbox.QUERY_ALL_INBOXES
     88                         : Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_INBOX);
     89             }
     90 
     91             return forMailbox(accountId, mailboxId);
     92         }
     93     }
     94 
     95     /**
     96      * Creates a view context for a given search.
     97      */
     98     public static MessageListContext forSearch(
     99             long accountId, long searchMailboxId, SearchParams searchParams) {
    100         Preconditions.checkArgument(
    101                 Account.isNormalAccount(accountId),
    102                 "Can only search in normal accounts");
    103         return new MessageListContext(accountId, searchMailboxId, searchParams);
    104     }
    105 
    106     /**
    107      * Creates a view context for a given mailbox.
    108      */
    109     public static MessageListContext forMailbox(long accountId, long mailboxId) {
    110         Preconditions.checkArgument(accountId != Account.NO_ACCOUNT, "Must specify an account");
    111         Preconditions.checkArgument(mailboxId != Mailbox.NO_MAILBOX, "Must specify a mailbox");
    112         return new MessageListContext(accountId, mailboxId, null);
    113     }
    114 
    115     public boolean isSearch() {
    116         return mSearchParams != null;
    117     }
    118 
    119     public long getSearchedMailbox() {
    120         return isSearch() ? mSearchParams.mMailboxId : Mailbox.NO_MAILBOX;
    121     }
    122 
    123     public SearchParams getSearchParams() {
    124         return mSearchParams;
    125     }
    126 
    127     public long getMailboxId() {
    128         return mMailboxId;
    129     }
    130 
    131     @Override
    132     public boolean equals(Object o) {
    133         if (o == this) {
    134             return true;
    135         }
    136         if ((o == null) || !(o instanceof MessageListContext)) {
    137             return false;
    138         }
    139 
    140         MessageListContext om = (MessageListContext) o;
    141         return mAccountId == om.mAccountId
    142                 && mMailboxId == om.mMailboxId
    143                 && Objects.equal(mSearchParams, om.mSearchParams);
    144     }
    145 
    146     @Override
    147     public int hashCode() {
    148         return Objects.hashCode(mAccountId, mMailboxId, mSearchParams);
    149     }
    150 
    151     @Override
    152     public String toString() {
    153         return "[MessageListContext " + mAccountId + ":" + mMailboxId + ":" + mSearchParams + "]";
    154     }
    155 
    156 
    157     private MessageListContext(Parcel in) {
    158         mAccountId = in.readLong();
    159         mMailboxId = in.readLong();
    160         mSearchParams = in.readParcelable(SearchParams.class.getClassLoader());
    161     }
    162 
    163     @Override
    164     public int describeContents() {
    165         return 0;
    166     }
    167 
    168     @Override
    169     public void writeToParcel(Parcel dest, int flags) {
    170         dest.writeLong(mAccountId);
    171         dest.writeLong(mMailboxId);
    172         dest.writeParcelable(mSearchParams, flags);
    173     }
    174 
    175     public static Parcelable.Creator<MessageListContext> CREATOR =
    176                 new Parcelable.Creator<MessageListContext>() {
    177         @Override
    178         public MessageListContext createFromParcel(Parcel source) {
    179             return new MessageListContext(source);
    180         }
    181 
    182         @Override
    183         public MessageListContext[] newArray(int size) {
    184             return new MessageListContext[size];
    185         }
    186     };
    187 }
    188