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 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 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 } 90 91 @Override // Binder interface 92 public void onSimSecureStateChanged(boolean simSecure) { 93 mSimSecure = simSecure; 94 } 95 96 public synchronized void setCurrentUser(int userId) { 97 mCurrentUserId = userId; 98 } 99 100 private synchronized int getCurrentUser() { 101 return mCurrentUserId; 102 } 103 104 @Override // Binder interface 105 public void onInputRestrictedStateChanged(boolean inputRestricted) { 106 mInputRestricted = inputRestricted; 107 } 108 109 @Override // Binder interface 110 public void onTrustedChanged(boolean trusted) { 111 mTrusted = trusted; 112 mCallback.onTrustedChanged(); 113 } 114 115 @Override // Binder interface 116 public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) { 117 mHasLockscreenWallpaper = hasLockscreenWallpaper; 118 } 119 120 public interface StateCallback { 121 void onTrustedChanged(); 122 } 123 124 public void dump(String prefix, PrintWriter pw) { 125 pw.println(prefix + TAG); 126 prefix += " "; 127 pw.println(prefix + "mIsShowing=" + mIsShowing); 128 pw.println(prefix + "mSimSecure=" + mSimSecure); 129 pw.println(prefix + "mInputRestricted=" + mInputRestricted); 130 pw.println(prefix + "mTrusted=" + mTrusted); 131 pw.println(prefix + "mCurrentUserId=" + mCurrentUserId); 132 } 133 }