SuggestionData.java revision 965d98377ddfdc52b772c2444d840000b665e000
1/*
2 * Copyright (C) 2009 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.quicksearchbox;
18
19/**
20 * Holds data for each suggest item including the display data and how to launch the result.
21 * Used for passing from the provider to the suggest cursor.
22 *
23 */
24public class SuggestionData {
25
26    private final Source mSource;
27    private String mFormat;
28    private String mText1;
29    private String mText2;
30    private String mText2Url;
31    private String mIcon1;
32    private String mIcon2;
33    private String mShortcutId;
34    private boolean mSpinnerWhileRefreshing;
35    private String mIntentAction;
36    private String mIntentData;
37    private String mIntentExtraData;
38    private String mSuggestionQuery;
39
40    public SuggestionData(Source source) {
41        mSource = source;
42    }
43
44    public Source getSuggestionSource() {
45        return mSource;
46    }
47
48    public String getSuggestionFormat() {
49        return mFormat;
50    }
51
52    public String getSuggestionText1() {
53        return mText1;
54    }
55
56    public String getSuggestionText2() {
57        return mText2;
58    }
59
60    public String getSuggestionText2Url() {
61        return mText2Url;
62    }
63
64    public String getSuggestionIcon1() {
65        return mIcon1;
66    }
67
68    public String getSuggestionIcon2() {
69        return mIcon2;
70    }
71
72    public boolean isSpinnerWhileRefreshing() {
73        return mSpinnerWhileRefreshing;
74    }
75
76    public String getIntentExtraData() {
77        return mIntentExtraData;
78    }
79
80    public String getShortcutId() {
81        return mShortcutId;
82    }
83
84    public String getSuggestionIntentAction() {
85        return mIntentAction;
86    }
87
88    public String getSuggestionIntentDataString() {
89        return mIntentData;
90    }
91
92    public String getSuggestionIntentExtraData() {
93        return mIntentExtraData;
94    }
95
96    public String getSuggestionQuery() {
97        return mSuggestionQuery;
98    }
99
100    public SuggestionData setFormat(String format) {
101        mFormat = format;
102        return this;
103    }
104
105    public SuggestionData setText1(String text1) {
106        mText1 = text1;
107        return this;
108    }
109
110    public SuggestionData setText2(String text2) {
111        mText2 = text2;
112        return this;
113    }
114
115    public SuggestionData setText2Url(String text2Url) {
116        mText2Url = text2Url;
117        return this;
118    }
119
120    public SuggestionData setIcon1(String icon1) {
121        mIcon1 = icon1;
122        return this;
123    }
124
125    public SuggestionData setIcon2(String icon2) {
126        mIcon2 = icon2;
127        return this;
128    }
129
130    public SuggestionData setIntentAction(String intentAction) {
131        mIntentAction = intentAction;
132        return this;
133    }
134
135    public SuggestionData setIntentData(String intentData) {
136        mIntentData = intentData;
137        return this;
138    }
139
140    public SuggestionData setIntentExtraData(String intentExtraData) {
141        mIntentExtraData = intentExtraData;
142        return this;
143    }
144
145    public SuggestionData setSuggestionQuery(String suggestionQuery) {
146        mSuggestionQuery = suggestionQuery;
147        return this;
148    }
149
150    public SuggestionData setShortcutId(String shortcutId) {
151        mShortcutId = shortcutId;
152        return this;
153    }
154
155    public SuggestionData setSpinnerWhileRefreshing(boolean spinnerWhileRefreshing) {
156        mSpinnerWhileRefreshing = spinnerWhileRefreshing;
157        return this;
158    }
159
160    private String makeKeyComponent(String str) {
161        return str == null ? "" : str;
162    }
163
164    public String getSuggestionKey() {
165        String action = makeKeyComponent(mIntentAction);
166        String data = makeKeyComponent(mIntentData);
167        String query = makeKeyComponent(mSuggestionQuery);
168        // calculating accurate size of string builder avoids an allocation vs starting with
169        // the default size and having to expand.
170        int size = action.length() + 2 + data.length() + query.length();
171        return new StringBuilder(size)
172                .append(action)
173                .append('#')
174                .append(data)
175                .append('#')
176                .append(query)
177                .toString();
178    }
179
180    public String getSuggestionLogType() {
181        return getSuggestionSource().getLogName();
182    }
183
184    @Override
185    public int hashCode() {
186        final int prime = 31;
187        int result = 1;
188        result = prime * result + ((mFormat == null) ? 0 : mFormat.hashCode());
189        result = prime * result + ((mIcon1 == null) ? 0 : mIcon1.hashCode());
190        result = prime * result + ((mIcon2 == null) ? 0 : mIcon2.hashCode());
191        result = prime * result + ((mIntentAction == null) ? 0 : mIntentAction.hashCode());
192        result = prime * result + ((mIntentData == null) ? 0 : mIntentData.hashCode());
193        result = prime * result + ((mIntentExtraData == null) ? 0 : mIntentExtraData.hashCode());
194        result = prime * result + ((mShortcutId == null) ? 0 : mShortcutId.hashCode());
195        result = prime * result + ((mSource == null) ? 0 : mSource.hashCode());
196        result = prime * result + (mSpinnerWhileRefreshing ? 1231 : 1237);
197        result = prime * result + ((mSuggestionQuery == null) ? 0 : mSuggestionQuery.hashCode());
198        result = prime * result + ((mText1 == null) ? 0 : mText1.hashCode());
199        result = prime * result + ((mText2 == null) ? 0 : mText2.hashCode());
200        return result;
201    }
202
203    @Override
204    public boolean equals(Object obj) {
205        if (this == obj)
206            return true;
207        if (obj == null)
208            return false;
209        if (getClass() != obj.getClass())
210            return false;
211        SuggestionData other = (SuggestionData)obj;
212        if (mFormat == null) {
213            if (other.mFormat != null)
214                return false;
215        } else if (!mFormat.equals(other.mFormat))
216            return false;
217        if (mIcon1 == null) {
218            if (other.mIcon1 != null)
219                return false;
220        } else if (!mIcon1.equals(other.mIcon1))
221            return false;
222        if (mIcon2 == null) {
223            if (other.mIcon2 != null)
224                return false;
225        } else if (!mIcon2.equals(other.mIcon2))
226            return false;
227        if (mIntentAction == null) {
228            if (other.mIntentAction != null)
229                return false;
230        } else if (!mIntentAction.equals(other.mIntentAction))
231            return false;
232        if (mIntentData == null) {
233            if (other.mIntentData != null)
234                return false;
235        } else if (!mIntentData.equals(other.mIntentData))
236            return false;
237        if (mIntentExtraData == null) {
238            if (other.mIntentExtraData != null)
239                return false;
240        } else if (!mIntentExtraData.equals(other.mIntentExtraData))
241            return false;
242        if (mShortcutId == null) {
243            if (other.mShortcutId != null)
244                return false;
245        } else if (!mShortcutId.equals(other.mShortcutId))
246            return false;
247        if (mSource == null) {
248            if (other.mSource != null)
249                return false;
250        } else if (!mSource.equals(other.mSource))
251            return false;
252        if (mSpinnerWhileRefreshing != other.mSpinnerWhileRefreshing)
253            return false;
254        if (mSuggestionQuery == null) {
255            if (other.mSuggestionQuery != null)
256                return false;
257        } else if (!mSuggestionQuery.equals(other.mSuggestionQuery))
258            return false;
259        if (mText1 == null) {
260            if (other.mText1 != null)
261                return false;
262        } else if (!mText1.equals(other.mText1))
263            return false;
264        if (mText2 == null) {
265            if (other.mText2 != null)
266                return false;
267        } else if (!mText2.equals(other.mText2))
268            return false;
269        return true;
270    }
271
272    /**
273     * Returns a string representation of the contents of this SuggestionData,
274     * for debugging purposes.
275     */
276    @Override
277    public String toString() {
278        StringBuilder builder = new StringBuilder("SuggestionData(");
279        appendField(builder, "source", mSource.getFlattenedComponentName());
280        appendField(builder, "text1", mText1);
281        appendField(builder, "intentAction", mIntentAction);
282        appendField(builder, "intentData", mIntentData);
283        appendField(builder, "query", mSuggestionQuery);
284        appendField(builder, "shortcutid", mShortcutId);
285        return builder.toString();
286    }
287
288    private void appendField(StringBuilder builder, String name, String value) {
289        if (value != null) {
290            builder.append(",").append(name).append("=").append(value);
291        }
292    }
293
294}
295