Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 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; |
Nate Myren | 17f2159 | 2019-05-09 13:58:06 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
Philip P. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 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 | */ |
Nate Myren | 17f2159 | 2019-05-09 13:58:06 -0700 | [diff] [blame] | 36 | @TestApi |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 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 | |
Philip P. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 42 | private final @NonNull CharSequence mLabel; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 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 | */ |
Philip P. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 52 | public RuntimePermissionPresentationInfo(@NonNull CharSequence label, |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 53 | boolean granted, boolean standard) { |
Philip P. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 54 | Preconditions.checkNotNull(label); |
| 55 | |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 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 | |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 67 | /** |
| 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. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 101 | public static final @NonNull Creator<RuntimePermissionPresentationInfo> CREATOR = |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 102 | new Creator<RuntimePermissionPresentationInfo>() { |
| 103 | public RuntimePermissionPresentationInfo createFromParcel(Parcel source) { |
Philip P. Moltmann | 7622add | 2019-03-01 11:18:25 -0800 | [diff] [blame] | 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); |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public RuntimePermissionPresentationInfo[] newArray(int size) { |
| 112 | return new RuntimePermissionPresentationInfo[size]; |
| 113 | } |
| 114 | }; |
| 115 | } |