1 /*
2  * Copyright (C) 2018 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 package android.app.admin;
17 
18 import android.annotation.UserIdInt;
19 
20 import com.android.server.LocalServices;
21 
22 /**
23  * Stores a copy of the set of device policies maintained by {@link DevicePolicyManager} that
24  * can be accessed from any place without risking dead locks.
25  *
26  * @hide
27  */
28 public abstract class DevicePolicyCache {
DevicePolicyCache()29     protected DevicePolicyCache() {
30     }
31 
32     /**
33      * @return the instance.
34      */
getInstance()35     public static DevicePolicyCache getInstance() {
36         final DevicePolicyManagerInternal dpmi =
37                 LocalServices.getService(DevicePolicyManagerInternal.class);
38         return (dpmi != null) ? dpmi.getDevicePolicyCache() : EmptyDevicePolicyCache.INSTANCE;
39     }
40 
41     /**
42      * See {@link DevicePolicyManager#getScreenCaptureDisabled}
43      */
isScreenCaptureAllowed(@serIdInt int userHandle, boolean ownerCanAddInternalSystemWindow)44     public abstract boolean isScreenCaptureAllowed(@UserIdInt int userHandle,
45             boolean ownerCanAddInternalSystemWindow);
46 
47     /**
48      * Caches {@link DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} of the
49      * given user with {@code null} passed in as argument.
50      */
getPasswordQuality(@serIdInt int userHandle)51     public abstract int getPasswordQuality(@UserIdInt int userHandle);
52 
53     /**
54      * Caches {@link DevicePolicyManager#getPermissionPolicy(android.content.ComponentName)} of
55      * the given user.
56      */
getPermissionPolicy(@serIdInt int userHandle)57     public abstract int getPermissionPolicy(@UserIdInt int userHandle);
58 
59     /**
60      * Caches {@link DevicePolicyManager#canAdminGrantSensorsPermissionsForUser(int)} for the
61      * given user.
62      */
canAdminGrantSensorsPermissionsForUser(@serIdInt int userHandle)63     public abstract boolean canAdminGrantSensorsPermissionsForUser(@UserIdInt int userHandle);
64 
65 
66     /**
67      * Empty implementation.
68      */
69     private static class EmptyDevicePolicyCache extends DevicePolicyCache {
70         private static final EmptyDevicePolicyCache INSTANCE = new EmptyDevicePolicyCache();
71 
72         @Override
isScreenCaptureAllowed(int userHandle, boolean ownerCanAddInternalSystemWindow)73         public boolean isScreenCaptureAllowed(int userHandle,
74                 boolean ownerCanAddInternalSystemWindow) {
75             return true;
76         }
77 
78         @Override
getPasswordQuality(int userHandle)79         public int getPasswordQuality(int userHandle) {
80             return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
81         }
82 
83         @Override
getPermissionPolicy(int userHandle)84         public int getPermissionPolicy(int userHandle) {
85             return DevicePolicyManager.PERMISSION_POLICY_PROMPT;
86         }
87 
88         @Override
canAdminGrantSensorsPermissionsForUser(int userHandle)89         public boolean canAdminGrantSensorsPermissionsForUser(int userHandle) {
90             return false;
91         }
92     }
93 }
94