blob: 039661349910082d618f4cf985383b9ab4d80cbb [file] [log] [blame]
John Spurlock657c62c2014-07-22 12:18:09 -04001/*
2 * Copyright (C) 2014 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
17package com.android.systemui.statusbar.policy;
18
Jason Monk8a3a9642015-06-05 11:01:30 -040019import android.app.ActivityManager;
20import android.content.Context;
Jason Monkabe19742015-09-29 09:47:06 -040021import android.os.RemoteException;
22import android.view.WindowManagerGlobal;
Winsonc0d70582016-01-29 10:24:39 -080023
Jason Monk8a3a9642015-06-05 11:01:30 -040024import com.android.keyguard.KeyguardUpdateMonitor;
25import com.android.keyguard.KeyguardUpdateMonitorCallback;
26import com.android.systemui.settings.CurrentUserTracker;
Jason Monk88529052016-11-04 13:29:58 -040027import com.android.systemui.statusbar.policy.KeyguardMonitor.Callback;
Jason Monk8a3a9642015-06-05 11:01:30 -040028
John Spurlock657c62c2014-07-22 12:18:09 -040029import java.util.ArrayList;
30
Jason Monk88529052016-11-04 13:29:58 -040031public class KeyguardMonitor extends KeyguardUpdateMonitorCallback
32 implements CallbackController<Callback> {
John Spurlock657c62c2014-07-22 12:18:09 -040033
34 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
35
Jason Monk8a3a9642015-06-05 11:01:30 -040036 private final Context mContext;
37 private final CurrentUserTracker mUserTracker;
38 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
39
40 private int mCurrentUser;
John Spurlock657c62c2014-07-22 12:18:09 -040041 private boolean mShowing;
42 private boolean mSecure;
Jim Miller69c12412016-10-06 19:11:03 -070043 private boolean mOccluded;
Selim Cineke8bae622015-07-15 13:24:06 -070044 private boolean mCanSkipBouncer;
Jason Monk8a3a9642015-06-05 11:01:30 -040045
46 private boolean mListening;
47
48 public KeyguardMonitor(Context context) {
49 mContext = context;
50 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
51 mUserTracker = new CurrentUserTracker(mContext) {
52 @Override
53 public void onUserSwitched(int newUserId) {
54 mCurrentUser = newUserId;
Selim Cineke8bae622015-07-15 13:24:06 -070055 updateCanSkipBouncerState();
Jason Monk8a3a9642015-06-05 11:01:30 -040056 }
57 };
58 }
John Spurlock657c62c2014-07-22 12:18:09 -040059
60 public void addCallback(Callback callback) {
61 mCallbacks.add(callback);
Jason Monk8a3a9642015-06-05 11:01:30 -040062 if (mCallbacks.size() != 0 && !mListening) {
63 mListening = true;
64 mCurrentUser = ActivityManager.getCurrentUser();
Selim Cineke8bae622015-07-15 13:24:06 -070065 updateCanSkipBouncerState();
Jason Monk8a3a9642015-06-05 11:01:30 -040066 mKeyguardUpdateMonitor.registerCallback(this);
67 mUserTracker.startTracking();
68 }
John Spurlock657c62c2014-07-22 12:18:09 -040069 }
70
71 public void removeCallback(Callback callback) {
Jason Monk8a3a9642015-06-05 11:01:30 -040072 if (mCallbacks.remove(callback) && mCallbacks.size() == 0 && mListening) {
73 mListening = false;
74 mKeyguardUpdateMonitor.removeCallback(this);
75 mUserTracker.stopTracking();
76 }
John Spurlock657c62c2014-07-22 12:18:09 -040077 }
78
79 public boolean isShowing() {
80 return mShowing;
81 }
82
83 public boolean isSecure() {
84 return mSecure;
85 }
86
Jim Miller69c12412016-10-06 19:11:03 -070087 public boolean isOccluded() {
88 return mOccluded;
89 }
90
Selim Cineke8bae622015-07-15 13:24:06 -070091 public boolean canSkipBouncer() {
92 return mCanSkipBouncer;
Jason Monk8a3a9642015-06-05 11:01:30 -040093 }
94
Jim Miller69c12412016-10-06 19:11:03 -070095 public void notifyKeyguardState(boolean showing, boolean secure, boolean occluded) {
96 if (mShowing == showing && mSecure == secure && mOccluded == occluded) return;
John Spurlock657c62c2014-07-22 12:18:09 -040097 mShowing = showing;
98 mSecure = secure;
Jim Miller69c12412016-10-06 19:11:03 -070099 mOccluded = occluded;
Jason Monk8a3a9642015-06-05 11:01:30 -0400100 notifyKeyguardChanged();
101 }
102
103 @Override
104 public void onTrustChanged(int userId) {
Selim Cineke8bae622015-07-15 13:24:06 -0700105 updateCanSkipBouncerState();
Jason Monk8a3a9642015-06-05 11:01:30 -0400106 notifyKeyguardChanged();
107 }
108
Jim Miller26e8fc02016-07-13 19:06:24 -0700109 public boolean isDeviceInteractive() {
110 return mKeyguardUpdateMonitor.isDeviceInteractive();
111 }
112
Selim Cineke8bae622015-07-15 13:24:06 -0700113 private void updateCanSkipBouncerState() {
114 mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser);
Jason Monk8a3a9642015-06-05 11:01:30 -0400115 }
116
117 private void notifyKeyguardChanged() {
John Spurlock657c62c2014-07-22 12:18:09 -0400118 for (Callback callback : mCallbacks) {
119 callback.onKeyguardChanged();
120 }
121 }
122
123 public interface Callback {
124 void onKeyguardChanged();
125 }
126}