1/*
2 * Copyright (C) 2013 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.internal.policy.impl.keyguard;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.os.RemoteException;
22import android.util.Slog;
23
24import com.android.internal.policy.IKeyguardService;
25import com.android.internal.policy.IKeyguardStateCallback;
26import com.android.internal.widget.LockPatternUtils;
27
28/**
29 * Maintains a cached copy of Keyguard's state.
30 * @hide
31 */
32public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub {
33    private static final String TAG = "KeyguardStateMonitor";
34
35    // These cache the current state of Keyguard to improve performance and avoid deadlock. After
36    // Keyguard changes its state, it always triggers a layout in window manager. Because
37    // IKeyguardStateCallback is synchronous and because these states are declared volatile, it's
38    // guaranteed that window manager picks up the new state all the time in the layout caused by
39    // the state change of Keyguard.
40    private volatile boolean mIsShowing;
41    private volatile boolean mSimSecure;
42    private volatile boolean mInputRestricted;
43
44    private final LockPatternUtils mLockPatternUtils;
45
46    public KeyguardStateMonitor(Context context, IKeyguardService service) {
47        mLockPatternUtils = new LockPatternUtils(context);
48        mLockPatternUtils.setCurrentUser(ActivityManager.getCurrentUser());
49        try {
50            service.addStateMonitorCallback(this);
51        } catch (RemoteException e) {
52            Slog.w(TAG, "Remote Exception", e);
53        }
54    }
55
56    public boolean isShowing() {
57        return mIsShowing;
58    }
59
60    public boolean isSecure() {
61        return mLockPatternUtils.isSecure() || mSimSecure;
62    }
63
64    public boolean isInputRestricted() {
65        return mInputRestricted;
66    }
67
68    @Override // Binder interface
69    public void onShowingStateChanged(boolean showing) {
70        mIsShowing = showing;
71    }
72
73    @Override // Binder interface
74    public void onSimSecureStateChanged(boolean simSecure) {
75        mSimSecure = simSecure;
76    }
77
78    public void setCurrentUser(int userId) {
79        mLockPatternUtils.setCurrentUser(userId);
80    }
81
82    @Override // Binder interface
83    public void onInputRestrictedStateChanged(boolean inputRestricted) {
84        mInputRestricted = inputRestricted;
85    }
86}