Prevent NPE in Launcher when an icon cannot be found.

Bug #2509023

Change-Id: I053c7c9a37ed4aeb4d78a9f62dfdeea09a3959aa
This commit is contained in:
Romain Guy 2010-03-15 14:44:42 -07:00
parent 552a608e3c
commit a28fd3fa7c
2 changed files with 37 additions and 24 deletions

View File

@ -24,9 +24,17 @@ import android.graphics.ColorFilter;
class FastBitmapDrawable extends Drawable { class FastBitmapDrawable extends Drawable {
private Bitmap mBitmap; private Bitmap mBitmap;
private int mWidth;
private int mHeight;
FastBitmapDrawable(Bitmap b) { FastBitmapDrawable(Bitmap b) {
mBitmap = b; mBitmap = b;
if (b != null) {
mWidth = mBitmap.getWidth();
mHeight = mBitmap.getHeight();
} else {
mWidth = mHeight = 0;
}
} }
@Override @Override
@ -49,26 +57,32 @@ class FastBitmapDrawable extends Drawable {
@Override @Override
public int getIntrinsicWidth() { public int getIntrinsicWidth() {
return mBitmap.getWidth(); return mWidth;
} }
@Override @Override
public int getIntrinsicHeight() { public int getIntrinsicHeight() {
return mBitmap.getHeight(); return mHeight;
} }
@Override @Override
public int getMinimumWidth() { public int getMinimumWidth() {
return mBitmap.getWidth(); return mWidth;
} }
@Override @Override
public int getMinimumHeight() { public int getMinimumHeight() {
return mBitmap.getHeight(); return mHeight;
} }
public void setBitmap(Bitmap b) { public void setBitmap(Bitmap b) {
mBitmap = b; mBitmap = b;
if (b != null) {
mWidth = mBitmap.getWidth();
mHeight = mBitmap.getHeight();
} else {
mWidth = mHeight = 0;
}
} }
public Bitmap getBitmap() { public Bitmap getBitmap() {

View File

@ -17,27 +17,14 @@
package com.android.launcher2; package com.android.launcher2;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent; import android.content.Intent;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.Canvas;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.util.Log;
import android.os.Process;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
/** /**
* Cache of application icons. Icons can be made from any thread. * Cache of application icons. Icons can be made from any thread.
@ -53,9 +40,10 @@ public class IconCache {
public Bitmap titleBitmap; public Bitmap titleBitmap;
} }
private LauncherApplication mContext; private final Bitmap mDefaultIcon;
private PackageManager mPackageManager; private final LauncherApplication mContext;
private Utilities.BubbleText mBubble; private final PackageManager mPackageManager;
private final Utilities.BubbleText mBubble;
private final HashMap<ComponentName, CacheEntry> mCache = private final HashMap<ComponentName, CacheEntry> mCache =
new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
@ -63,6 +51,18 @@ public class IconCache {
mContext = context; mContext = context;
mPackageManager = context.getPackageManager(); mPackageManager = context.getPackageManager();
mBubble = new Utilities.BubbleText(context); mBubble = new Utilities.BubbleText(context);
mDefaultIcon = makeDefaultIcon();
}
private Bitmap makeDefaultIcon() {
Drawable d = mPackageManager.getDefaultActivityIcon();
Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
Math.max(d.getIntrinsicHeight(), 1),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
d.setBounds(0, 0, b.getWidth(), b.getHeight());
d.draw(c);
return b;
} }
/** /**
@ -90,7 +90,7 @@ public class IconCache {
synchronized (mCache) { synchronized (mCache) {
CacheEntry entry = cacheLocked(application.componentName, info); CacheEntry entry = cacheLocked(application.componentName, info);
if (entry.titleBitmap == null) { if (entry.titleBitmap == null) {
entry.titleBitmap = mBubble.createTextBitmap(entry.title.toString()); entry.titleBitmap = mBubble.createTextBitmap(entry.title);
} }
application.title = entry.title; application.title = entry.title;
@ -104,7 +104,7 @@ public class IconCache {
ComponentName component = intent.getComponent(); ComponentName component = intent.getComponent();
if (resolveInfo == null || component == null) { if (resolveInfo == null || component == null) {
return null; return mDefaultIcon;
} }
CacheEntry entry = cacheLocked(component, resolveInfo); CacheEntry entry = cacheLocked(component, resolveInfo);
@ -137,4 +137,3 @@ public class IconCache {
return entry; return entry;
} }
} }