Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.content.pm;
     18 
     19 import android.annotation.NonNull;
     20 import android.annotation.SystemApi;
     21 import android.content.res.XmlResourceParser;
     22 
     23 import android.graphics.drawable.Drawable;
     24 import android.os.Bundle;
     25 import android.os.Parcel;
     26 import android.os.UserHandle;
     27 import android.text.BidiFormatter;
     28 import android.text.Html;
     29 import android.text.TextPaint;
     30 import android.text.TextUtils;
     31 import android.util.Printer;
     32 import android.text.BidiFormatter;
     33 import android.text.TextPaint;
     34 import android.text.Html;
     35 import java.text.Collator;
     36 import java.util.Comparator;
     37 
     38 /**
     39  * Base class containing information common to all package items held by
     40  * the package manager.  This provides a very common basic set of attributes:
     41  * a label, icon, and meta-data.  This class is not intended
     42  * to be used by itself; it is simply here to share common definitions
     43  * between all items returned by the package manager.  As such, it does not
     44  * itself implement Parcelable, but does provide convenience methods to assist
     45  * in the implementation of Parcelable in subclasses.
     46  */
     47 public class PackageItemInfo {
     48     private static final float MAX_LABEL_SIZE_PX = 500f;
     49     /**
     50      * Public name of this item. From the "android:name" attribute.
     51      */
     52     public String name;
     53 
     54     /**
     55      * Name of the package that this item is in.
     56      */
     57     public String packageName;
     58 
     59     /**
     60      * A string resource identifier (in the package's resources) of this
     61      * component's label.  From the "label" attribute or, if not set, 0.
     62      */
     63     public int labelRes;
     64 
     65     /**
     66      * The string provided in the AndroidManifest file, if any.  You
     67      * probably don't want to use this.  You probably want
     68      * {@link PackageManager#getApplicationLabel}
     69      */
     70     public CharSequence nonLocalizedLabel;
     71 
     72     /**
     73      * A drawable resource identifier (in the package's resources) of this
     74      * component's icon.  From the "icon" attribute or, if not set, 0.
     75      */
     76     public int icon;
     77 
     78     /**
     79      * A drawable resource identifier (in the package's resources) of this
     80      * component's banner.  From the "banner" attribute or, if not set, 0.
     81      */
     82     public int banner;
     83 
     84     /**
     85      * A drawable resource identifier (in the package's resources) of this
     86      * component's logo. Logos may be larger/wider than icons and are
     87      * displayed by certain UI elements in place of a name or name/icon
     88      * combination. From the "logo" attribute or, if not set, 0.
     89      */
     90     public int logo;
     91 
     92     /**
     93      * Additional meta-data associated with this component.  This field
     94      * will only be filled in if you set the
     95      * {@link PackageManager#GET_META_DATA} flag when requesting the info.
     96      */
     97     public Bundle metaData;
     98 
     99     /**
    100      * If different of UserHandle.USER_NULL, The icon of this item will be the one of that user.
    101      * @hide
    102      */
    103     public int showUserIcon;
    104 
    105     public PackageItemInfo() {
    106         showUserIcon = UserHandle.USER_NULL;
    107     }
    108 
    109     public PackageItemInfo(PackageItemInfo orig) {
    110         name = orig.name;
    111         if (name != null) name = name.trim();
    112         packageName = orig.packageName;
    113         labelRes = orig.labelRes;
    114         nonLocalizedLabel = orig.nonLocalizedLabel;
    115         if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim();
    116         icon = orig.icon;
    117         banner = orig.banner;
    118         logo = orig.logo;
    119         metaData = orig.metaData;
    120         showUserIcon = orig.showUserIcon;
    121     }
    122 
    123     /**
    124      * Retrieve the current textual label associated with this item.  This
    125      * will call back on the given PackageManager to load the label from
    126      * the application.
    127      *
    128      * @param pm A PackageManager from which the label can be loaded; usually
    129      * the PackageManager from which you originally retrieved this item.
    130      *
    131      * @return Returns a CharSequence containing the item's label.  If the
    132      * item does not have a label, its name is returned.
    133      */
    134     public CharSequence loadLabel(PackageManager pm) {
    135         if (nonLocalizedLabel != null) {
    136             return nonLocalizedLabel;
    137         }
    138         if (labelRes != 0) {
    139             CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo());
    140             if (label != null) {
    141                 return label.toString().trim();
    142             }
    143         }
    144         if (name != null) {
    145             return name;
    146         }
    147         return packageName;
    148     }
    149 
    150     /**
    151      * Same as {@link #loadLabel(PackageManager)} with the addition that
    152      * the returned label is safe for being presented in the UI since it
    153      * will not contain new lines and the length will be limited to a
    154      * reasonable amount. This prevents a malicious party to influence UI
    155      * layout via the app label misleading the user into performing a
    156      * detrimental for them action. If the label is too long it will be
    157      * truncated and ellipsized at the end.
    158      *
    159      * @param pm A PackageManager from which the label can be loaded; usually
    160      * the PackageManager from which you originally retrieved this item
    161      * @return Returns a CharSequence containing the item's label. If the
    162      * item does not have a label, its name is returned.
    163      *
    164      * @hide
    165      */
    166     @SystemApi
    167     public @NonNull CharSequence loadSafeLabel(@NonNull PackageManager pm) {
    168         // loadLabel() always returns non-null
    169         String label = loadLabel(pm).toString();
    170         // strip HTML tags to avoid <br> and other tags overwriting original message
    171         String labelStr = Html.fromHtml(label).toString();
    172 
    173         // If the label contains new line characters it may push the UI
    174         // down to hide a part of it. Labels shouldn't have new line
    175         // characters, so just truncate at the first time one is seen.
    176         final int labelLength = labelStr.length();
    177         int offset = 0;
    178         while (offset < labelLength) {
    179             final int codePoint = labelStr.codePointAt(offset);
    180             final int type = Character.getType(codePoint);
    181             if (type == Character.LINE_SEPARATOR
    182                     || type == Character.CONTROL
    183                     || type == Character.PARAGRAPH_SEPARATOR) {
    184                 labelStr = labelStr.substring(0, offset);
    185                 break;
    186             }
    187             // replace all non-break space to " " in order to be trimmed
    188             if (type == Character.SPACE_SEPARATOR) {
    189                 labelStr = labelStr.substring(0, offset) + " " + labelStr.substring(offset +
    190                         Character.charCount(codePoint));
    191             }
    192             offset += Character.charCount(codePoint);
    193         }
    194 
    195         labelStr = labelStr.trim();
    196         if (labelStr.isEmpty()) {
    197             return packageName;
    198         }
    199         TextPaint paint = new TextPaint();
    200         paint.setTextSize(42);
    201 
    202         return TextUtils.ellipsize(labelStr, paint, MAX_LABEL_SIZE_PX,
    203                 TextUtils.TruncateAt.END);
    204     }
    205 
    206     /**
    207      * Retrieve the current graphical icon associated with this item.  This
    208      * will call back on the given PackageManager to load the icon from
    209      * the application.
    210      *
    211      * @param pm A PackageManager from which the icon can be loaded; usually
    212      * the PackageManager from which you originally retrieved this item.
    213      *
    214      * @return Returns a Drawable containing the item's icon.  If the
    215      * item does not have an icon, the item's default icon is returned
    216      * such as the default activity icon.
    217      */
    218     public Drawable loadIcon(PackageManager pm) {
    219         return pm.loadItemIcon(this, getApplicationInfo());
    220     }
    221 
    222     /**
    223      * Retrieve the current graphical icon associated with this item without
    224      * the addition of a work badge if applicable.
    225      * This will call back on the given PackageManager to load the icon from
    226      * the application.
    227      *
    228      * @param pm A PackageManager from which the icon can be loaded; usually
    229      * the PackageManager from which you originally retrieved this item.
    230      *
    231      * @return Returns a Drawable containing the item's icon.  If the
    232      * item does not have an icon, the item's default icon is returned
    233      * such as the default activity icon.
    234      */
    235     public Drawable loadUnbadgedIcon(PackageManager pm) {
    236         return pm.loadUnbadgedItemIcon(this, getApplicationInfo());
    237     }
    238 
    239     /**
    240      * Retrieve the current graphical banner associated with this item.  This
    241      * will call back on the given PackageManager to load the banner from
    242      * the application.
    243      *
    244      * @param pm A PackageManager from which the banner can be loaded; usually
    245      * the PackageManager from which you originally retrieved this item.
    246      *
    247      * @return Returns a Drawable containing the item's banner.  If the item
    248      * does not have a banner, this method will return null.
    249      */
    250     public Drawable loadBanner(PackageManager pm) {
    251         if (banner != 0) {
    252             Drawable dr = pm.getDrawable(packageName, banner, getApplicationInfo());
    253             if (dr != null) {
    254                 return dr;
    255             }
    256         }
    257         return loadDefaultBanner(pm);
    258     }
    259 
    260     /**
    261      * Retrieve the default graphical icon associated with this item.
    262      *
    263      * @param pm A PackageManager from which the icon can be loaded; usually
    264      * the PackageManager from which you originally retrieved this item.
    265      *
    266      * @return Returns a Drawable containing the item's default icon
    267      * such as the default activity icon.
    268      *
    269      * @hide
    270      */
    271     public Drawable loadDefaultIcon(PackageManager pm) {
    272         return pm.getDefaultActivityIcon();
    273     }
    274 
    275     /**
    276      * Retrieve the default graphical banner associated with this item.
    277      *
    278      * @param pm A PackageManager from which the banner can be loaded; usually
    279      * the PackageManager from which you originally retrieved this item.
    280      *
    281      * @return Returns a Drawable containing the item's default banner
    282      * or null if no default logo is available.
    283      *
    284      * @hide
    285      */
    286     protected Drawable loadDefaultBanner(PackageManager pm) {
    287         return null;
    288     }
    289 
    290     /**
    291      * Retrieve the current graphical logo associated with this item. This
    292      * will call back on the given PackageManager to load the logo from
    293      * the application.
    294      *
    295      * @param pm A PackageManager from which the logo can be loaded; usually
    296      * the PackageManager from which you originally retrieved this item.
    297      *
    298      * @return Returns a Drawable containing the item's logo. If the item
    299      * does not have a logo, this method will return null.
    300      */
    301     public Drawable loadLogo(PackageManager pm) {
    302         if (logo != 0) {
    303             Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo());
    304             if (d != null) {
    305                 return d;
    306             }
    307         }
    308         return loadDefaultLogo(pm);
    309     }
    310 
    311     /**
    312      * Retrieve the default graphical logo associated with this item.
    313      *
    314      * @param pm A PackageManager from which the logo can be loaded; usually
    315      * the PackageManager from which you originally retrieved this item.
    316      *
    317      * @return Returns a Drawable containing the item's default logo
    318      * or null if no default logo is available.
    319      *
    320      * @hide
    321      */
    322     protected Drawable loadDefaultLogo(PackageManager pm) {
    323         return null;
    324     }
    325 
    326     /**
    327      * Load an XML resource attached to the meta-data of this item.  This will
    328      * retrieved the name meta-data entry, and if defined call back on the
    329      * given PackageManager to load its XML file from the application.
    330      *
    331      * @param pm A PackageManager from which the XML can be loaded; usually
    332      * the PackageManager from which you originally retrieved this item.
    333      * @param name Name of the meta-date you would like to load.
    334      *
    335      * @return Returns an XmlPullParser you can use to parse the XML file
    336      * assigned as the given meta-data.  If the meta-data name is not defined
    337      * or the XML resource could not be found, null is returned.
    338      */
    339     public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
    340         if (metaData != null) {
    341             int resid = metaData.getInt(name);
    342             if (resid != 0) {
    343                 return pm.getXml(packageName, resid, getApplicationInfo());
    344             }
    345         }
    346         return null;
    347     }
    348 
    349     /**
    350      * @hide Flag for dumping: include all details.
    351      */
    352     public static final int DUMP_FLAG_DETAILS = 1<<0;
    353 
    354     /**
    355      * @hide Flag for dumping: include nested ApplicationInfo.
    356      */
    357     public static final int DUMP_FLAG_APPLICATION = 1<<1;
    358 
    359     /**
    360      * @hide Flag for dumping: all flags to dump everything.
    361      */
    362     public static final int DUMP_FLAG_ALL = DUMP_FLAG_DETAILS | DUMP_FLAG_APPLICATION;
    363 
    364     protected void dumpFront(Printer pw, String prefix) {
    365         if (name != null) {
    366             pw.println(prefix + "name=" + name);
    367         }
    368         pw.println(prefix + "packageName=" + packageName);
    369         if (labelRes != 0 || nonLocalizedLabel != null || icon != 0 || banner != 0) {
    370             pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes)
    371                     + " nonLocalizedLabel=" + nonLocalizedLabel
    372                     + " icon=0x" + Integer.toHexString(icon)
    373                     + " banner=0x" + Integer.toHexString(banner));
    374         }
    375     }
    376 
    377     protected void dumpBack(Printer pw, String prefix) {
    378         // no back here
    379     }
    380 
    381     public void writeToParcel(Parcel dest, int parcelableFlags) {
    382         dest.writeString(name);
    383         dest.writeString(packageName);
    384         dest.writeInt(labelRes);
    385         TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);
    386         dest.writeInt(icon);
    387         dest.writeInt(logo);
    388         dest.writeBundle(metaData);
    389         dest.writeInt(banner);
    390         dest.writeInt(showUserIcon);
    391     }
    392 
    393     protected PackageItemInfo(Parcel source) {
    394         name = source.readString();
    395         packageName = source.readString();
    396         labelRes = source.readInt();
    397         nonLocalizedLabel
    398                 = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    399         icon = source.readInt();
    400         logo = source.readInt();
    401         metaData = source.readBundle();
    402         banner = source.readInt();
    403         showUserIcon = source.readInt();
    404     }
    405 
    406     /**
    407      * Get the ApplicationInfo for the application to which this item belongs,
    408      * if available, otherwise returns null.
    409      *
    410      * @return Returns the ApplicationInfo of this item, or null if not known.
    411      *
    412      * @hide
    413      */
    414     protected ApplicationInfo getApplicationInfo() {
    415         return null;
    416     }
    417 
    418     public static class DisplayNameComparator
    419             implements Comparator<PackageItemInfo> {
    420         public DisplayNameComparator(PackageManager pm) {
    421             mPM = pm;
    422         }
    423 
    424         public final int compare(PackageItemInfo aa, PackageItemInfo ab) {
    425             CharSequence  sa = aa.loadLabel(mPM);
    426             if (sa == null) sa = aa.name;
    427             CharSequence  sb = ab.loadLabel(mPM);
    428             if (sb == null) sb = ab.name;
    429             return sCollator.compare(sa.toString(), sb.toString());
    430         }
    431 
    432         private final Collator   sCollator = Collator.getInstance();
    433         private PackageManager   mPM;
    434     }
    435 }
    436