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 /** 29 * Maintains a cached copy of Keyguard's state. 30 * @hide 31 */ 32 public class KeyguardStateMonitor extends IKeyguardStateCallback.Stub { 33 private static final String TAG = "KeyguardStateMonitor"; 34 35 // These cache the current state of Keyguard to improve performance and avoid deadlock. After 36 // Keyguard changes its state, it always triggers a layout in window manager. Because 37 // IKeyguardStateCallback is synchronous and because these states are declared volatile, it's 38 // guaranteed that window manager picks up the new state all the time in the layout caused by 39 // the state change of Keyguard. To be extra safe, assume most restrictive values until Keyguard 40 // tells us the actual value. 41 private volatile boolean mIsShowing = true; 42 private volatile boolean mSimSecure = true; 43 private volatile boolean mInputRestricted = true; 44 45 private int mCurrentUserId; 46 47 private final LockPatternUtils mLockPatternUtils; 48 49 public KeyguardStateMonitor(Context context, IKeyguardService service) { 50 mLockPatternUtils = new LockPatternUtils(context); 51 mCurrentUserId = ActivityManager.getCurrentUser(); 52 try { 53 service.addStateMonitorCallback(this); 54 } catch (RemoteException e) { 55 Slog.w(TAG, "Remote Exception", e); 56 } 57 } 58 59 public boolean isShowing() { 60 return mIsShowing; 61 } 62 63 public boolean isSecure() { 64 return mLockPatternUtils.isSecure(getCurrentUser()) || mSimSecure; 65 } 66 67 public boolean isInputRestricted() { 68 return mInputRestricted; 69 } 70 71 @Override // Binder interface 72 public void onShowingStateChanged(boolean showing) { 73 mIsShowing = showing; 74 } 75 76 @Override // Binder interface 77 public void onSimSecureStateChanged(boolean simSecure) { 78 mSimSecure = simSecure; 79 } 80 81 public synchronized void setCurrentUser(int userId) { 82 mCurrentUserId = userId; 83 } 84 85 private synchronized int getCurrentUser() { 86 return mCurrentUserId; 87 } 88 89 @Override // Binder interface 90 public void onInputRestrictedStateChanged(boolean inputRestricted) { 91 mInputRestricted = inputRestricted; 92 } 93 }