Fix app icons on some Settings screens

App icons appeared as the default icon and sometimes without the corp badge on
Data Usage and Battery screens for applications that were only installed for
the managed profile. This CL fixes the issue.

Bug:16705204
Change-Id: I778d36554feb19f28f3cb9321a291cab3d3e17bb
This commit is contained in:
Zoltan Szatmary-Ban
2014-08-07 14:02:41 +01:00
parent f8be13d8ca
commit 3aaf0eb457
4 changed files with 68 additions and 21 deletions

View File

@@ -16,15 +16,20 @@
package com.android.settings.fuelgauge; package com.android.settings.fuelgauge;
import android.app.AppGlobals;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.BatteryStats; import android.os.BatteryStats;
import android.os.Handler; import android.os.Handler;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.util.Log;
import com.android.internal.os.BatterySipper; import com.android.internal.os.BatterySipper;
import com.android.settings.R; import com.android.settings.R;
@@ -256,9 +261,17 @@ public class BatteryEntry {
System.arraycopy(sipper.mPackages, 0, packageLabels, 0, sipper.mPackages.length); System.arraycopy(sipper.mPackages, 0, packageLabels, 0, sipper.mPackages.length);
// Convert package names to user-facing labels where possible // Convert package names to user-facing labels where possible
IPackageManager ipm = AppGlobals.getPackageManager();
final int userId = UserHandle.getUserId(uid);
for (int i = 0; i < packageLabels.length; i++) { for (int i = 0; i < packageLabels.length; i++) {
try { try {
ApplicationInfo ai = pm.getApplicationInfo(packageLabels[i], 0); final ApplicationInfo ai = ipm.getApplicationInfo(packageLabels[i],
0 /* no flags */, userId);
if (ai == null) {
Log.d(PowerUsageSummary.TAG, "Retrieving null app info for package "
+ packageLabels[i] + ", user " + userId);
continue;
}
CharSequence label = ai.loadLabel(pm); CharSequence label = ai.loadLabel(pm);
if (label != null) { if (label != null) {
packageLabels[i] = label.toString(); packageLabels[i] = label.toString();
@@ -268,10 +281,14 @@ public class BatteryEntry {
icon = ai.loadIcon(pm); icon = ai.loadIcon(pm);
break; break;
} }
} catch (PackageManager.NameNotFoundException e) { } catch (RemoteException e) {
Log.d(PowerUsageSummary.TAG, "Error while retrieving app info for package "
+ packageLabels[i] + ", user " + userId, e);
} }
} }
if (icon == null) icon = defaultActivityIcon; if (icon == null) {
icon = defaultActivityIcon;
}
if (packageLabels.length == 1) { if (packageLabels.length == 1) {
name = packageLabels[0]; name = packageLabels[0];
@@ -279,7 +296,12 @@ public class BatteryEntry {
// Look for an official name for this UID. // Look for an official name for this UID.
for (String pkgName : sipper.mPackages) { for (String pkgName : sipper.mPackages) {
try { try {
final PackageInfo pi = pm.getPackageInfo(pkgName, 0); final PackageInfo pi = ipm.getPackageInfo(pkgName, 0 /* no flags */, userId);
if (pi == null) {
Log.d(PowerUsageSummary.TAG, "Retrieving null package info for package "
+ pkgName + ", user " + userId);
continue;
}
if (pi.sharedUserLabel != 0) { if (pi.sharedUserLabel != 0) {
final CharSequence nm = pm.getText(pkgName, final CharSequence nm = pm.getText(pkgName,
pi.sharedUserLabel, pi.applicationInfo); pi.sharedUserLabel, pi.applicationInfo);
@@ -292,7 +314,9 @@ public class BatteryEntry {
break; break;
} }
} }
} catch (PackageManager.NameNotFoundException e) { } catch (RemoteException e) {
Log.d(PowerUsageSummary.TAG, "Error while retrieving package info for package "
+ pkgName + ", user " + userId, e);
} }
} }
} }

View File

@@ -54,7 +54,7 @@ public class PowerUsageSummary extends PreferenceFragment {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final String TAG = "PowerUsageSummary"; static final String TAG = "PowerUsageSummary";
private static final String KEY_APP_LIST = "app_list"; private static final String KEY_APP_LIST = "app_list";
@@ -293,7 +293,7 @@ public class PowerUsageSummary extends PreferenceFragment {
} }
final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid())); final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper); final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
final Drawable badgedIcon = mUm.getBadgedDrawableForUser(entry.getIcon(), final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
userHandle); userHandle);
final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(), final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
userHandle); userHandle);
@@ -337,7 +337,7 @@ public class PowerUsageSummary extends PreferenceFragment {
if (pgp != null) { if (pgp != null) {
final int userId = UserHandle.getUserId(entry.sipper.getUid()); final int userId = UserHandle.getUserId(entry.sipper.getUid());
final UserHandle userHandle = new UserHandle(userId); final UserHandle userHandle = new UserHandle(userId);
pgp.setIcon(mUm.getBadgedDrawableForUser(entry.getIcon(), userHandle)); pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
pgp.setTitle(entry.name); pgp.setTitle(entry.name);
} }
break; break;

View File

@@ -203,6 +203,11 @@ public class RecentLocationApps {
IPackageManager ipm = AppGlobals.getPackageManager(); IPackageManager ipm = AppGlobals.getPackageManager();
ApplicationInfo appInfo = ApplicationInfo appInfo =
ipm.getApplicationInfo(packageName, PackageManager.GET_META_DATA, userId); ipm.getApplicationInfo(packageName, PackageManager.GET_META_DATA, userId);
if (appInfo == null) {
Log.w(TAG, "Null application info retrieved for package " + packageName
+ ", userId " + userId);
return null;
}
Resources res = mActivity.getResources(); Resources res = mActivity.getResources();
final UserHandle userHandle = new UserHandle(userId); final UserHandle userHandle = new UserHandle(userId);
@@ -214,7 +219,8 @@ public class RecentLocationApps {
appLabel, highBattery, badgedAppLabel, appLabel, highBattery, badgedAppLabel,
new PackageEntryClickedListener(packageName)); new PackageEntryClickedListener(packageName));
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Error while retrieving application info", e); Log.w(TAG, "Error while retrieving application info for package " + packageName
+ ", userId " + userId, e);
} }
return preference; return preference;

