• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
18 import static java.lang.annotation.ElementType.METHOD;
19 import static java.lang.annotation.ElementType.TYPE;
20 import static java.lang.annotation.RetentionPolicy.CLASS;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.Target;
24 
25 /**
26  * Denotes that the annotated element enforces one or more permissions.
27  * <p/>
28  * Example of requiring a single permission:
29  * <pre>{@code
30  *   {@literal @}EnforcePermission(Manifest.permission.SET_WALLPAPER)
31  *   public abstract void setWallpaper(Bitmap bitmap) throws IOException;
32  *
33  *   {@literal @}RequiresPermission(ACCESS_COARSE_LOCATION)
34  *   public abstract Location getLastKnownLocation(String provider);
35  * }</pre>
36  * Example of requiring at least one permission from a set:
37  * <pre>{@code
38  *   {@literal @}EnforcePermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
39  *   public abstract Location getLastKnownLocation(String provider);
40  * }</pre>
41  * Example of requiring multiple permissions:
42  * <pre>{@code
43  *   {@literal @}EnforcePermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
44  *   public abstract Location getLastKnownLocation(String provider);
45  * }</pre>
46  * <p>
47  * This annotation is automatically added to generated code (e.g., aidl
48  * interface). It is rarely required to manually add it, unless prompted by a
49  * linter.
50  * </p>
51  *
52  * @see RequiresPermission
53  * @see RequiresNoPermission
54  * @hide
55  */
56 @Retention(CLASS)
57 @Target({METHOD,TYPE})
58 public @interface EnforcePermission {
59     /**
60      * The name of the permission that is required, if precisely one permission
61      * is required. If more than one permission is required, specify either
62      * {@link #allOf()} or {@link #anyOf()} instead.
63      * <p>
64      * If specified, {@link #anyOf()} and {@link #allOf()} must both be null.
65      */
value()66     String value() default "";
67 
68     /**
69      * Specifies a list of permission names that are all required.
70      * <p>
71      * If specified, {@link #anyOf()} and {@link #value()} must both be null.
72      */
allOf()73     String[] allOf() default {};
74 
75     /**
76      * Specifies a list of permission names where at least one is required
77      * <p>
78      * If specified, {@link #allOf()} and {@link #value()} must both be null.
79      */
anyOf()80     String[] anyOf() default {};
81 }
82