John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
| 20 | import android.content.Context; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.view.WindowManagerGlobal; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 23 | |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 24 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 25 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
| 26 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 27 | import com.android.systemui.statusbar.policy.KeyguardMonitor.Callback; |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 28 | |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 31 | public class KeyguardMonitor extends KeyguardUpdateMonitorCallback |
| 32 | implements CallbackController<Callback> { |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 33 | |
| 34 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
| 35 | |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 36 | private final Context mContext; |
| 37 | private final CurrentUserTracker mUserTracker; |
| 38 | private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
| 39 | |
| 40 | private int mCurrentUser; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 41 | private boolean mShowing; |
| 42 | private boolean mSecure; |
Jim Miller | 69c1241 | 2016-10-06 19:11:03 -0700 | [diff] [blame] | 43 | private boolean mOccluded; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 44 | private boolean mCanSkipBouncer; |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 45 | |
| 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 Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 55 | updateCanSkipBouncerState(); |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | } |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 59 | |
| 60 | public void addCallback(Callback callback) { |
| 61 | mCallbacks.add(callback); |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 62 | if (mCallbacks.size() != 0 && !mListening) { |
| 63 | mListening = true; |
| 64 | mCurrentUser = ActivityManager.getCurrentUser(); |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 65 | updateCanSkipBouncerState(); |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 66 | mKeyguardUpdateMonitor.registerCallback(this); |
| 67 | mUserTracker.startTracking(); |
| 68 | } |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public void removeCallback(Callback callback) { |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 72 | if (mCallbacks.remove(callback) && mCallbacks.size() == 0 && mListening) { |
| 73 | mListening = false; |
| 74 | mKeyguardUpdateMonitor.removeCallback(this); |
| 75 | mUserTracker.stopTracking(); |
| 76 | } |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public boolean isShowing() { |
| 80 | return mShowing; |
| 81 | } |
| 82 | |
| 83 | public boolean isSecure() { |
| 84 | return mSecure; |
| 85 | } |
| 86 | |
Jim Miller | 69c1241 | 2016-10-06 19:11:03 -0700 | [diff] [blame] | 87 | public boolean isOccluded() { |
| 88 | return mOccluded; |
| 89 | } |
| 90 | |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 91 | public boolean canSkipBouncer() { |
| 92 | return mCanSkipBouncer; |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Jim Miller | 69c1241 | 2016-10-06 19:11:03 -0700 | [diff] [blame] | 95 | public void notifyKeyguardState(boolean showing, boolean secure, boolean occluded) { |
| 96 | if (mShowing == showing && mSecure == secure && mOccluded == occluded) return; |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 97 | mShowing = showing; |
| 98 | mSecure = secure; |
Jim Miller | 69c1241 | 2016-10-06 19:11:03 -0700 | [diff] [blame] | 99 | mOccluded = occluded; |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 100 | notifyKeyguardChanged(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onTrustChanged(int userId) { |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 105 | updateCanSkipBouncerState(); |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 106 | notifyKeyguardChanged(); |
| 107 | } |
| 108 | |
Jim Miller | 26e8fc0 | 2016-07-13 19:06:24 -0700 | [diff] [blame] | 109 | public boolean isDeviceInteractive() { |
| 110 | return mKeyguardUpdateMonitor.isDeviceInteractive(); |
| 111 | } |
| 112 | |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 113 | private void updateCanSkipBouncerState() { |
| 114 | mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser); |
Jason Monk | 8a3a964 | 2015-06-05 11:01:30 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private void notifyKeyguardChanged() { |
John Spurlock | 657c62c | 2014-07-22 12:18:09 -0400 | [diff] [blame] | 118 | for (Callback callback : mCallbacks) { |
| 119 | callback.onKeyguardChanged(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public interface Callback { |
| 124 | void onKeyguardChanged(); |
| 125 | } |
| 126 | } |