| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 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.ContentResolver.OpenResourceIdResult; |
| 25 |
import android.content.Context; |
| 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; |
| 32 |
import android.graphics.drawable.Drawable; |
| 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; |
| 40 |
import android.util.TypedValue; |
| 41 |
import android.view.View; |
| 42 |
import android.view.View.OnClickListener; |
| 43 |
import android.view.ViewGroup; |
| 44 |
|
| 45 |
import com.android.internal.R; |
| 46 |
|
| 47 |
import java.io.FileNotFoundException; |
| 48 |
import java.io.IOException; |
| 49 |
import java.io.InputStream; |
| 50 |
import java.util.WeakHashMap; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListener { |
| 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 |
|
| 63 |
static final int REFINE_NONE = 0; |
| 64 |
static final int REFINE_BY_ENTRY = 1; |
| 65 |
static final int REFINE_ALL = 2; |
| 66 |
|
| 67 |
private final SearchManager mSearchManager; |
| 68 |
private final SearchView mSearchView; |
| 69 |
private final SearchableInfo mSearchable; |
| 70 |
private final Context mProviderContext; |
| 71 |
private final WeakHashMap<String, Drawable.ConstantState> mOutsideDrawablesCache; |
| 72 |
private final int mCommitIconResId; |
| 73 |
|
| 74 |
private boolean mClosed = false; |
| 75 |
private int mQueryRefinement = REFINE_BY_ENTRY; |
| 76 |
|
| 77 |
|
| 78 |
private ColorStateList mUrlColor; |
| 79 |
|
| 80 |
static final int INVALID_INDEX = -1; |
| 81 |
|
| 82 |
|
| 83 |
private int mText1Col = INVALID_INDEX; |
| 84 |
private int mText2Col = INVALID_INDEX; |
| 85 |
private int mText2UrlCol = INVALID_INDEX; |
| 86 |
private int mIconName1Col = INVALID_INDEX; |
| 87 |
private int mIconName2Col = INVALID_INDEX; |
| 88 |
private int mFlagsCol = INVALID_INDEX; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private static final long DELETE_KEY_POST_DELAY = 500L; |
| 98 |
|
| 99 |
public SuggestionsAdapter(Context context, SearchView searchView, SearchableInfo searchable, |
| 100 |
WeakHashMap<String, Drawable.ConstantState> outsideDrawablesCache) { |
| 101 |
super(context, searchView.getSuggestionRowLayout(), null , |
| 102 |
true ); |
| 103 |
|
| 104 |
mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
| 105 |
mSearchView = searchView; |
| 106 |
mSearchable = searchable; |
| 107 |
mCommitIconResId = searchView.getSuggestionCommitIconResId(); |
| 108 |
|
| 109 |
|
| 110 |
final Context activityContext = mSearchable.getActivityContext(mContext); |
| 111 |
mProviderContext = mSearchable.getProviderContext(mContext, activityContext); |
| 112 |
|
| 113 |
mOutsideDrawablesCache = outsideDrawablesCache; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
getFilter().setDelayer(new Filter.Delayer() { |
| 129 |
|
| 130 |
private int mPreviousLength = 0; |
| 131 |
|
| 132 |
public long getPostingDelay(CharSequence constraint) { |
| 133 |
if (constraint == null) return 0; |
| 134 |
|
| 135 |
long delay = constraint.length() < mPreviousLength ? DELETE_KEY_POST_DELAY : 0; |
| 136 |
mPreviousLength = constraint.length(); |
| 137 |
return delay; |
| 138 |
} |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void setQueryRefinement(int refineWhat) { |
| 153 |
mQueryRefinement = refineWhat; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public int getQueryRefinement() { |
| 161 |
return mQueryRefinement; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Override |
| 169 |
public boolean hasStableIds() { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@Override |
| 179 |
public Cursor runQueryOnBackgroundThread(CharSequence constraint) { |
| 180 |
if (DBG) Log.d(LOG_TAG, "runQueryOnBackgroundThread(" + constraint + ")"); |
| 181 |
String query = (constraint == null) ? "" : constraint.toString(); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
Cursor cursor = null; |
| 187 |
if (mSearchView.getVisibility() != View.VISIBLE |
| 188 |
|| mSearchView.getWindowVisibility() != View.VISIBLE) { |
| 189 |
return null; |
| 190 |
} |
| 191 |
|
| 192 |
try { |
| 193 |
cursor = mSearchManager.getSuggestions(mSearchable, query, QUERY_LIMIT); |
| 194 |
|
| 195 |
|
| 196 |
if (cursor != null) { |
| 197 |
cursor.getCount(); |
| 198 |
return cursor; |
| 199 |
} |
| 200 |
} catch (RuntimeException e) { |
| 201 |
Log.w(LOG_TAG, "Search suggestions query threw an exception.", e); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
public void close() { |
| 210 |
if (DBG) Log.d(LOG_TAG, "close()"); |
| 211 |
changeCursor(null); |
| 212 |
mClosed = true; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void notifyDataSetChanged() { |
| 217 |
if (DBG) Log.d(LOG_TAG, "notifyDataSetChanged"); |
| 218 |
super.notifyDataSetChanged(); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
updateSpinnerState(getCursor()); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void notifyDataSetInvalidated() { |
| 227 |
if (DBG) Log.d(LOG_TAG, "notifyDataSetInvalidated"); |
| 228 |
super.notifyDataSetInvalidated(); |
| 229 |
|
| 230 |
updateSpinnerState(getCursor()); |
| 231 |
} |
| 232 |
|
| 233 |
private void updateSpinnerState(Cursor cursor) { |
| 234 |
Bundle extras = cursor != null ? cursor.getExtras() : null; |
| 235 |
if (DBG) { |
| 236 |
Log.d(LOG_TAG, "updateSpinnerState - extra = " |
| 237 |
+ (extras != null |
| 238 |
? extras.getBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS) |
| 239 |
: null)); |
| 240 |
} |
| 241 |
|
| 242 |
if (extras != null |
| 243 |
&& extras.getBoolean(SearchManager.CURSOR_EXTRA_KEY_IN_PROGRESS)) { |
| 244 |
|
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@Override |
| 255 |
public void changeCursor(Cursor c) { |
| 256 |
if (DBG) Log.d(LOG_TAG, "changeCursor(" + c + ")"); |
| 257 |
|
| 258 |
if (mClosed) { |
| 259 |
Log.w(LOG_TAG, "Tried to change cursor after adapter was closed."); |
| 260 |
if (c != null) c.close(); |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
try { |
| 265 |
super.changeCursor(c); |
| 266 |
|
| 267 |
if (c != null) { |
| 268 |
mText1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 269 |
mText2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2); |
| 270 |
mText2UrlCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2_URL); |
| 271 |
mIconName1Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1); |
| 272 |
mIconName2Col = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_2); |
| 273 |
mFlagsCol = c.getColumnIndex(SearchManager.SUGGEST_COLUMN_FLAGS); |
| 274 |
} |
| 275 |
} catch (Exception e) { |
| 276 |
Log.e(LOG_TAG, "error changing cursor and caching columns", e); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
@Override |
| 284 |
public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 285 |
final View v = super.newView(context, cursor, parent); |
| 286 |
v.setTag(new ChildViewCache(v)); |
| 287 |
|
| 288 |
|
| 289 |
final ImageView iconRefine = v.findViewById(R.id.edit_query); |
| 290 |
iconRefine.setImageResource(mCommitIconResId); |
| 291 |
|
| 292 |
return v; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private final static class ChildViewCache { |
| 300 |
public final TextView mText1; |
| 301 |
public final TextView mText2; |
| 302 |
public final ImageView mIcon1; |
| 303 |
public final ImageView mIcon2; |
| 304 |
public final ImageView mIconRefine; |
| 305 |
|
| 306 |
public ChildViewCache(View v) { |
| 307 |
mText1 = v.findViewById(com.android.internal.R.id.text1); |
| 308 |
mText2 = v.findViewById(com.android.internal.R.id.text2); |
| 309 |
mIcon1 = v.findViewById(com.android.internal.R.id.icon1); |
| 310 |
mIcon2 = v.findViewById(com.android.internal.R.id.icon2); |
| 311 |
mIconRefine = v.findViewById(com.android.internal.R.id.edit_query); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public void bindView(View view, Context context, Cursor cursor) { |
| 317 |
ChildViewCache views = (ChildViewCache) view.getTag(); |
| 318 |
|
| 319 |
int flags = 0; |
| 320 |
if (mFlagsCol != INVALID_INDEX) { |
| 321 |
flags = cursor.getInt(mFlagsCol); |
| 322 |
} |
| 323 |
if (views.mText1 != null) { |
| 324 |
String text1 = getStringOrNull(cursor, mText1Col); |
| 325 |
setViewText(views.mText1, text1); |
| 326 |
} |
| 327 |
if (views.mText2 != null) { |
| 328 |
|
| 329 |
CharSequence text2 = getStringOrNull(cursor, mText2UrlCol); |
| 330 |
if (text2 != null) { |
| 331 |
text2 = formatUrl(context, text2); |
| 332 |
} else { |
| 333 |
text2 = getStringOrNull(cursor, mText2Col); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
if (TextUtils.isEmpty(text2)) { |
| 339 |
if (views.mText1 != null) { |
| 340 |
views.mText1.setSingleLine(false); |
| 341 |
views.mText1.setMaxLines(2); |
| 342 |
} |
| 343 |
} else { |
| 344 |
if (views.mText1 != null) { |
| 345 |
views.mText1.setSingleLine(true); |
| 346 |
views.mText1.setMaxLines(1); |
| 347 |
} |
| 348 |
} |
| 349 |
setViewText(views.mText2, text2); |
| 350 |
} |
| 351 |
|
| 352 |
if (views.mIcon1 != null) { |
| 353 |
setViewDrawable(views.mIcon1, getIcon1(cursor), View.INVISIBLE); |
| 354 |
} |
| 355 |
if (views.mIcon2 != null) { |
| 356 |
setViewDrawable(views.mIcon2, getIcon2(cursor), View.GONE); |
| 357 |
} |
| 358 |
if (mQueryRefinement == REFINE_ALL |
| 359 |
|| (mQueryRefinement == REFINE_BY_ENTRY |
| 360 |
&& (flags & SearchManager.FLAG_QUERY_REFINEMENT) != 0)) { |
| 361 |
views.mIconRefine.setVisibility(View.VISIBLE); |
| 362 |
views.mIconRefine.setTag(views.mText1.getText()); |
| 363 |
views.mIconRefine.setOnClickListener(this); |
| 364 |
} else { |
| 365 |
views.mIconRefine.setVisibility(View.GONE); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
public void onClick(View v) { |
| 370 |
Object tag = v.getTag(); |
| 371 |
if (tag instanceof CharSequence) { |
| 372 |
mSearchView.onQueryRefine((CharSequence) tag); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
private CharSequence formatUrl(Context context, CharSequence url) { |
| 377 |
if (mUrlColor == null) { |
| 378 |
|
| 379 |
TypedValue colorValue = new TypedValue(); |
| 380 |
context.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true); |
| 381 |
mUrlColor = context.getColorStateList(colorValue.resourceId); |
| 382 |
} |
| 383 |
|
| 384 |
SpannableString text = new SpannableString(url); |
| 385 |
text.setSpan(new TextAppearanceSpan(null, 0, 0, mUrlColor, null), |
| 386 |
0, url.length(), |
| 387 |
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 388 |
return text; |
| 389 |
} |
| 390 |
|
| 391 |
private void setViewText(TextView v, CharSequence text) { |
| 392 |
|
| 393 |
v.setText(text); |
| 394 |
|
| 395 |
if (TextUtils.isEmpty(text)) { |
| 396 |
v.setVisibility(View.GONE); |
| 397 |
} else { |
| 398 |
v.setVisibility(View.VISIBLE); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
private Drawable getIcon1(Cursor cursor) { |
| 403 |
if (mIconName1Col == INVALID_INDEX) { |
| 404 |
return null; |
| 405 |
} |
| 406 |
String value = cursor.getString(mIconName1Col); |
| 407 |
Drawable drawable = getDrawableFromResourceValue(value); |
| 408 |
if (drawable != null) { |
| 409 |
return drawable; |
| 410 |
} |
| 411 |
return getDefaultIcon1(cursor); |
| 412 |
} |
| 413 |
|
| 414 |
private Drawable getIcon2(Cursor cursor) { |
| 415 |
if (mIconName2Col == INVALID_INDEX) { |
| 416 |
return null; |
| 417 |
} |
| 418 |
String value = cursor.getString(mIconName2Col); |
| 419 |
return getDrawableFromResourceValue(value); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
private void setViewDrawable(ImageView v, Drawable drawable, int nullVisibility) { |
| 427 |
|
| 428 |
|
| 429 |
v.setImageDrawable(drawable); |
| 430 |
|
| 431 |
if (drawable == null) { |
| 432 |
v.setVisibility(nullVisibility); |
| 433 |
} else { |
| 434 |
v.setVisibility(View.VISIBLE); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
drawable.setVisible(false, false); |
| 442 |
drawable.setVisible(true, false); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
@Override |
| 455 |
public CharSequence convertToString(Cursor cursor) { |
| 456 |
if (cursor == null) { |
| 457 |
return null; |
| 458 |
} |
| 459 |
|
| 460 |
String query = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_QUERY); |
| 461 |
if (query != null) { |
| 462 |
return query; |
| 463 |
} |
| 464 |
|
| 465 |
if (mSearchable.shouldRewriteQueryFromData()) { |
| 466 |
String data = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_INTENT_DATA); |
| 467 |
if (data != null) { |
| 468 |
return data; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
if (mSearchable.shouldRewriteQueryFromText()) { |
| 473 |
String text1 = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 474 |
if (text1 != null) { |
| 475 |
return text1; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
return null; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
@Override |
| 489 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 490 |
try { |
| 491 |
return super.getView(position, convertView, parent); |
| 492 |
} catch (RuntimeException e) { |
| 493 |
Log.w(LOG_TAG, "Search suggestions cursor threw exception.", e); |
| 494 |
|
| 495 |
View v = newView(mContext, mCursor, parent); |
| 496 |
if (v != null) { |
| 497 |
ChildViewCache views = (ChildViewCache) v.getTag(); |
| 498 |
TextView tv = views.mText1; |
| 499 |
tv.setText(e.toString()); |
| 500 |
} |
| 501 |
return v; |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
@Override |
| 512 |
public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 513 |
try { |
| 514 |
return super.getDropDownView(position, convertView, parent); |
| 515 |
} catch (RuntimeException e) { |
| 516 |
Log.w(LOG_TAG, "Search suggestions cursor threw exception.", e); |
| 517 |
|
| 518 |
final Context context = mDropDownContext == null ? mContext : mDropDownContext; |
| 519 |
final View v = newDropDownView(context, mCursor, parent); |
| 520 |
if (v != null) { |
| 521 |
final ChildViewCache views = (ChildViewCache) v.getTag(); |
| 522 |
final TextView tv = views.mText1; |
| 523 |
tv.setText(e.toString()); |
| 524 |
} |
| 525 |
return v; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
private Drawable getDrawableFromResourceValue(String drawableId) { |
| 549 |
if (drawableId == null || drawableId.length() == 0 || "0".equals(drawableId)) { |
| 550 |
return null; |
| 551 |
} |
| 552 |
try { |
| 553 |
|
| 554 |
int resourceId = Integer.parseInt(drawableId); |
| 555 |
|
| 556 |
String drawableUri = ContentResolver.SCHEME_ANDROID_RESOURCE |
| 557 |
+ "://" + mProviderContext.getPackageName() + "/" + resourceId; |
| 558 |
|
| 559 |
Drawable drawable = checkIconCache(drawableUri); |
| 560 |
if (drawable != null) { |
| 561 |
return drawable; |
| 562 |
} |
| 563 |
|
| 564 |
drawable = mProviderContext.getDrawable(resourceId); |
| 565 |
|
| 566 |
storeInIconCache(drawableUri, drawable); |
| 567 |
return drawable; |
| 568 |
} catch (NumberFormatException nfe) { |
| 569 |
|
| 570 |
Drawable drawable = checkIconCache(drawableId); |
| 571 |
if (drawable != null) { |
| 572 |
return drawable; |
| 573 |
} |
| 574 |
Uri uri = Uri.parse(drawableId); |
| 575 |
drawable = getDrawable(uri); |
| 576 |
storeInIconCache(drawableId, drawable); |
| 577 |
return drawable; |
| 578 |
} catch (Resources.NotFoundException nfe) { |
| 579 |
|
| 580 |
Log.w(LOG_TAG, "Icon resource not found: " + drawableId); |
| 581 |
return null; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
private Drawable getDrawable(Uri uri) { |
| 591 |
try { |
| 592 |
String scheme = uri.getScheme(); |
| 593 |
if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { |
| 594 |
|
| 595 |
OpenResourceIdResult r = |
| 596 |
mProviderContext.getContentResolver().getResourceId(uri); |
| 597 |
try { |
| 598 |
return r.r.getDrawable(r.id, mProviderContext.getTheme()); |
| 599 |
} catch (Resources.NotFoundException ex) { |
| 600 |
throw new FileNotFoundException("Resource does not exist: " + uri); |
| 601 |
} |
| 602 |
} else { |
| 603 |
|
| 604 |
InputStream stream = mProviderContext.getContentResolver().openInputStream(uri); |
| 605 |
if (stream == null) { |
| 606 |
throw new FileNotFoundException("Failed to open " + uri); |
| 607 |
} |
| 608 |
try { |
| 609 |
return Drawable.createFromStream(stream, null); |
| 610 |
} finally { |
| 611 |
try { |
| 612 |
stream.close(); |
| 613 |
} catch (IOException ex) { |
| 614 |
Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex); |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
} catch (FileNotFoundException fnfe) { |
| 619 |
Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage()); |
| 620 |
return null; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
private Drawable checkIconCache(String resourceUri) { |
| 625 |
Drawable.ConstantState cached = mOutsideDrawablesCache.get(resourceUri); |
| 626 |
if (cached == null) { |
| 627 |
return null; |
| 628 |
} |
| 629 |
if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + resourceUri); |
| 630 |
return cached.newDrawable(); |
| 631 |
} |
| 632 |
|
| 633 |
private void storeInIconCache(String resourceUri, Drawable drawable) { |
| 634 |
if (drawable != null) { |
| 635 |
mOutsideDrawablesCache.put(resourceUri, drawable.getConstantState()); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
private Drawable getDefaultIcon1(Cursor cursor) { |
| 647 |
|
| 648 |
Drawable drawable = getActivityIconWithCache(mSearchable.getSearchActivity()); |
| 649 |
if (drawable != null) { |
| 650 |
return drawable; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
return mContext.getPackageManager().getDefaultActivityIcon(); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
private Drawable getActivityIconWithCache(ComponentName component) { |
| 666 |
|
| 667 |
String componentIconKey = component.flattenToShortString(); |
| 668 |
|
| 669 |
if (mOutsideDrawablesCache.containsKey(componentIconKey)) { |
| 670 |
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey); |
| 671 |
return cached == null ? null : cached.newDrawable(mProviderContext.getResources()); |
| 672 |
} |
| 673 |
|
| 674 |
Drawable drawable = getActivityIcon(component); |
| 675 |
|
| 676 |
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState(); |
| 677 |
mOutsideDrawablesCache.put(componentIconKey, toCache); |
| 678 |
return drawable; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
private Drawable getActivityIcon(ComponentName component) { |
| 689 |
PackageManager pm = mContext.getPackageManager(); |
| 690 |
final ActivityInfo activityInfo; |
| 691 |
try { |
| 692 |
activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA); |
| 693 |
} catch (NameNotFoundException ex) { |
| 694 |
Log.w(LOG_TAG, ex.toString()); |
| 695 |
return null; |
| 696 |
} |
| 697 |
int iconId = activityInfo.getIconResource(); |
| 698 |
if (iconId == 0) return null; |
| 699 |
String pkg = component.getPackageName(); |
| 700 |
Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo); |
| 701 |
if (drawable == null) { |
| 702 |
Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " |
| 703 |
+ component.flattenToShortString()); |
| 704 |
return null; |
| 705 |
} |
| 706 |
return drawable; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
public static String getColumnString(Cursor cursor, String columnName) { |
| 718 |
int col = cursor.getColumnIndex(columnName); |
| 719 |
return getStringOrNull(cursor, col); |
| 720 |
} |
| 721 |
|
| 722 |
private static String getStringOrNull(Cursor cursor, int col) { |
| 723 |
if (col == INVALID_INDEX) { |
| 724 |
return null; |
| 725 |
} |
| 726 |
try { |
| 727 |
return cursor.getString(col); |
| 728 |
} catch (Exception e) { |
| 729 |
Log.e(LOG_TAG, |
| 730 |
"unexpected error retrieving valid column from cursor, " |
| 731 |
+ "did the remote process die?", e); |
| 732 |
return null; |
| 733 |
} |
| 734 |
} |
| 735 |
} |