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