blob: 377071d4c46b98033e26faab6c82e255889a6fb8 [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
Amith Yamasani599dd7c2012-09-14 23:20:08 -070022import android.app.ActivityManagerNative;
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;
32import android.view.WindowManagerPolicy;
33
34public class KeyguardDisableHandler extends Handler {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080035 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardDisableHandler" : TAG_WM;
Craig Mautner5642a482012-08-23 12:16:53 -070036
37 private static final int ALLOW_DISABLE_YES = 1;
38 private static final int ALLOW_DISABLE_NO = 0;
39 private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
40 private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
41
42 // Message.what constants
43 static final int KEYGUARD_DISABLE = 1;
44 static final int KEYGUARD_REENABLE = 2;
45 static final int KEYGUARD_POLICY_CHANGED = 3;
46
47 final Context mContext;
48 final WindowManagerPolicy mPolicy;
49 KeyguardTokenWatcher mKeyguardTokenWatcher;
50
51 public KeyguardDisableHandler(final Context context, final WindowManagerPolicy policy) {
52 mContext = context;
53 mPolicy = policy;
54 }
55
56 @SuppressWarnings("unchecked")
57 @Override
58 public void handleMessage(Message msg) {
59 if (mKeyguardTokenWatcher == null) {
60 mKeyguardTokenWatcher = new KeyguardTokenWatcher(this);
61 }
62
63 switch (msg.what) {
64 case KEYGUARD_DISABLE:
65 final Pair<IBinder, String> pair = (Pair<IBinder, String>)msg.obj;
66 mKeyguardTokenWatcher.acquire(pair.first, pair.second);
67 break;
68
69 case KEYGUARD_REENABLE:
70 mKeyguardTokenWatcher.release((IBinder)msg.obj);
71 break;
72
73 case KEYGUARD_POLICY_CHANGED:
Craig Mautner5642a482012-08-23 12:16:53 -070074 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
Jason Monk4a3daac2014-12-09 14:32:02 -050075 if (mKeyguardTokenWatcher.isAcquired()) {
76 // If we are currently disabled we need to know if the keyguard
77 // should be re-enabled, so determine the allow state immediately.
78 mKeyguardTokenWatcher.updateAllowState();
79 if (mAllowDisableKeyguard != ALLOW_DISABLE_YES) {
80 mPolicy.enableKeyguard(true);
81 }
82 } else {
83 // lazily evaluate this next time we're asked to disable keyguard
84 mPolicy.enableKeyguard(true);
85 }
Craig Mautner5642a482012-08-23 12:16:53 -070086 break;
87 }
88 }
89
90 class KeyguardTokenWatcher extends TokenWatcher {
91
92 public KeyguardTokenWatcher(final Handler handler) {
93 super(handler, TAG);
94 }
95
Jason Monk4a3daac2014-12-09 14:32:02 -050096 public void updateAllowState() {
Craig Mautner5642a482012-08-23 12:16:53 -070097 // We fail safe and prevent disabling keyguard in the unlikely event this gets
98 // called before DevicePolicyManagerService has started.
Jason Monk4a3daac2014-12-09 14:32:02 -050099 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
100 Context.DEVICE_POLICY_SERVICE);
101 if (dpm != null) {
102 try {
103 mAllowDisableKeyguard = dpm.getPasswordQuality(null,
104 ActivityManagerNative.getDefault().getCurrentUser().id)
105 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
106 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
107 } catch (RemoteException re) {
108 // Nothing much we can do
Craig Mautner5642a482012-08-23 12:16:53 -0700109 }
110 }
Jason Monk4a3daac2014-12-09 14:32:02 -0500111 }
112
113 @Override
114 public void acquired() {
115 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
116 updateAllowState();
117 }
Craig Mautner5642a482012-08-23 12:16:53 -0700118 if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) {
119 mPolicy.enableKeyguard(false);
120 } else {
121 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
122 }
123 }
124
125 @Override
126 public void released() {
127 mPolicy.enableKeyguard(true);
128 }
129 }
130}