• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 com.android.permission.persistence;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.SystemApi.Client;
23 
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 
28 /**
29  * State of all runtime permissions.
30  *
31  * TODO(b/147914847): Remove @hide when it becomes the default.
32  * @hide
33  */
34 @SystemApi(client = Client.SYSTEM_SERVER)
35 public final class RuntimePermissionsState {
36 
37     /**
38      * Special value for {@link #mVersion} to indicate that no version was read.
39      */
40     public static final int NO_VERSION = -1;
41 
42     /**
43      * The version of the runtime permissions.
44      */
45     private final int mVersion;
46 
47     /**
48      * The fingerprint of the runtime permissions.
49      */
50     @Nullable
51     private final String mFingerprint;
52 
53     /**
54      * The runtime permissions by packages.
55      */
56     @NonNull
57     private final Map<String, List<PermissionState>> mPackagePermissions;
58 
59     /**
60      * The runtime permissions by shared users.
61      */
62     @NonNull
63     private final Map<String, List<PermissionState>> mSharedUserPermissions;
64 
65     /**
66      * Create a new instance of this class.
67      *
68      * @param version the version of the runtime permissions
69      * @param fingerprint the fingerprint of the runtime permissions
70      * @param packagePermissions the runtime permissions by packages
71      * @param sharedUserPermissions the runtime permissions by shared users
72      */
RuntimePermissionsState(int version, @Nullable String fingerprint, @NonNull Map<String, List<PermissionState>> packagePermissions, @NonNull Map<String, List<PermissionState>> sharedUserPermissions)73     public RuntimePermissionsState(int version, @Nullable String fingerprint,
74             @NonNull Map<String, List<PermissionState>> packagePermissions,
75             @NonNull Map<String, List<PermissionState>> sharedUserPermissions) {
76         mVersion = version;
77         mFingerprint = fingerprint;
78         mPackagePermissions = packagePermissions;
79         mSharedUserPermissions = sharedUserPermissions;
80     }
81 
82     /**
83      * Get the version of the runtime permissions.
84      *
85      * @return the version of the runtime permissions
86      */
getVersion()87     public int getVersion() {
88         return mVersion;
89     }
90 
91     /**
92      * Get the fingerprint of the runtime permissions.
93      *
94      * @return the fingerprint of the runtime permissions
95      */
96     @Nullable
getFingerprint()97     public String getFingerprint() {
98         return mFingerprint;
99     }
100 
101     /**
102      * Get the runtime permissions by packages.
103      *
104      * @return the runtime permissions by packages
105      */
106     @NonNull
getPackagePermissions()107     public Map<String, List<PermissionState>> getPackagePermissions() {
108         return mPackagePermissions;
109     }
110 
111     /**
112      * Get the runtime permissions by shared users.
113      *
114      * @return the runtime permissions by shared users
115      */
116     @NonNull
getSharedUserPermissions()117     public Map<String, List<PermissionState>> getSharedUserPermissions() {
118         return mSharedUserPermissions;
119     }
120 
121     @Override
equals(Object object)122     public boolean equals(Object object) {
123         if (this == object) {
124             return true;
125         }
126         if (object == null || getClass() != object.getClass()) {
127             return false;
128         }
129         RuntimePermissionsState that = (RuntimePermissionsState) object;
130         return mVersion == that.mVersion
131                 && Objects.equals(mFingerprint, that.mFingerprint)
132                 && Objects.equals(mPackagePermissions, that.mPackagePermissions)
133                 && Objects.equals(mSharedUserPermissions, that.mSharedUserPermissions);
134     }
135 
136     @Override
hashCode()137     public int hashCode() {
138         return Objects.hash(mVersion, mFingerprint, mPackagePermissions, mSharedUserPermissions);
139     }
140 
141     /**
142      * State of a single permission.
143      */
144     public static final class PermissionState {
145 
146         /**
147          * The name of the permission.
148          */
149         @NonNull
150         private final String mName;
151 
152         /**
153          * Whether the permission is granted.
154          */
155         private final boolean mGranted;
156 
157         /**
158          * The flags of the permission.
159          */
160         private final int mFlags;
161 
162         /**
163          * Create a new instance of this class.
164          *
165          * @param name the name of the permission
166          * @param granted whether the permission is granted
167          * @param flags the flags of the permission
168          */
PermissionState(@onNull String name, boolean granted, int flags)169         public PermissionState(@NonNull String name, boolean granted, int flags) {
170             mName = name;
171             mGranted = granted;
172             mFlags = flags;
173         }
174 
175         /**
176          * Get the name of the permission.
177          *
178          * @return the name of the permission
179          */
180         @NonNull
getName()181         public String getName() {
182             return mName;
183         }
184 
185         /**
186          * Get whether the permission is granted.
187          *
188          * @return whether the permission is granted
189          */
isGranted()190         public boolean isGranted() {
191             return mGranted;
192         }
193 
194         /**
195          * Get the flags of the permission.
196          *
197          * @return the flags of the permission
198          */
getFlags()199         public int getFlags() {
200             return mFlags;
201         }
202 
203         @Override
equals(Object object)204         public boolean equals(Object object) {
205             if (this == object) {
206                 return true;
207             }
208             if (object == null || getClass() != object.getClass()) {
209                 return false;
210             }
211             PermissionState that = (PermissionState) object;
212             return mGranted == that.mGranted
213                     && mFlags == that.mFlags
214                     && Objects.equals(mName, that.mName);
215         }
216 
217         @Override
hashCode()218         public int hashCode() {
219             return Objects.hash(mName, mGranted, mFlags);
220         }
221     }
222 }
223