blob: d696feabfc30b9a96430c34b71d4ca2beea3c91d [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;
Nate Myren17f21592019-05-09 13:58:06 -070021import android.annotation.TestApi;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080022import android.os.Parcel;
23import android.os.Parcelable;
24
Philip P. Moltmann7622add2019-03-01 11:18:25 -080025import com.android.internal.util.Preconditions;
26
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080027/**
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 */
Nate Myren17f21592019-05-09 13:58:06 -070036@TestApi
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080037@SystemApi
38public final class RuntimePermissionPresentationInfo implements Parcelable {
39 private static final int FLAG_GRANTED = 1 << 0;
40 private static final int FLAG_STANDARD = 1 << 1;
41
Philip P. Moltmann7622add2019-03-01 11:18:25 -080042 private final @NonNull CharSequence mLabel;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080043 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 */
Philip P. Moltmann7622add2019-03-01 11:18:25 -080052 public RuntimePermissionPresentationInfo(@NonNull CharSequence label,
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080053 boolean granted, boolean standard) {
Philip P. Moltmann7622add2019-03-01 11:18:25 -080054 Preconditions.checkNotNull(label);
55
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080056 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
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080067 /**
68 * @return Whether the permission is granted.
69 */
70 public boolean isGranted() {
71 return (mFlags & FLAG_GRANTED) != 0;
72 }
73
74 /**
75 * @return Whether the permission is platform-defined.
76 */
77 public boolean isStandard() {
78 return (mFlags & FLAG_STANDARD) != 0;
79 }
80
81 /**
82 * Gets the permission label.
83 *
84 * @return The label.
85 */
86 public @NonNull CharSequence getLabel() {
87 return mLabel;
88 }
89
90 @Override
91 public int describeContents() {
92 return 0;
93 }
94
95 @Override
96 public void writeToParcel(Parcel parcel, int flags) {
97 parcel.writeCharSequence(mLabel);
98 parcel.writeInt(mFlags);
99 }
100
Philip P. Moltmann7622add2019-03-01 11:18:25 -0800101 public static final @NonNull Creator<RuntimePermissionPresentationInfo> CREATOR =
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800102 new Creator<RuntimePermissionPresentationInfo>() {
103 public RuntimePermissionPresentationInfo createFromParcel(Parcel source) {
Philip P. Moltmann7622add2019-03-01 11:18:25 -0800104 CharSequence label = source.readCharSequence();
105 int flags = source.readInt();
106
107 return new RuntimePermissionPresentationInfo(label, (flags & FLAG_GRANTED) != 0,
108 (flags & FLAG_STANDARD) != 0);
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800109 }
110
111 public RuntimePermissionPresentationInfo[] newArray(int size) {
112 return new RuntimePermissionPresentationInfo[size];
113 }
114 };
115}