Home | History | Annotate | Download | only in policy
      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 class KeyguardMonitorImpl extends KeyguardUpdateMonitorCallback
     29         implements KeyguardMonitor {
     30 
     31     private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
     32 
     33     private final Context mContext;
     34     private final CurrentUserTracker mUserTracker;
     35     private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
     36 
     37     private int mCurrentUser;
     38     private boolean mShowing;
     39     private boolean mSecure;
     40     private boolean mOccluded;
     41     private boolean mCanSkipBouncer;
     42 
     43     private boolean mListening;
     44     private boolean mKeyguardFadingAway;
     45     private long mKeyguardFadingAwayDelay;
     46     private long mKeyguardFadingAwayDuration;
     47     private boolean mKeyguardGoingAway;
     48 
     49     public KeyguardMonitorImpl(Context context) {
     50         mContext = context;
     51         mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
     52         mUserTracker = new CurrentUserTracker(mContext) {
     53             @Override
     54             public void onUserSwitched(int newUserId) {
     55                 mCurrentUser = newUserId;
     56                 updateCanSkipBouncerState();
     57             }
     58         };
     59     }
     60 
     61     @Override
     62     public void addCallback(Callback callback) {
     63         mCallbacks.add(callback);
     64         if (mCallbacks.size() != 0 && !mListening) {
     65             mListening = true;
     66             mCurrentUser = ActivityManager.getCurrentUser();
     67             updateCanSkipBouncerState();
     68             mKeyguardUpdateMonitor.registerCallback(this);
     69             mUserTracker.startTracking();
     70         }
     71     }
     72 
     73     @Override
     74     public void removeCallback(Callback callback) {
     75         if (mCallbacks.remove(callback) && mCallbacks.size() == 0 && mListening) {
     76             mListening = false;
     77             mKeyguardUpdateMonitor.removeCallback(this);
     78             mUserTracker.stopTracking();
     79         }
     80     }
     81 
     82     @Override
     83     public boolean isShowing() {
     84         return mShowing;
     85     }
     86 
     87     @Override
     88     public boolean isSecure() {
     89         return mSecure;
     90     }
     91 
     92     @Override
     93     public boolean isOccluded() {
     94         return mOccluded;
     95     }
     96 
     97     @Override
     98     public boolean canSkipBouncer() {
     99         return mCanSkipBouncer;
    100     }
    101 
    102     public void notifyKeyguardState(boolean showing, boolean secure, boolean occluded) {
    103         if (mShowing == showing && mSecure == secure && mOccluded == occluded) return;
    104         mShowing = showing;
    105         mSecure = secure;
    106         mOccluded = occluded;
    107         notifyKeyguardChanged();
    108     }
    109 
    110     @Override
    111     public void onTrustChanged(int userId) {
    112         updateCanSkipBouncerState();
    113         notifyKeyguardChanged();
    114     }
    115 
    116     public boolean isDeviceInteractive() {
    117         return mKeyguardUpdateMonitor.isDeviceInteractive();
    118     }
    119 
    120     private void updateCanSkipBouncerState() {
    121         mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser);
    122     }
    123 
    124     private void notifyKeyguardChanged() {
    125         // Copy the list to allow removal during callback.
    126         new ArrayList<Callback>(mCallbacks).forEach(Callback::onKeyguardShowingChanged);
    127     }
    128 
    129     public void notifyKeyguardFadingAway(long delay, long fadeoutDuration) {
    130         mKeyguardFadingAway = true;
    131         mKeyguardFadingAwayDelay = delay;
    132         mKeyguardFadingAwayDuration = fadeoutDuration;
    133     }
    134 
    135     public void notifyKeyguardDoneFading() {
    136         mKeyguardFadingAway = false;
    137         mKeyguardGoingAway = false;
    138     }
    139 
    140     @Override
    141     public boolean isKeyguardFadingAway() {
    142         return mKeyguardFadingAway;
    143     }
    144 
    145     @Override
    146     public boolean isKeyguardGoingAway() {
    147         return mKeyguardGoingAway;
    148     }
    149 
    150     @Override
    151     public long getKeyguardFadingAwayDelay() {
    152         return mKeyguardFadingAwayDelay;
    153     }
    154 
    155     @Override
    156     public long getKeyguardFadingAwayDuration() {
    157         return mKeyguardFadingAwayDuration;
    158     }
    159 
    160     public void notifyKeyguardGoingAway(boolean keyguardGoingAway) {
    161         mKeyguardGoingAway = keyguardGoingAway;
    162     }
    163 }