2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.policy;
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.os.RemoteException;
22 import android.view.WindowManagerGlobal;
24 import com.android.keyguard.KeyguardUpdateMonitor;
25 import com.android.keyguard.KeyguardUpdateMonitorCallback;
26 import com.android.systemui.settings.CurrentUserTracker;
28 import java.util.ArrayList;
30 public final class KeyguardMonitor extends KeyguardUpdateMonitorCallback {
32 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
34 private final Context mContext;
35 private final CurrentUserTracker mUserTracker;
36 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
38 private int mCurrentUser;
39 private boolean mShowing;
40 private boolean mSecure;
41 private boolean mCanSkipBouncer;
43 private boolean mListening;
45 public KeyguardMonitor(Context context) {
47 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
48 mUserTracker = new CurrentUserTracker(mContext) {
50 public void onUserSwitched(int newUserId) {
51 mCurrentUser = newUserId;
52 updateCanSkipBouncerState();
57 public void addCallback(Callback callback) {
58 mCallbacks.add(callback);
59 if (mCallbacks.size() != 0 && !mListening) {
61 mCurrentUser = ActivityManager.getCurrentUser();
62 updateCanSkipBouncerState();
63 mKeyguardUpdateMonitor.registerCallback(this);
64 mUserTracker.startTracking();
68 public void removeCallback(Callback callback) {
69 if (mCallbacks.remove(callback) && mCallbacks.size() == 0 && mListening) {
71 mKeyguardUpdateMonitor.removeCallback(this);
72 mUserTracker.stopTracking();
76 public boolean isShowing() {
80 public boolean isSecure() {
84 public boolean canSkipBouncer() {
85 return mCanSkipBouncer;
88 public void unlock() {
90 WindowManagerGlobal.getWindowManagerService().dismissKeyguard();
91 } catch (RemoteException e) {
97 WindowManagerGlobal.getWindowManagerService().lockNow(null /* options */);
98 } catch (RemoteException e) {
102 public void notifyKeyguardState(boolean showing, boolean secure) {
103 if (mShowing == showing && mSecure == secure) return;
106 notifyKeyguardChanged();
110 public void onTrustChanged(int userId) {
111 updateCanSkipBouncerState();
112 notifyKeyguardChanged();
115 public boolean isDeviceInteractive() {
116 return mKeyguardUpdateMonitor.isDeviceInteractive();
119 private void updateCanSkipBouncerState() {
120 mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser);
123 private void notifyKeyguardChanged() {
124 for (Callback callback : mCallbacks) {
125 callback.onKeyguardChanged();
129 public interface Callback {
130 void onKeyguardChanged();