KeyguardStateMonitor.java revision e4044bb617ea849939058d953e250fcd540c75cc
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.server.policy.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
28import java.io.PrintWriter;
29
30/**
31 * Maintains a cached copy of Keyguard's state.
32 * @hide
33 */
34public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub {
35    private static final String TAG = "KeyguardStateMonitor";
36
37    // These cache the current state of Keyguard to improve performance and avoid deadlock. After
38    // Keyguard changes its state, it always triggers a layout in window manager. Because
39    // IKeyguardStateCallback is synchronous and because these states are declared volatile, it's
40    // guaranteed that window manager picks up the new state all the time in the layout caused by
41    // the state change of Keyguard. To be extra safe, assume most restrictive values until Keyguard
42    // tells us the actual value.
43    private volatile boolean mIsShowing = true;
44    private volatile boolean mSimSecure = true;
45    private volatile boolean mInputRestricted = true;
46
47    private int mCurrentUserId;
48
49    private final LockPatternUtils mLockPatternUtils;
50
51    public KeyguardStateMonitor(Context context, IKeyguardService service) {
52        mLockPatternUtils = new LockPatternUtils(context);
53        mCurrentUserId = ActivityManager.getCurrentUser();
54        try {
55            service.addStateMonitorCallback(this);
56        } catch (RemoteException e) {
57            Slog.w(TAG, "Remote Exception", e);
58        }
59    }
60
61    public boolean isShowing() {
62        return mIsShowing;
63    }
64
65    public boolean isSecure(int userId) {
66        return mLockPatternUtils.isSecure(userId) || mSimSecure;
67    }
68
69    public boolean isInputRestricted() {
70        return mInputRestricted;
71    }
72
73    @Override // Binder interface
74    public void onShowingStateChanged(boolean showing) {
75        mIsShowing = showing;
76    }
77
78    @Override // Binder interface
79    public void onSimSecureStateChanged(boolean simSecure) {
80        mSimSecure = simSecure;
81    }
82
83    public synchronized void setCurrentUser(int userId) {
84        mCurrentUserId = userId;
85    }
86
87    private synchronized int getCurrentUser() {
88        return mCurrentUserId;
89    }
90
91    @Override // Binder interface
92    public void onInputRestrictedStateChanged(boolean inputRestricted) {
93        mInputRestricted = inputRestricted;
94    }
95
96    public void dump(String prefix, PrintWriter pw) {
97        pw.println(prefix + TAG);
98        prefix += "  ";
99        pw.println(prefix + "mIsShowing=" + mIsShowing);
100        pw.println(prefix + "mSimSecure=" + mSimSecure);
101        pw.println(prefix + "mInputRestricted=" + mInputRestricted);
102        pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
103    }
104}