Home | History | Annotate | Download | only in service
      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.emailcommon.service;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 import android.util.SparseArray;
     22 
     23 import com.google.common.base.Objects;
     24 
     25 import java.util.Date;
     26 
     27 public class SearchParams implements Parcelable {
     28 
     29     private static final int DEFAULT_LIMIT = 10; // Need input on what this number should be
     30     private static final int DEFAULT_OFFSET = 0;
     31 
     32     // The id of the mailbox to be searched; if -1, all mailboxes MUST be searched
     33     public final long mMailboxId;
     34     // If true, all subfolders of the specified mailbox MUST be searched
     35     public boolean mIncludeChildren = true;
     36     // The search terms (the search MUST only select messages whose contents include all of the
     37     // search terms in the query)
     38     public final String mFilter;
     39     // The start date (GreaterThan) for date-windowing results
     40     public final Date mStartDate;
     41     // The end date (LessThan) for date-windowing results
     42     public final Date mEndDate;
     43     // The maximum number of results to be created by this search
     44     public int mLimit = DEFAULT_LIMIT;
     45     // If zero, specifies a "new" search; otherwise, asks for a continuation of the previous
     46     // query(ies) starting with the mOffset'th match (0 based)
     47     public int mOffset = DEFAULT_OFFSET;
     48     // The id of the "search" mailbox being used
     49     public long mSearchMailboxId;
     50 
     51     /**
     52      * Error codes returned by the searchMessages API
     53      */
     54     public static class SearchParamsError {
     55         public static final int CANT_SEARCH_ALL_MAILBOXES = -1;
     56         public static final int CANT_SEARCH_CHILDREN = -2;
     57     }
     58 
     59     public SearchParams(long mailboxId, String filter) {
     60         mMailboxId = mailboxId;
     61         mFilter = filter;
     62         mStartDate = null;
     63         mEndDate = null;
     64     }
     65 
     66     public SearchParams(long mailboxId, String filter, long searchMailboxId) {
     67         mMailboxId = mailboxId;
     68         mFilter = filter;
     69         mStartDate = null;
     70         mEndDate = null;
     71         mSearchMailboxId = searchMailboxId;
     72     }
     73 
     74     public SearchParams(long mailboxId, String filter, long searchMailboxId, Date startDate,
     75             Date endDate) {
     76         mMailboxId = mailboxId;
     77         mFilter = filter;
     78         mSearchMailboxId = searchMailboxId;
     79         mStartDate = startDate;
     80         mEndDate = endDate;
     81     }
     82 
     83     @Override
     84     public boolean equals(Object o) {
     85         if (o == this) {
     86             return true;
     87         }
     88         if ((o == null) || !(o instanceof SearchParams)) {
     89             return false;
     90         }
     91 
     92         SearchParams os = (SearchParams) o;
     93         return mMailboxId == os.mMailboxId
     94                 && mIncludeChildren == os.mIncludeChildren
     95                 && mFilter.equals(os.mFilter)
     96                 && Objects.equal(mStartDate, os.mStartDate)
     97                 && Objects.equal(mEndDate, os.mEndDate)
     98                 && mLimit == os.mLimit
     99                 && mOffset == os.mOffset;
    100     }
    101 
    102     @Override
    103     public int hashCode() {
    104         return Objects.hashCode(mMailboxId, mFilter, mStartDate, mEndDate, mLimit, mOffset);
    105     }
    106 
    107     @Override
    108     public String toString() {
    109         return "[SearchParams "
    110                 + mMailboxId + ":" + mFilter
    111                 + " (" + mOffset + ", " + mLimit + ")"
    112                 + " {" + mStartDate + ", " + mEndDate + "}"
    113                 + "]";
    114     }
    115 
    116     @Override
    117     public int describeContents() {
    118         return 0;
    119     }
    120 
    121     /**
    122      * Supports Parcelable
    123      */
    124     public static final Parcelable.Creator<SearchParams> CREATOR
    125         = new Parcelable.Creator<SearchParams>() {
    126         @Override
    127         public SearchParams createFromParcel(Parcel in) {
    128             return new SearchParams(in);
    129         }
    130 
    131         @Override
    132         public SearchParams[] newArray(int size) {
    133             return new SearchParams[size];
    134         }
    135     };
    136 
    137     /**
    138      * Supports Parcelable
    139      */
    140     @Override
    141     public void writeToParcel(Parcel dest, int flags) {
    142         dest.writeLong(mMailboxId);
    143         dest.writeInt(mIncludeChildren ? 1 : 0);
    144         dest.writeString(mFilter);
    145         dest.writeInt(mLimit);
    146         dest.writeInt(mOffset);
    147         SparseArray<Object> dateWindow = new SparseArray<Object>(2);
    148         if (mStartDate != null) {
    149             dateWindow.put(0, mStartDate.getTime());
    150         }
    151         if (mEndDate != null) {
    152             dateWindow.put(1, mEndDate.getTime());
    153         }
    154         dest.writeSparseArray(dateWindow);
    155     }
    156 
    157     /**
    158      * Supports Parcelable
    159      */
    160     public SearchParams(Parcel in) {
    161         mMailboxId = in.readLong();
    162         mIncludeChildren = in.readInt() == 1;
    163         mFilter = in.readString();
    164         mLimit = in.readInt();
    165         mOffset = in.readInt();
    166         SparseArray dateWindow = in.readSparseArray(this.getClass().getClassLoader());
    167         if (dateWindow.get(0) != null) {
    168             mStartDate = new Date((Long)dateWindow.get(0));
    169         } else {
    170             mStartDate = null;
    171         }
    172         if (dateWindow.get(1) != null) {
    173             mEndDate = new Date((Long)dateWindow.get(1));
    174         } else {
    175             mEndDate = null;
    176         }
    177     }
    178 }
    179