diff options
| author | 2009-09-21 11:29:51 -0400 | |
|---|---|---|
| committer | 2009-09-21 11:29:51 -0400 | |
| commit | a953ed4bc08cf5fc98f17a399adbd8afdd8293d5 (patch) | |
| tree | 76efce1d75ee59b94f357ca74e3f1cefd76a1ac0 | |
| parent | 03c87bf51cdf5a105b53b5d6fbf9cfbc6c93751e (diff) | |
| parent | 6d318b4330a93bd407b298278ea1ab98ee98f449 (diff) | |
Merge change 26148 into eclair
* changes:
Use icon URI as icon cache key in search dialog
| -rw-r--r-- | core/java/android/app/SuggestionsAdapter.java | 126 |
1 files changed, 73 insertions, 53 deletions
diff --git a/core/java/android/app/SuggestionsAdapter.java b/core/java/android/app/SuggestionsAdapter.java index 691f4a780da4..9234a9c5f172 100644 --- a/core/java/android/app/SuggestionsAdapter.java +++ b/core/java/android/app/SuggestionsAdapter.java @@ -26,7 +26,6 @@ import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.Resources; import android.database.Cursor; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; @@ -48,7 +47,6 @@ import android.widget.TextView; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.List; import java.util.WeakHashMap; /** @@ -574,69 +572,91 @@ class SuggestionsAdapter extends ResourceCursorAdapter { if (drawableId == null || drawableId.length() == 0 || "0".equals(drawableId)) { return null; } - - // First, check the cache. - Drawable.ConstantState cached = mOutsideDrawablesCache.get(drawableId); - if (cached != null) { - if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + drawableId); - return cached.newDrawable(mProviderContext.getResources()); - } - - Drawable drawable = null; try { - // Not cached, try using it as a plain resource ID in the provider's context. + // First, see if it's just an integer int resourceId = Integer.parseInt(drawableId); + // It's an int, look for it in the cache + String drawableUri = ContentResolver.SCHEME_ANDROID_RESOURCE + + "://" + mProviderContext.getPackageName() + "/" + resourceId; + // Must use URI as cache key, since ints are app-specific + Drawable drawable = checkIconCache(drawableUri); + if (drawable != null) { + return drawable; + } + // Not cached, find it by resource ID drawable = mProviderContext.getResources().getDrawable(resourceId); + // Stick it in the cache, using the URI as key + storeInIconCache(drawableUri, drawable); + return drawable; } catch (NumberFormatException nfe) { - // The id was not an integer resource id, use it as a URI - try { - Uri uri = Uri.parse(drawableId); - String scheme = uri.getScheme(); - if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { - // Load drawables through Resources, to get the source density information - OpenResourceIdResult r = - mProviderContext.getContentResolver().getResourceId(uri); - try { - drawable = r.r.getDrawable(r.id); - } catch (Resources.NotFoundException ex) { - throw new FileNotFoundException("Resource does not exist: " + uri); - } - } else { - // Let the ContentResolver handle content and file URIs. - InputStream stream = mProviderContext.getContentResolver().openInputStream(uri); - if (stream == null) { - throw new FileNotFoundException("Failed to open " + uri); - } - try { - drawable = Drawable.createFromStream(stream, null); - } finally { - try { - stream.close(); - } catch (IOException ex) { - Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex); - } - } - } - } catch (FileNotFoundException fnfe) { - Log.w(LOG_TAG, "Icon not found: " + drawableId + ", " + fnfe.getMessage()); - // drawable = null; + // It's not an integer, use it as a URI + Drawable drawable = checkIconCache(drawableId); + if (drawable != null) { + return drawable; } + Uri uri = Uri.parse(drawableId); + drawable = getDrawable(uri); + storeInIconCache(drawableId, drawable); + return drawable; } catch (Resources.NotFoundException nfe) { + // It was an integer, but it couldn't be found, bail out Log.w(LOG_TAG, "Icon resource not found: " + drawableId); - // drawable = null; + return null; } + } - if (drawable == null) { - if (DBG) Log.d(LOG_TAG, "Didn't find icon: " + drawableId); - } else { - if (DBG) { - Log.d(LOG_TAG, "Found icon: " + drawableId); + /** + * Gets a drawable by URI, without using the cache. + * + * @return A drawable, or {@code null} if the drawable could not be loaded. + */ + private Drawable getDrawable(Uri uri) { + try { + String scheme = uri.getScheme(); + if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) { + // Load drawables through Resources, to get the source density information + OpenResourceIdResult r = + mProviderContext.getContentResolver().getResourceId(uri); + try { + return r.r.getDrawable(r.id); + } catch (Resources.NotFoundException ex) { + throw new FileNotFoundException("Resource does not exist: " + uri); + } + } else { + // Let the ContentResolver handle content and file URIs. + InputStream stream = mProviderContext.getContentResolver().openInputStream(uri); + if (stream == null) { + throw new FileNotFoundException("Failed to open " + uri); + } + try { + return Drawable.createFromStream(stream, null); + } finally { + try { + stream.close(); + } catch (IOException ex) { + Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex); + } + } } - // Cache it so we don't do this lookup again - mOutsideDrawablesCache.put(drawableId, drawable.getConstantState()); + } catch (FileNotFoundException fnfe) { + Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage()); + return null; } + } - return drawable; + private Drawable checkIconCache(String resourceUri) { + Drawable.ConstantState cached = mOutsideDrawablesCache.get(resourceUri); + if (cached == null) { + return null; + } + if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + resourceUri); + return cached.newDrawable(); + } + + private void storeInIconCache(String resourceUri, Drawable drawable) { + if (drawable != null) { + mOutsideDrawablesCache.put(resourceUri, drawable.getConstantState()); + } } /** |