diff options
author | Bjorn Bringert <bringert@android.com> | 2009-07-14 10:21:47 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-07-14 10:21:47 -0700 |
commit | cc916aa6918c6c8addc5426c2512b087f83ad831 (patch) | |
tree | 885c29918558ca0278b92b8485b8cac64cad7f34 /core/java | |
parent | 6b316a7ff0ca90ce012226ac9f2011e9a3be2d54 (diff) | |
parent | c1d82e68eb4aae1b575e82b08e71a87670cc1532 (diff) | |
download | frameworks_base-cc916aa6918c6c8addc5426c2512b087f83ad831.zip frameworks_base-cc916aa6918c6c8addc5426c2512b087f83ad831.tar.gz frameworks_base-cc916aa6918c6c8addc5426c2512b087f83ad831.tar.bz2 |
am c1d82e68: Use activity icon when search suggestion icon is missing
Merge commit 'c1d82e68eb4aae1b575e82b08e71a87670cc1532'
* commit 'c1d82e68eb4aae1b575e82b08e71a87670cc1532':
Use activity icon when search suggestion icon is missing
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/android/app/SuggestionsAdapter.java | 127 |
1 files changed, 116 insertions, 11 deletions
diff --git a/core/java/android/app/SuggestionsAdapter.java b/core/java/android/app/SuggestionsAdapter.java index c8e952f..58e66b6 100644 --- a/core/java/android/app/SuggestionsAdapter.java +++ b/core/java/android/app/SuggestionsAdapter.java @@ -16,8 +16,13 @@ package android.app; +import android.app.SearchManager.DialogCursorProtocol; +import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.ColorStateList; import android.content.res.Resources; import android.database.Cursor; @@ -38,8 +43,6 @@ import android.widget.ImageView; import android.widget.ResourceCursorAdapter; import android.widget.TextView; -import static android.app.SearchManager.DialogCursorProtocol; - import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -310,19 +313,39 @@ class SuggestionsAdapter extends ResourceCursorAdapter { text2 = cursor.getString(mText2Col); } ((SuggestionItemView)view).setTextStrings(text1, text2, isHtml, mProviderContext); - setViewIcon(cursor, views.mIcon1, mIconName1Col); - setViewIcon(cursor, views.mIcon2, mIconName2Col); + if (views.mIcon1 != null) { + setViewDrawable(views.mIcon1, getIcon1(cursor)); + } + if (views.mIcon2 != null) { + setViewDrawable(views.mIcon2, getIcon2(cursor)); + } } - private void setViewIcon(Cursor cursor, ImageView v, int iconNameCol) { - if (v == null) { - return; - } - if (iconNameCol < 0) { - return; + private Drawable getIcon1(Cursor cursor) { + if (mIconName1Col < 0) { + return null; } - String value = cursor.getString(iconNameCol); + String value = cursor.getString(mIconName1Col); Drawable drawable = getDrawableFromResourceValue(value); + if (drawable != null) { + return drawable; + } + return getDefaultIcon1(cursor); + } + + private Drawable getIcon2(Cursor cursor) { + if (mIconName2Col < 0) { + return null; + } + String value = cursor.getString(mIconName2Col); + return getDrawableFromResourceValue(value); + } + + /** + * Sets the drawable in an image view, makes sure the view is only visible if there + * is a drawable. + */ + private void setViewDrawable(ImageView v, Drawable drawable) { // Set the icon even if the drawable is null, since we need to clear any // previous icon. v.setImageDrawable(drawable); @@ -474,6 +497,88 @@ class SuggestionsAdapter extends ResourceCursorAdapter { } /** + * Gets the left-hand side icon that will be used for the current suggestion + * if the suggestion contains an icon column but no icon or a broken icon. + * + * @param cursor A cursor positioned at the current suggestion. + * @return A non-null drawable. + */ + private Drawable getDefaultIcon1(Cursor cursor) { + // First check the component that the suggestion is originally from + String c = getColumnString(cursor, SearchManager.SUGGEST_COLUMN_INTENT_COMPONENT_NAME); + if (c != null) { + ComponentName component = ComponentName.unflattenFromString(c); + if (component != null) { + Drawable drawable = getActivityIconWithCache(component); + if (drawable != null) { + return drawable; + } + } else { + Log.w(LOG_TAG, "Bad component name: " + c); + } + } + + // Then check the component that gave us the suggestion + Drawable drawable = getActivityIconWithCache(mSearchable.getSearchActivity()); + if (drawable != null) { + return drawable; + } + + // Fall back to a default icon + return mContext.getPackageManager().getDefaultActivityIcon(); + } + + /** + * Gets the activity or application icon for an activity. + * Uses the local icon cache for fast repeated lookups. + * + * @param component Name of an activity. + * @return A drawable, or {@code null} if neither the activity nor the application + * has an icon set. + */ + private Drawable getActivityIconWithCache(ComponentName component) { + // First check the icon cache + String componentIconKey = component.flattenToShortString(); + // Using containsKey() since we also store null values. + if (mOutsideDrawablesCache.containsKey(componentIconKey)) { + return mOutsideDrawablesCache.get(componentIconKey); + } + // Then try the activity or application icon + Drawable drawable = getActivityIcon(component); + // Stick it in the cache so we don't do this lookup again. + mOutsideDrawablesCache.put(componentIconKey, drawable); + return drawable; + } + + /** + * Gets the activity or application icon for an activity. + * + * @param component Name of an activity. + * @return A drawable, or {@code null} if neither the acitivy or the application + * have an icon set. + */ + private Drawable getActivityIcon(ComponentName component) { + PackageManager pm = mContext.getPackageManager(); + final ActivityInfo activityInfo; + try { + activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA); + } catch (NameNotFoundException ex) { + Log.w(LOG_TAG, ex.toString()); + return null; + } + int iconId = activityInfo.getIconResource(); + if (iconId == 0) return null; + String pkg = component.getPackageName(); + Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo); + if (drawable == null) { + Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " + + component.flattenToShortString()); + return null; + } + return drawable; + } + + /** * Gets the value of a string column by name. * * @param cursor Cursor to read the value from. |