Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2008 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.os.Parcel;
     20 import android.os.Parcelable;
     21 import android.text.TextUtils;
     22 
     23 /**
     24  * Information you can retrieve about a particular security permission
     25  * known to the system.  This corresponds to information collected from the
     26  * AndroidManifest.xml's <permission> tags.
     27  */
     28 public class PermissionInfo extends PackageItemInfo implements Parcelable {
     29     /**
     30      * A normal application value for {@link #protectionLevel}, corresponding
     31      * to the <code>normal</code> value of
     32      * {@link android.R.attr#protectionLevel}.
     33      */
     34     public static final int PROTECTION_NORMAL = 0;
     35 
     36     /**
     37      * Dangerous value for {@link #protectionLevel}, corresponding
     38      * to the <code>dangerous</code> value of
     39      * {@link android.R.attr#protectionLevel}.
     40      */
     41     public static final int PROTECTION_DANGEROUS = 1;
     42 
     43     /**
     44      * System-level value for {@link #protectionLevel}, corresponding
     45      * to the <code>signature</code> value of
     46      * {@link android.R.attr#protectionLevel}.
     47      */
     48     public static final int PROTECTION_SIGNATURE = 2;
     49 
     50     /**
     51      * System-level value for {@link #protectionLevel}, corresponding
     52      * to the <code>signatureOrSystem</code> value of
     53      * {@link android.R.attr#protectionLevel}.
     54      */
     55     public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
     56 
     57     /**
     58      * Additional flag for {@link #protectionLevel}, corresponding
     59      * to the <code>system</code> value of
     60      * {@link android.R.attr#protectionLevel}.
     61      */
     62     public static final int PROTECTION_FLAG_SYSTEM = 0x10;
     63 
     64     /**
     65      * Additional flag for {@link #protectionLevel}, corresponding
     66      * to the <code>development</code> value of
     67      * {@link android.R.attr#protectionLevel}.
     68      */
     69     public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20;
     70 
     71     /**
     72      * Mask for {@link #protectionLevel}: the basic protection type.
     73      */
     74     public static final int PROTECTION_MASK_BASE = 0xf;
     75 
     76     /**
     77      * Mask for {@link #protectionLevel}: additional flag bits.
     78      */
     79     public static final int PROTECTION_MASK_FLAGS = 0xf0;
     80 
     81     /**
     82      * The level of access this permission is protecting, as per
     83      * {@link android.R.attr#protectionLevel}.  Values may be
     84      * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
     85      * {@link #PROTECTION_SIGNATURE}.  May also include the additional
     86      * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
     87      * (which only make sense in combination with the base
     88      * {@link #PROTECTION_SIGNATURE}.
     89      */
     90     public int protectionLevel;
     91 
     92     /**
     93      * The group this permission is a part of, as per
     94      * {@link android.R.attr#permissionGroup}.
     95      */
     96     public String group;
     97 
     98     /**
     99      * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
    100      * value of {@link android.R.attr#permissionFlags}.
    101      */
    102     public static final int FLAG_COSTS_MONEY = 1<<0;
    103 
    104     /**
    105      * Additional flags about this permission as given by
    106      * {@link android.R.attr#permissionFlags}.
    107      */
    108     public int flags;
    109 
    110     /**
    111      * A string resource identifier (in the package's resources) of this
    112      * permission's description.  From the "description" attribute or,
    113      * if not set, 0.
    114      */
    115     public int descriptionRes;
    116 
    117     /**
    118      * The description string provided in the AndroidManifest file, if any.  You
    119      * probably don't want to use this, since it will be null if the description
    120      * is in a resource.  You probably want
    121      * {@link PermissionInfo#loadDescription} instead.
    122      */
    123     public CharSequence nonLocalizedDescription;
    124 
    125     /** @hide */
    126     public static int fixProtectionLevel(int level) {
    127         if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
    128             level = PROTECTION_SIGNATURE | PROTECTION_FLAG_SYSTEM;
    129         }
    130         return level;
    131     }
    132 
    133     /** @hide */
    134     public static String protectionToString(int level) {
    135         String protLevel = "????";
    136         switch (level&PROTECTION_MASK_BASE) {
    137             case PermissionInfo.PROTECTION_DANGEROUS:
    138                 protLevel = "dangerous";
    139                 break;
    140             case PermissionInfo.PROTECTION_NORMAL:
    141                 protLevel = "normal";
    142                 break;
    143             case PermissionInfo.PROTECTION_SIGNATURE:
    144                 protLevel = "signature";
    145                 break;
    146             case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
    147                 protLevel = "signatureOrSystem";
    148                 break;
    149         }
    150         if ((level&PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) {
    151             protLevel += "|system";
    152         }
    153         if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
    154             protLevel += "|development";
    155         }
    156         return protLevel;
    157     }
    158 
    159     public PermissionInfo() {
    160     }
    161 
    162     public PermissionInfo(PermissionInfo orig) {
    163         super(orig);
    164         protectionLevel = orig.protectionLevel;
    165         flags = orig.flags;
    166         group = orig.group;
    167         descriptionRes = orig.descriptionRes;
    168         nonLocalizedDescription = orig.nonLocalizedDescription;
    169     }
    170 
    171     /**
    172      * Retrieve the textual description of this permission.  This
    173      * will call back on the given PackageManager to load the description from
    174      * the application.
    175      *
    176      * @param pm A PackageManager from which the label can be loaded; usually
    177      * the PackageManager from which you originally retrieved this item.
    178      *
    179      * @return Returns a CharSequence containing the permission's description.
    180      * If there is no description, null is returned.
    181      */
    182     public CharSequence loadDescription(PackageManager pm) {
    183         if (nonLocalizedDescription != null) {
    184             return nonLocalizedDescription;
    185         }
    186         if (descriptionRes != 0) {
    187             CharSequence label = pm.getText(packageName, descriptionRes, null);
    188             if (label != null) {
    189                 return label;
    190             }
    191         }
    192         return null;
    193     }
    194 
    195     public String toString() {
    196         return "PermissionInfo{"
    197             + Integer.toHexString(System.identityHashCode(this))
    198             + " " + name + "}";
    199     }
    200 
    201     public int describeContents() {
    202         return 0;
    203     }
    204 
    205     public void writeToParcel(Parcel dest, int parcelableFlags) {
    206         super.writeToParcel(dest, parcelableFlags);
    207         dest.writeInt(protectionLevel);
    208         dest.writeInt(flags);
    209         dest.writeString(group);
    210         dest.writeInt(descriptionRes);
    211         TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
    212     }
    213 
    214     public static final Creator<PermissionInfo> CREATOR =
    215         new Creator<PermissionInfo>() {
    216         public PermissionInfo createFromParcel(Parcel source) {
    217             return new PermissionInfo(source);
    218         }
    219         public PermissionInfo[] newArray(int size) {
    220             return new PermissionInfo[size];
    221         }
    222     };
    223 
    224     private PermissionInfo(Parcel source) {
    225         super(source);
    226         protectionLevel = source.readInt();
    227         flags = source.readInt();
    228         group = source.readString();
    229         descriptionRes = source.readInt();
    230         nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    231     }
    232 }
    233