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