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 */
16package android.support.annotation;
17
18import java.lang.annotation.Retention;
19import java.lang.annotation.Target;
20
21import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
22import static java.lang.annotation.ElementType.CONSTRUCTOR;
23import static java.lang.annotation.ElementType.FIELD;
24import static java.lang.annotation.ElementType.METHOD;
25import static java.lang.annotation.RetentionPolicy.CLASS;
26
27/**
28 * Denotes that the annotated element requires (or may require) one or more permissions.
29 * <p/>
30 * Example of requiring a single permission:
31 * <pre>{@code
32 *   &#64;RequiresPermission(Manifest.permission.SET_WALLPAPER)
33 *   public abstract void setWallpaper(Bitmap bitmap) throws IOException;
34 *
35 *   &#64;RequiresPermission(ACCESS_COARSE_LOCATION)
36 *   public abstract Location getLastKnownLocation(String provider);
37 * }</pre>
38 * Example of requiring at least one permission from a set:
39 * <pre>{@code
40 *   &#64;RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
41 *   public abstract Location getLastKnownLocation(String provider);
42 * }</pre>
43 * Example of requiring multiple permissions:
44 * <pre>{@code
45 *   &#64;RequiresPermission(allOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
46 *   public abstract Location getLastKnownLocation(String provider);
47 * }</pre>
48 * Example of requiring separate read and write permissions for a content provider:
49 * <pre>{@code
50 *   &#64;RequiresPermission.Read(&#64;RequiresPermission(READ_HISTORY_BOOKMARKS))
51 *   &#64;RequiresPermission.Write(&#64;RequiresPermission(WRITE_HISTORY_BOOKMARKS))
52 *   public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
53 * }</pre>
54 *
55 * @hide
56 */
57@Retention(CLASS)
58@Target({ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD})
59public @interface RequiresPermission {
60    /**
61     * The name of the permission that is required, if precisely one permission
62     * is required. If more than one permission is required, specify either
63     * {@link #allOf()} or {@link #anyOf()} instead.
64     * <p>
65     * If specified, {@link #anyOf()} and {@link #allOf()} must both be null.
66     */
67    String value() default "";
68
69    /**
70     * Specifies a list of permission names that are all required.
71     * <p>
72     * If specified, {@link #anyOf()} and {@link #value()} must both be null.
73     */
74    String[] allOf() default {};
75
76    /**
77     * Specifies a list of permission names where at least one is required
78     * <p>
79     * If specified, {@link #allOf()} and {@link #value()} must both be null.
80     */
81    String[] anyOf() default {};
82
83    /**
84     * If true, the permission may not be required in all cases (e.g. it may only be
85     * enforced on certain platforms, or for certain call parameters, etc.
86     */
87    boolean conditional() default false;
88
89    /**
90     * Specifies that the given permission is required for read operations
91     */
92    @Target(FIELD)
93    @interface Read {
94        RequiresPermission value();
95    }
96
97    /**
98     * Specifies that the given permission is required for write operations
99     */
100    @Target(FIELD)
101    @interface Write {
102        RequiresPermission value();
103    }
104}
105