Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 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; |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 21 | import android.util.SparseArray; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 22 | |
| 23 | import com.google.common.base.Objects; |
| 24 | |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 25 | import java.util.Date; |
| 26 | |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 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; |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 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; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 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; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 48 | // The id of the "search" mailbox being used |
| 49 | public long mSearchMailboxId; |
| 50 | |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 51 | public static final String SEARCH_FACTOR_ALL = "ALL"; |
| 52 | public static final String SEARCH_FACTOR_SUBJECT = "SUBJECT"; |
| 53 | public static final String SEARCH_FACTOR_SENDER = "SENDER"; |
| 54 | public static final String SEARCH_FACTOR_RECEIVER = "RECEIVER"; |
| 55 | public static final String BUNDLE_QUERY_FACTOR = "queryFactor"; |
| 56 | public static final String BUNDLE_QUERY_FILTER = "queryFilter"; |
| 57 | |
| 58 | public String mFactor = SEARCH_FACTOR_ALL; |
| 59 | |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Error codes returned by the searchMessages API |
| 62 | */ |
| 63 | public static class SearchParamsError { |
| 64 | public static final int CANT_SEARCH_ALL_MAILBOXES = -1; |
| 65 | public static final int CANT_SEARCH_CHILDREN = -2; |
| 66 | } |
| 67 | |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 68 | public SearchParams(long mailboxId, String filter, String factor) { |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 69 | mMailboxId = mailboxId; |
| 70 | mFilter = filter; |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 71 | mStartDate = null; |
| 72 | mEndDate = null; |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 73 | mFactor = factor; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public SearchParams(long mailboxId, String filter, long searchMailboxId) { |
| 77 | mMailboxId = mailboxId; |
| 78 | mFilter = filter; |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 79 | mStartDate = null; |
| 80 | mEndDate = null; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 81 | mSearchMailboxId = searchMailboxId; |
| 82 | } |
| 83 | |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 84 | public SearchParams(long mailboxId, String filter, String factor, long searchMailboxId) { |
| 85 | mMailboxId = mailboxId; |
| 86 | mFilter = filter; |
| 87 | mFactor = factor; |
| 88 | mStartDate = null; |
| 89 | mEndDate = null; |
| 90 | mSearchMailboxId = searchMailboxId; |
| 91 | } |
| 92 | |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 93 | public SearchParams(long mailboxId, String filter, long searchMailboxId, Date startDate, |
| 94 | Date endDate) { |
| 95 | mMailboxId = mailboxId; |
| 96 | mFilter = filter; |
| 97 | mSearchMailboxId = searchMailboxId; |
| 98 | mStartDate = startDate; |
| 99 | mEndDate = endDate; |
| 100 | } |
| 101 | |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public boolean equals(Object o) { |
| 104 | if (o == this) { |
| 105 | return true; |
| 106 | } |
| 107 | if ((o == null) || !(o instanceof SearchParams)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | SearchParams os = (SearchParams) o; |
| 112 | return mMailboxId == os.mMailboxId |
| 113 | && mIncludeChildren == os.mIncludeChildren |
| 114 | && mFilter.equals(os.mFilter) |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 115 | && Objects.equal(mStartDate, os.mStartDate) |
| 116 | && Objects.equal(mEndDate, os.mEndDate) |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 117 | && mLimit == os.mLimit |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 118 | && mOffset == os.mOffset |
| 119 | |
| 120 | && mFactor.equals(os.mFactor); |
| 121 | |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int hashCode() { |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 126 | return Objects.hashCode(mMailboxId, mFilter, mStartDate, mEndDate, mLimit, mOffset, |
| 127 | mFactor); |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 128 | } |
| 129 | |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 130 | |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public String toString() { |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 133 | return "SearchParams [mMailboxId=" + mMailboxId + ", mIncludeChildren=" |
| 134 | + mIncludeChildren + ", mFilter=" + mFilter + ", mStartDate=" |
| 135 | + mStartDate + ", mEndDate=" + mEndDate + ", mLimit=" + mLimit |
| 136 | + ", mOffset=" + mOffset + ", mSearchMailboxId=" |
| 137 | + mSearchMailboxId + ", mFactor=" + mFactor + "]"; |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int describeContents() { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Supports Parcelable |
| 147 | */ |
| 148 | public static final Parcelable.Creator<SearchParams> CREATOR |
| 149 | = new Parcelable.Creator<SearchParams>() { |
| 150 | @Override |
| 151 | public SearchParams createFromParcel(Parcel in) { |
| 152 | return new SearchParams(in); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public SearchParams[] newArray(int size) { |
| 157 | return new SearchParams[size]; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Supports Parcelable |
| 163 | */ |
| 164 | @Override |
| 165 | public void writeToParcel(Parcel dest, int flags) { |
| 166 | dest.writeLong(mMailboxId); |
| 167 | dest.writeInt(mIncludeChildren ? 1 : 0); |
| 168 | dest.writeString(mFilter); |
| 169 | dest.writeInt(mLimit); |
| 170 | dest.writeInt(mOffset); |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 171 | dest.writeString(mFactor); |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 172 | SparseArray<Object> dateWindow = new SparseArray<Object>(2); |
| 173 | if (mStartDate != null) { |
| 174 | dateWindow.put(0, mStartDate.getTime()); |
| 175 | } |
| 176 | if (mEndDate != null) { |
| 177 | dateWindow.put(1, mEndDate.getTime()); |
| 178 | } |
| 179 | dest.writeSparseArray(dateWindow); |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Supports Parcelable |
| 184 | */ |
| 185 | public SearchParams(Parcel in) { |
| 186 | mMailboxId = in.readLong(); |
| 187 | mIncludeChildren = in.readInt() == 1; |
| 188 | mFilter = in.readString(); |
| 189 | mLimit = in.readInt(); |
| 190 | mOffset = in.readInt(); |
weitan | ad4f767 | 2016-06-02 15:38:29 +0800 | [diff] [blame] | 191 | mFactor = in.readString(); |
Tony Mantler | 874c566 | 2013-10-03 11:47:55 -0700 | [diff] [blame] | 192 | SparseArray dateWindow = in.readSparseArray(this.getClass().getClassLoader()); |
| 193 | if (dateWindow.get(0) != null) { |
| 194 | mStartDate = new Date((Long)dateWindow.get(0)); |
| 195 | } else { |
| 196 | mStartDate = null; |
| 197 | } |
| 198 | if (dateWindow.get(1) != null) { |
| 199 | mEndDate = new Date((Long)dateWindow.get(1)); |
| 200 | } else { |
| 201 | mEndDate = null; |
| 202 | } |
Andrew Sapperstein | 345c43e | 2013-05-31 16:23:18 -0700 | [diff] [blame] | 203 | } |
| 204 | } |