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