Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package android.annotation; |
| 17 | |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 18 | import android.content.Intent; |
| 19 | |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 20 | import java.lang.annotation.Retention; |
| 21 | import java.lang.annotation.Target; |
| 22 | |
| 23 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; |
| 24 | import static java.lang.annotation.ElementType.CONSTRUCTOR; |
| 25 | import static java.lang.annotation.ElementType.FIELD; |
| 26 | import static java.lang.annotation.ElementType.METHOD; |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 27 | import static java.lang.annotation.ElementType.PARAMETER; |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 28 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 29 | |
| 30 | /** |
| 31 | * Denotes that the annotated element requires (or may require) one or more permissions. |
| 32 | * <p/> |
| 33 | * Example of requiring a single permission: |
| 34 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 35 | * {@literal @}RequiresPermission(Manifest.permission.SET_WALLPAPER) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 36 | * public abstract void setWallpaper(Bitmap bitmap) throws IOException; |
| 37 | * |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 38 | * {@literal @}RequiresPermission(ACCESS_COARSE_LOCATION) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 39 | * public abstract Location getLastKnownLocation(String provider); |
| 40 | * }</pre> |
| 41 | * Example of requiring at least one permission from a set: |
| 42 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 43 | * {@literal @}RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 44 | * public abstract Location getLastKnownLocation(String provider); |
| 45 | * }</pre> |
| 46 | * Example of requiring multiple permissions: |
| 47 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 48 | * {@literal @}RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 49 | * public abstract Location getLastKnownLocation(String provider); |
| 50 | * }</pre> |
| 51 | * Example of requiring separate read and write permissions for a content provider: |
| 52 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 53 | * {@literal @}RequiresPermission.Read(@RequiresPermission(READ_HISTORY_BOOKMARKS)) |
| 54 | * {@literal @}RequiresPermission.Write(@RequiresPermission(WRITE_HISTORY_BOOKMARKS)) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 55 | * public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks"); |
| 56 | * }</pre> |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 57 | * <p> |
| 58 | * When specified on a parameter, the annotation indicates that the method requires |
| 59 | * a permission which depends on the value of the parameter. For example, consider |
| 60 | * {@link android.app.Activity#startActivity(Intent)}: |
| 61 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 62 | * public void startActivity(@RequiresPermission Intent intent) { ... } |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 63 | * }</pre> |
| 64 | * Notice how there are no actual permission names listed in the annotation. The actual |
| 65 | * permissions required will depend on the particular intent passed in. For example, |
| 66 | * the code may look like this: |
| 67 | * <pre>{@code |
| 68 | * Intent intent = new Intent(Intent.ACTION_CALL); |
| 69 | * startActivity(intent); |
| 70 | * }</pre> |
| 71 | * and the actual permission requirement for this particular intent is described on |
| 72 | * the Intent name itself: |
| 73 | * <pre>{@code |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 74 | * {@literal @}RequiresPermission(Manifest.permission.CALL_PHONE) |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 75 | * public static final String ACTION_CALL = "android.intent.action.CALL"; |
| 76 | * }</pre> |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 77 | * |
| 78 | * @hide |
| 79 | */ |
| 80 | @Retention(SOURCE) |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 81 | @Target({ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD,PARAMETER}) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 82 | public @interface RequiresPermission { |
| 83 | /** |
| 84 | * The name of the permission that is required, if precisely one permission |
| 85 | * is required. If more than one permission is required, specify either |
| 86 | * {@link #allOf()} or {@link #anyOf()} instead. |
| 87 | * <p> |
| 88 | * If specified, {@link #anyOf()} and {@link #allOf()} must both be null. |
| 89 | */ |
| 90 | String value() default ""; |
| 91 | |
| 92 | /** |
| 93 | * Specifies a list of permission names that are all required. |
| 94 | * <p> |
| 95 | * If specified, {@link #anyOf()} and {@link #value()} must both be null. |
| 96 | */ |
| 97 | String[] allOf() default {}; |
| 98 | |
| 99 | /** |
| 100 | * Specifies a list of permission names where at least one is required |
| 101 | * <p> |
| 102 | * If specified, {@link #allOf()} and {@link #value()} must both be null. |
| 103 | */ |
| 104 | String[] anyOf() default {}; |
| 105 | |
| 106 | /** |
| 107 | * If true, the permission may not be required in all cases (e.g. it may only be |
| 108 | * enforced on certain platforms, or for certain call parameters, etc. |
| 109 | */ |
| 110 | boolean conditional() default false; |
| 111 | |
| 112 | /** |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 113 | * Specifies that the given permission is required for read operations. |
| 114 | * <p> |
| 115 | * When specified on a parameter, the annotation indicates that the method requires |
| 116 | * a permission which depends on the value of the parameter (and typically |
| 117 | * the corresponding field passed in will be one of a set of constants which have |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 118 | * been annotated with a <code>@RequiresPermission</code> annotation.) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 119 | */ |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 120 | @Target({FIELD, METHOD, PARAMETER}) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 121 | @interface Read { |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 122 | RequiresPermission value() default @RequiresPermission; |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 126 | * Specifies that the given permission is required for write operations. |
| 127 | * <p> |
| 128 | * When specified on a parameter, the annotation indicates that the method requires |
| 129 | * a permission which depends on the value of the parameter (and typically |
| 130 | * the corresponding field passed in will be one of a set of constants which have |
Neil Fuller | 71fbb81 | 2015-11-30 09:51:33 +0000 | [diff] [blame] | 131 | * been annotated with a <code>@RequiresPermission</code> annotation.) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 132 | */ |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 133 | @Target({FIELD, METHOD, PARAMETER}) |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 134 | @interface Write { |
Tor Norbye | 5fffc16 | 2015-07-05 15:48:27 -0700 | [diff] [blame] | 135 | RequiresPermission value() default @RequiresPermission; |
Tor Norbye | 8f506ed | 2015-04-08 12:58:57 -0700 | [diff] [blame] | 136 | } |
| 137 | } |