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 com.android.server.devicepolicy;
17 
18 import android.annotation.UserIdInt;
19 import android.app.admin.DevicePolicyCache;
20 import android.app.admin.DevicePolicyManager;
21 import android.util.SparseBooleanArray;
22 import android.util.SparseIntArray;
23 
24 import com.android.internal.annotations.GuardedBy;
25 
26 import java.io.PrintWriter;
27 
28 /**
29  * Implementation of {@link DevicePolicyCache}, to which {@link DevicePolicyManagerService} pushes
30  * policies.
31  *
32  * TODO Move other copies of policies into this class too.
33  */
34 public class DevicePolicyCacheImpl extends DevicePolicyCache {
35     /**
36      * Lock object. For simplicity we just always use this as the lock. We could use each object
37      * as a lock object to make it more fine-grained, but that'd make copy-paste error-prone.
38      */
39     private final Object mLock = new Object();
40 
41     @GuardedBy("mLock")
42     private final SparseBooleanArray mScreenCaptureDisabled = new SparseBooleanArray();
43 
44     @GuardedBy("mLock")
45     private final SparseIntArray mPasswordQuality = new SparseIntArray();
46 
onUserRemoved(int userHandle)47     public void onUserRemoved(int userHandle) {
48         synchronized (mLock) {
49             mScreenCaptureDisabled.delete(userHandle);
50             mPasswordQuality.delete(userHandle);
51         }
52     }
53 
54     @Override
getScreenCaptureDisabled(int userHandle)55     public boolean getScreenCaptureDisabled(int userHandle) {
56         synchronized (mLock) {
57             return mScreenCaptureDisabled.get(userHandle);
58         }
59     }
60 
setScreenCaptureDisabled(int userHandle, boolean disabled)61     public void setScreenCaptureDisabled(int userHandle, boolean disabled) {
62         synchronized (mLock) {
63             mScreenCaptureDisabled.put(userHandle, disabled);
64         }
65     }
66 
67     @Override
getPasswordQuality(@serIdInt int userHandle)68     public int getPasswordQuality(@UserIdInt int userHandle) {
69         synchronized (mLock) {
70             return mPasswordQuality.get(userHandle,
71                     DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
72         }
73     }
74 
75     /** Updat the password quality cache for the given user */
setPasswordQuality(int userHandle, int quality)76     public void setPasswordQuality(int userHandle, int quality) {
77         synchronized (mLock) {
78             mPasswordQuality.put(userHandle, quality);
79         }
80     }
81 
82     /** Dump content */
dump(String prefix, PrintWriter pw)83     public void dump(String prefix, PrintWriter pw) {
84         pw.println("Device policy cache");
85         pw.println(prefix + "Screen capture disabled: " + mScreenCaptureDisabled.toString());
86         pw.println(prefix + "Password quality: " + mPasswordQuality.toString());
87     }
88 }
89