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