1package android.content.pm; 2 3import android.content.res.XmlResourceParser; 4 5import android.graphics.drawable.Drawable; 6import android.os.Bundle; 7import android.os.Parcel; 8import android.text.TextUtils; 9import android.util.Printer; 10 11import java.text.Collator; 12import java.util.Comparator; 13 14/** 15 * Base class containing information common to all package items held by 16 * the package manager. This provides a very common basic set of attributes: 17 * a label, icon, and meta-data. This class is not intended 18 * to be used by itself; it is simply here to share common definitions 19 * between all items returned by the package manager. As such, it does not 20 * itself implement Parcelable, but does provide convenience methods to assist 21 * in the implementation of Parcelable in subclasses. 22 */ 23public class PackageItemInfo { 24 /** 25 * Public name of this item. From the "android:name" attribute. 26 */ 27 public String name; 28 29 /** 30 * Name of the package that this item is in. 31 */ 32 public String packageName; 33 34 /** 35 * A string resource identifier (in the package's resources) of this 36 * component's label. From the "label" attribute or, if not set, 0. 37 */ 38 public int labelRes; 39 40 /** 41 * The string provided in the AndroidManifest file, if any. You 42 * probably don't want to use this. You probably want 43 * {@link PackageManager#getApplicationLabel} 44 */ 45 public CharSequence nonLocalizedLabel; 46 47 /** 48 * A drawable resource identifier (in the package's resources) of this 49 * component's icon. From the "icon" attribute or, if not set, 0. 50 */ 51 public int icon; 52 53 /** 54 * Additional meta-data associated with this component. This field 55 * will only be filled in if you set the 56 * {@link PackageManager#GET_META_DATA} flag when requesting the info. 57 */ 58 public Bundle metaData; 59 60 public PackageItemInfo() { 61 } 62 63 public PackageItemInfo(PackageItemInfo orig) { 64 name = orig.name; 65 packageName = orig.packageName; 66 labelRes = orig.labelRes; 67 nonLocalizedLabel = orig.nonLocalizedLabel; 68 icon = orig.icon; 69 metaData = orig.metaData; 70 } 71 72 /** 73 * Retrieve the current textual label associated with this item. This 74 * will call back on the given PackageManager to load the label from 75 * the application. 76 * 77 * @param pm A PackageManager from which the label can be loaded; usually 78 * the PackageManager from which you originally retrieved this item. 79 * 80 * @return Returns a CharSequence containing the item's label. If the 81 * item does not have a label, its name is returned. 82 */ 83 public CharSequence loadLabel(PackageManager pm) { 84 if (nonLocalizedLabel != null) { 85 return nonLocalizedLabel; 86 } 87 if (labelRes != 0) { 88 CharSequence label = pm.getText(packageName, labelRes, null); 89 if (label != null) { 90 return label; 91 } 92 } 93 if(name != null) { 94 return name; 95 } 96 return packageName; 97 } 98 99 /** 100 * Retrieve the current graphical icon associated with this item. This 101 * will call back on the given PackageManager to load the icon from 102 * the application. 103 * 104 * @param pm A PackageManager from which the icon can be loaded; usually 105 * the PackageManager from which you originally retrieved this item. 106 * 107 * @return Returns a Drawable containing the item's icon. If the 108 * item does not have an icon, the default activity icon is returned. 109 */ 110 public Drawable loadIcon(PackageManager pm) { 111 if (icon != 0) { 112 Drawable dr = pm.getDrawable(packageName, icon, null); 113 if (dr != null) { 114 return dr; 115 } 116 } 117 return pm.getDefaultActivityIcon(); 118 } 119 120 /** 121 * Load an XML resource attached to the meta-data of this item. This will 122 * retrieved the name meta-data entry, and if defined call back on the 123 * given PackageManager to load its XML file from the application. 124 * 125 * @param pm A PackageManager from which the XML can be loaded; usually 126 * the PackageManager from which you originally retrieved this item. 127 * @param name Name of the meta-date you would like to load. 128 * 129 * @return Returns an XmlPullParser you can use to parse the XML file 130 * assigned as the given meta-data. If the meta-data name is not defined 131 * or the XML resource could not be found, null is returned. 132 */ 133 public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) { 134 if (metaData != null) { 135 int resid = metaData.getInt(name); 136 if (resid != 0) { 137 return pm.getXml(packageName, resid, null); 138 } 139 } 140 return null; 141 } 142 143 protected void dumpFront(Printer pw, String prefix) { 144 pw.println(prefix + "name=" + name); 145 pw.println(prefix + "packageName=" + packageName); 146 pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes) 147 + " nonLocalizedLabel=" + nonLocalizedLabel 148 + " icon=0x" + Integer.toHexString(icon)); 149 } 150 151 protected void dumpBack(Printer pw, String prefix) { 152 // no back here 153 } 154 155 public void writeToParcel(Parcel dest, int parcelableFlags) { 156 dest.writeString(name); 157 dest.writeString(packageName); 158 dest.writeInt(labelRes); 159 TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags); 160 dest.writeInt(icon); 161 dest.writeBundle(metaData); 162 } 163 164 protected PackageItemInfo(Parcel source) { 165 name = source.readString(); 166 packageName = source.readString(); 167 labelRes = source.readInt(); 168 nonLocalizedLabel 169 = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 170 icon = source.readInt(); 171 metaData = source.readBundle(); 172 } 173 174 public static class DisplayNameComparator 175 implements Comparator<PackageItemInfo> { 176 public DisplayNameComparator(PackageManager pm) { 177 mPM = pm; 178 } 179 180 public final int compare(PackageItemInfo aa, PackageItemInfo ab) { 181 CharSequence sa = aa.loadLabel(mPM); 182 if (sa == null) sa = aa.name; 183 CharSequence sb = ab.loadLabel(mPM); 184 if (sb == null) sb = ab.name; 185 return sCollator.compare(sa.toString(), sb.toString()); 186 } 187 188 private final Collator sCollator = Collator.getInstance(); 189 private PackageManager mPM; 190 } 191} 192