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 
17 package com.android.quicksearchbox;
18 
19 import com.google.common.annotations.VisibleForTesting;
20 
21 import android.content.ComponentName;
22 import android.content.Intent;
23 
24 
25 /**
26  * Holds data for each suggest item including the display data and how to launch the result.
27  * Used for passing from the provider to the suggest cursor.
28  */
29 public class SuggestionData implements Suggestion {
30 
31     private final Source mSource;
32     private String mFormat;
33     private String mText1;
34     private String mText2;
35     private String mText2Url;
36     private String mIcon1;
37     private String mIcon2;
38     private String mShortcutId;
39     private boolean mSpinnerWhileRefreshing;
40     private String mIntentAction;
41     private String mIntentData;
42     private String mIntentExtraData;
43     private String mSuggestionQuery;
44     private String mLogType;
45     private boolean mIsShortcut;
46     private boolean mIsHistory;
47     private SuggestionExtras mExtras;
48 
SuggestionData(Source source)49     public SuggestionData(Source source) {
50         mSource = source;
51     }
52 
getSuggestionSource()53     public Source getSuggestionSource() {
54         return mSource;
55     }
56 
getSuggestionFormat()57     public String getSuggestionFormat() {
58         return mFormat;
59     }
60 
getSuggestionText1()61     public String getSuggestionText1() {
62         return mText1;
63     }
64 
getSuggestionText2()65     public String getSuggestionText2() {
66         return mText2;
67     }
68 
getSuggestionText2Url()69     public String getSuggestionText2Url() {
70         return mText2Url;
71     }
72 
getSuggestionIcon1()73     public String getSuggestionIcon1() {
74         return mIcon1;
75     }
76 
getSuggestionIcon2()77     public String getSuggestionIcon2() {
78         return mIcon2;
79     }
80 
isSpinnerWhileRefreshing()81     public boolean isSpinnerWhileRefreshing() {
82         return mSpinnerWhileRefreshing;
83     }
84 
getIntentExtraData()85     public String getIntentExtraData() {
86         return mIntentExtraData;
87     }
88 
getShortcutId()89     public String getShortcutId() {
90         return mShortcutId;
91     }
92 
getSuggestionIntentAction()93     public String getSuggestionIntentAction() {
94         if (mIntentAction != null) return mIntentAction;
95         return mSource.getDefaultIntentAction();
96     }
97 
getSuggestionIntentComponent()98     public ComponentName getSuggestionIntentComponent() {
99         return mSource.getIntentComponent();
100     }
101 
getSuggestionIntentDataString()102     public String getSuggestionIntentDataString() {
103         return mIntentData;
104     }
105 
getSuggestionIntentExtraData()106     public String getSuggestionIntentExtraData() {
107         return mIntentExtraData;
108     }
109 
getSuggestionQuery()110     public String getSuggestionQuery() {
111         return mSuggestionQuery;
112     }
113 
getSuggestionLogType()114     public String getSuggestionLogType() {
115         return mLogType;
116     }
117 
isSuggestionShortcut()118     public boolean isSuggestionShortcut() {
119         return mIsShortcut;
120     }
121 
isWebSearchSuggestion()122     public boolean isWebSearchSuggestion() {
123         return Intent.ACTION_WEB_SEARCH.equals(getSuggestionIntentAction());
124     }
125 
isHistorySuggestion()126     public boolean isHistorySuggestion() {
127         return mIsHistory;
128     }
129 
130     @VisibleForTesting
setFormat(String format)131     public SuggestionData setFormat(String format) {
132         mFormat = format;
133         return this;
134     }
135 
136     @VisibleForTesting
setText1(String text1)137     public SuggestionData setText1(String text1) {
138         mText1 = text1;
139         return this;
140     }
141 
142     @VisibleForTesting
setText2(String text2)143     public SuggestionData setText2(String text2) {
144         mText2 = text2;
145         return this;
146     }
147 
148     @VisibleForTesting
setText2Url(String text2Url)149     public SuggestionData setText2Url(String text2Url) {
150         mText2Url = text2Url;
151         return this;
152     }
153 
154     @VisibleForTesting
setIcon1(String icon1)155     public SuggestionData setIcon1(String icon1) {
156         mIcon1 = icon1;
157         return this;
158     }
159 
160     @VisibleForTesting
setIcon2(String icon2)161     public SuggestionData setIcon2(String icon2) {
162         mIcon2 = icon2;
163         return this;
164     }
165 
166     @VisibleForTesting
setIntentAction(String intentAction)167     public SuggestionData setIntentAction(String intentAction) {
168         mIntentAction = intentAction;
169         return this;
170     }
171 
172     @VisibleForTesting
setIntentData(String intentData)173     public SuggestionData setIntentData(String intentData) {
174         mIntentData = intentData;
175         return this;
176     }
177 
178     @VisibleForTesting
setIntentExtraData(String intentExtraData)179     public SuggestionData setIntentExtraData(String intentExtraData) {
180         mIntentExtraData = intentExtraData;
181         return this;
182     }
183 
184     @VisibleForTesting
setSuggestionQuery(String suggestionQuery)185     public SuggestionData setSuggestionQuery(String suggestionQuery) {
186         mSuggestionQuery = suggestionQuery;
187         return this;
188     }
189 
190     @VisibleForTesting
setShortcutId(String shortcutId)191     public SuggestionData setShortcutId(String shortcutId) {
192         mShortcutId = shortcutId;
193         return this;
194     }
195 
196     @VisibleForTesting
setSpinnerWhileRefreshing(boolean spinnerWhileRefreshing)197     public SuggestionData setSpinnerWhileRefreshing(boolean spinnerWhileRefreshing) {
198         mSpinnerWhileRefreshing = spinnerWhileRefreshing;
199         return this;
200     }
201 
202     @VisibleForTesting
setSuggestionLogType(String logType)203     public SuggestionData setSuggestionLogType(String logType) {
204         mLogType = logType;
205         return this;
206     }
207 
208     @VisibleForTesting
setIsShortcut(boolean isShortcut)209     public SuggestionData setIsShortcut(boolean isShortcut) {
210         mIsShortcut = isShortcut;
211         return this;
212     }
213 
214     @VisibleForTesting
setIsHistory(boolean isHistory)215     public SuggestionData setIsHistory(boolean isHistory) {
216         mIsHistory = isHistory;
217         return this;
218     }
219 
220     @Override
hashCode()221     public int hashCode() {
222         final int prime = 31;
223         int result = 1;
224         result = prime * result + ((mFormat == null) ? 0 : mFormat.hashCode());
225         result = prime * result + ((mIcon1 == null) ? 0 : mIcon1.hashCode());
226         result = prime * result + ((mIcon2 == null) ? 0 : mIcon2.hashCode());
227         result = prime * result + ((mIntentAction == null) ? 0 : mIntentAction.hashCode());
228         result = prime * result + ((mIntentData == null) ? 0 : mIntentData.hashCode());
229         result = prime * result + ((mIntentExtraData == null) ? 0 : mIntentExtraData.hashCode());
230         result = prime * result + ((mLogType == null) ? 0 : mLogType.hashCode());
231         result = prime * result + ((mShortcutId == null) ? 0 : mShortcutId.hashCode());
232         result = prime * result + ((mSource == null) ? 0 : mSource.hashCode());
233         result = prime * result + (mSpinnerWhileRefreshing ? 1231 : 1237);
234         result = prime * result + ((mSuggestionQuery == null) ? 0 : mSuggestionQuery.hashCode());
235         result = prime * result + ((mText1 == null) ? 0 : mText1.hashCode());
236         result = prime * result + ((mText2 == null) ? 0 : mText2.hashCode());
237         return result;
238     }
239 
240     @Override
equals(Object obj)241     public boolean equals(Object obj) {
242         if (this == obj)
243             return true;
244         if (obj == null)
245             return false;
246         if (getClass() != obj.getClass())
247             return false;
248         SuggestionData other = (SuggestionData)obj;
249         if (mFormat == null) {
250             if (other.mFormat != null)
251                 return false;
252         } else if (!mFormat.equals(other.mFormat))
253             return false;
254         if (mIcon1 == null) {
255             if (other.mIcon1 != null)
256                 return false;
257         } else if (!mIcon1.equals(other.mIcon1))
258             return false;
259         if (mIcon2 == null) {
260             if (other.mIcon2 != null)
261                 return false;
262         } else if (!mIcon2.equals(other.mIcon2))
263             return false;
264         if (mIntentAction == null) {
265             if (other.mIntentAction != null)
266                 return false;
267         } else if (!mIntentAction.equals(other.mIntentAction))
268             return false;
269         if (mIntentData == null) {
270             if (other.mIntentData != null)
271                 return false;
272         } else if (!mIntentData.equals(other.mIntentData))
273             return false;
274         if (mIntentExtraData == null) {
275             if (other.mIntentExtraData != null)
276                 return false;
277         } else if (!mIntentExtraData.equals(other.mIntentExtraData))
278             return false;
279         if (mLogType == null) {
280             if (other.mLogType != null)
281                 return false;
282         } else if (!mLogType.equals(other.mLogType))
283             return false;
284         if (mShortcutId == null) {
285             if (other.mShortcutId != null)
286                 return false;
287         } else if (!mShortcutId.equals(other.mShortcutId))
288             return false;
289         if (mSource == null) {
290             if (other.mSource != null)
291                 return false;
292         } else if (!mSource.equals(other.mSource))
293             return false;
294         if (mSpinnerWhileRefreshing != other.mSpinnerWhileRefreshing)
295             return false;
296         if (mSuggestionQuery == null) {
297             if (other.mSuggestionQuery != null)
298                 return false;
299         } else if (!mSuggestionQuery.equals(other.mSuggestionQuery))
300             return false;
301         if (mText1 == null) {
302             if (other.mText1 != null)
303                 return false;
304         } else if (!mText1.equals(other.mText1))
305             return false;
306         if (mText2 == null) {
307             if (other.mText2 != null)
308                 return false;
309         } else if (!mText2.equals(other.mText2))
310             return false;
311         return true;
312     }
313 
314     /**
315      * Returns a string representation of the contents of this SuggestionData,
316      * for debugging purposes.
317      */
318     @Override
toString()319     public String toString() {
320         StringBuilder builder = new StringBuilder("SuggestionData(");
321         appendField(builder, "source", mSource.getName());
322         appendField(builder, "text1", mText1);
323         appendField(builder, "intentAction", mIntentAction);
324         appendField(builder, "intentData", mIntentData);
325         appendField(builder, "query", mSuggestionQuery);
326         appendField(builder, "shortcutid", mShortcutId);
327         appendField(builder, "logtype", mLogType);
328         return builder.toString();
329     }
330 
appendField(StringBuilder builder, String name, String value)331     private void appendField(StringBuilder builder, String name, String value) {
332         if (value != null) {
333             builder.append(",").append(name).append("=").append(value);
334         }
335     }
336 
337     @VisibleForTesting
setExtras(SuggestionExtras extras)338     public void setExtras(SuggestionExtras extras) {
339         mExtras = extras;
340     }
341 
getExtras()342     public SuggestionExtras getExtras() {
343         return mExtras;
344     }
345 
346 }
347