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 static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
22import android.app.ActivityManager;
23import android.app.admin.DevicePolicyManager;
24import android.content.Context;
25import android.os.Handler;
26import android.os.IBinder;
27import android.os.Message;
28import android.os.RemoteException;
29import android.os.TokenWatcher;
30import android.util.Log;
31import android.util.Pair;
32import android.view.WindowManagerPolicy;
33
34public class KeyguardDisableHandler extends Handler {
35    private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardDisableHandler" : TAG_WM;
36
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:
74                mAllowDisableKeyguard = ALLOW_DISABLE_UNKNOWN;
75                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                }
86                break;
87        }
88    }
89
90    class KeyguardTokenWatcher extends TokenWatcher {
91
92        public KeyguardTokenWatcher(final Handler handler) {
93            super(handler, TAG);
94        }
95
96        public void updateAllowState() {
97            // We fail safe and prevent disabling keyguard in the unlikely event this gets
98            // called before DevicePolicyManagerService has started.
99            DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
100                    Context.DEVICE_POLICY_SERVICE);
101            if (dpm != null) {
102                try {
103                    mAllowDisableKeyguard = dpm.getPasswordQuality(null,
104                            ActivityManager.getService().getCurrentUser().id)
105                            == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED ?
106                                    ALLOW_DISABLE_YES : ALLOW_DISABLE_NO;
107                } catch (RemoteException re) {
108                    // Nothing much we can do
109                }
110            }
111        }
112
113        @Override
114        public void acquired() {
115            if (mAllowDisableKeyguard == ALLOW_DISABLE_UNKNOWN) {
116                updateAllowState();
117            }
118            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}
131