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 19 import android.app.ActivityManager; 20 import android.content.Context; 21 22 import com.android.keyguard.KeyguardUpdateMonitor; 23 import com.android.keyguard.KeyguardUpdateMonitorCallback; 24 import com.android.systemui.settings.CurrentUserTracker; 25 26 import java.util.ArrayList; 27 28 public final class KeyguardMonitor extends KeyguardUpdateMonitorCallback { 29 30 private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); 31 32 private final Context mContext; 33 private final CurrentUserTracker mUserTracker; 34 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; 35 36 private int mCurrentUser; 37 private boolean mShowing; 38 private boolean mSecure; 39 private boolean mCanSkipBouncer; 40 41 private boolean mListening; 42 KeyguardMonitor(Context context)43 public KeyguardMonitor(Context context) { 44 mContext = context; 45 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); 46 mUserTracker = new CurrentUserTracker(mContext) { 47 @Override 48 public void onUserSwitched(int newUserId) { 49 mCurrentUser = newUserId; 50 updateCanSkipBouncerState(); 51 } 52 }; 53 } 54 addCallback(Callback callback)55 public void addCallback(Callback callback) { 56 mCallbacks.add(callback); 57 if (mCallbacks.size() != 0 && !mListening) { 58 mListening = true; 59 mCurrentUser = ActivityManager.getCurrentUser(); 60 updateCanSkipBouncerState(); 61 mKeyguardUpdateMonitor.registerCallback(this); 62 mUserTracker.startTracking(); 63 } 64 } 65 removeCallback(Callback callback)66 public void removeCallback(Callback callback) { 67 if (mCallbacks.remove(callback) && mCallbacks.size() == 0 && mListening) { 68 mListening = false; 69 mKeyguardUpdateMonitor.removeCallback(this); 70 mUserTracker.stopTracking(); 71 } 72 } 73 isShowing()74 public boolean isShowing() { 75 return mShowing; 76 } 77 isSecure()78 public boolean isSecure() { 79 return mSecure; 80 } 81 canSkipBouncer()82 public boolean canSkipBouncer() { 83 return mCanSkipBouncer; 84 } 85 notifyKeyguardState(boolean showing, boolean secure)86 public void notifyKeyguardState(boolean showing, boolean secure) { 87 if (mShowing == showing && mSecure == secure) return; 88 mShowing = showing; 89 mSecure = secure; 90 notifyKeyguardChanged(); 91 } 92 93 @Override onTrustChanged(int userId)94 public void onTrustChanged(int userId) { 95 updateCanSkipBouncerState(); 96 notifyKeyguardChanged(); 97 } 98 updateCanSkipBouncerState()99 private void updateCanSkipBouncerState() { 100 mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser); 101 } 102 notifyKeyguardChanged()103 private void notifyKeyguardChanged() { 104 for (Callback callback : mCallbacks) { 105 callback.onKeyguardChanged(); 106 } 107 } 108 109 public interface Callback { onKeyguardChanged()110 void onKeyguardChanged(); 111 } 112 }