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
44    /**
45     * Error codes returned by the searchMessages API
46     */
47    public static class SearchParamsError {
48        public static final int CANT_SEARCH_ALL_MAILBOXES = -1;
49        public static final int CANT_SEARCH_CHILDREN = -2;
50    }
51
52    public SearchParams(long mailboxId, String filter) {
53        mMailboxId = mailboxId;
54        mFilter = filter;
55    }
56
57    @Override
58    public boolean equals(Object o) {
59        if (o == this) {
60            return true;
61        }
62        if ((o == null) || !(o instanceof SearchParams)) {
63            return false;
64        }
65
66        SearchParams os = (SearchParams) o;
67        return mMailboxId == os.mMailboxId
68                && mIncludeChildren == os.mIncludeChildren
69                && mFilter.equals(os.mFilter)
70                && mLimit == os.mLimit
71                && mOffset == os.mOffset;
72    }
73
74    @Override
75    public int hashCode() {
76        return Objects.hashCode(mMailboxId, mFilter, mOffset);
77    }
78
79    @Override
80    public String toString() {
81        return "[SearchParams " + mMailboxId + ":" + mFilter + " (" + mOffset + ", " + mLimit + "]";
82    }
83
84    @Override
85    public int describeContents() {
86        return 0;
87    }
88
89    /**
90     * Supports Parcelable
91     */
92    public static final Parcelable.Creator<SearchParams> CREATOR
93        = new Parcelable.Creator<SearchParams>() {
94        @Override
95        public SearchParams createFromParcel(Parcel in) {
96            return new SearchParams(in);
97        }
98
99        @Override
100        public SearchParams[] newArray(int size) {
101            return new SearchParams[size];
102        }
103    };
104
105    /**
106     * Supports Parcelable
107     */
108    @Override
109    public void writeToParcel(Parcel dest, int flags) {
110        dest.writeLong(mMailboxId);
111        dest.writeInt(mIncludeChildren ? 1 : 0);
112        dest.writeString(mFilter);
113        dest.writeInt(mLimit);
114        dest.writeInt(mOffset);
115    }
116
117    /**
118     * Supports Parcelable
119     */
120    public SearchParams(Parcel in) {
121        mMailboxId = in.readLong();
122        mIncludeChildren = in.readInt() == 1;
123        mFilter = in.readString();
124        mLimit = in.readInt();
125        mOffset = in.readInt();
126    }
127}
128