blob: 8ed5faed6ed9dc3c035a0d0ca4e01ae93b42a6b6 [file] [log] [blame]
Andrew Sapperstein345c43e2013-05-31 16:23:18 -07001/*
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
17package com.android.emailcommon.service;
18
19import android.os.Parcel;
20import android.os.Parcelable;
Tony Mantler874c5662013-10-03 11:47:55 -070021import android.util.SparseArray;
Andrew Sapperstein345c43e2013-05-31 16:23:18 -070022
23import com.google.common.base.Objects;
24
Tony Mantler874c5662013-10-03 11:47:55 -070025import java.util.Date;
26
Andrew Sapperstein345c43e2013-05-31 16:23:18 -070027public 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 Mantler874c5662013-10-03 11:47:55 -070039 // 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 Sapperstein345c43e2013-05-31 16:23:18 -070043 // 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 Sapperstein345c43e2013-05-31 16:23:18 -070048 // The id of the "search" mailbox being used
49 public long mSearchMailboxId;
50
weitanad4f7672016-06-02 15:38:29 +080051 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 Sapperstein345c43e2013-05-31 16:23:18 -070060 /**
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
weitanad4f7672016-06-02 15:38:29 +080068 public SearchParams(long mailboxId, String filter, String factor) {
Andrew Sapperstein345c43e2013-05-31 16:23:18 -070069 mMailboxId = mailboxId;
70 mFilter = filter;
Tony Mantler874c5662013-10-03 11:47:55 -070071 mStartDate = null;
72 mEndDate = null;
weitanad4f7672016-06-02 15:38:29 +080073 mFactor = factor;
Andrew Sapperstein345c43e2013-05-31 16:23:18 -070074 }
75
76 public SearchParams(long mailboxId, String filter, long searchMailboxId) {
77 mMailboxId = mailboxId;
78 mFilter = filter;
Tony Mantler874c5662013-10-03 11:47:55 -070079 mStartDate = null;
80 mEndDate = null;
Andrew Sapperstein345c43e2013-05-31 16:23:18 -070081 mSearchMailboxId = searchMailboxId;
82 }
83
weitanad4f7672016-06-02 15:38:29 +080084 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 Mantler874c5662013-10-03 11:47:55 -070093 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 Sapperstein345c43e2013-05-31 16:23:18 -0700102 @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 Mantler874c5662013-10-03 11:47:55 -0700115 && Objects.equal(mStartDate, os.mStartDate)
116 && Objects.equal(mEndDate, os.mEndDate)
Andrew Sapperstein345c43e2013-05-31 16:23:18 -0700117 && mLimit == os.mLimit
weitanad4f7672016-06-02 15:38:29 +0800118 && mOffset == os.mOffset
119
120 && mFactor.equals(os.mFactor);
121
Andrew Sapperstein345c43e2013-05-31 16:23:18 -0700122 }
123
124 @Override
125 public int hashCode() {
weitanad4f7672016-06-02 15:38:29 +0800126 return Objects.hashCode(mMailboxId, mFilter, mStartDate, mEndDate, mLimit, mOffset,
127 mFactor);
Andrew Sapperstein345c43e2013-05-31 16:23:18 -0700128 }
129
weitanad4f7672016-06-02 15:38:29 +0800130
Andrew Sapperstein345c43e2013-05-31 16:23:18 -0700131 @Override
132 public String toString() {
weitanad4f7672016-06-02 15:38:29 +0800133 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 Sapperstein345c43e2013-05-31 16:23:18 -0700138 }
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);
weitanad4f7672016-06-02 15:38:29 +0800171 dest.writeString(mFactor);
Tony Mantler874c5662013-10-03 11:47:55 -0700172 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 Sapperstein345c43e2013-05-31 16:23:18 -0700180 }
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();
weitanad4f7672016-06-02 15:38:29 +0800191 mFactor = in.readString();
Tony Mantler874c5662013-10-03 11:47:55 -0700192 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 Sapperstein345c43e2013-05-31 16:23:18 -0700203 }
204}