• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.server.policy.keyguard;
18 
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.util.Slog;
23 
24 import com.android.internal.policy.IKeyguardService;
25 import com.android.internal.policy.IKeyguardStateCallback;
26 import com.android.internal.widget.LockPatternUtils;
27 
28 import java.io.PrintWriter;
29 
30 /**
31  * Maintains a cached copy of Keyguard's state.
32  * @hide
33  */
34 public 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     private final StateCallback mCallback;
53 
KeyguardStateMonitor(Context context, IKeyguardService service, StateCallback callback)54     public KeyguardStateMonitor(Context context, IKeyguardService service, StateCallback callback) {
55         mLockPatternUtils = new LockPatternUtils(context);
56         mCurrentUserId = ActivityManager.getCurrentUser();
57         mCallback = callback;
58 
59         try {
60             service.addStateMonitorCallback(this);
61         } catch (RemoteException e) {
62             Slog.w(TAG, "Remote Exception", e);
63         }
64     }
65 
isShowing()66     public boolean isShowing() {
67         return mIsShowing;
68     }
69 
isSecure(int userId)70     public boolean isSecure(int userId) {
71         return mLockPatternUtils.isSecure(userId) || mSimSecure;
72     }
73 
isInputRestricted()74     public boolean isInputRestricted() {
75         return mInputRestricted;
76     }
77 
isTrusted()78     public boolean isTrusted() {
79         return mTrusted;
80     }
81 
hasLockscreenWallpaper()82     public boolean hasLockscreenWallpaper() {
83         return mHasLockscreenWallpaper;
84     }
85 
86     @Override // Binder interface
onShowingStateChanged(boolean showing)87     public void onShowingStateChanged(boolean showing) {
88         mIsShowing = showing;
89 
90         mCallback.onShowingChanged();
91     }
92 
93     @Override // Binder interface
onSimSecureStateChanged(boolean simSecure)94     public void onSimSecureStateChanged(boolean simSecure) {
95         mSimSecure = simSecure;
96     }
97 
setCurrentUser(int userId)98     public synchronized void setCurrentUser(int userId) {
99         mCurrentUserId = userId;
100     }
101 
102     @Override // Binder interface
onInputRestrictedStateChanged(boolean inputRestricted)103     public void onInputRestrictedStateChanged(boolean inputRestricted) {
104         mInputRestricted = inputRestricted;
105     }
106 
107     @Override // Binder interface
onTrustedChanged(boolean trusted)108     public void onTrustedChanged(boolean trusted) {
109         mTrusted = trusted;
110         mCallback.onTrustedChanged();
111     }
112 
113     @Override // Binder interface
onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper)114     public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
115         mHasLockscreenWallpaper = hasLockscreenWallpaper;
116     }
117 
118     public interface StateCallback {
onTrustedChanged()119         void onTrustedChanged();
onShowingChanged()120         void onShowingChanged();
121     }
122 
dump(String prefix, PrintWriter pw)123     public void dump(String prefix, PrintWriter pw) {
124         pw.println(prefix + TAG);
125         prefix += "  ";
126         pw.println(prefix + "mIsShowing=" + mIsShowing);
127         pw.println(prefix + "mSimSecure=" + mSimSecure);
128         pw.println(prefix + "mInputRestricted=" + mInputRestricted);
129         pw.println(prefix + "mTrusted=" + mTrusted);
130         pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
131     }
132 }
133