1 /*
2  * Copyright (C) 2016 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.permission;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import com.android.internal.util.Preconditions;
26 
27 /**
28  * This class contains information about how a runtime permission
29  * is to be presented in the UI. A single runtime permission
30  * presented to the user may correspond to multiple platform defined
31  * permissions, e.g. the location permission may control both the
32  * coarse and fine platform permissions.
33  *
34  * @hide
35  */
36 @TestApi
37 @SystemApi
38 public final class RuntimePermissionPresentationInfo implements Parcelable {
39     private static final int FLAG_GRANTED = 1 << 0;
40     private static final int FLAG_STANDARD = 1 << 1;
41 
42     private final @NonNull CharSequence mLabel;
43     private final int mFlags;
44 
45     /**
46      * Creates a new instance.
47      *
48      * @param label The permission label.
49      * @param granted Whether the permission is granted.
50      * @param standard Whether this is a platform-defined permission.
51      */
RuntimePermissionPresentationInfo(@onNull CharSequence label, boolean granted, boolean standard)52     public RuntimePermissionPresentationInfo(@NonNull CharSequence label,
53             boolean granted, boolean standard) {
54         Preconditions.checkNotNull(label);
55 
56         mLabel = label;
57         int flags = 0;
58         if (granted) {
59             flags |= FLAG_GRANTED;
60         }
61         if (standard) {
62             flags |= FLAG_STANDARD;
63         }
64         mFlags = flags;
65     }
66 
67     /**
68      * @return Whether the permission is granted.
69      */
isGranted()70     public boolean isGranted() {
71         return (mFlags & FLAG_GRANTED) != 0;
72     }
73 
74     /**
75      * @return Whether the permission is platform-defined.
76      */
isStandard()77     public boolean isStandard() {
78         return (mFlags & FLAG_STANDARD) != 0;
79     }
80 
81     /**
82      * Gets the permission label.
83      *
84      * @return The label.
85      */
getLabel()86     public @NonNull CharSequence getLabel() {
87         return mLabel;
88     }
89 
90     @Override
describeContents()91     public int describeContents() {
92         return 0;
93     }
94 
95     @Override
writeToParcel(Parcel parcel, int flags)96     public void writeToParcel(Parcel parcel, int flags) {
97         parcel.writeCharSequence(mLabel);
98         parcel.writeInt(mFlags);
99     }
100 
101     public static final @NonNull Creator<RuntimePermissionPresentationInfo> CREATOR =
102             new Creator<RuntimePermissionPresentationInfo>() {
103         public RuntimePermissionPresentationInfo createFromParcel(Parcel source) {
104             CharSequence label = source.readCharSequence();
105             int flags = source.readInt();
106 
107             return new RuntimePermissionPresentationInfo(label, (flags & FLAG_GRANTED) != 0,
108                     (flags & FLAG_STANDARD) != 0);
109         }
110 
111         public RuntimePermissionPresentationInfo[] newArray(int size) {
112             return new RuntimePermissionPresentationInfo[size];
113         }
114     };
115 }
116