View File

@@ -16,8 +16,10 @@
package com.android.settings.net; package com.android.settings.net;
import android.app.AppGlobals;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
@@ -28,7 +30,9 @@ import android.net.ConnectivityManager;
import android.net.TrafficStats; import android.net.TrafficStats;
import android.os.UserManager; import android.os.UserManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.RemoteException;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import com.android.settings.R; import com.android.settings.R;
@@ -39,6 +43,7 @@ import com.android.settings.Utils;
* {@link TrafficStats#UID_TETHERING} and {@link UserInfo}. * {@link TrafficStats#UID_TETHERING} and {@link UserInfo}.
*/ */
public class UidDetailProvider { public class UidDetailProvider {
private static final String TAG = "DataUsage";
private final Context mContext; private final Context mContext;
private final SparseArray<UidDetail> mUidDetailCache; private final SparseArray<UidDetail> mUidDetailCache;
@@ -148,31 +153,43 @@ public class UidDetailProvider {
final String[] packageNames = pm.getPackagesForUid(uid); final String[] packageNames = pm.getPackagesForUid(uid);
final int length = packageNames != null ? packageNames.length : 0; final int length = packageNames != null ? packageNames.length : 0;
try { try {
final UserHandle userHandle = new UserHandle(UserHandle.getUserId(uid)); final int userId = UserHandle.getUserId(uid);
UserHandle userHandle = new UserHandle(userId);
IPackageManager ipm = AppGlobals.getPackageManager();
if (length == 1) { if (length == 1) {
final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0); final ApplicationInfo info = ipm.getApplicationInfo(packageNames[0],
0 /* no flags */, userId);
if (info != null) {
detail.label = info.loadLabel(pm).toString(); detail.label = info.loadLabel(pm).toString();
detail.icon = um.getBadgedDrawableForUser(info.loadIcon(pm), userHandle); detail.icon = um.getBadgedIconForUser(info.loadIcon(pm),
new UserHandle(userId));
}
} else if (length > 1) { } else if (length > 1) {
detail.detailLabels = new CharSequence[length]; detail.detailLabels = new CharSequence[length];
detail.detailContentDescriptions = new CharSequence[length]; detail.detailContentDescriptions = new CharSequence[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
final String packageName = packageNames[i]; final String packageName = packageNames[i];
final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0); final ApplicationInfo appInfo = ipm.getApplicationInfo(packageName,
0 /* no flags */, userId);
if (appInfo != null) {
detail.detailLabels[i] = appInfo.loadLabel(pm).toString(); detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
detail.detailContentDescriptions[i] = um.getBadgedLabelForUser( detail.detailContentDescriptions[i] = um.getBadgedLabelForUser(
detail.detailLabels[i], userHandle); detail.detailLabels[i], userHandle);
if (packageInfo.sharedUserLabel != 0) { if (packageInfo.sharedUserLabel != 0) {
detail.label = pm.getText(packageName, packageInfo.sharedUserLabel, detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
packageInfo.applicationInfo).toString(); packageInfo.applicationInfo).toString();
detail.icon = um.getBadgedDrawableForUser(appInfo.loadIcon(pm), userHandle); detail.icon = um.getBadgedIconForUser(appInfo.loadIcon(pm), userHandle);
}
} }
} }
} }
detail.contentDescription = um.getBadgedLabelForUser(detail.label, userHandle); detail.contentDescription = um.getBadgedLabelForUser(detail.label, userHandle);
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
Log.w(TAG, "Error while building UI detail for uid "+uid, e);
} catch (RemoteException e) {
Log.w(TAG, "Error while building UI detail for uid "+uid, e);
} }
if (TextUtils.isEmpty(detail.label)) { if (TextUtils.isEmpty(detail.label)) {