Show SD unavailable icon for apps on SD when ejected.

This change include a minor refactoring of PackageItemInfo and related
classes to eliminate code duplication and to avoid redundant work
searching for an ApplicationInfo instance we already have.

Bug: b/2537578
Change-Id: Id0794c3f055ea58b943028f7a84abc7dec9d0aac
diff --git a/core/java/android/content/pm/ApplicationInfo.java b/core/java/android/content/pm/ApplicationInfo.java
index 480504d..0a04e5b 100644
--- a/core/java/android/content/pm/ApplicationInfo.java
+++ b/core/java/android/content/pm/ApplicationInfo.java
@@ -16,6 +16,9 @@
 
 package android.content.pm;
 
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.util.Printer;
@@ -496,7 +499,7 @@
      */
     public CharSequence loadDescription(PackageManager pm) {
         if (descriptionRes != 0) {
-            CharSequence label = pm.getText(packageName, descriptionRes, null);
+            CharSequence label = pm.getText(packageName, descriptionRes, this);
             if (label != null) {
                 return label;
             }
@@ -514,4 +517,31 @@
                 FLAG_SUPPORTS_SMALL_SCREENS | FLAG_RESIZEABLE_FOR_SCREENS |
                 FLAG_SUPPORTS_SCREEN_DENSITIES);
     }
+    
+    /**
+     * @hide
+     */
+    @Override protected Drawable loadDefaultIcon(PackageManager pm) {
+        if ((flags & FLAG_EXTERNAL_STORAGE) != 0
+                && isPackageUnavailable(pm)) {
+            return Resources.getSystem().getDrawable(
+                    com.android.internal.R.drawable.sym_app_on_sd_unavailable_icon);
+        }
+        return pm.getDefaultActivityIcon();
+    }
+    
+    private boolean isPackageUnavailable(PackageManager pm) {
+        try {
+            return pm.getPackageInfo(packageName, 0) == null;
+        } catch (NameNotFoundException ex) {
+            return true;
+        }
+    }
+    
+    /**
+     * @hide
+     */
+    @Override protected ApplicationInfo getApplicationInfo() {
+        return this;
+    }
 }