• 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.server.pm.permission;
18 
19 import android.annotation.AppIdInt;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.UserHandle;
23 import android.util.ArraySet;
24 import android.util.SparseArray;
25 
26 /**
27  * Permission state for a user.
28  */
29 public final class UserPermissionState {
30     /**
31      * Whether the install permissions have been granted to a package, so that no install
32      * permissions should be added to it unless the package is upgraded.
33      */
34     @NonNull
35     private final ArraySet<String> mInstallPermissionsFixed = new ArraySet<>();
36 
37     /**
38      * Maps from app ID to {@link UidPermissionState}.
39      */
40     @NonNull
41     private final SparseArray<UidPermissionState> mUidStates = new SparseArray<>();
42 
areInstallPermissionsFixed(@onNull String packageName)43     public boolean areInstallPermissionsFixed(@NonNull String packageName) {
44         return mInstallPermissionsFixed.contains(packageName);
45     }
46 
setInstallPermissionsFixed(@onNull String packageName, boolean fixed)47     public void setInstallPermissionsFixed(@NonNull String packageName, boolean fixed) {
48         if (fixed) {
49             mInstallPermissionsFixed.add(packageName);
50         } else {
51             mInstallPermissionsFixed.remove(packageName);
52         }
53     }
54 
55     @Nullable
getUidState(@ppIdInt int appId)56     public UidPermissionState getUidState(@AppIdInt int appId) {
57         checkAppId(appId);
58         return mUidStates.get(appId);
59     }
60 
61     @NonNull
getOrCreateUidState(@ppIdInt int appId)62     public UidPermissionState getOrCreateUidState(@AppIdInt int appId) {
63         checkAppId(appId);
64         UidPermissionState uidState = mUidStates.get(appId);
65         if (uidState == null) {
66             uidState = new UidPermissionState();
67             mUidStates.put(appId, uidState);
68         }
69         return uidState;
70     }
71 
removeUidState(@ppIdInt int appId)72     public void removeUidState(@AppIdInt int appId) {
73         checkAppId(appId);
74         mUidStates.delete(appId);
75     }
76 
checkAppId(@ppIdInt int appId)77     private void checkAppId(@AppIdInt int appId) {
78         if (UserHandle.getUserId(appId) != 0) {
79             throw new IllegalArgumentException("Invalid app ID " + appId);
80         }
81     }
82 }
83