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
17package com.android.emailcommon.service;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import com.android.emailcommon.provider.Mailbox;
23import com.google.common.base.Objects;
24
25public class SearchParams implements Parcelable {
26    public static final long ALL_MAILBOXES = Mailbox.NO_MAILBOX;
27
28    private static final int DEFAULT_LIMIT = 10; // Need input on what this number should be
29    private static final int DEFAULT_OFFSET = 0;
30
31    // The id of the mailbox to be searched; if -1, all mailboxes MUST be searched
32    public final long mMailboxId;
33    // If true, all subfolders of the specified mailbox MUST be searched
34    public boolean mIncludeChildren = true;
35    // The search terms (the search MUST only select messages whose contents include all of the
36    // search terms in the query)
37    public final String mFilter;
38    // The maximum number of results to be created by this search
39    public int mLimit = DEFAULT_LIMIT;
40    // If zero, specifies a "new" search; otherwise, asks for a continuation of the previous
41    // query(ies) starting with the mOffset'th match (0 based)
42    public int mOffset = DEFAULT_OFFSET;
43    // The total number of results for this search
44    public int mTotalCount = 0;
45    // The id of the "search" mailbox being used
46    public long mSearchMailboxId;
47
48    /**
49     * Error codes returned by the searchMessages API
50     */
51    public static class SearchParamsError {
52        public static final int CANT_SEARCH_ALL_MAILBOXES = -1;
53        public static final int CANT_SEARCH_CHILDREN = -2;
54    }
55
56    public SearchParams(long mailboxId, String filter) {
57        mMailboxId = mailboxId;
58        mFilter = filter;
59    }
60
61    public SearchParams(long mailboxId, String filter, long searchMailboxId) {
62        mMailboxId = mailboxId;
63        mFilter = filter;
64        mSearchMailboxId = searchMailboxId;
65    }
66
67    @Override
68    public boolean equals(Object o) {
69        if (o == this) {
70            return true;
71        }
72        if ((o == null) || !(o instanceof SearchParams)) {
73            return false;
74        }
75
76        SearchParams os = (SearchParams) o;
77        return mMailboxId == os.mMailboxId
78                && mIncludeChildren == os.mIncludeChildren
79                && mFilter.equals(os.mFilter)
80                && mLimit == os.mLimit
81                && mOffset == os.mOffset;
82    }
83
84    @Override
85    public int hashCode() {
86        return Objects.hashCode(mMailboxId, mFilter, mOffset);
87    }
88
89    @Override
90    public String toString() {
91        return "[SearchParams " + mMailboxId + ":" + mFilter + " (" + mOffset + ", " + mLimit + "]";
92    }
93
94    @Override
95    public int describeContents() {
96        return 0;
97    }
98
99    /**
100     * Supports Parcelable
101     */
102    public static final Parcelable.Creator<SearchParams> CREATOR
103        = new Parcelable.Creator<SearchParams>() {
104        @Override
105        public SearchParams createFromParcel(Parcel in) {
106            return new SearchParams(in);
107        }
108
109        @Override
110        public SearchParams[] newArray(int size) {
111            return new SearchParams[size];
112        }
113    };
114
115    /**
116     * Supports Parcelable
117     */
118    @Override
119    public void writeToParcel(Parcel dest, int flags) {
120        dest.writeLong(mMailboxId);
121        dest.writeInt(mIncludeChildren ? 1 : 0);
122        dest.writeString(mFilter);
123        dest.writeInt(mLimit);
124        dest.writeInt(mOffset);
125    }
126
127    /**
128     * Supports Parcelable
129     */
130    public SearchParams(Parcel in) {
131        mMailboxId = in.readLong();
132        mIncludeChildren = in.readInt() == 1;
133        mFilter = in.readString();
134        mLimit = in.readInt();
135        mOffset = in.readInt();
136    }
137}
138