PermissionInfo.java revision c842393bc55509b094d3a71f164fcdbadf5c7997
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 17package android.content.pm; 18 19import android.annotation.SystemApi; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.text.TextUtils; 23 24/** 25 * Information you can retrieve about a particular security permission 26 * known to the system. This corresponds to information collected from the 27 * AndroidManifest.xml's <permission> tags. 28 */ 29public class PermissionInfo extends PackageItemInfo implements Parcelable { 30 /** 31 * A normal application value for {@link #protectionLevel}, corresponding 32 * to the <code>normal</code> value of 33 * {@link android.R.attr#protectionLevel}. 34 */ 35 public static final int PROTECTION_NORMAL = 0; 36 37 /** 38 * Dangerous value for {@link #protectionLevel}, corresponding 39 * to the <code>dangerous</code> value of 40 * {@link android.R.attr#protectionLevel}. 41 */ 42 public static final int PROTECTION_DANGEROUS = 1; 43 44 /** 45 * System-level value for {@link #protectionLevel}, corresponding 46 * to the <code>signature</code> value of 47 * {@link android.R.attr#protectionLevel}. 48 */ 49 public static final int PROTECTION_SIGNATURE = 2; 50 51 /** 52 * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED} 53 * instead. 54 */ 55 @Deprecated 56 public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3; 57 58 /** 59 * Additional flag for {@link #protectionLevel}, corresponding 60 * to the <code>privileged</code> value of 61 * {@link android.R.attr#protectionLevel}. 62 */ 63 public static final int PROTECTION_FLAG_PRIVILEGED = 0x10; 64 65 /** 66 * @deprecated Old name for {@link #PROTECTION_FLAG_PRIVILEGED}, which 67 * is now very confusing because it only applies to privileged apps, not all 68 * apps on the system image. 69 */ 70 @Deprecated 71 public static final int PROTECTION_FLAG_SYSTEM = 0x10; 72 73 /** 74 * Additional flag for {@link #protectionLevel}, corresponding 75 * to the <code>development</code> value of 76 * {@link android.R.attr#protectionLevel}. 77 */ 78 public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20; 79 80 /** 81 * Additional flag for {@link #protectionLevel}, corresponding 82 * to the <code>appop</code> value of 83 * {@link android.R.attr#protectionLevel}. 84 */ 85 public static final int PROTECTION_FLAG_APPOP = 0x40; 86 87 /** 88 * Additional flag for {@link #protectionLevel}, corresponding 89 * to the <code>pre23</code> value of 90 * {@link android.R.attr#protectionLevel}. 91 */ 92 public static final int PROTECTION_FLAG_PRE23 = 0x80; 93 94 /** 95 * Additional flag for {@link #protectionLevel}, corresponding 96 * to the <code>installer</code> value of 97 * {@link android.R.attr#protectionLevel}. 98 */ 99 public static final int PROTECTION_FLAG_INSTALLER = 0x100; 100 101 /** 102 * Additional flag for {@link #protectionLevel}, corresponding 103 * to the <code>verifier</code> value of 104 * {@link android.R.attr#protectionLevel}. 105 */ 106 public static final int PROTECTION_FLAG_VERIFIER = 0x200; 107 108 /** 109 * Additional flag for {@link #protectionLevel}, corresponding 110 * to the <code>preinstalled</code> value of 111 * {@link android.R.attr#protectionLevel}. 112 */ 113 public static final int PROTECTION_FLAG_PREINSTALLED = 0x400; 114 115 /** 116 * Additional flag for {@link #protectionLevel}, corresponding 117 * to the <code>setup</code> value of 118 * {@link android.R.attr#protectionLevel}. 119 */ 120 public static final int PROTECTION_FLAG_SETUP = 0x800; 121 122 /** 123 * Additional flag for {@link #protectionLevel}, corresponding 124 * to the <code>instant</code> value of 125 * {@link android.R.attr#protectionLevel}. 126 */ 127 public static final int PROTECTION_FLAG_INSTANT = 0x1000; 128 129 /** 130 * Additional flag for {@link #protectionLevel}, corresponding 131 * to the <code>runtime</code> value of 132 * {@link android.R.attr#protectionLevel}. 133 */ 134 public static final int PROTECTION_FLAG_RUNTIME_ONLY = 0x2000; 135 136 /** 137 * Additional flag for {@link #protectionLevel}, corresponding 138 * to the <code>oem</code> value of 139 * {@link android.R.attr#protectionLevel}. 140 * 141 * @hide 142 */ 143 @SystemApi 144 public static final int PROTECTION_FLAG_OEM = 0x4000; 145 146 /** 147 * Mask for {@link #protectionLevel}: the basic protection type. 148 */ 149 public static final int PROTECTION_MASK_BASE = 0xf; 150 151 /** 152 * Mask for {@link #protectionLevel}: additional flag bits. 153 */ 154 public static final int PROTECTION_MASK_FLAGS = 0xfff0; 155 156 /** 157 * The level of access this permission is protecting, as per 158 * {@link android.R.attr#protectionLevel}. Values may be 159 * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or 160 * {@link #PROTECTION_SIGNATURE}. May also include the additional 161 * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT} 162 * (which only make sense in combination with the base 163 * {@link #PROTECTION_SIGNATURE}. 164 */ 165 public int protectionLevel; 166 167 /** 168 * The group this permission is a part of, as per 169 * {@link android.R.attr#permissionGroup}. 170 */ 171 public String group; 172 173 /** 174 * Flag for {@link #flags}, corresponding to <code>costsMoney</code> 175 * value of {@link android.R.attr#permissionFlags}. 176 */ 177 public static final int FLAG_COSTS_MONEY = 1<<0; 178 179 /** 180 * Flag for {@link #flags}, corresponding to <code>removed</code> 181 * value of {@link android.R.attr#permissionFlags}. 182 * @hide 183 */ 184 @SystemApi 185 public static final int FLAG_REMOVED = 1<<1; 186 187 /** 188 * Flag for {@link #flags}, indicating that this permission has been 189 * installed into the system's globally defined permissions. 190 */ 191 public static final int FLAG_INSTALLED = 1<<30; 192 193 /** 194 * Additional flags about this permission as given by 195 * {@link android.R.attr#permissionFlags}. 196 */ 197 public int flags; 198 199 /** 200 * A string resource identifier (in the package's resources) of this 201 * permission's description. From the "description" attribute or, 202 * if not set, 0. 203 */ 204 public int descriptionRes; 205 206 /** 207 * A string resource identifier (in the package's resources) used to request the permissions. 208 * From the "request" attribute or, if not set, 0. 209 * 210 * @hide 211 */ 212 @SystemApi 213 public int requestRes; 214 215 /** 216 * The description string provided in the AndroidManifest file, if any. You 217 * probably don't want to use this, since it will be null if the description 218 * is in a resource. You probably want 219 * {@link PermissionInfo#loadDescription} instead. 220 */ 221 public CharSequence nonLocalizedDescription; 222 223 /** @hide */ 224 public static int fixProtectionLevel(int level) { 225 if (level == PROTECTION_SIGNATURE_OR_SYSTEM) { 226 level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED; 227 } 228 return level; 229 } 230 231 /** @hide */ 232 public static String protectionToString(int level) { 233 String protLevel = "????"; 234 switch (level & PROTECTION_MASK_BASE) { 235 case PermissionInfo.PROTECTION_DANGEROUS: 236 protLevel = "dangerous"; 237 break; 238 case PermissionInfo.PROTECTION_NORMAL: 239 protLevel = "normal"; 240 break; 241 case PermissionInfo.PROTECTION_SIGNATURE: 242 protLevel = "signature"; 243 break; 244 case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM: 245 protLevel = "signatureOrSystem"; 246 break; 247 } 248 if ((level & PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) { 249 protLevel += "|privileged"; 250 } 251 if ((level & PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { 252 protLevel += "|development"; 253 } 254 if ((level & PermissionInfo.PROTECTION_FLAG_APPOP) != 0) { 255 protLevel += "|appop"; 256 } 257 if ((level & PermissionInfo.PROTECTION_FLAG_PRE23) != 0) { 258 protLevel += "|pre23"; 259 } 260 if ((level & PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) { 261 protLevel += "|installer"; 262 } 263 if ((level & PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) { 264 protLevel += "|verifier"; 265 } 266 if ((level & PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) { 267 protLevel += "|preinstalled"; 268 } 269 if ((level & PermissionInfo.PROTECTION_FLAG_SETUP) != 0) { 270 protLevel += "|setup"; 271 } 272 if ((level & PermissionInfo.PROTECTION_FLAG_INSTANT) != 0) { 273 protLevel += "|instant"; 274 } 275 if ((level & PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0) { 276 protLevel += "|runtime"; 277 } 278 if ((level & PermissionInfo.PROTECTION_FLAG_OEM) != 0) { 279 protLevel += "|oem"; 280 } 281 return protLevel; 282 } 283 284 public PermissionInfo() { 285 } 286 287 public PermissionInfo(PermissionInfo orig) { 288 super(orig); 289 protectionLevel = orig.protectionLevel; 290 flags = orig.flags; 291 group = orig.group; 292 descriptionRes = orig.descriptionRes; 293 requestRes = orig.requestRes; 294 nonLocalizedDescription = orig.nonLocalizedDescription; 295 } 296 297 /** 298 * Retrieve the textual description of this permission. This 299 * will call back on the given PackageManager to load the description from 300 * the application. 301 * 302 * @param pm A PackageManager from which the label can be loaded; usually 303 * the PackageManager from which you originally retrieved this item. 304 * 305 * @return Returns a CharSequence containing the permission's description. 306 * If there is no description, null is returned. 307 */ 308 public CharSequence loadDescription(PackageManager pm) { 309 if (nonLocalizedDescription != null) { 310 return nonLocalizedDescription; 311 } 312 if (descriptionRes != 0) { 313 CharSequence label = pm.getText(packageName, descriptionRes, null); 314 if (label != null) { 315 return label; 316 } 317 } 318 return null; 319 } 320 321 @Override 322 public String toString() { 323 return "PermissionInfo{" 324 + Integer.toHexString(System.identityHashCode(this)) 325 + " " + name + "}"; 326 } 327 328 @Override 329 public int describeContents() { 330 return 0; 331 } 332 333 @Override 334 public void writeToParcel(Parcel dest, int parcelableFlags) { 335 super.writeToParcel(dest, parcelableFlags); 336 dest.writeInt(protectionLevel); 337 dest.writeInt(flags); 338 dest.writeString(group); 339 dest.writeInt(descriptionRes); 340 dest.writeInt(requestRes); 341 TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags); 342 } 343 344 /** @hide */ 345 public int calculateFootprint() { 346 int size = name.length(); 347 if (nonLocalizedLabel != null) { 348 size += nonLocalizedLabel.length(); 349 } 350 if (nonLocalizedDescription != null) { 351 size += nonLocalizedDescription.length(); 352 } 353 return size; 354 } 355 356 /** @hide */ 357 public boolean isAppOp() { 358 return (protectionLevel & PermissionInfo.PROTECTION_FLAG_APPOP) != 0; 359 } 360 361 public static final Creator<PermissionInfo> CREATOR = 362 new Creator<PermissionInfo>() { 363 @Override 364 public PermissionInfo createFromParcel(Parcel source) { 365 return new PermissionInfo(source); 366 } 367 @Override 368 public PermissionInfo[] newArray(int size) { 369 return new PermissionInfo[size]; 370 } 371 }; 372 373 private PermissionInfo(Parcel source) { 374 super(source); 375 protectionLevel = source.readInt(); 376 flags = source.readInt(); 377 group = source.readString(); 378 descriptionRes = source.readInt(); 379 requestRes = source.readInt(); 380 nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); 381 } 382} 383