blob: 4fce14cef3f0679198a23cec0058c2fddc66ba31 [file] [log] [blame]
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -08001/*
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
17package android.permission;
18
19import android.annotation.NonNull;
20import android.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23
Philip P. Moltmann7622add2019-03-01 11:18:25 -080024import com.android.internal.util.Preconditions;
25
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080026/**
27 * This class contains information about how a runtime permission
28 * is to be presented in the UI. A single runtime permission
29 * presented to the user may correspond to multiple platform defined
30 * permissions, e.g. the location permission may control both the
31 * coarse and fine platform permissions.
32 *
33 * @hide
34 */
35@SystemApi
36public final class RuntimePermissionPresentationInfo implements Parcelable {
37 private static final int FLAG_GRANTED = 1 << 0;
38 private static final int FLAG_STANDARD = 1 << 1;
39
Philip P. Moltmann7622add2019-03-01 11:18:25 -080040 private final @NonNull CharSequence mLabel;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080041 private final int mFlags;
42
43 /**
44 * Creates a new instance.
45 *
46 * @param label The permission label.
47 * @param granted Whether the permission is granted.
48 * @param standard Whether this is a platform-defined permission.
49 */
Philip P. Moltmann7622add2019-03-01 11:18:25 -080050 public RuntimePermissionPresentationInfo(@NonNull CharSequence label,
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080051 boolean granted, boolean standard) {
Philip P. Moltmann7622add2019-03-01 11:18:25 -080052 Preconditions.checkNotNull(label);
53
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080054 mLabel = label;
55 int flags = 0;
56 if (granted) {
57 flags |= FLAG_GRANTED;
58 }
59 if (standard) {
60 flags |= FLAG_STANDARD;
61 }
62 mFlags = flags;
63 }
64
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080065 /**
66 * @return Whether the permission is granted.
67 */
68 public boolean isGranted() {
69 return (mFlags & FLAG_GRANTED) != 0;
70 }
71
72 /**
73 * @return Whether the permission is platform-defined.
74 */
75 public boolean isStandard() {
76 return (mFlags & FLAG_STANDARD) != 0;
77 }
78
79 /**
80 * Gets the permission label.
81 *
82 * @return The label.
83 */
84 public @NonNull CharSequence getLabel() {
85 return mLabel;
86 }
87
88 @Override
89 public int describeContents() {
90 return 0;
91 }
92
93 @Override
94 public void writeToParcel(Parcel parcel, int flags) {
95 parcel.writeCharSequence(mLabel);
96 parcel.writeInt(mFlags);
97 }
98
Philip P. Moltmann7622add2019-03-01 11:18:25 -080099 public static final @NonNull Creator<RuntimePermissionPresentationInfo> CREATOR =
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800100 new Creator<RuntimePermissionPresentationInfo>() {
101 public RuntimePermissionPresentationInfo createFromParcel(Parcel source) {
Philip P. Moltmann7622add2019-03-01 11:18:25 -0800102 CharSequence label = source.readCharSequence();
103 int flags = source.readInt();
104
105 return new RuntimePermissionPresentationInfo(label, (flags & FLAG_GRANTED) != 0,
106 (flags & FLAG_STANDARD) != 0);
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800107 }
108
109 public RuntimePermissionPresentationInfo[] newArray(int size) {
110 return new RuntimePermissionPresentationInfo[size];
111 }
112 };
113}