blob: 4a20f1a039eff4c39d85f2a7d0b5df7ec4d8d013 [file] [log] [blame]
Craig Mautner5642a482012-08-23 12:16:53 -07001/*
2 * Copyright (C) 2012 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
17package com.android.server.wm;
18
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
Sudheer Shankadc589ac2016-11-10 15:30:17 -080022import android.app.ActivityManager;
Craig Mautner5642a482012-08-23 12:16:53 -070023import android.app.admin.DevicePolicyManager;
24import android.content.Context;
25import android.os.Handler;
26import android.os.IBinder;
27import android.os.Message;
Amith Yamasani599dd7c2012-09-14 23:20:08 -070028import android.os.RemoteException;
Craig Mautner5642a482012-08-23 12:16:53 -070029import android.os.TokenWatcher;
30import android.util.Log;
31import android.util.Pair;
Adrian Roose99bc052017-11-20 17:55:31 +010032
33import com.android.server.policy.WindowManagerPolicy;
Craig Mautner5642a482012-08-23 12:16:53 -070034
35public class KeyguardDisableHandler extends Handler {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080036 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardDisableHandler" : TAG_WM;
Craig Mautner5642a482012-08-23 12:16:53 -070037
38 private static final int ALLOW_DISABLE_YES = 1;
39 private static final int ALLOW_DISABLE_NO = 0;
40 private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
41 private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
42
43 // Message.what constants
44 static final int KEYGUARD_DISABLE = 1;
45 static final int KEYGUARD_REENABLE = 2;
46 static final int KEYGUARD_POLICY_CHANGED = 3;
47
48 final Context mContext;
49 final WindowManagerPolicy mPolicy;
50 KeyguardTokenWatcher mKeyguardTokenWatcher;
51
52 public KeyguardDisableHandler(final Context context, final WindowManagerPolicy policy) {
53 mContext = context;
54 mPolicy = policy;
55 }
56
57 @SuppressWarnings("unchecked")
58 @Override
59 public void handleMessage(Message msg) {
60 if (mKeyguardTokenWatcher == null) {
61 mKeyguardTokenWatcher = new KeyguardTokenWatcher(this);
62 }
63
64 switch (msg.what) {
65 case KEYGUARD_DISABLE:
66 final Pair<IBinder, String> pair = (Pair<IBinder, String>)msg.obj;
67 mKeyguardTokenWatcher.acquire(pair.first, pair.second);
68 break;
69
70 case KEYGUARD_REENABLE:
71 mKeyguardTokenWatcher.release((IBinder)msg.obj);
72 break;
73
74 case KEYGUARD_POLICY_CHANGED:
Craig Mautner5642a482012-08-23 12:16:53 -070075 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
Jason Monk4a3daac2014-12-09 14:32:02 -050076 if (mKeyguardTokenWatcher.isAcquired()) {
77 // If we are currently disabled we need to know if the keyguard
78 // should be re-enabled, so determine the allow state immediately.
79 mKeyguardTokenWatcher.updateAllowState();
80 if (mAllowDisableKeyguard != ALLOW_DISABLE_YES) {
81 mPolicy.enableKeyguard(true);
82 }
83 } else {
84 // lazily evaluate this next time we're asked to disable keyguard
85 mPolicy.enableKeyguard(true);
86 }
Craig Mautner5642a482012-08-23 12:16:53 -070087 break;
88 }
89 }
90
91 class KeyguardTokenWatcher extends TokenWatcher {
92
93 public KeyguardTokenWatcher(final Handler handler) {
94 super(handler, TAG);
95 }
96
Jason Monk4a3daac2014-12-09 14:32:02 -050097 public void updateAllowState() {
Craig Mautner5642a482012-08-23 12:16:53 -070098 // We fail safe and prevent disabling keyguard in the unlikely event this gets
99 // called before DevicePolicyManagerService has started.
Jason Monk4a3daac2014-12-09 14:32:02 -0500100 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
101 Context.DEVICE_POLICY_SERVICE);
102 if (dpm != null) {
103 try {
104 mAllowDisableKeyguard = dpm.getPasswordQuality(null,
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800105 ActivityManager.getService().getCurrentUser().id)
Jason Monk4a3daac2014-12-09 14:32:02 -0500106 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
107 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
108 } catch (RemoteException re) {
109 // Nothing much we can do
Craig Mautner5642a482012-08-23 12:16:53 -0700110 }
111 }
Jason Monk4a3daac2014-12-09 14:32:02 -0500112 }
113
114 @Override
115 public void acquired() {
116 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
117 updateAllowState();
118 }
Craig Mautner5642a482012-08-23 12:16:53 -0700119 if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) {
120 mPolicy.enableKeyguard(false);
121 } else {
122 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
123 }
124 }
125
126 @Override
127 public void released() {
128 mPolicy.enableKeyguard(true);
129 }
130 }
131}