The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Svet Ganov | 2a1376d | 2016-02-22 17:20:35 -0800 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | import 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 | */ |
| 29 | public 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 | /** |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 52 | * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED} |
| 53 | * instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 55 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3; |
| 57 | |
| 58 | /** |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 59 | * Additional flag for {@link #protectionLevel}, corresponding |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 60 | * to the <code>privileged</code> value of |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 61 | * {@link android.R.attr#protectionLevel}. |
| 62 | */ |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 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 |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 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 | /** |
Dianne Hackborn | 33f5ddd | 2014-07-21 15:35:45 -0700 | [diff] [blame] | 81 | * Additional flag for {@link #protectionLevel}, corresponding |
Jeff Sharkey | e9b78fd | 2014-08-13 14:16:50 -0700 | [diff] [blame] | 82 | * to the <code>appop</code> value of |
Dianne Hackborn | 33f5ddd | 2014-07-21 15:35:45 -0700 | [diff] [blame] | 83 | * {@link android.R.attr#protectionLevel}. |
| 84 | */ |
| 85 | public static final int PROTECTION_FLAG_APPOP = 0x40; |
| 86 | |
| 87 | /** |
Dianne Hackborn | de15eda | 2015-07-01 12:30:54 -0700 | [diff] [blame] | 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 | /** |
Svetoslav | 3e7d977 | 2015-07-06 18:31:23 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 116 | * Mask for {@link #protectionLevel}: the basic protection type. |
| 117 | */ |
| 118 | public static final int PROTECTION_MASK_BASE = 0xf; |
| 119 | |
| 120 | /** |
| 121 | * Mask for {@link #protectionLevel}: additional flag bits. |
| 122 | */ |
Svetoslav | 3e7d977 | 2015-07-06 18:31:23 -0700 | [diff] [blame] | 123 | public static final int PROTECTION_MASK_FLAGS = 0xff0; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 126 | * The level of access this permission is protecting, as per |
| 127 | * {@link android.R.attr#protectionLevel}. Values may be |
| 128 | * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or |
| 129 | * {@link #PROTECTION_SIGNATURE}. May also include the additional |
| 130 | * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT} |
| 131 | * (which only make sense in combination with the base |
| 132 | * {@link #PROTECTION_SIGNATURE}. |
| 133 | */ |
| 134 | public int protectionLevel; |
| 135 | |
| 136 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * The group this permission is a part of, as per |
| 138 | * {@link android.R.attr#permissionGroup}. |
| 139 | */ |
| 140 | public String group; |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Flag for {@link #flags}, corresponding to <code>costsMoney</code> |
| 144 | * value of {@link android.R.attr#permissionFlags}. |
| 145 | */ |
| 146 | public static final int FLAG_COSTS_MONEY = 1<<0; |
| 147 | |
| 148 | /** |
Svet Ganov | 2a1376d | 2016-02-22 17:20:35 -0800 | [diff] [blame] | 149 | * Flag for {@link #flags}, corresponding to <code>removed</code> |
Svet Ganov | 3e0be74 | 2015-08-07 23:06:00 -0700 | [diff] [blame] | 150 | * value of {@link android.R.attr#permissionFlags}. |
| 151 | * @hide |
| 152 | */ |
Svet Ganov | 2a1376d | 2016-02-22 17:20:35 -0800 | [diff] [blame] | 153 | @SystemApi |
| 154 | public static final int FLAG_REMOVED = 1<<1; |
Svet Ganov | 3e0be74 | 2015-08-07 23:06:00 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Dianne Hackborn | cfbfafe | 2015-07-21 16:57:51 -0700 | [diff] [blame] | 157 | * Flag for {@link #flags}, indicating that this permission has been |
| 158 | * installed into the system's globally defined permissions. |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 159 | */ |
Dianne Hackborn | cfbfafe | 2015-07-21 16:57:51 -0700 | [diff] [blame] | 160 | public static final int FLAG_INSTALLED = 1<<30; |
Svetoslav Ganov | 6d2c0e5 | 2015-06-23 16:33:36 +0000 | [diff] [blame] | 161 | |
| 162 | /** |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 163 | * Additional flags about this permission as given by |
| 164 | * {@link android.R.attr#permissionFlags}. |
| 165 | */ |
| 166 | public int flags; |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | /** |
| 169 | * A string resource identifier (in the package's resources) of this |
| 170 | * permission's description. From the "description" attribute or, |
| 171 | * if not set, 0. |
| 172 | */ |
| 173 | public int descriptionRes; |
| 174 | |
| 175 | /** |
| 176 | * The description string provided in the AndroidManifest file, if any. You |
| 177 | * probably don't want to use this, since it will be null if the description |
| 178 | * is in a resource. You probably want |
| 179 | * {@link PermissionInfo#loadDescription} instead. |
| 180 | */ |
| 181 | public CharSequence nonLocalizedDescription; |
| 182 | |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 183 | /** @hide */ |
| 184 | public static int fixProtectionLevel(int level) { |
| 185 | if (level == PROTECTION_SIGNATURE_OR_SYSTEM) { |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 186 | level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 187 | } |
| 188 | return level; |
| 189 | } |
| 190 | |
| 191 | /** @hide */ |
| 192 | public static String protectionToString(int level) { |
| 193 | String protLevel = "????"; |
| 194 | switch (level&PROTECTION_MASK_BASE) { |
| 195 | case PermissionInfo.PROTECTION_DANGEROUS: |
| 196 | protLevel = "dangerous"; |
| 197 | break; |
| 198 | case PermissionInfo.PROTECTION_NORMAL: |
| 199 | protLevel = "normal"; |
| 200 | break; |
| 201 | case PermissionInfo.PROTECTION_SIGNATURE: |
| 202 | protLevel = "signature"; |
| 203 | break; |
| 204 | case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM: |
| 205 | protLevel = "signatureOrSystem"; |
| 206 | break; |
| 207 | } |
Dianne Hackborn | a90c8de | 2015-07-07 17:25:25 -0700 | [diff] [blame] | 208 | if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) { |
| 209 | protLevel += "|privileged"; |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 210 | } |
| 211 | if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) { |
| 212 | protLevel += "|development"; |
| 213 | } |
Dianne Hackborn | 33f5ddd | 2014-07-21 15:35:45 -0700 | [diff] [blame] | 214 | if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) { |
| 215 | protLevel += "|appop"; |
| 216 | } |
Dianne Hackborn | de15eda | 2015-07-01 12:30:54 -0700 | [diff] [blame] | 217 | if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) { |
| 218 | protLevel += "|pre23"; |
| 219 | } |
Dianne Hackborn | cfbfafe | 2015-07-21 16:57:51 -0700 | [diff] [blame] | 220 | if ((level&PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) { |
| 221 | protLevel += "|installer"; |
| 222 | } |
| 223 | if ((level&PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) { |
| 224 | protLevel += "|verifier"; |
| 225 | } |
| 226 | if ((level&PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) { |
| 227 | protLevel += "|preinstalled"; |
| 228 | } |
Dianne Hackborn | e639da7 | 2012-02-21 15:11:13 -0800 | [diff] [blame] | 229 | return protLevel; |
| 230 | } |
| 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | public PermissionInfo() { |
| 233 | } |
| 234 | |
| 235 | public PermissionInfo(PermissionInfo orig) { |
| 236 | super(orig); |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 237 | protectionLevel = orig.protectionLevel; |
| 238 | flags = orig.flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | group = orig.group; |
| 240 | descriptionRes = orig.descriptionRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | nonLocalizedDescription = orig.nonLocalizedDescription; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Retrieve the textual description of this permission. This |
| 246 | * will call back on the given PackageManager to load the description from |
| 247 | * the application. |
| 248 | * |
| 249 | * @param pm A PackageManager from which the label can be loaded; usually |
| 250 | * the PackageManager from which you originally retrieved this item. |
| 251 | * |
| 252 | * @return Returns a CharSequence containing the permission's description. |
| 253 | * If there is no description, null is returned. |
| 254 | */ |
| 255 | public CharSequence loadDescription(PackageManager pm) { |
| 256 | if (nonLocalizedDescription != null) { |
| 257 | return nonLocalizedDescription; |
| 258 | } |
| 259 | if (descriptionRes != 0) { |
| 260 | CharSequence label = pm.getText(packageName, descriptionRes, null); |
| 261 | if (label != null) { |
| 262 | return label; |
| 263 | } |
| 264 | } |
| 265 | return null; |
| 266 | } |
| 267 | |
| 268 | public String toString() { |
| 269 | return "PermissionInfo{" |
| 270 | + Integer.toHexString(System.identityHashCode(this)) |
| 271 | + " " + name + "}"; |
| 272 | } |
| 273 | |
| 274 | public int describeContents() { |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 279 | super.writeToParcel(dest, parcelableFlags); |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 280 | dest.writeInt(protectionLevel); |
| 281 | dest.writeInt(flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | dest.writeString(group); |
| 283 | dest.writeInt(descriptionRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags); |
| 285 | } |
| 286 | |
| 287 | public static final Creator<PermissionInfo> CREATOR = |
| 288 | new Creator<PermissionInfo>() { |
| 289 | public PermissionInfo createFromParcel(Parcel source) { |
| 290 | return new PermissionInfo(source); |
| 291 | } |
| 292 | public PermissionInfo[] newArray(int size) { |
| 293 | return new PermissionInfo[size]; |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | private PermissionInfo(Parcel source) { |
| 298 | super(source); |
Dianne Hackborn | 2ca2c87 | 2012-09-16 16:03:36 -0700 | [diff] [blame] | 299 | protectionLevel = source.readInt(); |
| 300 | flags = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | group = source.readString(); |
| 302 | descriptionRes = source.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source); |
| 304 | } |
| 305 | } |