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