blob: c1420a86c3dc7cf0a7efba0bacbc0549dc240604 [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
Amith Yamasani599dd7c2012-09-14 23:20:08 -070019import android.app.ActivityManagerNative;
Craig Mautner5642a482012-08-23 12:16:53 -070020import android.app.admin.DevicePolicyManager;
21import android.content.Context;
22import android.os.Handler;
23import android.os.IBinder;
24import android.os.Message;
Amith Yamasani599dd7c2012-09-14 23:20:08 -070025import android.os.RemoteException;
Craig Mautner5642a482012-08-23 12:16:53 -070026import android.os.TokenWatcher;
27import android.util.Log;
28import android.util.Pair;
29import android.view.WindowManagerPolicy;
30
31public class KeyguardDisableHandler extends Handler {
32 private static final String TAG = "KeyguardDisableHandler";
33
34 private static final int ALLOW_DISABLE_YES = 1;
35 private static final int ALLOW_DISABLE_NO = 0;
36 private static final int ALLOW_DISABLE_UNKNOWN = -1; // check with DevicePolicyManager
37 private int mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN; // sync'd by mKeyguardTokenWatcher
38
39 // Message.what constants
40 static final int KEYGUARD_DISABLE = 1;
41 static final int KEYGUARD_REENABLE = 2;
42 static final int KEYGUARD_POLICY_CHANGED = 3;
43
44 final Context mContext;
45 final WindowManagerPolicy mPolicy;
46 KeyguardTokenWatcher mKeyguardTokenWatcher;
47
48 public KeyguardDisableHandler(final Context context, final WindowManagerPolicy policy) {
49 mContext = context;
50 mPolicy = policy;
51 }
52
53 @SuppressWarnings("unchecked")
54 @Override
55 public void handleMessage(Message msg) {
56 if (mKeyguardTokenWatcher == null) {
57 mKeyguardTokenWatcher = new KeyguardTokenWatcher(this);
58 }
59
60 switch (msg.what) {
61 case KEYGUARD_DISABLE:
62 final Pair<IBinder, String> pair = (Pair<IBinder, String>)msg.obj;
63 mKeyguardTokenWatcher.acquire(pair.first, pair.second);
64 break;
65
66 case KEYGUARD_REENABLE:
67 mKeyguardTokenWatcher.release((IBinder)msg.obj);
68 break;
69
70 case KEYGUARD_POLICY_CHANGED:
71 mPolicy.enableKeyguard(true);
72 // lazily evaluate this next time we're asked to disable keyguard
73 mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
74 break;
75 }
76 }
77
78 class KeyguardTokenWatcher extends TokenWatcher {
79
80 public KeyguardTokenWatcher(final Handler handler) {
81 super(handler, TAG);
82 }
83
84 @Override
85 public void acquired() {
86 // We fail safe and prevent disabling keyguard in the unlikely event this gets
87 // called before DevicePolicyManagerService has started.
88 if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
89 DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
90 Context.DEVICE_POLICY_SERVICE);
91 if (dpm != null) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -070092 try {
93 mAllowDisableKeyguard = dpm.getPasswordQuality(null,
94 ActivityManagerNative.getDefault().getCurrentUser().id)
95 == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
96 ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
97 } catch (RemoteException re) {
98 // Nothing much we can do
99 }
Craig Mautner5642a482012-08-23 12:16:53 -0700100 }
101 }
102 if (mAllowDisableKeyguard == ALLOW_DISABLE_YES) {
103 mPolicy.enableKeyguard(false);
104 } else {
105 Log.v(TAG, "Not disabling keyguard since device policy is enforced");
106 }
107 }
108
109 @Override
110 public void released() {
111 mPolicy.enableKeyguard(true);
112 }
113 }
114}