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
diff --git a/src/com/android/settings/fuelgauge/BatteryEntry.java b/src/com/android/settings/fuelgauge/BatteryEntry.java
index 92eaade..4ff4dfd 100644
--- a/src/com/android/settings/fuelgauge/BatteryEntry.java
+++ b/src/com/android/settings/fuelgauge/BatteryEntry.java
@@ -16,15 +16,20 @@
 
 package com.android.settings.fuelgauge;
 
+import android.app.AppGlobals;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
 import android.graphics.drawable.Drawable;
 import android.os.BatteryStats;
 import android.os.Handler;
+import android.os.RemoteException;
+import android.os.UserHandle;
 import android.os.UserManager;
+import android.util.Log;
 
 import com.android.internal.os.BatterySipper;
 import com.android.settings.R;
@@ -256,9 +261,17 @@
         System.arraycopy(sipper.mPackages, 0, packageLabels, 0, sipper.mPackages.length);
 
         // 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++) {
             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);
                 if (label != null) {
                     packageLabels[i] = label.toString();
@@ -268,10 +281,14 @@
                     icon = ai.loadIcon(pm);
                     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) {
             name = packageLabels[0];
@@ -279,7 +296,12 @@
             // Look for an official name for this UID.
             for (String pkgName : sipper.mPackages) {
                 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) {
                         final CharSequence nm = pm.getText(pkgName,
                                 pi.sharedUserLabel, pi.applicationInfo);
@@ -292,7 +314,9 @@
                             break;
                         }
                     }
-                } catch (PackageManager.NameNotFoundException e) {
+                } catch (RemoteException e) {
+                    Log.d(PowerUsageSummary.TAG, "Error while retrieving package info for package "
+                            + pkgName + ", user " + userId, e);
                 }
             }
         }
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index ad8afa5..b26ba65 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -54,7 +54,7 @@
 
     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";
 
@@ -293,7 +293,7 @@
                 }
                 final UserHandle userHandle = new UserHandle(UserHandle.getUserId(sipper.getUid()));
                 final BatteryEntry entry = new BatteryEntry(getActivity(), mHandler, mUm, sipper);
-                final Drawable badgedIcon = mUm.getBadgedDrawableForUser(entry.getIcon(),
+                final Drawable badgedIcon = mUm.getBadgedIconForUser(entry.getIcon(),
                         userHandle);
                 final CharSequence contentDescription = mUm.getBadgedLabelForUser(entry.getLabel(),
                         userHandle);
@@ -337,7 +337,7 @@
                     if (pgp != null) {
                         final int userId = UserHandle.getUserId(entry.sipper.getUid());
                         final UserHandle userHandle = new UserHandle(userId);
-                        pgp.setIcon(mUm.getBadgedDrawableForUser(entry.getIcon(), userHandle));
+                        pgp.setIcon(mUm.getBadgedIconForUser(entry.getIcon(), userHandle));
                         pgp.setTitle(entry.name);
                     }
                     break;
diff --git a/src/com/android/settings/location/RecentLocationApps.java b/src/com/android/settings/location/RecentLocationApps.java
index 13d282d..7c59927 100644
--- a/src/com/android/settings/location/RecentLocationApps.java
+++ b/src/com/android/settings/location/RecentLocationApps.java
@@ -203,6 +203,11 @@
             IPackageManager ipm = AppGlobals.getPackageManager();
             ApplicationInfo appInfo =
                     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();
 
             final UserHandle userHandle = new UserHandle(userId);
@@ -214,7 +219,8 @@
                     appLabel, highBattery, badgedAppLabel,
                     new PackageEntryClickedListener(packageName));
         } 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;
diff --git a/src/com/android/settings/net/UidDetailProvider.java b/src/com/android/settings/net/UidDetailProvider.java
index b933025..4b54137 100644
--- a/src/com/android/settings/net/UidDetailProvider.java
+++ b/src/com/android/settings/net/UidDetailProvider.java
@@ -16,8 +16,10 @@
 
 package com.android.settings.net;
 
+import android.app.AppGlobals;
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
@@ -28,7 +30,9 @@
 import android.net.TrafficStats;
 import android.os.UserManager;
 import android.os.UserHandle;
+import android.os.RemoteException;
 import android.text.TextUtils;
+import android.util.Log;
 import android.util.SparseArray;
 
 import com.android.settings.R;
@@ -39,6 +43,7 @@
  * {@link TrafficStats#UID_TETHERING} and {@link UserInfo}.
  */
 public class UidDetailProvider {
+    private static final String TAG = "DataUsage";
     private final Context mContext;
     private final SparseArray<UidDetail> mUidDetailCache;
 
@@ -148,31 +153,43 @@
         final String[] packageNames = pm.getPackagesForUid(uid);
         final int length = packageNames != null ? packageNames.length : 0;
         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) {
-                final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
-                detail.label = info.loadLabel(pm).toString();
-                detail.icon = um.getBadgedDrawableForUser(info.loadIcon(pm), userHandle);
+                final ApplicationInfo info = ipm.getApplicationInfo(packageNames[0],
+                        0 /* no flags */, userId);
+                if (info != null) {
+                    detail.label = info.loadLabel(pm).toString();
+                    detail.icon = um.getBadgedIconForUser(info.loadIcon(pm),
+                            new UserHandle(userId));
+                }
             } else if (length > 1) {
                 detail.detailLabels = new CharSequence[length];
                 detail.detailContentDescriptions = new CharSequence[length];
                 for (int i = 0; i < length; i++) {
                     final String packageName = packageNames[i];
                     final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
-                    final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
+                    final ApplicationInfo appInfo = ipm.getApplicationInfo(packageName,
+                            0 /* no flags */, userId);
 
-                    detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
-                    detail.detailContentDescriptions[i] = um.getBadgedLabelForUser(
-                            detail.detailLabels[i], userHandle);
-                    if (packageInfo.sharedUserLabel != 0) {
-                        detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
-                                packageInfo.applicationInfo).toString();
-                        detail.icon = um.getBadgedDrawableForUser(appInfo.loadIcon(pm), userHandle);
+                    if (appInfo != null) {
+                        detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
+                        detail.detailContentDescriptions[i] = um.getBadgedLabelForUser(
+                                detail.detailLabels[i], userHandle);
+                        if (packageInfo.sharedUserLabel != 0) {
+                            detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
+                                    packageInfo.applicationInfo).toString();
+                            detail.icon = um.getBadgedIconForUser(appInfo.loadIcon(pm), userHandle);
+                        }
                     }
                 }
             }
             detail.contentDescription = um.getBadgedLabelForUser(detail.label, userHandle);
         } 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)) {