Skip to content

Commit 7797cae

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "Cache drawable constant states instead of drawables Bug #5678369" into ics-mr1
2 parents e8ba2ab + 39fe17c commit 7797cae

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

core/java/android/app/ApplicationPackageManager.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public List<InstrumentationInfo> queryInstrumentation(
625625
return info.activityInfo.loadIcon(this);
626626
}
627627

628-
throw new NameNotFoundException(intent.toURI());
628+
throw new NameNotFoundException(intent.toUri(0));
629629
}
630630

631631
@Override public Drawable getDefaultActivityIcon() {
@@ -728,15 +728,22 @@ static void configurationChanged() {
728728

729729
private Drawable getCachedIcon(ResourceName name) {
730730
synchronized (sSync) {
731-
WeakReference<Drawable> wr = sIconCache.get(name);
731+
WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
732732
if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
733733
+ name + ": " + wr);
734734
if (wr != null) { // we have the activity
735-
Drawable dr = wr.get();
736-
if (dr != null) {
737-
if (DEBUG_ICONS) Log.v(TAG, "Get cached drawable for "
738-
+ name + ": " + dr);
739-
return dr;
735+
Drawable.ConstantState state = wr.get();
736+
if (state != null) {
737+
if (DEBUG_ICONS) {
738+
Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
739+
}
740+
// Note: It's okay here to not use the newDrawable(Resources) variant
741+
// of the API. The ConstantState comes from a drawable that was
742+
// originally created by passing the proper app Resources instance
743+
// which means the state should already contain the proper
744+
// resources specific information (like density.) See
745+
// BitmapDrawable.BitmapState for instance.
746+
return state.newDrawable();
740747
}
741748
// our entry has been purged
742749
sIconCache.remove(name);
@@ -747,14 +754,12 @@ private Drawable getCachedIcon(ResourceName name) {
747754

748755
private void putCachedIcon(ResourceName name, Drawable dr) {
749756
synchronized (sSync) {
750-
sIconCache.put(name, new WeakReference<Drawable>(dr));
751-
if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable for "
752-
+ name + ": " + dr);
757+
sIconCache.put(name, new WeakReference<Drawable.ConstantState>(dr.getConstantState()));
758+
if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
753759
}
754760
}
755761

756-
static final void handlePackageBroadcast(int cmd, String[] pkgList,
757-
boolean hasPkgInfo) {
762+
static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
758763
boolean immediateGc = false;
759764
if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
760765
immediateGc = true;
@@ -1226,8 +1231,8 @@ public VerifierDeviceIdentity getVerifierDeviceIdentity() {
12261231
private final IPackageManager mPM;
12271232

12281233
private static final Object sSync = new Object();
1229-
private static HashMap<ResourceName, WeakReference<Drawable> > sIconCache
1230-
= new HashMap<ResourceName, WeakReference<Drawable> >();
1231-
private static HashMap<ResourceName, WeakReference<CharSequence> > sStringCache
1232-
= new HashMap<ResourceName, WeakReference<CharSequence> >();
1234+
private static HashMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
1235+
= new HashMap<ResourceName, WeakReference<Drawable.ConstantState>>();
1236+
private static HashMap<ResourceName, WeakReference<CharSequence>> sStringCache
1237+
= new HashMap<ResourceName, WeakReference<CharSequence>>();
12331238
}

0 commit comments

Comments
 (0)