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