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