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