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 static android.app.admin.DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
19 import static android.app.admin.DevicePolicyManager.ContentProtectionPolicy;
20 
21 import android.annotation.UserIdInt;
22 
23 import com.android.server.LocalServices;
24 
25 import java.util.Collections;
26 import java.util.Map;
27 
28 /**
29  * Stores a copy of the set of device policies maintained by {@link DevicePolicyManager} that
30  * can be accessed from any place without risking dead locks.
31  *
32  * @hide
33  */
34 public abstract class DevicePolicyCache {
DevicePolicyCache()35     protected DevicePolicyCache() {
36     }
37 
38     /**
39      * @return the instance.
40      */
getInstance()41     public static DevicePolicyCache getInstance() {
42         final DevicePolicyManagerInternal dpmi =
43                 LocalServices.getService(DevicePolicyManagerInternal.class);
44         return (dpmi != null) ? dpmi.getDevicePolicyCache() : EmptyDevicePolicyCache.INSTANCE;
45     }
46 
47     /**
48      * See {@link DevicePolicyManager#getScreenCaptureDisabled}
49      */
isScreenCaptureAllowed(@serIdInt int userHandle)50     public abstract boolean isScreenCaptureAllowed(@UserIdInt int userHandle);
51 
52     /**
53      * Caches {@link DevicePolicyManager#getPasswordQuality(android.content.ComponentName)} of the
54      * given user with {@code null} passed in as argument.
55      */
getPasswordQuality(@serIdInt int userHandle)56     public abstract int getPasswordQuality(@UserIdInt int userHandle);
57 
58     /**
59      * Caches {@link DevicePolicyManager#getPermissionPolicy(android.content.ComponentName)} of
60      * the given user.
61      */
getPermissionPolicy(@serIdInt int userHandle)62     public abstract int getPermissionPolicy(@UserIdInt int userHandle);
63 
64     /**
65      * Caches {@link DevicePolicyManager#getContentProtectionPolicy(android.content.ComponentName)}
66      * of the given user.
67      */
getContentProtectionPolicy(@serIdInt int userId)68     public abstract @ContentProtectionPolicy int getContentProtectionPolicy(@UserIdInt int userId);
69 
70     /**
71      * True if there is an admin on the device who can grant sensor permissions.
72      */
canAdminGrantSensorsPermissions()73     public abstract boolean canAdminGrantSensorsPermissions();
74 
75     /**
76      * Returns a map of package names to package names, for which all launcher shortcuts which
77      * match a key package name should be modified to launch the corresponding value package
78      * name in the managed profile. The overridden shortcut should be badged accordingly.
79      */
getLauncherShortcutOverrides()80     public abstract Map<String, String> getLauncherShortcutOverrides();
81 
82     /**
83      * Empty implementation.
84      */
85     private static class EmptyDevicePolicyCache extends DevicePolicyCache {
86         private static final EmptyDevicePolicyCache INSTANCE = new EmptyDevicePolicyCache();
87 
88         @Override
isScreenCaptureAllowed(int userHandle)89         public boolean isScreenCaptureAllowed(int userHandle) {
90             return true;
91         }
92 
93         @Override
getPasswordQuality(int userHandle)94         public int getPasswordQuality(int userHandle) {
95             return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
96         }
97 
98         @Override
getPermissionPolicy(int userHandle)99         public int getPermissionPolicy(int userHandle) {
100             return DevicePolicyManager.PERMISSION_POLICY_PROMPT;
101         }
102 
103         @Override
getContentProtectionPolicy(@serIdInt int userId)104         public @ContentProtectionPolicy int getContentProtectionPolicy(@UserIdInt int userId) {
105             return CONTENT_PROTECTION_DISABLED;
106         }
107 
108         @Override
canAdminGrantSensorsPermissions()109         public boolean canAdminGrantSensorsPermissions() {
110             return false;
111         }
112         @Override
getLauncherShortcutOverrides()113         public Map<String, String>  getLauncherShortcutOverrides() {
114             return Collections.EMPTY_MAP;
115         }
116     }
117 }
118