KeyguardStateMonitor.java revision d11d1a9486d44b98e28b70c25711ebfc283b746e
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    private volatile boolean mTrusted = false;
47    private volatile boolean mHasLockscreenWallpaper = false;
48
49    private int mCurrentUserId;
50
51    private final LockPatternUtils mLockPatternUtils;
52
53    public KeyguardStateMonitor(Context context, IKeyguardService service) {
54        mLockPatternUtils = new LockPatternUtils(context);
55        mCurrentUserId = ActivityManager.getCurrentUser();
56        try {
57            service.addStateMonitorCallback(this);
58        } catch (RemoteException e) {
59            Slog.w(TAG, "Remote Exception", e);
60        }
61    }
62
63    public boolean isShowing() {
64        return mIsShowing;
65    }
66
67    public boolean isSecure(int userId) {
68        return mLockPatternUtils.isSecure(userId) || mSimSecure;
69    }
70
71    public boolean isInputRestricted() {
72        return mInputRestricted;
73    }
74
75    public boolean isTrusted() {
76        return mTrusted;
77    }
78
79    public boolean hasLockscreenWallpaper() {
80        return mHasLockscreenWallpaper;
81    }
82
83    @Override // Binder interface
84    public void onShowingStateChanged(boolean showing) {
85        mIsShowing = showing;
86    }
87
88    @Override // Binder interface
89    public void onSimSecureStateChanged(boolean simSecure) {
90        mSimSecure = simSecure;
91    }
92
93    public synchronized void setCurrentUser(int userId) {
94        mCurrentUserId = userId;
95    }
96
97    private synchronized int getCurrentUser() {
98        return mCurrentUserId;
99    }
100
101    @Override // Binder interface
102    public void onInputRestrictedStateChanged(boolean inputRestricted) {
103        mInputRestricted = inputRestricted;
104    }
105
106    @Override // Binder interface
107    public void onTrustedChanged(boolean trusted) {
108        mTrusted = trusted;
109    }
110
111    @Override // Binder interface
112    public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
113        mHasLockscreenWallpaper = hasLockscreenWallpaper;
114    }
115
116    public void dump(String prefix, PrintWriter pw) {
117        pw.println(prefix + TAG);
118        prefix += "  ";
119        pw.println(prefix + "mIsShowing=" + mIsShowing);
120        pw.println(prefix + "mSimSecure=" + mSimSecure);
121        pw.println(prefix + "mInputRestricted=" + mInputRestricted);
122        pw.println(prefix + "mTrusted=" + mTrusted);
123        pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
124    }
125}