• 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 enforcing 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 @}EnforcePermission(ACCESS_COARSE_LOCATION)
34  *   public abstract Location getLastKnownLocation(String provider);
35  * }</pre>
36  * Example of enforcing 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 enforcing 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 should be applied to AIDL method definitions that you want
48  * to protect with permissions. Inside the class that inherits from the
49  * generated Stub class, in the corresponding method implementation, the first
50  * instruction must be a call to the auxiliary method generated by the AIDL
51  * compiler. The auxiliary will be named {@code methodName_enforcePermission}.
52  * A linter ensures that this method is called when required.
53  * </p><p>
54  * <b>Warning:</b>In Android {@link android.os.Build.VERSION_CODES#TIRAMISU},
55  * it should only be used for methods that are only called remotely, never
56  * locally (see b/241171714).
57  * </p>
58  *
59  * @see RequiresPermission
60  * @see RequiresNoPermission
61  * @hide
62  */
63 @Retention(CLASS)
64 @Target({METHOD})
65 public @interface EnforcePermission {
66     /**
67      * The name of the permission that is required, if precisely one permission
68      * is required. If more than one permission is required, specify either
69      * {@link #allOf()} or {@link #anyOf()} instead.
70      * <p>
71      * If specified, {@link #anyOf()} and {@link #allOf()} must both be null.
72      */
value()73     String value() default "";
74 
75     /**
76      * Specifies a list of permission names that are all required.
77      * <p>
78      * If specified, {@link #anyOf()} and {@link #value()} must both be null.
79      */
allOf()80     String[] allOf() default {};
81 
82     /**
83      * Specifies a list of permission names where at least one is required
84      * <p>
85      * If specified, {@link #allOf()} and {@link #value()} must both be null.
86      */
anyOf()87     String[] anyOf() default {};
88 }
89