Remove a local copy of a context object from StyleManager.
StyleManager was caching a context object received from the
instantiating object. This is bad, as it can lead to memory leaks. Now
all calls onto StyleManager that will need a context require the context
to be passed into the method.
diff --git a/src/com/android/contacts/StyleManager.java b/src/com/android/contacts/StyleManager.java
index 92c4797..d14b1c5 100644
--- a/src/com/android/contacts/StyleManager.java
+++ b/src/com/android/contacts/StyleManager.java
@@ -55,13 +55,10 @@
private static final String TAG_ICON_DEFAULT = "icon-default";
private static final String KEY_JOIN_CHAR = "|";
- private Context mContext;
-
private StyleManager(Context context) {
mIconCache = new WeakHashMap<String, Bitmap>();
mStyleSetCache = new HashMap<String, StyleSet>();
- mContext = context;
- registerIntentReceivers();
+ registerIntentReceivers(context);
}
/**
@@ -78,12 +75,12 @@
return sInstance;
}
- private void registerIntentReceivers() {
+ private void registerIntentReceivers(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
- mContext.registerReceiver(this, filter);
+ context.registerReceiver(this, filter);
}
@Override
@@ -119,8 +116,8 @@
* @param packageName
* @return Bitmap holding the default icon.
*/
- public Bitmap getDefaultIcon(String packageName) {
- return getMimetypeIcon(packageName, DEFAULT_MIMETYPE);
+ public Bitmap getDefaultIcon(Context context, String packageName) {
+ return getMimetypeIcon(context, packageName, DEFAULT_MIMETYPE);
}
/**
@@ -130,7 +127,7 @@
* @param packageName
* @return Bitmap holding the default icon.
*/
- public Bitmap getMimetypeIcon(String packageName, String mimetype) {
+ public Bitmap getMimetypeIcon(Context context, String packageName, String mimetype) {
String key = getKey(packageName, mimetype);
synchronized(mIconCache) {
@@ -139,20 +136,20 @@
// loadIcon() may return null, which is fine since, if no icon was found we want to
// store a null value so we know not to look next time.
- mIconCache.put(key, loadIcon(packageName, mimetype));
+ mIconCache.put(key, loadIcon(context, packageName, mimetype));
}
return mIconCache.get(key);
}
}
- private Bitmap loadIcon(String packageName, String mimetype) {
+ private Bitmap loadIcon(Context context, String packageName, String mimetype) {
StyleSet ss = null;
synchronized(mStyleSetCache) {
if (!mStyleSetCache.containsKey(packageName)) {
// Cache miss
try {
- StyleSet inflated = inflateStyleSet(packageName);
+ StyleSet inflated = inflateStyleSet(context, packageName);
mStyleSetCache.put(packageName, inflated);
} catch (InflateException e) {
// If inflation failed keep a null entry so we know not to try again.
@@ -172,12 +169,12 @@
return null;
}
- return BitmapFactory.decodeResource(mContext.getResources(),
+ return BitmapFactory.decodeResource(context.getResources(),
iconRes, null);
}
- private StyleSet inflateStyleSet(String packageName) throws InflateException {
- final PackageManager pm = mContext.getPackageManager();
+ private StyleSet inflateStyleSet(Context context, String packageName) throws InflateException {
+ final PackageManager pm = context.getPackageManager();
final ApplicationInfo ai;
try {
@@ -221,14 +218,14 @@
String mimetype;
if (TAG_ICON.equals(parser.getName())) {
- a = mContext.obtainStyledAttributes(attrs, android.R.styleable.Icon);
+ a = context.obtainStyledAttributes(attrs, android.R.styleable.Icon);
mimetype = a.getString(com.android.internal.R.styleable.Icon_mimeType);
if (mimetype != null) {
styleSet.addIcon(mimetype,
a.getResourceId(com.android.internal.R.styleable.Icon_icon, -1));
}
} else if (TAG_ICON_DEFAULT.equals(parser.getName())) {
- a = mContext.obtainStyledAttributes(attrs, android.R.styleable.IconDefault);
+ a = context.obtainStyledAttributes(attrs, android.R.styleable.IconDefault);
styleSet.addIcon(DEFAULT_MIMETYPE,
a.getResourceId(
com.android.internal.R.styleable.IconDefault_icon, -1));
diff --git a/tests/src/com/android/contacts/StyleManagerTests.java b/tests/src/com/android/contacts/StyleManagerTests.java
index 48e7cba..aaf54be 100644
--- a/tests/src/com/android/contacts/StyleManagerTests.java
+++ b/tests/src/com/android/contacts/StyleManagerTests.java
@@ -51,7 +51,7 @@
}
public void testGetMimetypeIcon() {
- Bitmap phoneIconFromSm = mStyleManager.getMimetypeIcon(PACKAGE_NAME, PHONE_MIMETYPE);
+ Bitmap phoneIconFromSm = mStyleManager.getMimetypeIcon(mContext, PACKAGE_NAME, PHONE_MIMETYPE);
int smHeight = phoneIconFromSm.getHeight();
int smWidth = phoneIconFromSm.getWidth();
@@ -70,14 +70,14 @@
}
public void testGetMissingMimetypeIcon() {
- Bitmap postalIconFromSm = mStyleManager.getMimetypeIcon(PACKAGE_NAME,
+ Bitmap postalIconFromSm = mStyleManager.getMimetypeIcon(mContext, PACKAGE_NAME,
"vnd.android.cursor.item/postal-address");
assertNull(postalIconFromSm);
}
public void testGetDefaultIcon() {
- Bitmap defaultIconFromSm = mStyleManager.getDefaultIcon(PACKAGE_NAME);
+ Bitmap defaultIconFromSm = mStyleManager.getDefaultIcon(mContext, PACKAGE_NAME);
int smHeight = defaultIconFromSm.getHeight();
int smWidth = defaultIconFromSm.getWidth();
@@ -103,14 +103,14 @@
assertTrue(mStyleManager.getStyleSetCacheSize() == 0);
// Getting the icon should add it to the cache.
- mStyleManager.getDefaultIcon(PACKAGE_NAME);
+ mStyleManager.getDefaultIcon(mContext, PACKAGE_NAME);
assertTrue(mStyleManager.getIconCacheSize() == 1);
assertTrue(mStyleManager.getStyleSetCacheSize() == 1);
assertTrue(mStyleManager.isIconCacheHit(PACKAGE_NAME, StyleManager.DEFAULT_MIMETYPE));
assertFalse(mStyleManager.isIconCacheHit(PACKAGE_NAME, PHONE_MIMETYPE));
assertTrue(mStyleManager.isStyleSetCacheHit(PACKAGE_NAME));
- mStyleManager.getMimetypeIcon(PACKAGE_NAME, PHONE_MIMETYPE);
+ mStyleManager.getMimetypeIcon(mContext, PACKAGE_NAME, PHONE_MIMETYPE);
assertTrue(mStyleManager.getIconCacheSize() == 2);
assertTrue(mStyleManager.getStyleSetCacheSize() == 1);
assertTrue(mStyleManager.isIconCacheHit(PACKAGE_NAME, StyleManager.DEFAULT_MIMETYPE));