Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 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 android.widget; |
| 18 | |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 19 | import android.app.SearchDialog; |
| 20 | import android.app.SearchManager; |
| 21 | import android.app.SearchableInfo; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.ContentResolver.OpenResourceIdResult; |
| 26 | import android.content.pm.ActivityInfo; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.PackageManager.NameNotFoundException; |
| 29 | import android.content.res.ColorStateList; |
| 30 | import android.content.res.Resources; |
| 31 | import android.database.Cursor; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 33 | import android.net.Uri; |
| 34 | import android.os.Bundle; |
| 35 | import android.text.Spannable; |
| 36 | import android.text.SpannableString; |
| 37 | import android.text.TextUtils; |
| 38 | import android.text.style.TextAppearanceSpan; |
| 39 | import android.util.Log; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 40 | import android.util.TypedValue; |
| 41 | import android.view.View; |
| 42 | import android.view.ViewGroup; |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 43 | import android.view.View.OnClickListener; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 44 | |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 45 | import com.android.internal.R; |
| 46 | |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 47 | import java.io.FileNotFoundException; |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
| 50 | import java.util.WeakHashMap; |
| 51 | |
| 52 | /** |
| 53 | * Provides the contents for the suggestion drop-down list.in {@link SearchDialog}. |
| 54 | * |
| 55 | * @hide |
| 56 | */ |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 57 | class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListener { |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 58 | |
| 59 | private static final boolean DBG = false; |
| 60 | private static final String LOG_TAG = "SuggestionsAdapter"; |
| 61 | private static final int QUERY_LIMIT = 50; |
| 62 | |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 63 | static final int REFINE_NONE = 0; |
| 64 | static final int REFINE_BY_ENTRY = 1; |
| 65 | static final int REFINE_ALL = 2; |
| 66 | |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 67 | private SearchManager mSearchManager; |
| 68 | private SearchView mSearchView; |
| 69 | private SearchableInfo mSearchable; |
| 70 | private Context mProviderContext; |
| 71 | private WeakHashMap<String, Drawable.ConstantState> mOutsideDrawablesCache; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 72 | private boolean mClosed = false; |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 73 | private int mQueryRefinement = REFINE_BY_ENTRY; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 74 | |
| 75 | // URL color |
| 76 | private ColorStateList mUrlColor; |
| 77 | |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 78 | static final int INVALID_INDEX = -1; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 79 | |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 80 | // Cached column indexes, updated when the cursor changes. |
| 81 | private int mText1Col = INVALID_INDEX; |
| 82 | private int mText2Col = INVALID_INDEX; |
| 83 | private int mText2UrlCol = INVALID_INDEX; |
| 84 | private int mIconName1Col = INVALID_INDEX; |
| 85 | private int mIconName2Col = INVALID_INDEX; |
| 86 | private int mFlagsCol = INVALID_INDEX; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 87 | |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 88 | // private final Runnable mStartSpinnerRunnable; |
| 89 | // private final Runnable mStopSpinnerRunnable; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * The amount of time we delay in the filter when the user presses the delete key. |
| 93 | * @see Filter#setDelayer(android.widget.Filter.Delayer). |
| 94 | */ |
| 95 | private static final long DELETE_KEY_POST_DELAY = 500L; |
| 96 | |
| 97 | public SuggestionsAdapter(Context context, SearchView searchView, |
| 98 | SearchableInfo searchable, |
| 99 | WeakHashMap<String, Drawable.ConstantState> outsideDrawablesCache) { |
| 100 | super(context, |
| 101 | com.android.internal.R.layout.search_dropdown_item_icons_2line, |
| 102 | null, // no initial cursor |
| 103 | true); // auto-requery |
| 104 | mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
| 105 | mSearchView = searchView; |
| 106 | mSearchable = searchable; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 107 | // set up provider resources (gives us icons, etc.) |
| 108 | Context activityContext = mSearchable.getActivityContext(mContext); |
| 109 | mProviderContext = mSearchable.getProviderContext(mContext, activityContext); |
| 110 | |
| 111 | mOutsideDrawablesCache = outsideDrawablesCache; |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 112 | |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 113 | // mStartSpinnerRunnable = new Runnable() { |
| 114 | // public void run() { |
| 115 | // // mSearchView.setWorking(true); // TODO: |
| 116 | // } |
| 117 | // }; |
| 118 | // |
| 119 | // mStopSpinnerRunnable = new Runnable() { |
| 120 | // public void run() { |
| 121 | // // mSearchView.setWorking(false); // TODO: |
| 122 | // } |
| 123 | // }; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 124 | |
| 125 | // delay 500ms when deleting |
| 126 | getFilter().setDelayer(new Filter.Delayer() { |
| 127 | |
| 128 | private int mPreviousLength = 0; |
| 129 | |
| 130 | public long getPostingDelay(CharSequence constraint) { |
| 131 | if (constraint == null) return 0; |
| 132 | |
| 133 | long delay = constraint.length() < mPreviousLength ? DELETE_KEY_POST_DELAY : 0; |
| 134 | mPreviousLength = constraint.length(); |
| 135 | return delay; |
| 136 | } |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | /** |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 141 | * Enables query refinement for all suggestions. This means that an additional icon |
| 142 | * will be shown for each entry. When clicked, the suggested text on that line will be |
| 143 | * copied to the query text field. |
| 144 | * <p> |
| 145 | * |
| 146 | * @param refine which queries to refine. Possible values are {@link #REFINE_NONE}, |
| 147 | * {@link #REFINE_BY_ENTRY}, and {@link #REFINE_ALL}. |
| 148 | */ |
| 149 | public void setQueryRefinement(int refineWhat) { |
| 150 | mQueryRefinement = refineWhat; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the current query refinement preference. |
| 155 | * @return value of query refinement preference |
| 156 | */ |
| 157 | public int getQueryRefinement() { |
| 158 | return mQueryRefinement; |
| 159 | } |
| 160 | |
| 161 | /** |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 162 | * Overridden to always return <code>false</code>, since we cannot be sure that |
| 163 | * suggestion sources return stable IDs. |
| 164 | */ |
| 165 | @Override |
| 166 | public boolean hasStableIds() { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Use the search suggestions provider to obtain a live cursor. This will be called |
| 172 | * in a worker thread, so it's OK if the query is slow (e.g. round trip for suggestions). |
| 173 | * The results will be processed in the UI thread and changeCursor() will be called. |
| 174 | */ |
| 175 | @Override |
| 176 | public Cursor runQueryOnBackgroundThread(CharSequence constraint) { |
| 177 | if (DBG) Log.d(LOG_TAG, "runQueryOnBackgroundThread(" + constraint + ")"); |
| 178 | String query = (constraint == null) ? "" : constraint.toString(); |
| 179 | /** |
| 180 | * for in app search we show the progress spinner until the cursor is returned with |
| 181 | * the results. |
| 182 | */ |
| 183 | Cursor cursor = null; |
Amith Yamasani | 8790764 | 2011-11-03 11:32:44 -0700 | [diff] [blame] | 184 | if (mSearchView.getVisibility() != View.VISIBLE |
| 185 | || mSearchView.getWindowVisibility() != View.VISIBLE) { |
| 186 | return null; |
| 187 | } |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 188 | //mSearchView.getWindow().getDecorView().post(mStartSpinnerRunnable); // TODO: |
| 189 | try { |
| 190 | cursor = mSearchManager.getSuggestions(mSearchable, query, QUERY_LIMIT); |
| 191 | // trigger fill window so the spinner stays up until the results are copied over and |
| 192 | // closer to being ready |
| 193 | if (cursor != null) { |
| 194 | cursor.getCount(); |
| 195 | return cursor; |
| 196 | } |
| 197 | } catch (RuntimeException e) { |
| 198 | Log.w(LOG_TAG, "Search suggestions query threw an exception.", e); |
| 199 | } |
| 200 | // If cursor is null or an exception was thrown, stop the spinner and return null. |
| 201 | // changeCursor doesn't get called if cursor is null |
| 202 | // mSearchView.getWindow().getDecorView().post(mStopSpinnerRunnable); // TODO: |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | public void close() { |
| 207 | if (DBG) Log.d(LOG_TAG, "close()"); |
| 208 | changeCursor(null); |
| 209 | mClosed = true; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void notifyDataSetChanged() { |
| 214 | if (DBG) Log.d(LOG_TAG, "notifyDataSetChanged"); |
| 215 | super.notifyDataSetChanged(); |
| 216 | |
| 217 | // mSearchView.onDataSetChanged(); // TODO: |
| 218 | |
| 219 | updateSpinnerState(getCursor()); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void notifyDataSetInvalidated() { |
| 224 | if (DBG) Log.d(LOG_TAG, "notifyDataSetInvalidated"); |
| 225 | super.notifyDataSetInvalidated(); |
| 226 | |
| 227 | updateSpinnerState(getCursor()); |
| 228 | } |
| 229 | |
| 230 | private void updateSpinnerState(Cursor cursor) { |
| 231 | Bundle extras = cursor != null ? cursor.getExtras() : null; |
| 232 | if (DBG) { |
| 233 | Log.d(LOG_TAG, "updateSpinnerState - extra = " |
| 234 | + (extras != null |
| 235 | ? extras.getBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS) |
| 236 | : null)); |
| 237 | } |
| 238 | // Check if the Cursor indicates that the query is not complete and show the spinner |
| 239 | if (extras != null |
| 240 | && extras.getBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS)) { |
| 241 | // mSearchView.getWindow().getDecorView().post(mStartSpinnerRunnable); // TODO: |
| 242 | return; |
| 243 | } |
| 244 | // If cursor is null or is done, stop the spinner |
| 245 | // mSearchView.getWindow().getDecorView().post(mStopSpinnerRunnable); // TODO: |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Cache columns. |
| 250 | */ |
| 251 | @Override |
| 252 | public void changeCursor(Cursor c) { |
| 253 | if (DBG) Log.d(LOG_TAG, "changeCursor(" + c + ")"); |
| 254 | |
| 255 | if (mClosed) { |
| 256 | Log.w(LOG_TAG, "Tried to change cursor after adapter was closed."); |
| 257 | if (c != null) c.close(); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | try { |
| 262 | super.changeCursor(c); |
| 263 | |
| 264 | if (c != null) { |
| 265 | mText1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 266 | mText2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2); |
| 267 | mText2UrlCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL); |
| 268 | mIconName1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1); |
| 269 | mIconName2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_2); |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 270 | mFlagsCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_FLAGS); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 271 | } |
| 272 | } catch (Exception e) { |
| 273 | Log.e(LOG_TAG, "error changing cursor and caching columns", e); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Tags the view with cached child view look-ups. |
| 279 | */ |
| 280 | @Override |
| 281 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 282 | View v = super.newView(context, cursor, parent); |
| 283 | v.setTag(new ChildViewCache(v)); |
| 284 | return v; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Cache of the child views of drop-drown list items, to avoid looking up the children |
| 289 | * each time the contents of a list item are changed. |
| 290 | */ |
| 291 | private final static class ChildViewCache { |
| 292 | public final TextView mText1; |
| 293 | public final TextView mText2; |
| 294 | public final ImageView mIcon1; |
| 295 | public final ImageView mIcon2; |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 296 | public final ImageView mIconRefine; |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 297 | |
| 298 | public ChildViewCache(View v) { |
| 299 | mText1 = (TextView) v.findViewById(com.android.internal.R.id.text1); |
| 300 | mText2 = (TextView) v.findViewById(com.android.internal.R.id.text2); |
| 301 | mIcon1 = (ImageView) v.findViewById(com.android.internal.R.id.icon1); |
| 302 | mIcon2 = (ImageView) v.findViewById(com.android.internal.R.id.icon2); |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 303 | mIconRefine = (ImageView) v.findViewById(com.android.internal.R.id.edit_query); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void bindView(View view, Context context, Cursor cursor) { |
| 309 | ChildViewCache views = (ChildViewCache) view.getTag(); |
| 310 | |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 311 | int flags = 0; |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 312 | if (mFlagsCol != INVALID_INDEX) { |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 313 | flags = cursor.getInt(mFlagsCol); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 314 | } |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 315 | if (views.mText1 != null) { |
| 316 | String text1 = getStringOrNull(cursor, mText1Col); |
| 317 | setViewText(views.mText1, text1); |
| 318 | } |
| 319 | if (views.mText2 != null) { |
| 320 | // First check TEXT_2_URL |
| 321 | CharSequence text2 = getStringOrNull(cursor, mText2UrlCol); |
| 322 | if (text2 != null) { |
| 323 | text2 = formatUrl(text2); |
| 324 | } else { |
| 325 | text2 = getStringOrNull(cursor, mText2Col); |
| 326 | } |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 327 | |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 328 | // If no second line of text is indicated, allow the first line of text |
| 329 | // to be up to two lines if it wants to be. |
| 330 | if (TextUtils.isEmpty(text2)) { |
| 331 | if (views.mText1 != null) { |
| 332 | views.mText1.setSingleLine(false); |
| 333 | views.mText1.setMaxLines(2); |
| 334 | } |
| 335 | } else { |
| 336 | if (views.mText1 != null) { |
| 337 | views.mText1.setSingleLine(true); |
| 338 | views.mText1.setMaxLines(1); |
| 339 | } |
| 340 | } |
| 341 | setViewText(views.mText2, text2); |
| 342 | } |
| 343 | |
| 344 | if (views.mIcon1 != null) { |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 345 | setViewDrawable(views.mIcon1, getIcon1(cursor), View.INVISIBLE); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 346 | } |
| 347 | if (views.mIcon2 != null) { |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 348 | setViewDrawable(views.mIcon2, getIcon2(cursor), View.GONE); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 349 | } |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 350 | if (mQueryRefinement == REFINE_ALL |
| 351 | || (mQueryRefinement == REFINE_BY_ENTRY |
| 352 | && (flags & SearchManager.FLAG_QUERY_REFINEMENT) != 0)) { |
| 353 | views.mIconRefine.setVisibility(View.VISIBLE); |
| 354 | views.mIconRefine.setTag(views.mText1.getText()); |
| 355 | views.mIconRefine.setOnClickListener(this); |
| 356 | } else { |
| 357 | views.mIconRefine.setVisibility(View.GONE); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | public void onClick(View v) { |
| 362 | Object tag = v.getTag(); |
| 363 | if (tag instanceof CharSequence) { |
| 364 | mSearchView.onQueryRefine((CharSequence) tag); |
| 365 | } |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private CharSequence formatUrl(CharSequence url) { |
| 369 | if (mUrlColor == null) { |
| 370 | // Lazily get the URL color from the current theme. |
| 371 | TypedValue colorValue = new TypedValue(); |
| 372 | mContext.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true); |
| 373 | mUrlColor = mContext.getResources().getColorStateList(colorValue.resourceId); |
| 374 | } |
| 375 | |
| 376 | SpannableString text = new SpannableString(url); |
| 377 | text.setSpan(new TextAppearanceSpan(null, 0, 0, mUrlColor, null), |
| 378 | 0, url.length(), |
| 379 | Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 380 | return text; |
| 381 | } |
| 382 | |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 383 | private void setViewText(TextView v, CharSequence text) { |
| 384 | // Set the text even if it's null, since we need to clear any previous text. |
| 385 | v.setText(text); |
| 386 | |
| 387 | if (TextUtils.isEmpty(text)) { |
| 388 | v.setVisibility(View.GONE); |
| 389 | } else { |
| 390 | v.setVisibility(View.VISIBLE); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | private Drawable getIcon1(Cursor cursor) { |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 395 | if (mIconName1Col == INVALID_INDEX) { |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 396 | return null; |
| 397 | } |
| 398 | String value = cursor.getString(mIconName1Col); |
| 399 | Drawable drawable = getDrawableFromResourceValue(value); |
| 400 | if (drawable != null) { |
| 401 | return drawable; |
| 402 | } |
| 403 | return getDefaultIcon1(cursor); |
| 404 | } |
| 405 | |
| 406 | private Drawable getIcon2(Cursor cursor) { |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 407 | if (mIconName2Col == INVALID_INDEX) { |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 408 | return null; |
| 409 | } |
| 410 | String value = cursor.getString(mIconName2Col); |
| 411 | return getDrawableFromResourceValue(value); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Sets the drawable in an image view, makes sure the view is only visible if there |
| 416 | * is a drawable. |
| 417 | */ |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 418 | private void setViewDrawable(ImageView v, Drawable drawable, int nullVisibility) { |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 419 | // Set the icon even if the drawable is null, since we need to clear any |
| 420 | // previous icon. |
| 421 | v.setImageDrawable(drawable); |
| 422 | |
| 423 | if (drawable == null) { |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 424 | v.setVisibility(nullVisibility); |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 425 | } else { |
| 426 | v.setVisibility(View.VISIBLE); |
| 427 | |
| 428 | // This is a hack to get any animated drawables (like a 'working' spinner) |
| 429 | // to animate. You have to setVisible true on an AnimationDrawable to get |
| 430 | // it to start animating, but it must first have been false or else the |
| 431 | // call to setVisible will be ineffective. We need to clear up the story |
| 432 | // about animated drawables in the future, see http://b/1878430. |
| 433 | drawable.setVisible(false, false); |
| 434 | drawable.setVisible(true, false); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Gets the text to show in the query field when a suggestion is selected. |
| 440 | * |
| 441 | * @param cursor The Cursor to read the suggestion data from. The Cursor should already |
| 442 | * be moved to the suggestion that is to be read from. |
| 443 | * @return The text to show, or <code>null</code> if the query should not be |
| 444 | * changed when selecting this suggestion. |
| 445 | */ |
| 446 | @Override |
| 447 | public CharSequence convertToString(Cursor cursor) { |
| 448 | if (cursor == null) { |
| 449 | return null; |
| 450 | } |
| 451 | |
| 452 | String query = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_QUERY); |
| 453 | if (query != null) { |
| 454 | return query; |
| 455 | } |
| 456 | |
| 457 | if (mSearchable.shouldRewriteQueryFromData()) { |
| 458 | String data = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_INTENT_DATA); |
| 459 | if (data != null) { |
| 460 | return data; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (mSearchable.shouldRewriteQueryFromText()) { |
| 465 | String text1 = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 466 | if (text1 != null) { |
| 467 | return text1; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * This method is overridden purely to provide a bit of protection against |
| 476 | * flaky content providers. |
| 477 | * |
| 478 | * @see android.widget.ListAdapter#getView(int, View, ViewGroup) |
| 479 | */ |
| 480 | @Override |
| 481 | public View getView(int position, View convertView, ViewGroup parent) { |
| 482 | try { |
| 483 | return super.getView(position, convertView, parent); |
| 484 | } catch (RuntimeException e) { |
| 485 | Log.w(LOG_TAG, "Search suggestions cursor threw exception.", e); |
| 486 | // Put exception string in item title |
| 487 | View v = newView(mContext, mCursor, parent); |
| 488 | if (v != null) { |
| 489 | ChildViewCache views = (ChildViewCache) v.getTag(); |
| 490 | TextView tv = views.mText1; |
| 491 | tv.setText(e.toString()); |
| 492 | } |
| 493 | return v; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Gets a drawable given a value provided by a suggestion provider. |
| 499 | * |
| 500 | * This value could be just the string value of a resource id |
| 501 | * (e.g., "2130837524"), in which case we will try to retrieve a drawable from |
| 502 | * the provider's resources. If the value is not an integer, it is |
| 503 | * treated as a Uri and opened with |
| 504 | * {@link ContentResolver#openOutputStream(android.net.Uri, String)}. |
| 505 | * |
| 506 | * All resources and URIs are read using the suggestion provider's context. |
| 507 | * |
| 508 | * If the string is not formatted as expected, or no drawable can be found for |
| 509 | * the provided value, this method returns null. |
| 510 | * |
| 511 | * @param drawableId a string like "2130837524", |
| 512 | * "android.resource://com.android.alarmclock/2130837524", |
| 513 | * or "content://contacts/photos/253". |
| 514 | * @return a Drawable, or null if none found |
| 515 | */ |
| 516 | private Drawable getDrawableFromResourceValue(String drawableId) { |
| 517 | if (drawableId == null || drawableId.length() == 0 || "0".equals(drawableId)) { |
| 518 | return null; |
| 519 | } |
| 520 | try { |
| 521 | // First, see if it's just an integer |
| 522 | int resourceId = Integer.parseInt(drawableId); |
| 523 | // It's an int, look for it in the cache |
| 524 | String drawableUri = ContentResolver.SCHEME_ANDROID_RESOURCE |
| 525 | + "://" + mProviderContext.getPackageName() + "/" + resourceId; |
| 526 | // Must use URI as cache key, since ints are app-specific |
| 527 | Drawable drawable = checkIconCache(drawableUri); |
| 528 | if (drawable != null) { |
| 529 | return drawable; |
| 530 | } |
| 531 | // Not cached, find it by resource ID |
| 532 | drawable = mProviderContext.getResources().getDrawable(resourceId); |
| 533 | // Stick it in the cache, using the URI as key |
| 534 | storeInIconCache(drawableUri, drawable); |
| 535 | return drawable; |
| 536 | } catch (NumberFormatException nfe) { |
| 537 | // It's not an integer, use it as a URI |
| 538 | Drawable drawable = checkIconCache(drawableId); |
| 539 | if (drawable != null) { |
| 540 | return drawable; |
| 541 | } |
| 542 | Uri uri = Uri.parse(drawableId); |
| 543 | drawable = getDrawable(uri); |
| 544 | storeInIconCache(drawableId, drawable); |
| 545 | return drawable; |
| 546 | } catch (Resources.NotFoundException nfe) { |
| 547 | // It was an integer, but it couldn't be found, bail out |
| 548 | Log.w(LOG_TAG, "Icon resource not found: " + drawableId); |
| 549 | return null; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Gets a drawable by URI, without using the cache. |
| 555 | * |
| 556 | * @return A drawable, or {@code null} if the drawable could not be loaded. |
| 557 | */ |
| 558 | private Drawable getDrawable(Uri uri) { |
| 559 | try { |
| 560 | String scheme = uri.getScheme(); |
| 561 | if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 562 | // Load drawables through Resources, to get the source density information |
| 563 | OpenResourceIdResult r = |
| 564 | mProviderContext.getContentResolver().getResourceId(uri); |
| 565 | try { |
| 566 | return r.r.getDrawable(r.id); |
| 567 | } catch (Resources.NotFoundException ex) { |
| 568 | throw new FileNotFoundException("Resource does not exist: " + uri); |
| 569 | } |
| 570 | } else { |
| 571 | // Let the ContentResolver handle content and file URIs. |
| 572 | InputStream stream = mProviderContext.getContentResolver().openInputStream(uri); |
| 573 | if (stream == null) { |
| 574 | throw new FileNotFoundException("Failed to open " + uri); |
| 575 | } |
| 576 | try { |
| 577 | return Drawable.createFromStream(stream, null); |
| 578 | } finally { |
| 579 | try { |
| 580 | stream.close(); |
| 581 | } catch (IOException ex) { |
| 582 | Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | } catch (FileNotFoundException fnfe) { |
| 587 | Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage()); |
| 588 | return null; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | private Drawable checkIconCache(String resourceUri) { |
| 593 | Drawable.ConstantState cached = mOutsideDrawablesCache.get(resourceUri); |
| 594 | if (cached == null) { |
| 595 | return null; |
| 596 | } |
| 597 | if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + resourceUri); |
| 598 | return cached.newDrawable(); |
| 599 | } |
| 600 | |
| 601 | private void storeInIconCache(String resourceUri, Drawable drawable) { |
| 602 | if (drawable != null) { |
| 603 | mOutsideDrawablesCache.put(resourceUri, drawable.getConstantState()); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Gets the left-hand side icon that will be used for the current suggestion |
| 609 | * if the suggestion contains an icon column but no icon or a broken icon. |
| 610 | * |
| 611 | * @param cursor A cursor positioned at the current suggestion. |
| 612 | * @return A non-null drawable. |
| 613 | */ |
| 614 | private Drawable getDefaultIcon1(Cursor cursor) { |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 615 | // Check the component that gave us the suggestion |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 616 | Drawable drawable = getActivityIconWithCache(mSearchable.getSearchActivity()); |
| 617 | if (drawable != null) { |
| 618 | return drawable; |
| 619 | } |
| 620 | |
| 621 | // Fall back to a default icon |
| 622 | return mContext.getPackageManager().getDefaultActivityIcon(); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Gets the activity or application icon for an activity. |
| 627 | * Uses the local icon cache for fast repeated lookups. |
| 628 | * |
| 629 | * @param component Name of an activity. |
| 630 | * @return A drawable, or {@code null} if neither the activity nor the application |
| 631 | * has an icon set. |
| 632 | */ |
| 633 | private Drawable getActivityIconWithCache(ComponentName component) { |
| 634 | // First check the icon cache |
| 635 | String componentIconKey = component.flattenToShortString(); |
| 636 | // Using containsKey() since we also store null values. |
| 637 | if (mOutsideDrawablesCache.containsKey(componentIconKey)) { |
| 638 | Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey); |
| 639 | return cached == null ? null : cached.newDrawable(mProviderContext.getResources()); |
| 640 | } |
| 641 | // Then try the activity or application icon |
| 642 | Drawable drawable = getActivityIcon(component); |
| 643 | // Stick it in the cache so we don't do this lookup again. |
| 644 | Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState(); |
| 645 | mOutsideDrawablesCache.put(componentIconKey, toCache); |
| 646 | return drawable; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Gets the activity or application icon for an activity. |
| 651 | * |
| 652 | * @param component Name of an activity. |
| 653 | * @return A drawable, or {@code null} if neither the acitivy or the application |
| 654 | * have an icon set. |
| 655 | */ |
| 656 | private Drawable getActivityIcon(ComponentName component) { |
| 657 | PackageManager pm = mContext.getPackageManager(); |
| 658 | final ActivityInfo activityInfo; |
| 659 | try { |
| 660 | activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA); |
| 661 | } catch (NameNotFoundException ex) { |
| 662 | Log.w(LOG_TAG, ex.toString()); |
| 663 | return null; |
| 664 | } |
| 665 | int iconId = activityInfo.getIconResource(); |
| 666 | if (iconId == 0) return null; |
| 667 | String pkg = component.getPackageName(); |
| 668 | Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo); |
| 669 | if (drawable == null) { |
| 670 | Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " |
| 671 | + component.flattenToShortString()); |
| 672 | return null; |
| 673 | } |
| 674 | return drawable; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Gets the value of a string column by name. |
| 679 | * |
| 680 | * @param cursor Cursor to read the value from. |
| 681 | * @param columnName The name of the column to read. |
| 682 | * @return The value of the given column, or <code>null</null> |
| 683 | * if the cursor does not contain the given column. |
| 684 | */ |
| 685 | public static String getColumnString(Cursor cursor, String columnName) { |
| 686 | int col = cursor.getColumnIndex(columnName); |
| 687 | return getStringOrNull(cursor, col); |
| 688 | } |
| 689 | |
| 690 | private static String getStringOrNull(Cursor cursor, int col) { |
Amith Yamasani | de6e2ca | 2011-01-19 16:19:35 -0800 | [diff] [blame] | 691 | if (col == INVALID_INDEX) { |
Amith Yamasani | 733cbd5 | 2010-09-03 12:21:39 -0700 | [diff] [blame] | 692 | return null; |
| 693 | } |
| 694 | try { |
| 695 | return cursor.getString(col); |
| 696 | } catch (Exception e) { |
| 697 | Log.e(LOG_TAG, |
| 698 | "unexpected error retrieving valid column from cursor, " |
| 699 | + "did the remote process die?", e); |
| 700 | return null; |
| 701 | } |
| 702 | } |
| 703 | } |