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 group this permission is a part of, as per
     83      * {@link android.R.attr#permissionGroup}.
     84      */
     85     public String group;
     86 
     87     /**
     88      * A string resource identifier (in the package's resources) of this
     89      * permission's description.  From the "description" attribute or,
     90      * if not set, 0.
     91      */
     92     public int descriptionRes;
     93 
     94     /**
     95      * The description string provided in the AndroidManifest file, if any.  You
     96      * probably don't want to use this, since it will be null if the description
     97      * is in a resource.  You probably want
     98      * {@link PermissionInfo#loadDescription} instead.
     99      */
    100     public CharSequence nonLocalizedDescription;
    101 
    102     /**
    103      * The level of access this permission is protecting, as per
    104      * {@link android.R.attr#protectionLevel}.  Values may be
    105      * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
    106      * {@link #PROTECTION_SIGNATURE}.  May also include the additional
    107      * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
    108      * (which only make sense in combination with the base
    109      * {@link #PROTECTION_SIGNATURE}.
    110      */
    111     public int protectionLevel;
    112 
    113     /** @hide */
    114     public static int fixProtectionLevel(int level) {
    115         if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
    116             level = PROTECTION_SIGNATURE | PROTECTION_FLAG_SYSTEM;
    117         }
    118         return level;
    119     }
    120 
    121     /** @hide */
    122     public static String protectionToString(int level) {
    123         String protLevel = "????";
    124         switch (level&PROTECTION_MASK_BASE) {
    125             case PermissionInfo.PROTECTION_DANGEROUS:
    126                 protLevel = "dangerous";
    127                 break;
    128             case PermissionInfo.PROTECTION_NORMAL:
    129                 protLevel = "normal";
    130                 break;
    131             case PermissionInfo.PROTECTION_SIGNATURE:
    132                 protLevel = "signature";
    133                 break;
    134             case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
    135                 protLevel = "signatureOrSystem";
    136                 break;
    137         }
    138         if ((level&PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) {
    139             protLevel += "|system";
    140         }
    141         if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
    142             protLevel += "|development";
    143         }
    144         return protLevel;
    145     }
    146 
    147     public PermissionInfo() {
    148     }
    149 
    150     public PermissionInfo(PermissionInfo orig) {
    151         super(orig);
    152         group = orig.group;
    153         descriptionRes = orig.descriptionRes;
    154         protectionLevel = orig.protectionLevel;
    155         nonLocalizedDescription = orig.nonLocalizedDescription;
    156     }
    157 
    158     /**
    159      * Retrieve the textual description of this permission.  This
    160      * will call back on the given PackageManager to load the description from
    161      * the application.
    162      *
    163      * @param pm A PackageManager from which the label can be loaded; usually
    164      * the PackageManager from which you originally retrieved this item.
    165      *
    166      * @return Returns a CharSequence containing the permission's description.
    167      * If there is no description, null is returned.
    168      */
    169     public CharSequence loadDescription(PackageManager pm) {
    170         if (nonLocalizedDescription != null) {
    171             return nonLocalizedDescription;
    172         }
    173         if (descriptionRes != 0) {
    174             CharSequence label = pm.getText(packageName, descriptionRes, null);
    175             if (label != null) {
    176                 return label;
    177             }
    178         }
    179         return null;
    180     }
    181 
    182     public String toString() {
    183         return "PermissionInfo{"
    184             + Integer.toHexString(System.identityHashCode(this))
    185             + " " + name + "}";
    186     }
    187 
    188     public int describeContents() {
    189         return 0;
    190     }
    191 
    192     public void writeToParcel(Parcel dest, int parcelableFlags) {
    193         super.writeToParcel(dest, parcelableFlags);
    194         dest.writeString(group);
    195         dest.writeInt(descriptionRes);
    196         dest.writeInt(protectionLevel);
    197         TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
    198     }
    199 
    200     public static final Creator<PermissionInfo> CREATOR =
    201         new Creator<PermissionInfo>() {
    202         public PermissionInfo createFromParcel(Parcel source) {
    203             return new PermissionInfo(source);
    204         }
    205         public PermissionInfo[] newArray(int size) {
    206             return new PermissionInfo[size];
    207         }
    208     };
    209 
    210     private PermissionInfo(Parcel source) {
    211         super(source);
    212         group = source.readString();
    213         descriptionRes = source.readInt();
    214         protectionLevel = source.readInt();
    215         nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
    216     }
    217 }
    218