• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.keyguard;
18 
19 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
20 import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
21 import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_LAUNCHER_CLEAR_SNAPSHOT;
22 import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
23 
24 import static com.android.internal.config.sysui.SystemUiDeviceConfigFlags.NAV_BAR_HANDLE_SHOW_OVER_LOCKSCREEN;
25 import static com.android.internal.jank.InteractionJankMonitor.CUJ_LOCKSCREEN_OCCLUSION;
26 import static com.android.internal.jank.InteractionJankMonitor.CUJ_LOCKSCREEN_TRANSITION_FROM_AOD;
27 import static com.android.internal.jank.InteractionJankMonitor.CUJ_LOCKSCREEN_UNLOCK_ANIMATION;
28 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_TRUSTAGENT_EXPIRED;
29 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
30 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
31 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
32 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_NON_STRONG_BIOMETRICS_TIMEOUT;
33 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_TIMEOUT;
34 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_FOR_UNATTENDED_UPDATE;
35 import static com.android.systemui.DejankUtils.whitelistIpcs;
36 import static com.android.systemui.keyguard.ui.viewmodel.DreamingToLockscreenTransitionViewModel.LOCKSCREEN_ANIMATION_DURATION_MS;
37 import static com.android.systemui.keyguard.ui.viewmodel.LockscreenToDreamingTransitionViewModel.DREAMING_ANIMATION_DURATION_MS;
38 
39 import android.animation.Animator;
40 import android.animation.AnimatorListenerAdapter;
41 import android.animation.ValueAnimator;
42 import android.app.ActivityTaskManager;
43 import android.app.AlarmManager;
44 import android.app.PendingIntent;
45 import android.app.StatusBarManager;
46 import android.app.WindowConfiguration;
47 import android.app.trust.TrustManager;
48 import android.content.BroadcastReceiver;
49 import android.content.ComponentName;
50 import android.content.ContentResolver;
51 import android.content.Context;
52 import android.content.Intent;
53 import android.content.IntentFilter;
54 import android.content.pm.PackageManager.NameNotFoundException;
55 import android.content.pm.UserInfo;
56 import android.graphics.Matrix;
57 import android.hardware.biometrics.BiometricSourceType;
58 import android.media.AudioAttributes;
59 import android.media.AudioManager;
60 import android.media.SoundPool;
61 import android.os.Binder;
62 import android.os.Bundle;
63 import android.os.DeadObjectException;
64 import android.os.Handler;
65 import android.os.IBinder;
66 import android.os.Looper;
67 import android.os.Message;
68 import android.os.PowerManager;
69 import android.os.RemoteException;
70 import android.os.ServiceManager;
71 import android.os.SystemClock;
72 import android.os.SystemProperties;
73 import android.os.Trace;
74 import android.os.UserHandle;
75 import android.os.UserManager;
76 import android.provider.DeviceConfig;
77 import android.provider.Settings;
78 import android.telephony.SubscriptionManager;
79 import android.telephony.TelephonyManager;
80 import android.util.EventLog;
81 import android.util.Log;
82 import android.util.Slog;
83 import android.util.SparseBooleanArray;
84 import android.util.SparseIntArray;
85 import android.view.IRemoteAnimationFinishedCallback;
86 import android.view.IRemoteAnimationRunner;
87 import android.view.RemoteAnimationTarget;
88 import android.view.SyncRtSurfaceTransactionApplier;
89 import android.view.View;
90 import android.view.ViewGroup;
91 import android.view.WindowManager;
92 import android.view.WindowManagerPolicyConstants;
93 import android.view.animation.Animation;
94 import android.view.animation.AnimationUtils;
95 
96 import androidx.annotation.NonNull;
97 import androidx.annotation.Nullable;
98 import androidx.annotation.VisibleForTesting;
99 
100 import com.android.internal.jank.InteractionJankMonitor;
101 import com.android.internal.jank.InteractionJankMonitor.Configuration;
102 import com.android.internal.policy.IKeyguardDismissCallback;
103 import com.android.internal.policy.IKeyguardExitCallback;
104 import com.android.internal.policy.IKeyguardStateCallback;
105 import com.android.internal.policy.ScreenDecorationsUtils;
106 import com.android.internal.statusbar.IStatusBarService;
107 import com.android.internal.util.LatencyTracker;
108 import com.android.internal.widget.LockPatternUtils;
109 import com.android.keyguard.KeyguardConstants;
110 import com.android.keyguard.KeyguardDisplayManager;
111 import com.android.keyguard.KeyguardSecurityView;
112 import com.android.keyguard.KeyguardUpdateMonitor;
113 import com.android.keyguard.KeyguardUpdateMonitorCallback;
114 import com.android.keyguard.KeyguardViewController;
115 import com.android.keyguard.ViewMediatorCallback;
116 import com.android.keyguard.mediator.ScreenOnCoordinator;
117 import com.android.systemui.CoreStartable;
118 import com.android.systemui.DejankUtils;
119 import com.android.systemui.Dumpable;
120 import com.android.systemui.EventLogTags;
121 import com.android.systemui.R;
122 import com.android.systemui.animation.ActivityLaunchAnimator;
123 import com.android.systemui.animation.Interpolators;
124 import com.android.systemui.animation.LaunchAnimator;
125 import com.android.systemui.broadcast.BroadcastDispatcher;
126 import com.android.systemui.classifier.FalsingCollector;
127 import com.android.systemui.dagger.qualifiers.UiBackground;
128 import com.android.systemui.dreams.DreamOverlayStateController;
129 import com.android.systemui.dump.DumpManager;
130 import com.android.systemui.keyguard.dagger.KeyguardModule;
131 import com.android.systemui.navigationbar.NavigationModeController;
132 import com.android.systemui.plugins.statusbar.StatusBarStateController;
133 import com.android.systemui.settings.UserTracker;
134 import com.android.systemui.shade.NotificationPanelViewController;
135 import com.android.systemui.shade.ShadeController;
136 import com.android.systemui.shade.ShadeExpansionStateManager;
137 import com.android.systemui.shared.system.QuickStepContract;
138 import com.android.systemui.statusbar.CommandQueue;
139 import com.android.systemui.statusbar.NotificationShadeDepthController;
140 import com.android.systemui.statusbar.NotificationShadeWindowController;
141 import com.android.systemui.statusbar.SysuiStatusBarStateController;
142 import com.android.systemui.statusbar.phone.BiometricUnlockController;
143 import com.android.systemui.statusbar.phone.CentralSurfaces;
144 import com.android.systemui.statusbar.phone.DozeParameters;
145 import com.android.systemui.statusbar.phone.KeyguardBypassController;
146 import com.android.systemui.statusbar.phone.ScreenOffAnimationController;
147 import com.android.systemui.statusbar.phone.ScrimController;
148 import com.android.systemui.statusbar.policy.KeyguardStateController;
149 import com.android.systemui.statusbar.policy.UserSwitcherController;
150 import com.android.systemui.util.DeviceConfigProxy;
151 
152 import java.io.PrintWriter;
153 import java.util.ArrayList;
154 import java.util.concurrent.Executor;
155 
156 import dagger.Lazy;
157 
158 /**
159  * Mediates requests related to the keyguard.  This includes queries about the
160  * state of the keyguard, power management events that effect whether the keyguard
161  * should be shown or reset, callbacks to the phone window manager to notify
162  * it of when the keyguard is showing, and events from the keyguard view itself
163  * stating that the keyguard was successfully unlocked.
164  *
165  * Note that the keyguard view is shown when the screen is off (as appropriate)
166  * so that once the screen comes on, it will be ready immediately.
167  *
168  * Example queries about the keyguard:
169  * - is {movement, key} one that should wake the keyguard?
170  * - is the keyguard showing?
171  * - are input events restricted due to the state of the keyguard?
172  *
173  * Callbacks to the phone window manager:
174  * - the keyguard is showing
175  *
176  * Example external events that translate to keyguard view changes:
177  * - screen turned off -> reset the keyguard, and show it, so it will be ready
178  *   next time the screen turns on
179  * - keyboard is slid open -> if the keyguard is not secure, hide it
180  *
181  * Events from the keyguard view:
182  * - user successfully unlocked keyguard -> hide keyguard view, and no longer
183  *   restrict input events.
184  *
185  * Note: in addition to normal power management events that effect the state of
186  * whether the keyguard should be showing, external apps and services may request
187  * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}.  When
188  * false, this will override all other conditions for turning on the keyguard.
189  *
190  * Threading and synchronization:
191  * This class is created by the initialization routine of the {@link WindowManagerPolicyConstants},
192  * and runs on its thread.  The keyguard UI is created from that thread in the
193  * constructor of this class.  The apis may be called from other threads, including the
194  * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s.
195  * Therefore, methods on this class are synchronized, and any action that is pointed
196  * directly to the keyguard UI is posted to a {@link android.os.Handler} to ensure it is taken on the UI
197  * thread of the keyguard.
198  */
199 public class KeyguardViewMediator implements CoreStartable, Dumpable,
200         StatusBarStateController.StateListener {
201     private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000;
202     private static final long KEYGUARD_DONE_PENDING_TIMEOUT_MS = 3000;
203 
204     private static final boolean DEBUG = KeyguardConstants.DEBUG;
205     private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
206 
207     private final static String TAG = "KeyguardViewMediator";
208 
209     private static final String DELAYED_KEYGUARD_ACTION =
210         "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD";
211     private static final String DELAYED_LOCK_PROFILE_ACTION =
212             "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_LOCK";
213 
214     private static final String SYSTEMUI_PERMISSION = "com.android.systemui.permission.SELF";
215 
216     // used for handler messages
217     private static final int SHOW = 1;
218     private static final int HIDE = 2;
219     private static final int RESET = 3;
220     private static final int VERIFY_UNLOCK = 4;
221     private static final int NOTIFY_FINISHED_GOING_TO_SLEEP = 5;
222     private static final int KEYGUARD_DONE = 7;
223     private static final int KEYGUARD_DONE_DRAWING = 8;
224     private static final int SET_OCCLUDED = 9;
225     private static final int KEYGUARD_TIMEOUT = 10;
226     private static final int DISMISS = 11;
227     private static final int START_KEYGUARD_EXIT_ANIM = 12;
228     private static final int KEYGUARD_DONE_PENDING_TIMEOUT = 13;
229     private static final int NOTIFY_STARTED_WAKING_UP = 14;
230     private static final int NOTIFY_STARTED_GOING_TO_SLEEP = 17;
231     private static final int SYSTEM_READY = 18;
232     private static final int CANCEL_KEYGUARD_EXIT_ANIM = 19;
233 
234     /**
235      * The default amount of time we stay awake (used for all key input)
236      */
237     public static final int AWAKE_INTERVAL_BOUNCER_MS = 10000;
238 
239     /**
240      * How long to wait after the screen turns off due to timeout before
241      * turning on the keyguard (i.e, the user has this much time to turn
242      * the screen back on without having to face the keyguard).
243      */
244     private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
245 
246     /**
247      * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
248      * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
249      * that is re-enabling the keyguard.
250      */
251     private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
252 
253     private static final int UNOCCLUDE_ANIMATION_DURATION = 250;
254 
255     /**
256      * How far down to animate the unoccluding activity, in terms of percent of the activity's
257      * height.
258      */
259     private static final float UNOCCLUDE_TRANSLATE_DISTANCE_PERCENT = 0.1f;
260 
261     /**
262      * Boolean option for doKeyguardLocked/doKeyguardTimeout which, when set to true, forces the
263      * keyguard to show even if it is disabled for the current user.
264      */
265     public static final String OPTION_FORCE_SHOW = "force_show";
266     private final DreamOverlayStateController mDreamOverlayStateController;
267 
268     /** The stream type that the lock sounds are tied to. */
269     private int mUiSoundsStreamType;
270 
271     private AlarmManager mAlarmManager;
272     private AudioManager mAudioManager;
273     private StatusBarManager mStatusBarManager;
274     private final IStatusBarService mStatusBarService;
275     private final IBinder mStatusBarDisableToken = new Binder();
276     private final UserTracker mUserTracker;
277     private final SysuiStatusBarStateController mStatusBarStateController;
278     private final Executor mUiBgExecutor;
279     private final ScreenOffAnimationController mScreenOffAnimationController;
280     private final Lazy<NotificationShadeDepthController> mNotificationShadeDepthController;
281     private final Lazy<ShadeController> mShadeController;
282 
283     private boolean mSystemReady;
284     private boolean mBootCompleted;
285     private boolean mBootSendUserPresent;
286     private boolean mShuttingDown;
287     private boolean mDozing;
288     private boolean mAnimatingScreenOff;
289     private final Context mContext;
290     private final FalsingCollector mFalsingCollector;
291 
292     /** High level access to the power manager for WakeLocks */
293     private final PowerManager mPM;
294 
295     /** TrustManager for letting it know when we change visibility */
296     private final TrustManager mTrustManager;
297 
298     /** UserSwitcherController for creating guest user on boot complete */
299     private final UserSwitcherController mUserSwitcherController;
300 
301     /**
302      * Used to keep the device awake while to ensure the keyguard finishes opening before
303      * we sleep.
304      */
305     private PowerManager.WakeLock mShowKeyguardWakeLock;
306 
307     private final Lazy<KeyguardViewController> mKeyguardViewControllerLazy;
308 
309     // these are protected by synchronized (this)
310 
311     /**
312      * External apps (like the phone app) can tell us to disable the keyguard.
313      */
314     private boolean mExternallyEnabled = true;
315 
316     /**
317      * Remember if an external call to {@link #setKeyguardEnabled} with value
318      * false caused us to hide the keyguard, so that we need to reshow it once
319      * the keyguard is re-enabled with another call with value true.
320      */
321     private boolean mNeedToReshowWhenReenabled = false;
322 
323     // cached value of whether we are showing (need to know this to quickly
324     // answer whether the input should be restricted)
325     private boolean mShowing;
326 
327     // AOD is enabled and status bar is in AOD state.
328     private boolean mAodShowing;
329 
330     // Dream overlay is visible.
331     private boolean mDreamOverlayShowing;
332 
333     /** Cached value of #isInputRestricted */
334     private boolean mInputRestricted;
335 
336     // true if the keyguard is hidden by another window
337     private boolean mOccluded = false;
338 
339     /**
340      * Whether the {@link #mOccludeAnimationController} is currently playing the occlusion
341      * animation.
342      */
343     private boolean mOccludeAnimationPlaying = false;
344 
345     private boolean mWakeAndUnlocking = false;
346 
347     /**
348      * Helps remember whether the screen has turned on since the last time
349      * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
350      */
351     private int mDelayedShowingSequence;
352 
353     /**
354      * Similar to {@link #mDelayedProfileShowingSequence}, but it is for profile case.
355      */
356     private int mDelayedProfileShowingSequence;
357 
358     private final DismissCallbackRegistry mDismissCallbackRegistry;
359 
360     // the properties of the keyguard
361 
362     private final KeyguardUpdateMonitor mUpdateMonitor;
363     private final Lazy<NotificationShadeWindowController> mNotificationShadeWindowControllerLazy;
364 
365     /**
366      * Last SIM state reported by the telephony system.
367      * Index is the slotId - in case of multiple SIM cards.
368      */
369     private final SparseIntArray mLastSimStates = new SparseIntArray();
370 
371     /**
372      * Indicates if a SIM card had the SIM PIN enabled during the initialization, before
373      * reaching the SIM_STATE_READY state. The flag is reset to false at SIM_STATE_READY.
374      * Index is the slotId - in case of multiple SIM cards.
375      */
376     private final SparseBooleanArray mSimWasLocked = new SparseBooleanArray();
377 
378     private boolean mDeviceInteractive;
379     private boolean mGoingToSleep;
380 
381     // last known state of the cellular connection
382     private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
383 
384     /**
385      * Whether a hide is pending and we are just waiting for #startKeyguardExitAnimation to be
386      * called.
387      * */
388     private boolean mHiding;
389 
390     /**
391      * we send this intent when the keyguard is dismissed.
392      */
393     private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
394             .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
395                     | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
396                     | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
397 
398     /**
399      * {@link #setKeyguardEnabled} waits on this condition when it re-enables
400      * the keyguard.
401      */
402     private boolean mWaitingUntilKeyguardVisible = false;
403     private final LockPatternUtils mLockPatternUtils;
404     private final BroadcastDispatcher mBroadcastDispatcher;
405     private boolean mKeyguardDonePending = false;
406     private boolean mHideAnimationRun = false;
407     private boolean mHideAnimationRunning = false;
408 
409     private SoundPool mLockSounds;
410     private int mLockSoundId;
411     private int mUnlockSoundId;
412     private int mTrustedSoundId;
413     private int mLockSoundStreamId;
414     private final float mPowerButtonY;
415     private final float mWindowCornerRadius;
416 
417     /**
418      * The duration in milliseconds of the dream open animation.
419      */
420     private final int mDreamOpenAnimationDuration;
421 
422     /**
423      * The duration in milliseconds of the dream close animation.
424      */
425     private final int mDreamCloseAnimationDuration;
426 
427     /**
428      * The animation used for hiding keyguard. This is used to fetch the animation timings if
429      * WindowManager is not providing us with them.
430      */
431     private Animation mHideAnimation;
432 
433     /**
434      * The volume applied to the lock/unlock sounds.
435      */
436     private float mLockSoundVolume;
437 
438     /**
439      * For managing external displays
440      */
441     private final KeyguardDisplayManager mKeyguardDisplayManager;
442 
443     private final ArrayList<IKeyguardStateCallback> mKeyguardStateCallbacks = new ArrayList<>();
444 
445     /**
446      * When starting going to sleep, we figured out that we need to reset Keyguard state and this
447      * should be committed when finished going to sleep.
448      */
449     private boolean mPendingReset;
450 
451     /**
452      * When starting going to sleep, we figured out that we need to lock Keyguard and this should be
453      * committed when finished going to sleep.
454      */
455     private boolean mPendingLock;
456 
457     /**
458      * When starting to go away, flag a need to show the PIN lock so the keyguard can be brought
459      * back.
460      */
461     private boolean mPendingPinLock = false;
462 
463     /**
464      * Whether a power button gesture (such as double tap for camera) has been detected. This is
465      * delivered directly from {@link KeyguardService}, immediately upon the gesture being detected.
466      * This is used in {@link #onStartedWakingUp} to decide whether to execute the pending lock, or
467      * ignore and reset it because we are actually launching an activity.
468      *
469      * This needs to be delivered directly to us, rather than waiting for
470      * {@link CommandQueue#onCameraLaunchGestureDetected}, because that call is asynchronous and is
471      * often delivered after the call to {@link #onStartedWakingUp}, which results in us locking the
472      * keyguard and then launching the activity behind it.
473      */
474     private boolean mPowerGestureIntercepted = false;
475 
476     /**
477      * Controller for showing individual "work challenge" lock screen windows inside managed profile
478      * tasks when the current user has been unlocked but the profile is still locked.
479      */
480     private WorkLockActivityController mWorkLockController;
481 
482     private boolean mLockLater;
483     private boolean mShowHomeOverLockscreen;
484     private boolean mInGestureNavigationMode;
485     private CharSequence mCustomMessage;
486 
487     /**
488      * Whether the RemoteAnimation on the app/launcher surface behind the keyguard is 'running'.
489      * Note that this does not necessarily mean the surface is currently in motion - we may be
490      * 'animating' it along with the user's finger during a swipe to unlock gesture, a gesture that
491      * can be paused or reversed.
492      */
493     private boolean mSurfaceBehindRemoteAnimationRunning;
494 
495     /**
496      * Whether we've asked to make the app/launcher surface behind the keyguard visible, via a call
497      * to {@link android.app.IActivityTaskManager#keyguardGoingAway(int)}.
498      *
499      * Since that's an IPC, this doesn't necessarily mean the remote animation has started yet.
500      * {@link #mSurfaceBehindRemoteAnimationRunning} will be true if the call completed and the
501      * animation is now running.
502      */
503     private boolean mSurfaceBehindRemoteAnimationRequested = false;
504 
505     /**
506      * Callback to run to end the RemoteAnimation on the app/launcher surface behind the keyguard.
507      */
508     private IRemoteAnimationFinishedCallback mSurfaceBehindRemoteAnimationFinishedCallback;
509 
510     /**
511      * The animation runner to use for the next exit animation.
512      */
513     private IRemoteAnimationRunner mKeyguardExitAnimationRunner;
514 
515     private CentralSurfaces mCentralSurfaces;
516 
517     private final DeviceConfig.OnPropertiesChangedListener mOnPropertiesChangedListener =
518             new DeviceConfig.OnPropertiesChangedListener() {
519             @Override
520             public void onPropertiesChanged(DeviceConfig.Properties properties) {
521                 if (properties.getKeyset().contains(NAV_BAR_HANDLE_SHOW_OVER_LOCKSCREEN)) {
522                     mShowHomeOverLockscreen = properties.getBoolean(
523                             NAV_BAR_HANDLE_SHOW_OVER_LOCKSCREEN, true /* defaultValue */);
524                 }
525             }
526     };
527 
528     private final DreamOverlayStateController.Callback mDreamOverlayStateCallback =
529             new DreamOverlayStateController.Callback() {
530                 @Override
531                 public void onStateChanged() {
532                     mDreamOverlayShowing = mDreamOverlayStateController.isOverlayActive();
533                 }
534             };
535 
536     KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
537 
538         @Override
539         public void onKeyguardVisibilityChanged(boolean visible) {
540             synchronized (KeyguardViewMediator.this) {
541                 if (!visible && mPendingPinLock) {
542                     Log.i(TAG, "PIN lock requested, starting keyguard");
543 
544                     // Bring the keyguard back in order to show the PIN lock
545                     mPendingPinLock = false;
546                     doKeyguardLocked(null);
547                 }
548             }
549         }
550 
551         @Override
552         public void onUserSwitching(int userId) {
553             if (DEBUG) Log.d(TAG, String.format("onUserSwitching %d", userId));
554             // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
555             // We need to force a reset of the views, since lockNow (called by
556             // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
557             synchronized (KeyguardViewMediator.this) {
558                 resetKeyguardDonePendingLocked();
559                 if (mLockPatternUtils.isLockScreenDisabled(userId)) {
560                     // If we are switching to a user that has keyguard disabled, dismiss keyguard.
561                     dismiss(null /* callback */, null /* message */);
562                 } else {
563                     resetStateLocked();
564                 }
565                 adjustStatusBarLocked();
566             }
567         }
568 
569         @Override
570         public void onUserSwitchComplete(int userId) {
571             if (DEBUG) Log.d(TAG, String.format("onUserSwitchComplete %d", userId));
572             if (userId != UserHandle.USER_SYSTEM) {
573                 UserInfo info = UserManager.get(mContext).getUserInfo(userId);
574                 // Don't try to dismiss if the user has Pin/Pattern/Password set
575                 if (info == null || mLockPatternUtils.isSecure(userId)) {
576                     return;
577                 } else if (info.isGuest() || info.isDemo()) {
578                     // If we just switched to a guest, try to dismiss keyguard.
579                     dismiss(null /* callback */, null /* message */);
580                 }
581             }
582         }
583 
584         @Override
585         public void onDeviceProvisioned() {
586             sendUserPresentBroadcast();
587             synchronized (KeyguardViewMediator.this) {
588                 // If system user is provisioned, we might want to lock now to avoid showing launcher
589                 if (mustNotUnlockCurrentUser()) {
590                     doKeyguardLocked(null);
591                 }
592             }
593         }
594 
595         @Override
596         public void onSimStateChanged(int subId, int slotId, int simState) {
597 
598             if (DEBUG_SIM_STATES) {
599                 Log.d(TAG, "onSimStateChanged(subId=" + subId + ", slotId=" + slotId
600                         + ",state=" + simState + ")");
601             }
602 
603             int size = mKeyguardStateCallbacks.size();
604             boolean simPinSecure = mUpdateMonitor.isSimPinSecure();
605             for (int i = size - 1; i >= 0; i--) {
606                 try {
607                     mKeyguardStateCallbacks.get(i).onSimSecureStateChanged(simPinSecure);
608                 } catch (RemoteException e) {
609                     Slog.w(TAG, "Failed to call onSimSecureStateChanged", e);
610                     if (e instanceof DeadObjectException) {
611                         mKeyguardStateCallbacks.remove(i);
612                     }
613                 }
614             }
615 
616             boolean lastSimStateWasLocked;
617             synchronized (KeyguardViewMediator.this) {
618                 int lastState = mLastSimStates.get(slotId);
619                 lastSimStateWasLocked = (lastState == TelephonyManager.SIM_STATE_PIN_REQUIRED
620                         || lastState == TelephonyManager.SIM_STATE_PUK_REQUIRED);
621                 mLastSimStates.append(slotId, simState);
622             }
623 
624             switch (simState) {
625                 case TelephonyManager.SIM_STATE_NOT_READY:
626                 case TelephonyManager.SIM_STATE_ABSENT:
627                     // only force lock screen in case of missing sim if user hasn't
628                     // gone through setup wizard
629                     synchronized (KeyguardViewMediator.this) {
630                         if (shouldWaitForProvisioning()) {
631                             if (!mShowing) {
632                                 if (DEBUG_SIM_STATES) Log.d(TAG, "ICC_ABSENT isn't showing,"
633                                         + " we need to show the keyguard since the "
634                                         + "device isn't provisioned yet.");
635                                 doKeyguardLocked(null);
636                             } else {
637                                 resetStateLocked();
638                             }
639                         }
640                         if (simState == TelephonyManager.SIM_STATE_ABSENT) {
641                             // MVNO SIMs can become transiently NOT_READY when switching networks,
642                             // so we should only lock when they are ABSENT.
643                             if (lastSimStateWasLocked) {
644                                 if (DEBUG_SIM_STATES) Log.d(TAG, "SIM moved to ABSENT when the "
645                                         + "previous state was locked. Reset the state.");
646                                 resetStateLocked();
647                             }
648                             mSimWasLocked.append(slotId, false);
649                         }
650                     }
651                     break;
652                 case TelephonyManager.SIM_STATE_PIN_REQUIRED:
653                 case TelephonyManager.SIM_STATE_PUK_REQUIRED:
654                     synchronized (KeyguardViewMediator.this) {
655                         mSimWasLocked.append(slotId, true);
656                         mPendingPinLock = true;
657                         if (!mShowing) {
658                             if (DEBUG_SIM_STATES) Log.d(TAG,
659                                     "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
660                                     + "showing; need to show keyguard so user can enter sim pin");
661                             doKeyguardLocked(null);
662                         } else {
663                             resetStateLocked();
664                         }
665                     }
666                     break;
667                 case TelephonyManager.SIM_STATE_PERM_DISABLED:
668                     synchronized (KeyguardViewMediator.this) {
669                         if (!mShowing) {
670                             if (DEBUG_SIM_STATES) Log.d(TAG, "PERM_DISABLED and "
671                                   + "keygaurd isn't showing.");
672                             doKeyguardLocked(null);
673                         } else {
674                             if (DEBUG_SIM_STATES) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
675                                   + "show permanently disabled message in lockscreen.");
676                             resetStateLocked();
677                         }
678                     }
679                     break;
680                 case TelephonyManager.SIM_STATE_READY:
681                     synchronized (KeyguardViewMediator.this) {
682                         if (DEBUG_SIM_STATES) Log.d(TAG, "READY, reset state? " + mShowing);
683                         if (mShowing && mSimWasLocked.get(slotId, false)) {
684                             if (DEBUG_SIM_STATES) Log.d(TAG, "SIM moved to READY when the "
685                                     + "previously was locked. Reset the state.");
686                             mSimWasLocked.append(slotId, false);
687                             resetStateLocked();
688                         }
689                     }
690                     break;
691                 default:
692                     if (DEBUG_SIM_STATES) Log.v(TAG, "Unspecific state: " + simState);
693                     break;
694             }
695         }
696 
697         @Override
698         public void onBiometricAuthFailed(BiometricSourceType biometricSourceType) {
699             final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
700             if (mLockPatternUtils.isSecure(currentUser)) {
701                 mLockPatternUtils.getDevicePolicyManager().reportFailedBiometricAttempt(
702                         currentUser);
703             }
704         }
705 
706         @Override
707         public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType,
708                 boolean isStrongBiometric) {
709             if (mLockPatternUtils.isSecure(userId)) {
710                 mLockPatternUtils.getDevicePolicyManager().reportSuccessfulBiometricAttempt(
711                         userId);
712             }
713         }
714 
715         @Override
716         public void onTrustChanged(int userId) {
717             if (userId == KeyguardUpdateMonitor.getCurrentUser()) {
718                 synchronized (KeyguardViewMediator.this) {
719                     notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(userId));
720                 }
721             }
722         }
723 
724         @Override
725         public void onStrongAuthStateChanged(int userId) {
726             if (mLockPatternUtils.isUserInLockdown(KeyguardUpdateMonitor.getCurrentUser())) {
727                 doKeyguardLocked(null);
728             }
729         }
730     };
731 
732     ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
733 
734         @Override
735         public void userActivity() {
736             KeyguardViewMediator.this.userActivity();
737         }
738 
739         @Override
740         public void keyguardDone(boolean strongAuth, int targetUserId) {
741             if (targetUserId != KeyguardUpdateMonitor.getCurrentUser()) {
742                 return;
743             }
744             if (DEBUG) Log.d(TAG, "keyguardDone");
745             tryKeyguardDone();
746         }
747 
748         @Override
749         public void keyguardDoneDrawing() {
750             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDoneDrawing");
751             mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
752             Trace.endSection();
753         }
754 
755         @Override
756         public void setNeedsInput(boolean needsInput) {
757             mKeyguardViewControllerLazy.get().setNeedsInput(needsInput);
758         }
759 
760         @Override
761         public void keyguardDonePending(boolean strongAuth, int targetUserId) {
762             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDonePending");
763             if (DEBUG) Log.d(TAG, "keyguardDonePending");
764             if (targetUserId != KeyguardUpdateMonitor.getCurrentUser()) {
765                 Trace.endSection();
766                 return;
767             }
768 
769             mKeyguardDonePending = true;
770             mHideAnimationRun = true;
771             mHideAnimationRunning = true;
772             mKeyguardViewControllerLazy.get()
773                     .startPreHideAnimation(mHideAnimationFinishedRunnable);
774             mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_PENDING_TIMEOUT,
775                     KEYGUARD_DONE_PENDING_TIMEOUT_MS);
776             Trace.endSection();
777         }
778 
779         @Override
780         public void keyguardGone() {
781             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardGone");
782             if (DEBUG) Log.d(TAG, "keyguardGone");
783             mKeyguardViewControllerLazy.get().setKeyguardGoingAwayState(false);
784             mKeyguardDisplayManager.hide();
785             Trace.endSection();
786         }
787 
788         @Override
789         public void readyForKeyguardDone() {
790             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#readyForKeyguardDone");
791             if (mKeyguardDonePending) {
792                 mKeyguardDonePending = false;
793                 tryKeyguardDone();
794             }
795             Trace.endSection();
796         }
797 
798         @Override
799         public void resetKeyguard() {
800             resetStateLocked();
801         }
802 
803         @Override
804         public void onCancelClicked() {
805             mKeyguardViewControllerLazy.get().onCancelClicked();
806         }
807 
808         @Override
809         public void playTrustedSound() {
810             KeyguardViewMediator.this.playTrustedSound();
811         }
812 
813         @Override
814         public boolean isScreenOn() {
815             return mDeviceInteractive;
816         }
817 
818         @Override
819         public int getBouncerPromptReason() {
820             int currentUser = KeyguardUpdateMonitor.getCurrentUser();
821             boolean trustAgentsEnabled = mUpdateMonitor.isTrustUsuallyManaged(currentUser);
822             boolean biometricsEnrolled =
823                     mUpdateMonitor.isUnlockingWithBiometricsPossible(currentUser);
824             boolean any = trustAgentsEnabled || biometricsEnrolled;
825             KeyguardUpdateMonitor.StrongAuthTracker strongAuthTracker =
826                     mUpdateMonitor.getStrongAuthTracker();
827             int strongAuth = strongAuthTracker.getStrongAuthForUser(currentUser);
828             boolean allowedNonStrongAfterIdleTimeout =
829                     strongAuthTracker.isNonStrongBiometricAllowedAfterIdleTimeout(currentUser);
830 
831             if (any && !strongAuthTracker.hasUserAuthenticatedSinceBoot()) {
832                 return KeyguardSecurityView.PROMPT_REASON_RESTART;
833             } else if (any && (strongAuth & STRONG_AUTH_REQUIRED_AFTER_TIMEOUT) != 0) {
834                 return KeyguardSecurityView.PROMPT_REASON_TIMEOUT;
835             } else if ((strongAuth & STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW) != 0) {
836                 return KeyguardSecurityView.PROMPT_REASON_DEVICE_ADMIN;
837             } else if (trustAgentsEnabled
838                     && (strongAuth & SOME_AUTH_REQUIRED_AFTER_USER_REQUEST) != 0) {
839                 return KeyguardSecurityView.PROMPT_REASON_USER_REQUEST;
840             } else if (trustAgentsEnabled
841                     && (strongAuth & SOME_AUTH_REQUIRED_AFTER_TRUSTAGENT_EXPIRED) != 0) {
842                 return KeyguardSecurityView.PROMPT_REASON_TRUSTAGENT_EXPIRED;
843             } else if (any && ((strongAuth & STRONG_AUTH_REQUIRED_AFTER_LOCKOUT) != 0
844                     || mUpdateMonitor.isFingerprintLockedOut())) {
845                 return KeyguardSecurityView.PROMPT_REASON_AFTER_LOCKOUT;
846             } else if (any && (strongAuth & STRONG_AUTH_REQUIRED_FOR_UNATTENDED_UPDATE) != 0) {
847                 return KeyguardSecurityView.PROMPT_REASON_PREPARE_FOR_UPDATE;
848             } else if (any && (strongAuth
849                     & STRONG_AUTH_REQUIRED_AFTER_NON_STRONG_BIOMETRICS_TIMEOUT) != 0) {
850                 return KeyguardSecurityView.PROMPT_REASON_NON_STRONG_BIOMETRIC_TIMEOUT;
851             } else if (any && !allowedNonStrongAfterIdleTimeout) {
852                 return KeyguardSecurityView.PROMPT_REASON_NON_STRONG_BIOMETRIC_TIMEOUT;
853             }
854             return KeyguardSecurityView.PROMPT_REASON_NONE;
855         }
856 
857         @Override
858         public CharSequence consumeCustomMessage() {
859             final CharSequence message = mCustomMessage;
860             mCustomMessage = null;
861             return message;
862         }
863     };
864 
865     /**
866      * Animation launch controller for activities that occlude the keyguard.
867      */
868     @VisibleForTesting
869     final ActivityLaunchAnimator.Controller mOccludeAnimationController =
870             new ActivityLaunchAnimator.Controller() {
871                 @Override
872                 public void onLaunchAnimationStart(boolean isExpandingFullyAbove) {
873                     mOccludeAnimationPlaying = true;
874                     mScrimControllerLazy.get().setOccludeAnimationPlaying(true);
875                 }
876 
877                 @Override
878                 public void onLaunchAnimationCancelled(@Nullable Boolean newKeyguardOccludedState) {
879                     Log.d(TAG, "Occlude launch animation cancelled. Occluded state is now: "
880                             + mOccluded);
881                     mOccludeAnimationPlaying = false;
882 
883                     // Ensure keyguard state is set correctly if we're cancelled.
884                     mCentralSurfaces.updateIsKeyguard();
885                     mScrimControllerLazy.get().setOccludeAnimationPlaying(false);
886                 }
887 
888                 @Override
889                 public void onLaunchAnimationEnd(boolean launchIsFullScreen) {
890                     if (launchIsFullScreen) {
891                         mShadeController.get().instantCollapseShade();
892                     }
893 
894                     mOccludeAnimationPlaying = false;
895 
896                     // Hide the keyguard now that we're done launching the occluding activity over
897                     // it.
898                     mCentralSurfaces.updateIsKeyguard();
899                     mScrimControllerLazy.get().setOccludeAnimationPlaying(false);
900 
901                     mInteractionJankMonitor.end(CUJ_LOCKSCREEN_OCCLUSION);
902                 }
903 
904                 @NonNull
905                 @Override
906                 public ViewGroup getLaunchContainer() {
907                     return ((ViewGroup) mKeyguardViewControllerLazy.get()
908                             .getViewRootImpl().getView());
909                 }
910 
911                 @Override
912                 public void setLaunchContainer(@NonNull ViewGroup launchContainer) {
913                     // No-op, launch container is always the shade.
914                     Log.wtf(TAG, "Someone tried to change the launch container for the "
915                             + "ActivityLaunchAnimator, which should never happen.");
916                 }
917 
918                 @NonNull
919                 @Override
920                 public LaunchAnimator.State createAnimatorState() {
921                     final int fullWidth = getLaunchContainer().getWidth();
922                     final int fullHeight = getLaunchContainer().getHeight();
923 
924                     if (mUpdateMonitor.isSecureCameraLaunchedOverKeyguard()) {
925                         final float initialHeight = fullHeight / 3f;
926                         final float initialWidth = fullWidth / 3f;
927 
928                         // Start the animation near the power button, at one-third size, since the
929                         // camera was launched from the power button.
930                         return new LaunchAnimator.State(
931                                 (int) (mPowerButtonY - initialHeight / 2f) /* top */,
932                                 (int) (mPowerButtonY + initialHeight / 2f) /* bottom */,
933                                 (int) (fullWidth - initialWidth) /* left */,
934                                 fullWidth /* right */,
935                                 mWindowCornerRadius, mWindowCornerRadius);
936                     } else {
937                         final float initialHeight = fullHeight / 2f;
938                         final float initialWidth = fullWidth / 2f;
939 
940                         // Start the animation in the center of the screen, scaled down to half
941                         // size.
942                         return new LaunchAnimator.State(
943                                 (int) (fullHeight - initialHeight) / 2,
944                                 (int) (initialHeight + (fullHeight - initialHeight) / 2),
945                                 (int) (fullWidth - initialWidth) / 2,
946                                 (int) (initialWidth + (fullWidth - initialWidth) / 2),
947                                 mWindowCornerRadius, mWindowCornerRadius);
948                     }
949                 }
950             };
951 
952     private IRemoteAnimationRunner mOccludeAnimationRunner =
953             new OccludeActivityLaunchRemoteAnimationRunner(mOccludeAnimationController);
954 
955     private final IRemoteAnimationRunner mOccludeByDreamAnimationRunner =
956             new IRemoteAnimationRunner.Stub() {
957                 @Nullable private ValueAnimator mOccludeByDreamAnimator;
958 
959                 @Override
960                 public void onAnimationCancelled(boolean isKeyguardOccluded) {
961                     mContext.getMainExecutor().execute(() -> {
962                         if (mOccludeByDreamAnimator != null) {
963                             mOccludeByDreamAnimator.cancel();
964                         }
965                     });
966                     Log.d(TAG, "Occlude by Dream animation cancelled.");
967                 }
968 
969                 @Override
970                 public void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
971                         RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
972                         IRemoteAnimationFinishedCallback finishedCallback) throws RemoteException {
973                     if (!handleOnAnimationStart(
974                                 transit, apps, wallpapers, nonApps, finishedCallback)) {
975                         // Usually we rely on animation completion to synchronize occluded status,
976                         // but there was no animation to play, so just update it now.
977                         setOccluded(true /* isOccluded */, false /* animate */);
978                     }
979                 }
980 
981                 private boolean handleOnAnimationStart(int transit, RemoteAnimationTarget[] apps,
982                         RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
983                         IRemoteAnimationFinishedCallback finishedCallback) throws RemoteException {
984                     if (apps == null || apps.length == 0 || apps[0] == null) {
985                         if (DEBUG) {
986                             Log.d(TAG, "No apps provided to the OccludeByDream runner; "
987                                     + "skipping occluding animation.");
988                         }
989                         finishedCallback.onAnimationFinished();
990                         return false;
991                     }
992 
993                     final RemoteAnimationTarget primary = apps[0];
994                     final boolean isDream = (apps[0].taskInfo.topActivityType
995                             == WindowConfiguration.ACTIVITY_TYPE_DREAM);
996                     if (!isDream) {
997                         Log.w(TAG, "The occluding app isn't Dream; "
998                                 + "finishing up. Please check that the config is correct.");
999                         finishedCallback.onAnimationFinished();
1000                         return false;
1001                     }
1002 
1003                     final SyncRtSurfaceTransactionApplier applier =
1004                             new SyncRtSurfaceTransactionApplier(
1005                                     mKeyguardViewControllerLazy.get().getViewRootImpl().getView());
1006 
1007                     mContext.getMainExecutor().execute(() -> {
1008                         if (mOccludeByDreamAnimator != null) {
1009                             mOccludeByDreamAnimator.cancel();
1010                         }
1011 
1012                         mOccludeByDreamAnimator = ValueAnimator.ofFloat(0f, 1f);
1013                         mOccludeByDreamAnimator.setDuration(mDreamOpenAnimationDuration);
1014                         mOccludeByDreamAnimator.setInterpolator(Interpolators.LINEAR);
1015                         mOccludeByDreamAnimator.addUpdateListener(
1016                                 animation -> {
1017                                     SyncRtSurfaceTransactionApplier.SurfaceParams.Builder
1018                                             paramsBuilder =
1019                                             new SyncRtSurfaceTransactionApplier.SurfaceParams
1020                                                     .Builder(primary.leash)
1021                                                     .withAlpha(animation.getAnimatedFraction());
1022                                     applier.scheduleApply(paramsBuilder.build());
1023                                 });
1024                         mOccludeByDreamAnimator.addListener(new AnimatorListenerAdapter() {
1025                             private boolean mIsCancelled = false;
1026                             @Override
1027                             public void onAnimationCancel(Animator animation) {
1028                                 mIsCancelled = true;
1029                             }
1030 
1031                             @Override
1032                             public void onAnimationEnd(Animator animation) {
1033                                 try {
1034                                     if (!mIsCancelled) {
1035                                         // We're already on the main thread, don't queue this call
1036                                         handleSetOccluded(true /* isOccluded */,
1037                                                 false /* animate */);
1038                                     }
1039                                     finishedCallback.onAnimationFinished();
1040                                     mOccludeByDreamAnimator = null;
1041                                 } catch (RemoteException e) {
1042                                     e.printStackTrace();
1043                                 }
1044                             }
1045                         });
1046 
1047                         mOccludeByDreamAnimator.start();
1048                     });
1049                     return true;
1050                 }
1051             };
1052 
1053     /**
1054      * Animation controller for activities that unocclude the keyguard. This does not use the
1055      * ActivityLaunchAnimator since we're just translating down, rather than emerging from a view
1056      * or the power button.
1057      */
1058     private final IRemoteAnimationRunner mUnoccludeAnimationRunner =
1059             new IRemoteAnimationRunner.Stub() {
1060 
1061                 @Nullable private ValueAnimator mUnoccludeAnimator;
1062                 private final Matrix mUnoccludeMatrix = new Matrix();
1063 
1064                 @Override
1065                 public void onAnimationCancelled(boolean isKeyguardOccluded) {
1066                     mContext.getMainExecutor().execute(() -> {
1067                         if (mUnoccludeAnimator != null) {
1068                             mUnoccludeAnimator.cancel();
1069                         }
1070                     });
1071 
1072                     Log.d(TAG, "Unocclude animation cancelled.");
1073                     mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_OCCLUSION);
1074                 }
1075 
1076                 @Override
1077                 public void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
1078                         RemoteAnimationTarget[] wallpapers,
1079                         RemoteAnimationTarget[] nonApps,
1080                         IRemoteAnimationFinishedCallback finishedCallback) throws RemoteException {
1081                     Log.d(TAG, "UnoccludeAnimator#onAnimationStart. Set occluded = false.");
1082                     mInteractionJankMonitor.begin(
1083                             createInteractionJankMonitorConf(CUJ_LOCKSCREEN_OCCLUSION)
1084                                     .setTag("UNOCCLUDE"));
1085                     setOccluded(false /* isOccluded */, true /* animate */);
1086 
1087                     if (apps == null || apps.length == 0 || apps[0] == null) {
1088                         Log.d(TAG, "No apps provided to unocclude runner; "
1089                                 + "skipping animation and unoccluding.");
1090                         finishedCallback.onAnimationFinished();
1091                         return;
1092                     }
1093 
1094                     final RemoteAnimationTarget primary = apps[0];
1095                     final boolean isDream = (apps[0].taskInfo.topActivityType
1096                             == WindowConfiguration.ACTIVITY_TYPE_DREAM);
1097 
1098                     final SyncRtSurfaceTransactionApplier applier =
1099                             new SyncRtSurfaceTransactionApplier(
1100                                     mKeyguardViewControllerLazy.get().getViewRootImpl().getView());
1101 
1102 
1103                     mContext.getMainExecutor().execute(() -> {
1104                         if (mUnoccludeAnimator != null) {
1105                             mUnoccludeAnimator.cancel();
1106                         }
1107 
1108                         mUnoccludeAnimator = ValueAnimator.ofFloat(1f, 0f);
1109                         mUnoccludeAnimator.setDuration(isDream ? mDreamCloseAnimationDuration
1110                                 : UNOCCLUDE_ANIMATION_DURATION);
1111                         mUnoccludeAnimator.setInterpolator(Interpolators.TOUCH_RESPONSE);
1112                         mUnoccludeAnimator.addUpdateListener(
1113                                 animation -> {
1114                                     final float animatedValue =
1115                                             (float) animation.getAnimatedValue();
1116 
1117                                     final float surfaceHeight = primary.screenSpaceBounds.height();
1118 
1119                                     // Fade for all types of activities.
1120                                     SyncRtSurfaceTransactionApplier.SurfaceParams.Builder
1121                                             paramsBuilder =
1122                                             new SyncRtSurfaceTransactionApplier.SurfaceParams
1123                                                     .Builder(primary.leash)
1124                                                     .withAlpha(animatedValue);
1125                                     // Set translate if the occluding activity isn't Dream.
1126                                     if (!isDream) {
1127                                         mUnoccludeMatrix.setTranslate(
1128                                                 0f,
1129                                                 (1f - animatedValue)
1130                                                         * surfaceHeight
1131                                                         * UNOCCLUDE_TRANSLATE_DISTANCE_PERCENT);
1132 
1133                                         paramsBuilder.withMatrix(mUnoccludeMatrix).withCornerRadius(
1134                                                 mWindowCornerRadius);
1135                                     }
1136                                     applier.scheduleApply(paramsBuilder.build());
1137                                 });
1138                         mUnoccludeAnimator.addListener(new AnimatorListenerAdapter() {
1139                             @Override
1140                             public void onAnimationEnd(Animator animation) {
1141                                 try {
1142                                     finishedCallback.onAnimationFinished();
1143                                     mUnoccludeAnimator = null;
1144 
1145                                     mInteractionJankMonitor.end(CUJ_LOCKSCREEN_OCCLUSION);
1146                                 } catch (RemoteException e) {
1147                                     e.printStackTrace();
1148                                 }
1149                             }
1150                         });
1151 
1152                         mUnoccludeAnimator.start();
1153                     });
1154                 }
1155             };
1156 
1157     private DeviceConfigProxy mDeviceConfig;
1158     private DozeParameters mDozeParameters;
1159 
1160     private final KeyguardStateController mKeyguardStateController;
1161     private final KeyguardStateController.Callback mKeyguardStateControllerCallback =
1162             new KeyguardStateController.Callback() {
1163         @Override
1164         public void onPrimaryBouncerShowingChanged() {
1165             synchronized (KeyguardViewMediator.this) {
1166                 if (mKeyguardStateController.isPrimaryBouncerShowing()) {
1167                     mPendingPinLock = false;
1168                 }
1169                 adjustStatusBarLocked(mKeyguardStateController.isPrimaryBouncerShowing(), false);
1170             }
1171         }
1172     };
1173 
1174     private final Lazy<KeyguardUnlockAnimationController> mKeyguardUnlockAnimationControllerLazy;
1175     private final InteractionJankMonitor mInteractionJankMonitor;
1176     private boolean mWallpaperSupportsAmbientMode;
1177     private ScreenOnCoordinator mScreenOnCoordinator;
1178 
1179     private Lazy<ActivityLaunchAnimator> mActivityLaunchAnimator;
1180     private Lazy<ScrimController> mScrimControllerLazy;
1181 
1182     /**
1183      * Injected constructor. See {@link KeyguardModule}.
1184      */
KeyguardViewMediator( Context context, UserTracker userTracker, FalsingCollector falsingCollector, LockPatternUtils lockPatternUtils, BroadcastDispatcher broadcastDispatcher, Lazy<KeyguardViewController> statusBarKeyguardViewManagerLazy, DismissCallbackRegistry dismissCallbackRegistry, KeyguardUpdateMonitor keyguardUpdateMonitor, DumpManager dumpManager, @UiBackground Executor uiBgExecutor, PowerManager powerManager, TrustManager trustManager, UserSwitcherController userSwitcherController, DeviceConfigProxy deviceConfig, NavigationModeController navigationModeController, KeyguardDisplayManager keyguardDisplayManager, DozeParameters dozeParameters, SysuiStatusBarStateController statusBarStateController, KeyguardStateController keyguardStateController, Lazy<KeyguardUnlockAnimationController> keyguardUnlockAnimationControllerLazy, ScreenOffAnimationController screenOffAnimationController, Lazy<NotificationShadeDepthController> notificationShadeDepthController, ScreenOnCoordinator screenOnCoordinator, InteractionJankMonitor interactionJankMonitor, DreamOverlayStateController dreamOverlayStateController, Lazy<ShadeController> shadeControllerLazy, Lazy<NotificationShadeWindowController> notificationShadeWindowControllerLazy, Lazy<ActivityLaunchAnimator> activityLaunchAnimator, Lazy<ScrimController> scrimControllerLazy)1185     public KeyguardViewMediator(
1186             Context context,
1187             UserTracker userTracker,
1188             FalsingCollector falsingCollector,
1189             LockPatternUtils lockPatternUtils,
1190             BroadcastDispatcher broadcastDispatcher,
1191             Lazy<KeyguardViewController> statusBarKeyguardViewManagerLazy,
1192             DismissCallbackRegistry dismissCallbackRegistry,
1193             KeyguardUpdateMonitor keyguardUpdateMonitor, DumpManager dumpManager,
1194             @UiBackground Executor uiBgExecutor, PowerManager powerManager,
1195             TrustManager trustManager,
1196             UserSwitcherController userSwitcherController,
1197             DeviceConfigProxy deviceConfig,
1198             NavigationModeController navigationModeController,
1199             KeyguardDisplayManager keyguardDisplayManager,
1200             DozeParameters dozeParameters,
1201             SysuiStatusBarStateController statusBarStateController,
1202             KeyguardStateController keyguardStateController,
1203             Lazy<KeyguardUnlockAnimationController> keyguardUnlockAnimationControllerLazy,
1204             ScreenOffAnimationController screenOffAnimationController,
1205             Lazy<NotificationShadeDepthController> notificationShadeDepthController,
1206             ScreenOnCoordinator screenOnCoordinator,
1207             InteractionJankMonitor interactionJankMonitor,
1208             DreamOverlayStateController dreamOverlayStateController,
1209             Lazy<ShadeController> shadeControllerLazy,
1210             Lazy<NotificationShadeWindowController> notificationShadeWindowControllerLazy,
1211             Lazy<ActivityLaunchAnimator> activityLaunchAnimator,
1212             Lazy<ScrimController> scrimControllerLazy) {
1213         mContext = context;
1214         mUserTracker = userTracker;
1215         mFalsingCollector = falsingCollector;
1216         mLockPatternUtils = lockPatternUtils;
1217         mBroadcastDispatcher = broadcastDispatcher;
1218         mKeyguardViewControllerLazy = statusBarKeyguardViewManagerLazy;
1219         mDismissCallbackRegistry = dismissCallbackRegistry;
1220         mNotificationShadeDepthController = notificationShadeDepthController;
1221         mUiBgExecutor = uiBgExecutor;
1222         mUpdateMonitor = keyguardUpdateMonitor;
1223         mPM = powerManager;
1224         mTrustManager = trustManager;
1225         mUserSwitcherController = userSwitcherController;
1226         mStatusBarService = IStatusBarService.Stub.asInterface(
1227                 ServiceManager.getService(Context.STATUS_BAR_SERVICE));
1228         mKeyguardDisplayManager = keyguardDisplayManager;
1229         mShadeController = shadeControllerLazy;
1230         dumpManager.registerDumpable(getClass().getName(), this);
1231         mDeviceConfig = deviceConfig;
1232         mScreenOnCoordinator = screenOnCoordinator;
1233         mNotificationShadeWindowControllerLazy = notificationShadeWindowControllerLazy;
1234         mShowHomeOverLockscreen = mDeviceConfig.getBoolean(
1235                 DeviceConfig.NAMESPACE_SYSTEMUI,
1236                 NAV_BAR_HANDLE_SHOW_OVER_LOCKSCREEN,
1237                 /* defaultValue = */ true);
1238         mDeviceConfig.addOnPropertiesChangedListener(
1239                 DeviceConfig.NAMESPACE_SYSTEMUI,
1240                 mHandler::post,
1241                 mOnPropertiesChangedListener);
1242         mInGestureNavigationMode =
1243                 QuickStepContract.isGesturalMode(navigationModeController.addListener(mode -> {
1244                     mInGestureNavigationMode = QuickStepContract.isGesturalMode(mode);
1245                 }));
1246         mDozeParameters = dozeParameters;
1247 
1248         mStatusBarStateController = statusBarStateController;
1249         statusBarStateController.addCallback(this);
1250 
1251         mKeyguardStateController = keyguardStateController;
1252         keyguardStateController.addCallback(mKeyguardStateControllerCallback);
1253         mKeyguardUnlockAnimationControllerLazy = keyguardUnlockAnimationControllerLazy;
1254         mScreenOffAnimationController = screenOffAnimationController;
1255         mInteractionJankMonitor = interactionJankMonitor;
1256         mDreamOverlayStateController = dreamOverlayStateController;
1257 
1258         mActivityLaunchAnimator = activityLaunchAnimator;
1259         mScrimControllerLazy = scrimControllerLazy;
1260 
1261         mPowerButtonY = context.getResources().getDimensionPixelSize(
1262                 R.dimen.physical_power_button_center_screen_location_y);
1263         mWindowCornerRadius = ScreenDecorationsUtils.getWindowCornerRadius(context);
1264 
1265         mDreamOpenAnimationDuration = (int) DREAMING_ANIMATION_DURATION_MS;
1266         mDreamCloseAnimationDuration = (int) LOCKSCREEN_ANIMATION_DURATION_MS;
1267     }
1268 
userActivity()1269     public void userActivity() {
1270         mPM.userActivity(SystemClock.uptimeMillis(), false);
1271     }
1272 
mustNotUnlockCurrentUser()1273     boolean mustNotUnlockCurrentUser() {
1274         return UserManager.isSplitSystemUser()
1275                 && KeyguardUpdateMonitor.getCurrentUser() == UserHandle.USER_SYSTEM;
1276     }
1277 
setupLocked()1278     private void setupLocked() {
1279         mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
1280         mShowKeyguardWakeLock.setReferenceCounted(false);
1281 
1282         IntentFilter filter = new IntentFilter();
1283         filter.addAction(Intent.ACTION_SHUTDOWN);
1284         mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
1285 
1286         final IntentFilter delayedActionFilter = new IntentFilter();
1287         delayedActionFilter.addAction(DELAYED_KEYGUARD_ACTION);
1288         delayedActionFilter.addAction(DELAYED_LOCK_PROFILE_ACTION);
1289         delayedActionFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
1290         mContext.registerReceiver(mDelayedLockBroadcastReceiver, delayedActionFilter,
1291                 SYSTEMUI_PERMISSION, null /* scheduler */,
1292                 Context.RECEIVER_EXPORTED_UNAUDITED);
1293 
1294         mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
1295 
1296         KeyguardUpdateMonitor.setCurrentUser(mUserTracker.getUserId());
1297 
1298         // Assume keyguard is showing (unless it's disabled) until we know for sure, unless Keyguard
1299         // is disabled.
1300         if (isKeyguardServiceEnabled()) {
1301             setShowingLocked(!shouldWaitForProvisioning()
1302                     && !mLockPatternUtils.isLockScreenDisabled(
1303                             KeyguardUpdateMonitor.getCurrentUser()), true /* forceCallbacks */);
1304         } else {
1305             // The system's keyguard is disabled or missing.
1306             setShowingLocked(false /* showing */, true /* forceCallbacks */);
1307         }
1308 
1309         final ContentResolver cr = mContext.getContentResolver();
1310 
1311         mDeviceInteractive = mPM.isInteractive();
1312 
1313         mLockSounds = new SoundPool.Builder()
1314                 .setMaxStreams(1)
1315                 .setAudioAttributes(
1316                         new AudioAttributes.Builder()
1317                                 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
1318                                 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
1319                                 .build())
1320                 .build();
1321         String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
1322         if (soundPath != null) {
1323             mLockSoundId = mLockSounds.load(soundPath, 1);
1324         }
1325         if (soundPath == null || mLockSoundId == 0) {
1326             Log.w(TAG, "failed to load lock sound from " + soundPath);
1327         }
1328         soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
1329         if (soundPath != null) {
1330             mUnlockSoundId = mLockSounds.load(soundPath, 1);
1331         }
1332         if (soundPath == null || mUnlockSoundId == 0) {
1333             Log.w(TAG, "failed to load unlock sound from " + soundPath);
1334         }
1335         soundPath = Settings.Global.getString(cr, Settings.Global.TRUSTED_SOUND);
1336         if (soundPath != null) {
1337             mTrustedSoundId = mLockSounds.load(soundPath, 1);
1338         }
1339         if (soundPath == null || mTrustedSoundId == 0) {
1340             Log.w(TAG, "failed to load trusted sound from " + soundPath);
1341         }
1342 
1343         int lockSoundDefaultAttenuation = mContext.getResources().getInteger(
1344                 com.android.internal.R.integer.config_lockSoundVolumeDb);
1345         mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
1346 
1347         mHideAnimation = AnimationUtils.loadAnimation(mContext,
1348                 com.android.internal.R.anim.lock_screen_behind_enter);
1349 
1350         mWorkLockController = new WorkLockActivityController(mContext, mUserTracker);
1351     }
1352 
1353     @Override
start()1354     public void start() {
1355         synchronized (this) {
1356             setupLocked();
1357         }
1358     }
1359 
1360     /**
1361      * Let us know that the system is ready after startup.
1362      */
onSystemReady()1363     public void onSystemReady() {
1364         mHandler.obtainMessage(SYSTEM_READY).sendToTarget();
1365     }
1366 
handleSystemReady()1367     private void handleSystemReady() {
1368         synchronized (this) {
1369             if (DEBUG) Log.d(TAG, "onSystemReady");
1370             mSystemReady = true;
1371             doKeyguardLocked(null);
1372             mUpdateMonitor.registerCallback(mUpdateCallback);
1373             adjustStatusBarLocked();
1374             mDreamOverlayStateController.addCallback(mDreamOverlayStateCallback);
1375         }
1376         // Most services aren't available until the system reaches the ready state, so we
1377         // send it here when the device first boots.
1378         maybeSendUserPresentBroadcast();
1379     }
1380 
1381     /**
1382      * Called to let us know the screen was turned off.
1383      * @param offReason either {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_USER} or
1384      * {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_TIMEOUT}.
1385      */
onStartedGoingToSleep(@indowManagerPolicyConstants.OffReason int offReason)1386     public void onStartedGoingToSleep(@WindowManagerPolicyConstants.OffReason int offReason) {
1387         if (DEBUG) Log.d(TAG, "onStartedGoingToSleep(" + offReason + ")");
1388         synchronized (this) {
1389             mDeviceInteractive = false;
1390             mPowerGestureIntercepted = false;
1391             mGoingToSleep = true;
1392 
1393             // Lock immediately based on setting if secure (user has a pin/pattern/password).
1394             // This also "locks" the device when not secure to provide easy access to the
1395             // camera while preventing unwanted input.
1396             int currentUser = KeyguardUpdateMonitor.getCurrentUser();
1397             final boolean lockImmediately =
1398                     mLockPatternUtils.getPowerButtonInstantlyLocks(currentUser)
1399                             || !mLockPatternUtils.isSecure(currentUser);
1400             long timeout = getLockTimeout(KeyguardUpdateMonitor.getCurrentUser());
1401             mLockLater = false;
1402             if (mShowing && !mKeyguardStateController.isKeyguardGoingAway()) {
1403                 // If we are going to sleep but the keyguard is showing (and will continue to be
1404                 // showing, not in the process of going away) then reset its state. Otherwise, let
1405                 // this fall through and explicitly re-lock the keyguard.
1406                 mPendingReset = true;
1407             } else if (
1408                     (offReason == WindowManagerPolicyConstants.OFF_BECAUSE_OF_TIMEOUT
1409                             && timeout > 0)
1410                             || (offReason == WindowManagerPolicyConstants.OFF_BECAUSE_OF_USER
1411                             && !lockImmediately)) {
1412                 doKeyguardLaterLocked(timeout);
1413                 mLockLater = true;
1414             } else if (!mLockPatternUtils.isLockScreenDisabled(currentUser)) {
1415                 setPendingLock(true);
1416             }
1417 
1418             if (mPendingLock) {
1419                 playSounds(true);
1420             }
1421         }
1422 
1423         mUpdateMonitor.dispatchStartedGoingToSleep(offReason);
1424 
1425         // Reset keyguard going away state, so we can start listening for fingerprint. We
1426         // explicitly DO NOT want to call
1427         // mKeyguardViewControllerLazy.get().setKeyguardGoingAwayState(false)
1428         // here, since that will mess with the device lock state.
1429         mUpdateMonitor.dispatchKeyguardGoingAway(false);
1430 
1431         notifyStartedGoingToSleep();
1432     }
1433 
1434     /**
1435      * Called to let us know the screen finished turning off.
1436      * @param offReason either {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_USER} or
1437      * {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_TIMEOUT}.
1438      */
onFinishedGoingToSleep( @indowManagerPolicyConstants.OffReason int offReason, boolean cameraGestureTriggered)1439     public void onFinishedGoingToSleep(
1440             @WindowManagerPolicyConstants.OffReason int offReason, boolean cameraGestureTriggered) {
1441         if (DEBUG) Log.d(TAG, "onFinishedGoingToSleep(" + offReason + ")");
1442         synchronized (this) {
1443             mDeviceInteractive = false;
1444             mGoingToSleep = false;
1445             mWakeAndUnlocking = false;
1446             mAnimatingScreenOff = mDozeParameters.shouldAnimateDozingChange();
1447 
1448             resetKeyguardDonePendingLocked();
1449             mHideAnimationRun = false;
1450 
1451             notifyFinishedGoingToSleep();
1452 
1453             if (cameraGestureTriggered) {
1454 
1455                 // Just to make sure, make sure the device is awake.
1456                 mContext.getSystemService(PowerManager.class).wakeUp(SystemClock.uptimeMillis(),
1457                         PowerManager.WAKE_REASON_CAMERA_LAUNCH,
1458                         "com.android.systemui:CAMERA_GESTURE_PREVENT_LOCK");
1459                 setPendingLock(false);
1460                 mPendingReset = false;
1461             }
1462 
1463             if (mPendingReset) {
1464                 resetStateLocked();
1465                 mPendingReset = false;
1466             }
1467 
1468             maybeHandlePendingLock();
1469 
1470             // We do not have timeout and power button instant lock setting for profile lock.
1471             // So we use the personal setting if there is any. But if there is no device
1472             // we need to make sure we lock it immediately when the screen is off.
1473             if (!mLockLater && !cameraGestureTriggered) {
1474                 doKeyguardForChildProfilesLocked();
1475             }
1476 
1477         }
1478         mUpdateMonitor.dispatchFinishedGoingToSleep(offReason);
1479     }
1480 
1481     /**
1482      * Locks the keyguard if {@link #mPendingLock} is true, and there are no reasons to further
1483      * delay the pending lock.
1484      *
1485      * If you do delay handling the pending lock, you must ensure that this method is ALWAYS called
1486      * again when the condition causing the delay changes. Otherwise, the device may remain unlocked
1487      * indefinitely.
1488      */
maybeHandlePendingLock()1489     public void maybeHandlePendingLock() {
1490         if (mPendingLock) {
1491 
1492             // The screen off animation is playing or is about to be, so if we lock now, the
1493             // foreground app will vanish and the keyguard will jump-cut in. Delay it, until either:
1494             //   - The screen off animation ends. We will call maybeHandlePendingLock from
1495             //     the end action in UnlockedScreenOffAnimationController#animateInKeyguard.
1496             //   - The screen off animation is cancelled by the device waking back up. We will call
1497             //     maybeHandlePendingLock from KeyguardViewMediator#onStartedWakingUp.
1498             if (mScreenOffAnimationController.shouldDelayKeyguardShow()) {
1499                 if (DEBUG) {
1500                     Log.d(TAG, "#maybeHandlePendingLock: not handling because the screen off "
1501                             + "animation's shouldDelayKeyguardShow() returned true. This should be "
1502                             + "handled soon by #onStartedWakingUp, or by the end actions of the "
1503                             + "screen off animation.");
1504                 }
1505 
1506                 return;
1507             }
1508 
1509             // The device was re-locked while in the process of unlocking. If we lock now, callbacks
1510             // in the unlock sequence might end up re-unlocking the device. Delay the lock until the
1511             // keyguard is done going away. We'll call maybeHandlePendingLock again in
1512             // StatusBar#finishKeyguardFadingAway, which is always responsible for setting
1513             // isKeyguardGoingAway to false.
1514             if (mKeyguardStateController.isKeyguardGoingAway()) {
1515                 if (DEBUG) {
1516                     Log.d(TAG, "#maybeHandlePendingLock: not handling because the keyguard is "
1517                             + "going away. This should be handled shortly by "
1518                             + "StatusBar#finishKeyguardFadingAway.");
1519                 }
1520 
1521                 return;
1522             }
1523 
1524             if (DEBUG) {
1525                 Log.d(TAG, "#maybeHandlePendingLock: handling pending lock; locking keyguard.");
1526             }
1527 
1528             doKeyguardLocked(null);
1529             setPendingLock(false);
1530         }
1531     }
1532 
isKeyguardServiceEnabled()1533     private boolean isKeyguardServiceEnabled() {
1534         try {
1535             return mContext.getPackageManager().getServiceInfo(
1536                     new ComponentName(mContext, KeyguardService.class), 0).isEnabled();
1537         } catch (NameNotFoundException e) {
1538             return true;
1539         }
1540     }
1541 
getLockTimeout(int userId)1542     private long getLockTimeout(int userId) {
1543         // if the screen turned off because of timeout or the user hit the power button,
1544         // and we don't need to lock immediately, set an alarm
1545         // to enable it a bit later (i.e, give the user a chance
1546         // to turn the screen back on within a certain window without
1547         // having to unlock the screen)
1548         final ContentResolver cr = mContext.getContentResolver();
1549 
1550         // From SecuritySettings
1551         final long lockAfterTimeout = Settings.Secure.getIntForUser(cr,
1552                 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
1553                 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT, userId);
1554 
1555         // From DevicePolicyAdmin
1556         final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
1557                 .getMaximumTimeToLock(null, userId);
1558 
1559         long timeout;
1560 
1561         if (policyTimeout <= 0) {
1562             timeout = lockAfterTimeout;
1563         } else {
1564             // From DisplaySettings
1565             long displayTimeout = Settings.System.getIntForUser(cr, SCREEN_OFF_TIMEOUT,
1566                     KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT, userId);
1567 
1568             // policy in effect. Make sure we don't go beyond policy limit.
1569             displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
1570             timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
1571             timeout = Math.max(timeout, 0);
1572         }
1573         return timeout;
1574     }
1575 
doKeyguardLaterLocked()1576     private void doKeyguardLaterLocked() {
1577         long timeout = getLockTimeout(KeyguardUpdateMonitor.getCurrentUser());
1578         if (timeout == 0) {
1579             doKeyguardLocked(null);
1580         } else {
1581             doKeyguardLaterLocked(timeout);
1582         }
1583     }
1584 
doKeyguardLaterLocked(long timeout)1585     private void doKeyguardLaterLocked(long timeout) {
1586         // Lock in the future
1587         long when = SystemClock.elapsedRealtime() + timeout;
1588         Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
1589         intent.setPackage(mContext.getPackageName());
1590         intent.putExtra("seq", mDelayedShowingSequence);
1591         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1592         PendingIntent sender = PendingIntent.getBroadcast(mContext,
1593                 0, intent, PendingIntent.FLAG_CANCEL_CURRENT |  PendingIntent.FLAG_IMMUTABLE);
1594         mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
1595         if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
1596                          + mDelayedShowingSequence);
1597         doKeyguardLaterForChildProfilesLocked();
1598     }
1599 
doKeyguardLaterForChildProfilesLocked()1600     private void doKeyguardLaterForChildProfilesLocked() {
1601         UserManager um = UserManager.get(mContext);
1602         for (int profileId : um.getEnabledProfileIds(UserHandle.myUserId())) {
1603             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(profileId)) {
1604                 long userTimeout = getLockTimeout(profileId);
1605                 if (userTimeout == 0) {
1606                     doKeyguardForChildProfilesLocked();
1607                 } else {
1608                     long userWhen = SystemClock.elapsedRealtime() + userTimeout;
1609                     Intent lockIntent = new Intent(DELAYED_LOCK_PROFILE_ACTION);
1610                     lockIntent.setPackage(mContext.getPackageName());
1611                     lockIntent.putExtra("seq", mDelayedProfileShowingSequence);
1612                     lockIntent.putExtra(Intent.EXTRA_USER_ID, profileId);
1613                     lockIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
1614                     PendingIntent lockSender = PendingIntent.getBroadcast(
1615                             mContext, 0, lockIntent,
1616                             PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
1617                     mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
1618                             userWhen, lockSender);
1619                 }
1620             }
1621         }
1622     }
1623 
doKeyguardForChildProfilesLocked()1624     private void doKeyguardForChildProfilesLocked() {
1625         UserManager um = UserManager.get(mContext);
1626         for (int profileId : um.getEnabledProfileIds(UserHandle.myUserId())) {
1627             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(profileId)) {
1628                 lockProfile(profileId);
1629             }
1630         }
1631     }
1632 
cancelDoKeyguardLaterLocked()1633     private void cancelDoKeyguardLaterLocked() {
1634         mDelayedShowingSequence++;
1635     }
1636 
cancelDoKeyguardForChildProfilesLocked()1637     private void cancelDoKeyguardForChildProfilesLocked() {
1638         mDelayedProfileShowingSequence++;
1639     }
1640 
1641     /**
1642      * It will let us know when the device is waking up.
1643      */
onStartedWakingUp(@owerManager.WakeReason int pmWakeReason, boolean cameraGestureTriggered)1644     public void onStartedWakingUp(@PowerManager.WakeReason int pmWakeReason,
1645             boolean cameraGestureTriggered) {
1646         Trace.beginSection("KeyguardViewMediator#onStartedWakingUp");
1647 
1648         // TODO: Rename all screen off/on references to interactive/sleeping
1649         synchronized (this) {
1650             mDeviceInteractive = true;
1651             if (mPendingLock && !cameraGestureTriggered && !mWakeAndUnlocking) {
1652                 doKeyguardLocked(null);
1653             }
1654             mAnimatingScreenOff = false;
1655             cancelDoKeyguardLaterLocked();
1656             cancelDoKeyguardForChildProfilesLocked();
1657             if (DEBUG) Log.d(TAG, "onStartedWakingUp, seq = " + mDelayedShowingSequence);
1658             notifyStartedWakingUp();
1659         }
1660         mUpdateMonitor.dispatchStartedWakingUp(pmWakeReason);
1661         maybeSendUserPresentBroadcast();
1662         Trace.endSection();
1663     }
1664 
onScreenTurnedOff()1665     public void onScreenTurnedOff() {
1666         mUpdateMonitor.dispatchScreenTurnedOff();
1667     }
1668 
maybeSendUserPresentBroadcast()1669     private void maybeSendUserPresentBroadcast() {
1670         if (mSystemReady && mLockPatternUtils.isLockScreenDisabled(
1671                 KeyguardUpdateMonitor.getCurrentUser())) {
1672             // Lock screen is disabled because the user has set the preference to "None".
1673             // In this case, send out ACTION_USER_PRESENT here instead of in
1674             // handleKeyguardDone()
1675             sendUserPresentBroadcast();
1676         } else if (mSystemReady && shouldWaitForProvisioning()) {
1677             // Skipping the lockscreen because we're not yet provisioned, but we still need to
1678             // notify the StrongAuthTracker that it's now safe to run trust agents, in case the
1679             // user sets a credential later.
1680             mLockPatternUtils.userPresent(KeyguardUpdateMonitor.getCurrentUser());
1681         }
1682     }
1683 
1684     /**
1685      * A dream started.  We should lock after the usual screen-off lock timeout but only
1686      * if there is a secure lock pattern.
1687      */
onDreamingStarted()1688     public void onDreamingStarted() {
1689         mUpdateMonitor.dispatchDreamingStarted();
1690         synchronized (this) {
1691             if (mDeviceInteractive
1692                     && mLockPatternUtils.isSecure(KeyguardUpdateMonitor.getCurrentUser())) {
1693                 doKeyguardLaterLocked();
1694             }
1695         }
1696     }
1697 
1698     /**
1699      * A dream stopped.
1700      */
onDreamingStopped()1701     public void onDreamingStopped() {
1702         mUpdateMonitor.dispatchDreamingStopped();
1703         synchronized (this) {
1704             if (mDeviceInteractive) {
1705                 cancelDoKeyguardLaterLocked();
1706             }
1707         }
1708     }
1709 
1710     /**
1711      * Same semantics as {@link WindowManagerPolicyConstants#enableKeyguard}; provide
1712      * a way for external stuff to override normal keyguard behavior.  For instance
1713      * the phone app disables the keyguard when it receives incoming calls.
1714      */
setKeyguardEnabled(boolean enabled)1715     public void setKeyguardEnabled(boolean enabled) {
1716         synchronized (this) {
1717             if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
1718 
1719             mExternallyEnabled = enabled;
1720 
1721             if (!enabled && mShowing) {
1722                 // hiding keyguard that is showing, remember to reshow later
1723                 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
1724                         + "disabling status bar expansion");
1725                 mNeedToReshowWhenReenabled = true;
1726                 updateInputRestrictedLocked();
1727                 hideLocked();
1728             } else if (enabled && mNeedToReshowWhenReenabled) {
1729                 // re-enabled after previously hidden, reshow
1730                 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
1731                         + "status bar expansion");
1732                 mNeedToReshowWhenReenabled = false;
1733                 updateInputRestrictedLocked();
1734 
1735                 showLocked(null);
1736 
1737                 // block until we know the keyguard is done drawing (and post a message
1738                 // to unblock us after a timeout, so we don't risk blocking too long
1739                 // and causing an ANR).
1740                 mWaitingUntilKeyguardVisible = true;
1741                 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING,
1742                         KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
1743                 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
1744                 while (mWaitingUntilKeyguardVisible) {
1745                     try {
1746                         wait();
1747                     } catch (InterruptedException e) {
1748                         Thread.currentThread().interrupt();
1749                     }
1750                 }
1751                 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
1752             }
1753         }
1754     }
1755 
1756     /**
1757      * @see android.app.KeyguardManager#exitKeyguardSecurely
1758      */
verifyUnlock(IKeyguardExitCallback callback)1759     public void verifyUnlock(IKeyguardExitCallback callback) {
1760         Trace.beginSection("KeyguardViewMediator#verifyUnlock");
1761         synchronized (this) {
1762             if (DEBUG) Log.d(TAG, "verifyUnlock");
1763             if (shouldWaitForProvisioning()) {
1764                 // don't allow this api when the device isn't provisioned
1765                 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
1766                 try {
1767                     callback.onKeyguardExitResult(false);
1768                 } catch (RemoteException e) {
1769                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1770                 }
1771             } else if (mExternallyEnabled) {
1772                 // this only applies when the user has externally disabled the
1773                 // keyguard.  this is unexpected and means the user is not
1774                 // using the api properly.
1775                 Log.w(TAG, "verifyUnlock called when not externally disabled");
1776                 try {
1777                     callback.onKeyguardExitResult(false);
1778                 } catch (RemoteException e) {
1779                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1780                 }
1781             } else if (!isSecure()) {
1782 
1783                 // Keyguard is not secure, no need to do anything, and we don't need to reshow
1784                 // the Keyguard after the client releases the Keyguard lock.
1785                 mExternallyEnabled = true;
1786                 mNeedToReshowWhenReenabled = false;
1787                 updateInputRestricted();
1788                 try {
1789                     callback.onKeyguardExitResult(true);
1790                 } catch (RemoteException e) {
1791                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1792                 }
1793             } else {
1794 
1795                 // Since we prevent apps from hiding the Keyguard if we are secure, this should be
1796                 // a no-op as well.
1797                 try {
1798                     callback.onKeyguardExitResult(false);
1799                 } catch (RemoteException e) {
1800                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1801                 }
1802             }
1803         }
1804         Trace.endSection();
1805     }
1806 
1807     /**
1808      * Is the keyguard currently showing and not being force hidden?
1809      */
isShowingAndNotOccluded()1810     public boolean isShowingAndNotOccluded() {
1811         return mShowing && !mOccluded;
1812     }
1813 
isOccludeAnimationPlaying()1814     public boolean isOccludeAnimationPlaying() {
1815         return mOccludeAnimationPlaying;
1816     }
1817 
1818     /**
1819      * Notify us when the keyguard is occluded by another window
1820      */
setOccluded(boolean isOccluded, boolean animate)1821     public void setOccluded(boolean isOccluded, boolean animate) {
1822         Log.d(TAG, "setOccluded(" + isOccluded + ")");
1823 
1824         Trace.beginSection("KeyguardViewMediator#setOccluded");
1825         if (DEBUG) Log.d(TAG, "setOccluded " + isOccluded);
1826         mHandler.removeMessages(SET_OCCLUDED);
1827         Message msg = mHandler.obtainMessage(SET_OCCLUDED, isOccluded ? 1 : 0, animate ? 1 : 0);
1828         mHandler.sendMessage(msg);
1829         Trace.endSection();
1830     }
1831 
getOccludeAnimationRunner()1832     public IRemoteAnimationRunner getOccludeAnimationRunner() {
1833         return mOccludeAnimationRunner;
1834     }
1835 
getOccludeByDreamAnimationRunner()1836     public IRemoteAnimationRunner getOccludeByDreamAnimationRunner() {
1837         return mOccludeByDreamAnimationRunner;
1838     }
1839 
getUnoccludeAnimationRunner()1840     public IRemoteAnimationRunner getUnoccludeAnimationRunner() {
1841         return mUnoccludeAnimationRunner;
1842     }
1843 
isHiding()1844     public boolean isHiding() {
1845         return mHiding;
1846     }
1847 
isAnimatingScreenOff()1848     public boolean isAnimatingScreenOff() {
1849         return mAnimatingScreenOff;
1850     }
1851 
1852     /**
1853      * Handles SET_OCCLUDED message sent by setOccluded()
1854      */
handleSetOccluded(boolean isOccluded, boolean animate)1855     private void handleSetOccluded(boolean isOccluded, boolean animate) {
1856         Trace.beginSection("KeyguardViewMediator#handleSetOccluded");
1857         Log.d(TAG, "handleSetOccluded(" + isOccluded + ")");
1858         EventLogTags.writeSysuiKeyguard(isOccluded ? 1 : 0, animate ? 1 : 0);
1859 
1860         mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_TRANSITION_FROM_AOD);
1861 
1862         synchronized (KeyguardViewMediator.this) {
1863             if (mHiding && isOccluded) {
1864                 // We're in the process of going away but WindowManager wants to show a
1865                 // SHOW_WHEN_LOCKED activity instead.
1866                 // TODO(bc-unlock): Migrate to remote animation.
1867                 startKeyguardExitAnimation(0, 0);
1868             }
1869 
1870             if (mOccluded != isOccluded) {
1871                 mOccluded = isOccluded;
1872                 mKeyguardViewControllerLazy.get().setOccluded(isOccluded, animate
1873                         && mDeviceInteractive);
1874                 adjustStatusBarLocked();
1875             }
1876         }
1877         Trace.endSection();
1878     }
1879 
1880     /**
1881      * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
1882      * This must be safe to call from any thread and with any window manager locks held.
1883      */
doKeyguardTimeout(Bundle options)1884     public void doKeyguardTimeout(Bundle options) {
1885         mHandler.removeMessages(KEYGUARD_TIMEOUT);
1886         Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
1887         // Treat these messages with priority - A call to timeout means the device should lock
1888         // as soon as possible and not wait for other messages on the thread to process first.
1889         mHandler.sendMessageAtFrontOfQueue(msg);
1890     }
1891 
1892     /**
1893      * Given the state of the keyguard, is the input restricted?
1894      * Input is restricted when the keyguard is showing, or when the keyguard
1895      * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
1896      */
isInputRestricted()1897     public boolean isInputRestricted() {
1898         return mShowing || mNeedToReshowWhenReenabled;
1899     }
1900 
updateInputRestricted()1901     private void updateInputRestricted() {
1902         synchronized (this) {
1903             updateInputRestrictedLocked();
1904         }
1905     }
1906 
updateInputRestrictedLocked()1907     private void updateInputRestrictedLocked() {
1908         boolean inputRestricted = isInputRestricted();
1909         if (mInputRestricted != inputRestricted) {
1910             mInputRestricted = inputRestricted;
1911             int size = mKeyguardStateCallbacks.size();
1912             for (int i = size - 1; i >= 0; i--) {
1913                 final IKeyguardStateCallback callback = mKeyguardStateCallbacks.get(i);
1914                 try {
1915                     callback.onInputRestrictedStateChanged(inputRestricted);
1916                 } catch (RemoteException e) {
1917                     Slog.w(TAG, "Failed to call onDeviceProvisioned", e);
1918                     if (e instanceof DeadObjectException) {
1919                         mKeyguardStateCallbacks.remove(callback);
1920                     }
1921                 }
1922             }
1923         }
1924     }
1925 
1926     /**
1927      * Enable the keyguard if the settings are appropriate.
1928      */
doKeyguardLocked(Bundle options)1929     private void doKeyguardLocked(Bundle options) {
1930         // if another app is disabling us, don't show
1931         if (!mExternallyEnabled
1932             && !mLockPatternUtils.isUserInLockdown(KeyguardUpdateMonitor.getCurrentUser())) {
1933             if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
1934 
1935             mNeedToReshowWhenReenabled = true;
1936             return;
1937         }
1938 
1939         // If the keyguard is already showing, see if we don't need to bother re-showing it. Check
1940         // flags in both files to account for the hiding animation which results in a delay and
1941         // discrepancy between flags. If we're in the middle of hiding, do not short circuit so that
1942         // we explicitly re-set state.
1943         if (mShowing && mKeyguardStateController.isShowing()) {
1944             if (mPM.isInteractive() && !mHiding) {
1945                 // It's already showing, and we're not trying to show it while the screen is off.
1946                 // We can simply reset all of the views.
1947                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing (instead, resetting) because it is "
1948                         + "already showing, we're interactive, and we were not previously hiding. "
1949                         + "It should be safe to short-circuit here.");
1950                 resetStateLocked();
1951                 return;
1952             } else {
1953                 // We are trying to show the keyguard while the screen is off or while we were in
1954                 // the middle of hiding - this results from race conditions involving locking while
1955                 // unlocking. Don't short-circuit here and ensure the keyguard is fully re-shown.
1956                 Log.e(TAG,
1957                         "doKeyguard: already showing, but re-showing because we're interactive or "
1958                                 + "were in the middle of hiding.");
1959             }
1960         }
1961 
1962         // In split system user mode, we never unlock system user.
1963         if (!mustNotUnlockCurrentUser()
1964                 || !mUpdateMonitor.isDeviceProvisioned()) {
1965 
1966             // if the setup wizard hasn't run yet, don't show
1967             final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim", false);
1968             final boolean absent = SubscriptionManager.isValidSubscriptionId(
1969                     mUpdateMonitor.getNextSubIdForState(TelephonyManager.SIM_STATE_ABSENT));
1970             final boolean disabled = SubscriptionManager.isValidSubscriptionId(
1971                     mUpdateMonitor.getNextSubIdForState(TelephonyManager.SIM_STATE_PERM_DISABLED));
1972             final boolean lockedOrMissing = mUpdateMonitor.isSimPinSecure()
1973                     || ((absent || disabled) && requireSim);
1974 
1975             if (!lockedOrMissing && shouldWaitForProvisioning()) {
1976                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
1977                         + " and the sim is not locked or missing");
1978                 return;
1979             }
1980 
1981             boolean forceShow = options != null && options.getBoolean(OPTION_FORCE_SHOW, false);
1982             if (mLockPatternUtils.isLockScreenDisabled(KeyguardUpdateMonitor.getCurrentUser())
1983                     && !lockedOrMissing && !forceShow) {
1984                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
1985                 return;
1986             }
1987         }
1988 
1989         if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
1990         showLocked(options);
1991     }
1992 
lockProfile(int userId)1993     private void lockProfile(int userId) {
1994         mTrustManager.setDeviceLockedForUser(userId, true);
1995     }
1996 
shouldWaitForProvisioning()1997     private boolean shouldWaitForProvisioning() {
1998         return !mUpdateMonitor.isDeviceProvisioned() && !isSecure();
1999     }
2000 
2001     /**
2002      * Dismiss the keyguard through the security layers.
2003      * @param callback Callback to be informed about the result
2004      * @param message Message that should be displayed on the bouncer.
2005      */
handleDismiss(IKeyguardDismissCallback callback, CharSequence message)2006     private void handleDismiss(IKeyguardDismissCallback callback, CharSequence message) {
2007         if (mShowing) {
2008             if (callback != null) {
2009                 mDismissCallbackRegistry.addCallback(callback);
2010             }
2011             mCustomMessage = message;
2012             mKeyguardViewControllerLazy.get().dismissAndCollapse();
2013         } else if (callback != null) {
2014             new DismissCallbackWrapper(callback).notifyDismissError();
2015         }
2016     }
2017 
dismiss(IKeyguardDismissCallback callback, CharSequence message)2018     public void dismiss(IKeyguardDismissCallback callback, CharSequence message) {
2019         mHandler.obtainMessage(DISMISS, new DismissMessage(callback, message)).sendToTarget();
2020     }
2021 
2022     /**
2023      * Send message to keyguard telling it to reset its state.
2024      * @see #handleReset
2025      */
resetStateLocked()2026     private void resetStateLocked() {
2027         if (DEBUG) Log.e(TAG, "resetStateLocked");
2028         Message msg = mHandler.obtainMessage(RESET);
2029         mHandler.sendMessage(msg);
2030     }
2031 
2032     /**
2033      * Send message to keyguard telling it to verify unlock
2034      * @see #handleVerifyUnlock()
2035      */
verifyUnlockLocked()2036     private void verifyUnlockLocked() {
2037         if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
2038         mHandler.sendEmptyMessage(VERIFY_UNLOCK);
2039     }
2040 
notifyStartedGoingToSleep()2041     private void notifyStartedGoingToSleep() {
2042         if (DEBUG) Log.d(TAG, "notifyStartedGoingToSleep");
2043         mHandler.sendEmptyMessage(NOTIFY_STARTED_GOING_TO_SLEEP);
2044     }
2045 
notifyFinishedGoingToSleep()2046     private void notifyFinishedGoingToSleep() {
2047         if (DEBUG) Log.d(TAG, "notifyFinishedGoingToSleep");
2048         mHandler.sendEmptyMessage(NOTIFY_FINISHED_GOING_TO_SLEEP);
2049     }
2050 
notifyStartedWakingUp()2051     private void notifyStartedWakingUp() {
2052         if (DEBUG) Log.d(TAG, "notifyStartedWakingUp");
2053         mHandler.sendEmptyMessage(NOTIFY_STARTED_WAKING_UP);
2054     }
2055 
2056     /**
2057      * Send message to keyguard telling it to show itself
2058      * @see #handleShow
2059      */
showLocked(Bundle options)2060     private void showLocked(Bundle options) {
2061         Trace.beginSection("KeyguardViewMediator#showLocked acquiring mShowKeyguardWakeLock");
2062         if (DEBUG) Log.d(TAG, "showLocked");
2063         // ensure we stay awake until we are finished displaying the keyguard
2064         mShowKeyguardWakeLock.acquire();
2065         Message msg = mHandler.obtainMessage(SHOW, options);
2066         // Treat these messages with priority - This call can originate from #doKeyguardTimeout,
2067         // meaning the device should lock as soon as possible and not wait for other messages on
2068         // the thread to process first.
2069         mHandler.sendMessageAtFrontOfQueue(msg);
2070         Trace.endSection();
2071     }
2072 
2073     /**
2074      * Send message to keyguard telling it to hide itself
2075      * @see #handleHide()
2076      */
hideLocked()2077     private void hideLocked() {
2078         Trace.beginSection("KeyguardViewMediator#hideLocked");
2079         if (DEBUG) Log.d(TAG, "hideLocked");
2080         Message msg = mHandler.obtainMessage(HIDE);
2081         mHandler.sendMessage(msg);
2082         Trace.endSection();
2083     }
2084 
2085     /**
2086      * Hide the keyguard and let {@code runner} handle the animation.
2087      *
2088      * This method should typically be called after {@link ViewMediatorCallback#keyguardDonePending}
2089      * was called, when we are ready to hide the keyguard. It will do nothing if we were not
2090      * expecting the keyguard to go away when called.
2091      */
hideWithAnimation(IRemoteAnimationRunner runner)2092     public void hideWithAnimation(IRemoteAnimationRunner runner) {
2093         if (!mKeyguardDonePending) {
2094             return;
2095         }
2096 
2097         mKeyguardExitAnimationRunner = runner;
2098         mViewMediatorCallback.readyForKeyguardDone();
2099     }
2100 
2101     /**
2102      * Disable notification shade background blurs until the keyguard is dismissed.
2103      * (Used during app launch animations)
2104      */
setBlursDisabledForAppLaunch(boolean disabled)2105     public void setBlursDisabledForAppLaunch(boolean disabled) {
2106         mNotificationShadeDepthController.get().setBlursDisabledForAppLaunch(disabled);
2107     }
2108 
isSecure()2109     public boolean isSecure() {
2110         return isSecure(KeyguardUpdateMonitor.getCurrentUser());
2111     }
2112 
isSecure(int userId)2113     public boolean isSecure(int userId) {
2114         return mLockPatternUtils.isSecure(userId)
2115                 || mUpdateMonitor.isSimPinSecure();
2116     }
2117 
2118     /**
2119      * Whether any of the SIMs on the device are secured with a PIN. If so, the keyguard should not
2120      * be dismissable until the PIN is entered, even if the device itself has no lock set.
2121      */
isAnySimPinSecure()2122     public boolean isAnySimPinSecure() {
2123         for (int i = 0; i < mLastSimStates.size(); i++) {
2124             final int key = mLastSimStates.keyAt(i);
2125             if (KeyguardUpdateMonitor.isSimPinSecure(mLastSimStates.get(key))) {
2126                 return true;
2127             }
2128         }
2129 
2130         return false;
2131     }
2132 
setSwitchingUser(boolean switching)2133     public void setSwitchingUser(boolean switching) {
2134         mUpdateMonitor.setSwitchingUser(switching);
2135     }
2136 
2137     /**
2138      * Update the newUserId. Call while holding WindowManagerService lock.
2139      * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
2140      *
2141      * @param newUserId The id of the incoming user.
2142      */
setCurrentUser(int newUserId)2143     public void setCurrentUser(int newUserId) {
2144         KeyguardUpdateMonitor.setCurrentUser(newUserId);
2145         synchronized (this) {
2146             notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(newUserId));
2147         }
2148     }
2149 
2150     /**
2151      * This broadcast receiver should be registered with the SystemUI permission.
2152      */
2153     private final BroadcastReceiver mDelayedLockBroadcastReceiver = new BroadcastReceiver() {
2154         @Override
2155         public void onReceive(Context context, Intent intent) {
2156             if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
2157                 final int sequence = intent.getIntExtra("seq", 0);
2158                 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
2159                         + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
2160                 synchronized (KeyguardViewMediator.this) {
2161                     if (mDelayedShowingSequence == sequence) {
2162                         doKeyguardLocked(null);
2163                     }
2164                 }
2165             } else if (DELAYED_LOCK_PROFILE_ACTION.equals(intent.getAction())) {
2166                 final int sequence = intent.getIntExtra("seq", 0);
2167                 int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, 0);
2168                 if (userId != 0) {
2169                     synchronized (KeyguardViewMediator.this) {
2170                         if (mDelayedProfileShowingSequence == sequence) {
2171                             lockProfile(userId);
2172                         }
2173                     }
2174                 }
2175             }
2176         }
2177     };
2178 
2179     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
2180         @Override
2181         public void onReceive(Context context, Intent intent) {
2182             if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
2183                 synchronized (KeyguardViewMediator.this){
2184                     mShuttingDown = true;
2185                 }
2186             }
2187         }
2188     };
2189 
keyguardDone()2190     private void keyguardDone() {
2191         Trace.beginSection("KeyguardViewMediator#keyguardDone");
2192         if (DEBUG) Log.d(TAG, "keyguardDone()");
2193         userActivity();
2194         EventLog.writeEvent(70000, 2);
2195         Message msg = mHandler.obtainMessage(KEYGUARD_DONE);
2196         mHandler.sendMessage(msg);
2197         Trace.endSection();
2198     }
2199 
2200     /**
2201      * This handler will be associated with the policy thread, which will also
2202      * be the UI thread of the keyguard.  Since the apis of the policy, and therefore
2203      * this class, can be called by other threads, any action that directly
2204      * interacts with the keyguard ui should be posted to this handler, rather
2205      * than called directly.
2206      */
2207     private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
2208         @Override
2209         public void handleMessage(Message msg) {
2210             switch (msg.what) {
2211                 case SHOW:
2212                     handleShow((Bundle) msg.obj);
2213                     break;
2214                 case HIDE:
2215                     handleHide();
2216                     break;
2217                 case RESET:
2218                     handleReset();
2219                     break;
2220                 case VERIFY_UNLOCK:
2221                     Trace.beginSection("KeyguardViewMediator#handleMessage VERIFY_UNLOCK");
2222                     handleVerifyUnlock();
2223                     Trace.endSection();
2224                     break;
2225                 case NOTIFY_STARTED_GOING_TO_SLEEP:
2226                     handleNotifyStartedGoingToSleep();
2227                     break;
2228                 case NOTIFY_FINISHED_GOING_TO_SLEEP:
2229                     handleNotifyFinishedGoingToSleep();
2230                     break;
2231                 case NOTIFY_STARTED_WAKING_UP:
2232                     Trace.beginSection(
2233                             "KeyguardViewMediator#handleMessage NOTIFY_STARTED_WAKING_UP");
2234                     handleNotifyStartedWakingUp();
2235                     Trace.endSection();
2236                     break;
2237                 case KEYGUARD_DONE:
2238                     Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE");
2239                     handleKeyguardDone();
2240                     Trace.endSection();
2241                     break;
2242                 case KEYGUARD_DONE_DRAWING:
2243                     Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE_DRAWING");
2244                     handleKeyguardDoneDrawing();
2245                     Trace.endSection();
2246                     break;
2247                 case SET_OCCLUDED:
2248                     Trace.beginSection("KeyguardViewMediator#handleMessage SET_OCCLUDED");
2249                     handleSetOccluded(msg.arg1 != 0, msg.arg2 != 0);
2250                     Trace.endSection();
2251                     break;
2252                 case KEYGUARD_TIMEOUT:
2253                     synchronized (KeyguardViewMediator.this) {
2254                         doKeyguardLocked((Bundle) msg.obj);
2255                     }
2256                     break;
2257                 case DISMISS:
2258                     final DismissMessage message = (DismissMessage) msg.obj;
2259                     handleDismiss(message.getCallback(), message.getMessage());
2260                     break;
2261                 case START_KEYGUARD_EXIT_ANIM:
2262                     Trace.beginSection(
2263                             "KeyguardViewMediator#handleMessage START_KEYGUARD_EXIT_ANIM");
2264                     synchronized (KeyguardViewMediator.this) {
2265                         mHiding = true;
2266                     }
2267                     StartKeyguardExitAnimParams params = (StartKeyguardExitAnimParams) msg.obj;
2268                     mNotificationShadeWindowControllerLazy.get().batchApplyWindowLayoutParams(
2269                             () -> {
2270                                 handleStartKeyguardExitAnimation(params.startTime,
2271                                         params.fadeoutDuration,
2272                                         params.mApps, params.mWallpapers, params.mNonApps,
2273                                         params.mFinishedCallback);
2274                                 mFalsingCollector.onSuccessfulUnlock();
2275                             });
2276                     Trace.endSection();
2277                     break;
2278                 case CANCEL_KEYGUARD_EXIT_ANIM:
2279                     Trace.beginSection(
2280                             "KeyguardViewMediator#handleMessage CANCEL_KEYGUARD_EXIT_ANIM");
2281                     handleCancelKeyguardExitAnimation();
2282                     Trace.endSection();
2283                     break;
2284                 case KEYGUARD_DONE_PENDING_TIMEOUT:
2285                     Trace.beginSection("KeyguardViewMediator#handleMessage"
2286                             + " KEYGUARD_DONE_PENDING_TIMEOUT");
2287                     Log.w(TAG, "Timeout while waiting for activity drawn!");
2288                     Trace.endSection();
2289                     break;
2290                 case SYSTEM_READY:
2291                     handleSystemReady();
2292                     break;
2293             }
2294         }
2295     };
2296 
tryKeyguardDone()2297     private void tryKeyguardDone() {
2298         if (DEBUG) {
2299             Log.d(TAG, "tryKeyguardDone: pending - " + mKeyguardDonePending + ", animRan - "
2300                     + mHideAnimationRun + " animRunning - " + mHideAnimationRunning);
2301         }
2302         if (!mKeyguardDonePending && mHideAnimationRun && !mHideAnimationRunning) {
2303             handleKeyguardDone();
2304         } else if (mSurfaceBehindRemoteAnimationRunning) {
2305             // We're already running the keyguard exit animation, likely due to an in-progress swipe
2306             // to unlock.
2307            exitKeyguardAndFinishSurfaceBehindRemoteAnimation(false /* cancelled */);
2308         } else if (!mHideAnimationRun) {
2309             if (DEBUG) Log.d(TAG, "tryKeyguardDone: starting pre-hide animation");
2310             mHideAnimationRun = true;
2311             mHideAnimationRunning = true;
2312             mKeyguardViewControllerLazy.get()
2313                     .startPreHideAnimation(mHideAnimationFinishedRunnable);
2314         }
2315     }
2316 
2317     /**
2318      * @see #keyguardDone
2319      * @see #KEYGUARD_DONE
2320      */
handleKeyguardDone()2321     private void handleKeyguardDone() {
2322         Trace.beginSection("KeyguardViewMediator#handleKeyguardDone");
2323         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
2324         mUiBgExecutor.execute(() -> {
2325             if (mLockPatternUtils.isSecure(currentUser)) {
2326                 mLockPatternUtils.getDevicePolicyManager().reportKeyguardDismissed(currentUser);
2327             }
2328         });
2329         if (DEBUG) Log.d(TAG, "handleKeyguardDone");
2330         synchronized (this) {
2331             resetKeyguardDonePendingLocked();
2332         }
2333 
2334         if (mGoingToSleep) {
2335             mUpdateMonitor.clearBiometricRecognizedWhenKeyguardDone(currentUser);
2336             Log.i(TAG, "Device is going to sleep, aborting keyguardDone");
2337             return;
2338         }
2339         setPendingLock(false); // user may have authenticated during the screen off animation
2340 
2341         handleHide();
2342         mUpdateMonitor.clearBiometricRecognizedWhenKeyguardDone(currentUser);
2343         Trace.endSection();
2344     }
2345 
sendUserPresentBroadcast()2346     private void sendUserPresentBroadcast() {
2347         synchronized (this) {
2348             if (mBootCompleted) {
2349                 int currentUserId = KeyguardUpdateMonitor.getCurrentUser();
2350                 final UserHandle currentUser = new UserHandle(currentUserId);
2351                 final UserManager um = (UserManager) mContext.getSystemService(
2352                         Context.USER_SERVICE);
2353                 mUiBgExecutor.execute(() -> {
2354                     for (int profileId : um.getProfileIdsWithDisabled(currentUser.getIdentifier())) {
2355                         mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, UserHandle.of(profileId));
2356                     }
2357                     mLockPatternUtils.userPresent(currentUserId);
2358                 });
2359             } else {
2360                 mBootSendUserPresent = true;
2361             }
2362         }
2363     }
2364 
2365     /**
2366      * @see #keyguardDone
2367      * @see #KEYGUARD_DONE_DRAWING
2368      */
handleKeyguardDoneDrawing()2369     private void handleKeyguardDoneDrawing() {
2370         Trace.beginSection("KeyguardViewMediator#handleKeyguardDoneDrawing");
2371         synchronized(this) {
2372             if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
2373             if (mWaitingUntilKeyguardVisible) {
2374                 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
2375                 mWaitingUntilKeyguardVisible = false;
2376                 notifyAll();
2377 
2378                 // there will usually be two of these sent, one as a timeout, and one
2379                 // as a result of the callback, so remove any remaining messages from
2380                 // the queue
2381                 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
2382             }
2383         }
2384         Trace.endSection();
2385     }
2386 
playSounds(boolean locked)2387     private void playSounds(boolean locked) {
2388         playSound(locked ? mLockSoundId : mUnlockSoundId);
2389     }
2390 
playSound(int soundId)2391     private void playSound(int soundId) {
2392         if (soundId == 0) return;
2393         final ContentResolver cr = mContext.getContentResolver();
2394         int lockscreenSoundsEnabled = Settings.System.getIntForUser(cr,
2395                 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1,
2396                 KeyguardUpdateMonitor.getCurrentUser());
2397         if (lockscreenSoundsEnabled == 1) {
2398 
2399             mLockSounds.stop(mLockSoundStreamId);
2400             // Init mAudioManager
2401             if (mAudioManager == null) {
2402                 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
2403                 if (mAudioManager == null) return;
2404                 mUiSoundsStreamType = mAudioManager.getUiSoundsStreamType();
2405             }
2406 
2407             mUiBgExecutor.execute(() -> {
2408                 // If the stream is muted, don't play the sound
2409                 if (mAudioManager.isStreamMute(mUiSoundsStreamType)) return;
2410 
2411                 int id = mLockSounds.play(soundId,
2412                         mLockSoundVolume, mLockSoundVolume, 1/*priority*/, 0/*loop*/, 1.0f/*rate*/);
2413                 synchronized (this) {
2414                     mLockSoundStreamId = id;
2415                 }
2416             });
2417 
2418         }
2419     }
2420 
playTrustedSound()2421     private void playTrustedSound() {
2422         playSound(mTrustedSoundId);
2423     }
2424 
updateActivityLockScreenState(boolean showing, boolean aodShowing)2425     private void updateActivityLockScreenState(boolean showing, boolean aodShowing) {
2426         mUiBgExecutor.execute(() -> {
2427             if (DEBUG) {
2428                 Log.d(TAG, "updateActivityLockScreenState(" + showing + ", " + aodShowing + ")");
2429             }
2430             try {
2431                 ActivityTaskManager.getService().setLockScreenShown(showing, aodShowing);
2432             } catch (RemoteException e) {
2433             }
2434         });
2435     }
2436 
2437     /**
2438      * Handle message sent by {@link #showLocked}.
2439      * @see #SHOW
2440      */
handleShow(Bundle options)2441     private void handleShow(Bundle options) {
2442         Trace.beginSection("KeyguardViewMediator#handleShow");
2443         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
2444         if (mLockPatternUtils.isSecure(currentUser)) {
2445             mLockPatternUtils.getDevicePolicyManager().reportKeyguardSecured(currentUser);
2446         }
2447         synchronized (KeyguardViewMediator.this) {
2448             if (!mSystemReady) {
2449                 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
2450                 return;
2451             } else {
2452                 if (DEBUG) Log.d(TAG, "handleShow");
2453             }
2454 
2455             mKeyguardExitAnimationRunner = null;
2456             mWakeAndUnlocking = false;
2457             setPendingLock(false);
2458 
2459             // Force if we we're showing in the middle of hiding, to ensure we end up in the correct
2460             // state.
2461             setShowingLocked(true, mHiding /* force */);
2462             if (mHiding) {
2463                 Log.d(TAG, "Forcing setShowingLocked because mHiding=true, which means we're "
2464                         + "showing in the middle of hiding.");
2465             }
2466             mHiding = false;
2467 
2468             mKeyguardViewControllerLazy.get().show(options);
2469             resetKeyguardDonePendingLocked();
2470             mHideAnimationRun = false;
2471             adjustStatusBarLocked();
2472             userActivity();
2473             mUpdateMonitor.setKeyguardGoingAway(false);
2474             mKeyguardViewControllerLazy.get().setKeyguardGoingAwayState(false);
2475             mShowKeyguardWakeLock.release();
2476         }
2477         mKeyguardDisplayManager.show();
2478 
2479         scheduleNonStrongBiometricIdleTimeout();
2480 
2481         Trace.endSection();
2482     }
2483 
2484     /**
2485      * Schedule 4-hour idle timeout for non-strong biometrics when the device is locked
2486      */
scheduleNonStrongBiometricIdleTimeout()2487     private void scheduleNonStrongBiometricIdleTimeout() {
2488         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
2489         // If unlocking with non-strong (i.e. weak or convenience) biometrics is possible, schedule
2490         // 4hr idle timeout after which non-strong biometrics can't be used to unlock device until
2491         // unlocking with strong biometric or primary auth (i.e. PIN/pattern/password)
2492         if (mUpdateMonitor.isUnlockingWithNonStrongBiometricsPossible(currentUser)) {
2493             if (DEBUG) {
2494                 Log.d(TAG, "scheduleNonStrongBiometricIdleTimeout: schedule an alarm for "
2495                         + "currentUser=" + currentUser);
2496             }
2497             mLockPatternUtils.scheduleNonStrongBiometricIdleTimeout(currentUser);
2498         }
2499     }
2500 
2501     private final Runnable mKeyguardGoingAwayRunnable = new Runnable() {
2502         @Override
2503         public void run() {
2504             Trace.beginSection("KeyguardViewMediator.mKeyGuardGoingAwayRunnable");
2505             if (DEBUG) Log.d(TAG, "keyguardGoingAway");
2506             mKeyguardViewControllerLazy.get().keyguardGoingAway();
2507 
2508             int flags = 0;
2509             if (mKeyguardViewControllerLazy.get().shouldDisableWindowAnimationsForUnlock()
2510                     || mWakeAndUnlocking && !mWallpaperSupportsAmbientMode) {
2511                 flags |= KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
2512             }
2513             if (mKeyguardViewControllerLazy.get().isGoingToNotificationShade()
2514                     || mWakeAndUnlocking && mWallpaperSupportsAmbientMode) {
2515                 // When the wallpaper supports ambient mode, the scrim isn't fully opaque during
2516                 // wake and unlock, and we should fade in the app on top of the wallpaper
2517                 flags |= WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
2518             }
2519             if (mKeyguardViewControllerLazy.get().isUnlockWithWallpaper()) {
2520                 flags |= KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
2521             }
2522             if (mKeyguardViewControllerLazy.get().shouldSubtleWindowAnimationsForUnlock()) {
2523                 flags |= WindowManagerPolicyConstants
2524                         .KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS;
2525             }
2526 
2527             // If we are unlocking to the launcher, clear the snapshot so that any changes as part
2528             // of the in-window animations are reflected. This is needed even if we're not actually
2529             // playing in-window animations for this particular unlock since a previous unlock might
2530             // have changed the Launcher state.
2531             if (mWakeAndUnlocking
2532                     && KeyguardUnlockAnimationController.Companion.isNexusLauncherUnderneath()) {
2533                 flags |= KEYGUARD_GOING_AWAY_FLAG_TO_LAUNCHER_CLEAR_SNAPSHOT;
2534             }
2535 
2536             mUpdateMonitor.setKeyguardGoingAway(true);
2537             mKeyguardViewControllerLazy.get().setKeyguardGoingAwayState(true);
2538 
2539             // Don't actually hide the Keyguard at the moment, wait for window
2540             // manager until it tells us it's safe to do so with
2541             // startKeyguardExitAnimation.
2542             // Posting to mUiOffloadThread to ensure that calls to ActivityTaskManager will be in
2543             // order.
2544             final int keyguardFlag = flags;
2545             mUiBgExecutor.execute(() -> {
2546                 try {
2547                     ActivityTaskManager.getService().keyguardGoingAway(keyguardFlag);
2548                 } catch (RemoteException e) {
2549                     Log.e(TAG, "Error while calling WindowManager", e);
2550                 }
2551             });
2552             Trace.endSection();
2553         }
2554     };
2555 
2556     private final Runnable mHideAnimationFinishedRunnable = () -> {
2557         Log.e(TAG, "mHideAnimationFinishedRunnable#run");
2558         mHideAnimationRunning = false;
2559         tryKeyguardDone();
2560     };
2561 
2562     /**
2563      * Handle message sent by {@link #hideLocked()}
2564      * @see #HIDE
2565      */
handleHide()2566     private void handleHide() {
2567         Trace.beginSection("KeyguardViewMediator#handleHide");
2568 
2569         // It's possible that the device was unlocked (via BOUNCER) while dozing. It's time to
2570         // wake up.
2571         if (mAodShowing) {
2572             mPM.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE,
2573                     "com.android.systemui:BOUNCER_DOZING");
2574         }
2575 
2576         synchronized (KeyguardViewMediator.this) {
2577             if (DEBUG) Log.d(TAG, "handleHide");
2578 
2579             if (mustNotUnlockCurrentUser()) {
2580                 // In split system user mode, we never unlock system user. The end user has to
2581                 // switch to another user.
2582                 // TODO: We should stop it early by disabling the swipe up flow. Right now swipe up
2583                 // still completes and makes the screen blank.
2584                 if (DEBUG) Log.d(TAG, "Split system user, quit unlocking.");
2585                 mKeyguardExitAnimationRunner = null;
2586                 return;
2587             }
2588             mHiding = true;
2589 
2590             if (mShowing && !mOccluded) {
2591                 mKeyguardGoingAwayRunnable.run();
2592             } else {
2593                 // TODO(bc-unlock): Fill parameters
2594                 mNotificationShadeWindowControllerLazy.get().batchApplyWindowLayoutParams(() -> {
2595                     handleStartKeyguardExitAnimation(
2596                             SystemClock.uptimeMillis() + mHideAnimation.getStartOffset(),
2597                             mHideAnimation.getDuration(), null /* apps */, null /* wallpapers */,
2598                             null /* nonApps */, null /* finishedCallback */);
2599                 });
2600             }
2601 
2602             // It's possible that the device was unlocked (via BOUNCER or Fingerprint) while
2603             // dreaming. It's time to wake up.
2604             if (mDreamOverlayShowing) {
2605                 mPM.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE,
2606                         "com.android.systemui:UNLOCK_DREAMING");
2607             }
2608         }
2609         Trace.endSection();
2610     }
2611 
handleStartKeyguardExitAnimation(long startTime, long fadeoutDuration, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)2612     private void handleStartKeyguardExitAnimation(long startTime, long fadeoutDuration,
2613             RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers,
2614             RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback) {
2615         Trace.beginSection("KeyguardViewMediator#handleStartKeyguardExitAnimation");
2616         Log.d(TAG, "handleStartKeyguardExitAnimation startTime=" + startTime
2617                 + " fadeoutDuration=" + fadeoutDuration);
2618         synchronized (KeyguardViewMediator.this) {
2619 
2620             // Tell ActivityManager that we canceled the keyguard animation if
2621             // handleStartKeyguardExitAnimation was called, but we're not hiding the keyguard,
2622             // unless we're animating the surface behind the keyguard and will be hiding the
2623             // keyguard shortly.
2624             if (!mHiding
2625                     && !mSurfaceBehindRemoteAnimationRequested
2626                     && !mKeyguardStateController.isFlingingToDismissKeyguardDuringSwipeGesture()) {
2627                 if (finishedCallback != null) {
2628                     // There will not execute animation, send a finish callback to ensure the remote
2629                     // animation won't hang there.
2630                     try {
2631                         finishedCallback.onAnimationFinished();
2632                     } catch (RemoteException e) {
2633                         Slog.w(TAG, "Failed to call onAnimationFinished", e);
2634                     }
2635                 }
2636                 setShowingLocked(mShowing, true /* force */);
2637                 return;
2638             }
2639             mHiding = false;
2640             IRemoteAnimationRunner runner = mKeyguardExitAnimationRunner;
2641             mKeyguardExitAnimationRunner = null;
2642 
2643             LatencyTracker.getInstance(mContext)
2644                     .onActionEnd(LatencyTracker.ACTION_LOCKSCREEN_UNLOCK);
2645 
2646             if (KeyguardService.sEnableRemoteKeyguardGoingAwayAnimation && runner != null
2647                     && finishedCallback != null) {
2648                 // Wrap finishedCallback to clean up the keyguard state once the animation is done.
2649                 IRemoteAnimationFinishedCallback callback =
2650                         new IRemoteAnimationFinishedCallback() {
2651                             @Override
2652                             public void onAnimationFinished() throws RemoteException {
2653                                 try {
2654                                     finishedCallback.onAnimationFinished();
2655                                 } catch (RemoteException e) {
2656                                     Slog.w(TAG, "Failed to call onAnimationFinished", e);
2657                                 }
2658                                 onKeyguardExitFinished();
2659                                 mKeyguardViewControllerLazy.get().hide(0 /* startTime */,
2660                                         0 /* fadeoutDuration */);
2661                                 mInteractionJankMonitor.end(CUJ_LOCKSCREEN_UNLOCK_ANIMATION);
2662                             }
2663 
2664                             @Override
2665                             public IBinder asBinder() {
2666                                 return finishedCallback.asBinder();
2667                             }
2668                         };
2669                 try {
2670                     mInteractionJankMonitor.begin(
2671                             createInteractionJankMonitorConf(
2672                                     CUJ_LOCKSCREEN_UNLOCK_ANIMATION, "RunRemoteAnimation"));
2673                     runner.onAnimationStart(WindowManager.TRANSIT_KEYGUARD_GOING_AWAY, apps,
2674                             wallpapers, nonApps, callback);
2675                 } catch (RemoteException e) {
2676                     Slog.w(TAG, "Failed to call onAnimationStart", e);
2677                 }
2678 
2679             // When remaining on the shade, there's no need to do a fancy remote animation,
2680             // it will dismiss the panel in that case.
2681             } else if (KeyguardService.sEnableRemoteKeyguardGoingAwayAnimation
2682                     && !mStatusBarStateController.leaveOpenOnKeyguardHide()
2683                     && apps != null && apps.length > 0) {
2684                 mSurfaceBehindRemoteAnimationFinishedCallback = finishedCallback;
2685                 mSurfaceBehindRemoteAnimationRunning = true;
2686 
2687                 mInteractionJankMonitor.begin(
2688                         createInteractionJankMonitorConf(
2689                                 CUJ_LOCKSCREEN_UNLOCK_ANIMATION, "DismissPanel"));
2690 
2691                 // Pass the surface and metadata to the unlock animation controller.
2692                 mKeyguardUnlockAnimationControllerLazy.get()
2693                         .notifyStartSurfaceBehindRemoteAnimation(
2694                                 apps, startTime, mSurfaceBehindRemoteAnimationRequested);
2695             } else {
2696                 mInteractionJankMonitor.begin(
2697                         createInteractionJankMonitorConf(
2698                                 CUJ_LOCKSCREEN_UNLOCK_ANIMATION, "RemoteAnimationDisabled"));
2699 
2700                 mKeyguardViewControllerLazy.get().hide(startTime, fadeoutDuration);
2701 
2702                 // TODO(bc-animation): When remote animation is enabled for keyguard exit animation,
2703                 // apps, wallpapers and finishedCallback are set to non-null. nonApps is not yet
2704                 // supported, so it's always null.
2705                 mContext.getMainExecutor().execute(() -> {
2706                     if (finishedCallback == null) {
2707                         mKeyguardUnlockAnimationControllerLazy.get()
2708                                 .notifyFinishedKeyguardExitAnimation(false /* cancelled */);
2709                         mInteractionJankMonitor.end(CUJ_LOCKSCREEN_UNLOCK_ANIMATION);
2710                         return;
2711                     }
2712 
2713                     // TODO(bc-unlock): Sample animation, just to apply alpha animation on the app.
2714                     final SyncRtSurfaceTransactionApplier applier =
2715                             new SyncRtSurfaceTransactionApplier(
2716                                     mKeyguardViewControllerLazy.get().getViewRootImpl().getView());
2717                     final RemoteAnimationTarget primary = apps[0];
2718                     ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
2719                     anim.setDuration(400 /* duration */);
2720                     anim.setInterpolator(Interpolators.LINEAR);
2721                     anim.addUpdateListener((ValueAnimator animation) -> {
2722                         SyncRtSurfaceTransactionApplier.SurfaceParams params =
2723                                 new SyncRtSurfaceTransactionApplier.SurfaceParams.Builder(
2724                                         primary.leash)
2725                                         .withAlpha(animation.getAnimatedFraction())
2726                                         .build();
2727                         applier.scheduleApply(params);
2728                     });
2729                     anim.addListener(new AnimatorListenerAdapter() {
2730                         @Override
2731                         public void onAnimationEnd(Animator animation) {
2732                             try {
2733                                 finishedCallback.onAnimationFinished();
2734                             } catch (RemoteException e) {
2735                                 Slog.e(TAG, "RemoteException");
2736                             } finally {
2737                                 mInteractionJankMonitor.end(CUJ_LOCKSCREEN_UNLOCK_ANIMATION);
2738                             }
2739                         }
2740 
2741                         @Override
2742                         public void onAnimationCancel(Animator animation) {
2743                             try {
2744                                 finishedCallback.onAnimationFinished();
2745                             } catch (RemoteException e) {
2746                                 Slog.e(TAG, "RemoteException");
2747                             } finally {
2748                                 mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_UNLOCK_ANIMATION);
2749                             }
2750                         }
2751                     });
2752                     anim.start();
2753                 });
2754 
2755                 onKeyguardExitFinished();
2756             }
2757         }
2758 
2759         Trace.endSection();
2760     }
2761 
onKeyguardExitFinished()2762     private void onKeyguardExitFinished() {
2763         // only play "unlock" noises if not on a call (since the incall UI
2764         // disables the keyguard)
2765         if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
2766             playSounds(false);
2767         }
2768 
2769         setShowingLocked(false);
2770         mWakeAndUnlocking = false;
2771         mDismissCallbackRegistry.notifyDismissSucceeded();
2772         resetKeyguardDonePendingLocked();
2773         mHideAnimationRun = false;
2774         adjustStatusBarLocked();
2775         sendUserPresentBroadcast();
2776     }
2777 
createInteractionJankMonitorConf(int cuj)2778     private Configuration.Builder createInteractionJankMonitorConf(int cuj) {
2779         return createInteractionJankMonitorConf(cuj, null /* tag */);
2780     }
2781 
createInteractionJankMonitorConf(int cuj, @Nullable String tag)2782     private Configuration.Builder createInteractionJankMonitorConf(int cuj, @Nullable String tag) {
2783         final Configuration.Builder builder = Configuration.Builder.withView(
2784                 cuj, mKeyguardViewControllerLazy.get().getViewRootImpl().getView());
2785 
2786         return tag != null ? builder.setTag(tag) : builder;
2787     }
2788 
2789     /**
2790      * Whether we're currently animating between the keyguard and the app/launcher surface behind
2791      * it, or will be shortly (which happens if we started a fling to dismiss the keyguard).
2792      */
isAnimatingBetweenKeyguardAndSurfaceBehindOrWillBe()2793     public boolean isAnimatingBetweenKeyguardAndSurfaceBehindOrWillBe() {
2794         return mSurfaceBehindRemoteAnimationRunning
2795                 || mKeyguardStateController.isFlingingToDismissKeyguard();
2796     }
2797 
2798     /**
2799      * Called if the keyguard exit animation has been cancelled.
2800      *
2801      * This can happen due to the system cancelling the RemoteAnimation (due to a timeout, a new
2802      * app transition before finishing the current RemoteAnimation, or the keyguard being re-shown).
2803      */
handleCancelKeyguardExitAnimation()2804     private void handleCancelKeyguardExitAnimation() {
2805         if (mPendingLock) {
2806             Log.d(TAG, "#handleCancelKeyguardExitAnimation: keyguard exit animation cancelled. "
2807                     + "There's a pending lock, so we were cancelled because the device was locked "
2808                     + "again during the unlock sequence. We should end up locked.");
2809 
2810             // A lock is pending, meaning the keyguard exit animation was cancelled because we're
2811             // re-locking. We should just end the surface-behind animation without exiting the
2812             // keyguard. The pending lock will be handled by onFinishedGoingToSleep().
2813             finishSurfaceBehindRemoteAnimation(true);
2814         } else {
2815             Log.d(TAG, "#handleCancelKeyguardExitAnimation: keyguard exit animation cancelled. "
2816                     + "No pending lock, we should end up unlocked with the app/launcher visible.");
2817 
2818             // No lock is pending, so the animation was cancelled during the unlock sequence, but
2819             // we should end up unlocked. Show the surface and exit the keyguard.
2820             showSurfaceBehindKeyguard();
2821             exitKeyguardAndFinishSurfaceBehindRemoteAnimation(true /* cancelled */);
2822         }
2823     }
2824 
2825     /**
2826      * Called when we're done running the keyguard exit animation, we should now end up unlocked.
2827      *
2828      * This will call {@link #handleCancelKeyguardExitAnimation()} to let WM know that we're done
2829      * with the RemoteAnimation, actually hide the keyguard, and clean up state related to the
2830      * keyguard exit animation.
2831      *
2832      * @param cancelled {@code true} if the animation was cancelled before it finishes.
2833      */
exitKeyguardAndFinishSurfaceBehindRemoteAnimation(boolean cancelled)2834     public void exitKeyguardAndFinishSurfaceBehindRemoteAnimation(boolean cancelled) {
2835         Log.d(TAG, "onKeyguardExitRemoteAnimationFinished");
2836         if (!mSurfaceBehindRemoteAnimationRunning && !mSurfaceBehindRemoteAnimationRequested) {
2837             Log.d(TAG, "skip onKeyguardExitRemoteAnimationFinished cancelled=" + cancelled
2838                     + " surfaceAnimationRunning=" + mSurfaceBehindRemoteAnimationRunning
2839                     + " surfaceAnimationRequested=" + mSurfaceBehindRemoteAnimationRequested);
2840             return;
2841         }
2842 
2843         // Block the panel from expanding, in case we were doing a swipe to dismiss gesture.
2844         mKeyguardViewControllerLazy.get().blockPanelExpansionFromCurrentTouch();
2845         final boolean wasShowing = mShowing;
2846         InteractionJankMonitor.getInstance().end(CUJ_LOCKSCREEN_UNLOCK_ANIMATION);
2847 
2848         // Post layout changes to the next frame, so we don't hang at the end of the animation.
2849         DejankUtils.postAfterTraversal(() -> {
2850             onKeyguardExitFinished();
2851 
2852             if (mKeyguardStateController.isDismissingFromSwipe() || wasShowing) {
2853                 Log.d(TAG, "onKeyguardExitRemoteAnimationFinished"
2854                         + "#hideKeyguardViewAfterRemoteAnimation");
2855                 mKeyguardUnlockAnimationControllerLazy.get().hideKeyguardViewAfterRemoteAnimation();
2856             } else {
2857                 Log.d(TAG, "skip hideKeyguardViewAfterRemoteAnimation"
2858                         + " dismissFromSwipe=" + mKeyguardStateController.isDismissingFromSwipe()
2859                         + " wasShowing=" + wasShowing);
2860             }
2861 
2862             finishSurfaceBehindRemoteAnimation(cancelled);
2863 
2864             // Dispatch the callback on animation finishes.
2865             mUpdateMonitor.dispatchKeyguardDismissAnimationFinished();
2866         });
2867 
2868         mKeyguardUnlockAnimationControllerLazy.get().notifyFinishedKeyguardExitAnimation(
2869                 cancelled);
2870     }
2871 
2872     /**
2873      * Tells the ActivityTaskManager that the keyguard is planning to go away, so that it makes the
2874      * surface behind the keyguard visible and calls {@link #handleStartKeyguardExitAnimation} with
2875      * the parameters needed to animate the surface.
2876      */
showSurfaceBehindKeyguard()2877     public void showSurfaceBehindKeyguard() {
2878         mSurfaceBehindRemoteAnimationRequested = true;
2879 
2880         try {
2881             int flags = KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS
2882                     | KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
2883 
2884             // If we are unlocking to the launcher, clear the snapshot so that any changes as part
2885             // of the in-window animations are reflected. This is needed even if we're not actually
2886             // playing in-window animations for this particular unlock since a previous unlock might
2887             // have changed the Launcher state.
2888             if (KeyguardUnlockAnimationController.Companion.isNexusLauncherUnderneath()) {
2889                 flags |= KEYGUARD_GOING_AWAY_FLAG_TO_LAUNCHER_CLEAR_SNAPSHOT;
2890             }
2891 
2892             ActivityTaskManager.getService().keyguardGoingAway(flags);
2893             mKeyguardStateController.notifyKeyguardGoingAway(true);
2894         } catch (RemoteException e) {
2895             mSurfaceBehindRemoteAnimationRequested = false;
2896             e.printStackTrace();
2897         }
2898     }
2899 
2900     /** Hides the surface behind the keyguard by re-showing the keyguard/activity lock screen. */
hideSurfaceBehindKeyguard()2901     public void hideSurfaceBehindKeyguard() {
2902         mSurfaceBehindRemoteAnimationRequested = false;
2903         mKeyguardStateController.notifyKeyguardGoingAway(false);
2904         if (mShowing) {
2905             setShowingLocked(true, true);
2906         }
2907     }
2908 
2909     /**
2910      * Whether we have requested to show the surface behind the keyguard, even if it's not yet
2911      * visible due to IPC delay.
2912      */
requestedShowSurfaceBehindKeyguard()2913     public boolean requestedShowSurfaceBehindKeyguard() {
2914         return mSurfaceBehindRemoteAnimationRequested;
2915     }
2916 
isAnimatingBetweenKeyguardAndSurfaceBehind()2917     public boolean isAnimatingBetweenKeyguardAndSurfaceBehind() {
2918         return mSurfaceBehindRemoteAnimationRunning;
2919     }
2920 
2921     /**
2922      * If it's running, finishes the RemoteAnimation on the surface behind the keyguard and resets
2923      * related state.
2924      *
2925      * This does not set keyguard state to either locked or unlocked, it simply ends the remote
2926      * animation on the surface behind the keyguard. This can be called by
2927      */
finishSurfaceBehindRemoteAnimation(boolean cancelled)2928     void finishSurfaceBehindRemoteAnimation(boolean cancelled) {
2929         mSurfaceBehindRemoteAnimationRequested = false;
2930         mSurfaceBehindRemoteAnimationRunning = false;
2931         mKeyguardStateController.notifyKeyguardGoingAway(false);
2932 
2933         if (mSurfaceBehindRemoteAnimationFinishedCallback != null) {
2934             try {
2935                 mSurfaceBehindRemoteAnimationFinishedCallback.onAnimationFinished();
2936                 mSurfaceBehindRemoteAnimationFinishedCallback = null;
2937             } catch (RemoteException e) {
2938                 e.printStackTrace();
2939             }
2940         }
2941     }
2942 
adjustStatusBarLocked()2943     private void adjustStatusBarLocked() {
2944         adjustStatusBarLocked(false /* forceHideHomeRecentsButtons */,
2945                 false /* forceClearFlags */);
2946     }
2947 
adjustStatusBarLocked(boolean forceHideHomeRecentsButtons, boolean forceClearFlags)2948     private void adjustStatusBarLocked(boolean forceHideHomeRecentsButtons,
2949             boolean forceClearFlags) {
2950         if (mStatusBarManager == null) {
2951             mStatusBarManager = (StatusBarManager)
2952                     mContext.getSystemService(Context.STATUS_BAR_SERVICE);
2953         }
2954 
2955         if (mStatusBarManager == null) {
2956             Log.w(TAG, "Could not get status bar manager");
2957         } else {
2958             // Disable aspects of the system/status/navigation bars that must not be re-enabled by
2959             // windows that appear on top, ever
2960             int flags = StatusBarManager.DISABLE_NONE;
2961 
2962             // TODO (b/155663717) After restart, status bar will not properly hide home button
2963             //  unless disable is called to show un-hide it once first
2964             if (forceClearFlags) {
2965                 try {
2966                     mStatusBarService.disableForUser(flags, mStatusBarDisableToken,
2967                             mContext.getPackageName(), mUserTracker.getUserId());
2968                 } catch (RemoteException e) {
2969                     Log.d(TAG, "Failed to force clear flags", e);
2970                 }
2971             }
2972 
2973             if (forceHideHomeRecentsButtons || isShowingAndNotOccluded()) {
2974                 if (!mShowHomeOverLockscreen || !mInGestureNavigationMode) {
2975                     flags |= StatusBarManager.DISABLE_HOME;
2976                 }
2977                 flags |= StatusBarManager.DISABLE_RECENT;
2978             }
2979 
2980             if (DEBUG) {
2981                 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mOccluded=" + mOccluded
2982                         + " isSecure=" + isSecure() + " force=" + forceHideHomeRecentsButtons
2983                         +  " --> flags=0x" + Integer.toHexString(flags));
2984             }
2985 
2986             try {
2987                 mStatusBarService.disableForUser(flags, mStatusBarDisableToken,
2988                         mContext.getPackageName(), mUserTracker.getUserId());
2989             } catch (RemoteException e) {
2990                 Log.d(TAG, "Failed to set disable flags: " + flags, e);
2991             }
2992         }
2993     }
2994 
2995     /**
2996      * Handle message sent by {@link #resetStateLocked}
2997      * @see #RESET
2998      */
handleReset()2999     private void handleReset() {
3000         synchronized (KeyguardViewMediator.this) {
3001             if (DEBUG) Log.d(TAG, "handleReset");
3002             mKeyguardViewControllerLazy.get().reset(true /* hideBouncerWhenShowing */);
3003         }
3004 
3005         scheduleNonStrongBiometricIdleTimeout();
3006     }
3007 
3008     /**
3009      * Handle message sent by {@link #verifyUnlock}
3010      * @see #VERIFY_UNLOCK
3011      */
handleVerifyUnlock()3012     private void handleVerifyUnlock() {
3013         Trace.beginSection("KeyguardViewMediator#handleVerifyUnlock");
3014         synchronized (KeyguardViewMediator.this) {
3015             if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
3016             setShowingLocked(true);
3017             mKeyguardViewControllerLazy.get().dismissAndCollapse();
3018         }
3019         Trace.endSection();
3020     }
3021 
handleNotifyStartedGoingToSleep()3022     private void handleNotifyStartedGoingToSleep() {
3023         synchronized (KeyguardViewMediator.this) {
3024             if (DEBUG) Log.d(TAG, "handleNotifyStartedGoingToSleep");
3025             mKeyguardViewControllerLazy.get().onStartedGoingToSleep();
3026         }
3027     }
3028 
3029     /**
3030      * Handle message sent by {@link #notifyFinishedGoingToSleep()}
3031      * @see #NOTIFY_FINISHED_GOING_TO_SLEEP
3032      */
handleNotifyFinishedGoingToSleep()3033     private void handleNotifyFinishedGoingToSleep() {
3034         synchronized (KeyguardViewMediator.this) {
3035             if (DEBUG) Log.d(TAG, "handleNotifyFinishedGoingToSleep");
3036             mKeyguardViewControllerLazy.get().onFinishedGoingToSleep();
3037         }
3038     }
3039 
handleNotifyStartedWakingUp()3040     private void handleNotifyStartedWakingUp() {
3041         Trace.beginSection("KeyguardViewMediator#handleMotifyStartedWakingUp");
3042         synchronized (KeyguardViewMediator.this) {
3043             if (DEBUG) Log.d(TAG, "handleNotifyWakingUp");
3044             mKeyguardViewControllerLazy.get().onStartedWakingUp();
3045         }
3046         Trace.endSection();
3047     }
3048 
resetKeyguardDonePendingLocked()3049     private void resetKeyguardDonePendingLocked() {
3050         mKeyguardDonePending = false;
3051         mHandler.removeMessages(KEYGUARD_DONE_PENDING_TIMEOUT);
3052     }
3053 
3054     @Override
onBootCompleted()3055     public void onBootCompleted() {
3056         synchronized (this) {
3057             if (mContext.getResources().getBoolean(
3058                     com.android.internal.R.bool.config_guestUserAutoCreated)) {
3059                 // TODO(b/191067027): Move post-boot guest creation to system_server
3060                 mUserSwitcherController.schedulePostBootGuestCreation();
3061             }
3062             mBootCompleted = true;
3063             adjustStatusBarLocked(false, true);
3064             if (mBootSendUserPresent) {
3065                 sendUserPresentBroadcast();
3066             }
3067         }
3068     }
3069 
onWakeAndUnlocking()3070     public void onWakeAndUnlocking() {
3071         Trace.beginSection("KeyguardViewMediator#onWakeAndUnlocking");
3072         mWakeAndUnlocking = true;
3073 
3074         keyguardDone();
3075         Trace.endSection();
3076     }
3077 
3078     /**
3079      * Registers the CentralSurfaces to which the Keyguard View is mounted.
3080      *
3081      * @param centralSurfaces
3082      * @param panelView
3083      * @param biometricUnlockController
3084      * @param notificationContainer
3085      * @param bypassController
3086      * @return the View Controller for the Keyguard View this class is mediating.
3087      */
registerCentralSurfaces(CentralSurfaces centralSurfaces, NotificationPanelViewController panelView, @Nullable ShadeExpansionStateManager shadeExpansionStateManager, BiometricUnlockController biometricUnlockController, View notificationContainer, KeyguardBypassController bypassController)3088     public KeyguardViewController registerCentralSurfaces(CentralSurfaces centralSurfaces,
3089             NotificationPanelViewController panelView,
3090             @Nullable ShadeExpansionStateManager shadeExpansionStateManager,
3091             BiometricUnlockController biometricUnlockController,
3092             View notificationContainer, KeyguardBypassController bypassController) {
3093         mCentralSurfaces = centralSurfaces;
3094         mKeyguardViewControllerLazy.get().registerCentralSurfaces(
3095                 centralSurfaces,
3096                 panelView,
3097                 shadeExpansionStateManager,
3098                 biometricUnlockController,
3099                 notificationContainer,
3100                 bypassController);
3101         return mKeyguardViewControllerLazy.get();
3102     }
3103 
3104     /**
3105      * Notifies to System UI that the activity behind has now been drawn, and it's safe to remove
3106      * the wallpaper and keyguard flag, and WindowManager has started running keyguard exit
3107      * animation.
3108      *
3109      * @param startTime the start time of the animation in uptime milliseconds. Deprecated.
3110      * @param fadeoutDuration the duration of the exit animation, in milliseconds Deprecated.
3111      * @deprecated Will be migrate to remote animation soon.
3112      */
3113     @Deprecated
startKeyguardExitAnimation(long startTime, long fadeoutDuration)3114     public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
3115         startKeyguardExitAnimation(0, startTime, fadeoutDuration, null, null, null, null);
3116     }
3117 
3118     /**
3119      * Notifies to System UI that the activity behind has now been drawn, and it's safe to remove
3120      * the wallpaper and keyguard flag, and System UI should start running keyguard exit animation.
3121      *
3122      * @param apps The list of apps to animate.
3123      * @param wallpapers The list of wallpapers to animate.
3124      * @param nonApps The list of non-app windows such as Bubbles to animate.
3125      * @param finishedCallback The callback to invoke when the animation is finished.
3126      */
startKeyguardExitAnimation(@indowManager.TransitionOldType int transit, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)3127     public void startKeyguardExitAnimation(@WindowManager.TransitionOldType int transit,
3128             RemoteAnimationTarget[] apps,
3129             RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
3130             IRemoteAnimationFinishedCallback finishedCallback) {
3131         startKeyguardExitAnimation(transit, 0, 0, apps, wallpapers, nonApps, finishedCallback);
3132     }
3133 
3134     /**
3135      * Notifies to System UI that the activity behind has now been drawn, and it's safe to remove
3136      * the wallpaper and keyguard flag, and start running keyguard exit animation.
3137      *
3138      * @param startTime the start time of the animation in uptime milliseconds. Deprecated.
3139      * @param fadeoutDuration the duration of the exit animation, in milliseconds Deprecated.
3140      * @param apps The list of apps to animate.
3141      * @param wallpapers The list of wallpapers to animate.
3142      * @param nonApps The list of non-app windows such as Bubbles to animate.
3143      * @param finishedCallback The callback to invoke when the animation is finished.
3144      */
startKeyguardExitAnimation(@indowManager.TransitionOldType int transit, long startTime, long fadeoutDuration, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)3145     private void startKeyguardExitAnimation(@WindowManager.TransitionOldType int transit,
3146             long startTime, long fadeoutDuration,
3147             RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers,
3148             RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback) {
3149         Trace.beginSection("KeyguardViewMediator#startKeyguardExitAnimation");
3150         mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_TRANSITION_FROM_AOD);
3151         Message msg = mHandler.obtainMessage(START_KEYGUARD_EXIT_ANIM,
3152                 new StartKeyguardExitAnimParams(transit, startTime, fadeoutDuration, apps,
3153                         wallpapers, nonApps, finishedCallback));
3154         mHandler.sendMessage(msg);
3155         Trace.endSection();
3156     }
3157 
3158     /**
3159      * Cancel the keyguard exit animation, usually because we were swiping to unlock but WM starts
3160      * a new remote animation before finishing the keyguard exit animation.
3161      *
3162      * This will dismiss the keyguard.
3163      */
cancelKeyguardExitAnimation()3164     public void cancelKeyguardExitAnimation() {
3165         Trace.beginSection("KeyguardViewMediator#cancelKeyguardExitAnimation");
3166         Message msg = mHandler.obtainMessage(CANCEL_KEYGUARD_EXIT_ANIM);
3167         mHandler.sendMessage(msg);
3168         Trace.endSection();
3169     }
3170 
onShortPowerPressedGoHome()3171     public void onShortPowerPressedGoHome() {
3172         // do nothing
3173     }
3174 
dismissKeyguardToLaunch(Intent intentToLaunch)3175     public void dismissKeyguardToLaunch(Intent intentToLaunch) {
3176         // do nothing
3177     }
3178 
onSystemKeyPressed(int keycode)3179     public void onSystemKeyPressed(int keycode) {
3180         // do nothing
3181     }
3182 
getViewMediatorCallback()3183     public ViewMediatorCallback getViewMediatorCallback() {
3184         return mViewMediatorCallback;
3185     }
3186 
3187     @Override
dump(PrintWriter pw, String[] args)3188     public void dump(PrintWriter pw, String[] args) {
3189         pw.print("  mSystemReady: "); pw.println(mSystemReady);
3190         pw.print("  mBootCompleted: "); pw.println(mBootCompleted);
3191         pw.print("  mBootSendUserPresent: "); pw.println(mBootSendUserPresent);
3192         pw.print("  mExternallyEnabled: "); pw.println(mExternallyEnabled);
3193         pw.print("  mShuttingDown: "); pw.println(mShuttingDown);
3194         pw.print("  mNeedToReshowWhenReenabled: "); pw.println(mNeedToReshowWhenReenabled);
3195         pw.print("  mShowing: "); pw.println(mShowing);
3196         pw.print("  mInputRestricted: "); pw.println(mInputRestricted);
3197         pw.print("  mOccluded: "); pw.println(mOccluded);
3198         pw.print("  mDelayedShowingSequence: "); pw.println(mDelayedShowingSequence);
3199         pw.print("  mDeviceInteractive: "); pw.println(mDeviceInteractive);
3200         pw.print("  mGoingToSleep: "); pw.println(mGoingToSleep);
3201         pw.print("  mHiding: "); pw.println(mHiding);
3202         pw.print("  mDozing: "); pw.println(mDozing);
3203         pw.print("  mAodShowing: "); pw.println(mAodShowing);
3204         pw.print("  mWaitingUntilKeyguardVisible: "); pw.println(mWaitingUntilKeyguardVisible);
3205         pw.print("  mKeyguardDonePending: "); pw.println(mKeyguardDonePending);
3206         pw.print("  mHideAnimationRun: "); pw.println(mHideAnimationRun);
3207         pw.print("  mPendingReset: "); pw.println(mPendingReset);
3208         pw.print("  mPendingLock: "); pw.println(mPendingLock);
3209         pw.print("  wakeAndUnlocking: "); pw.println(mWakeAndUnlocking);
3210         pw.print("  mPendingPinLock: "); pw.println(mPendingPinLock);
3211     }
3212 
3213     /**
3214      * @param dozing true when AOD - or ambient mode - is showing.
3215      */
setDozing(boolean dozing)3216     public void setDozing(boolean dozing) {
3217         if (dozing == mDozing) {
3218             return;
3219         }
3220         mDozing = dozing;
3221         if (!dozing) {
3222             mAnimatingScreenOff = false;
3223         }
3224 
3225         // Don't hide the keyguard due to a doze change if there's a lock pending, because we're
3226         // just going to show it again.
3227         // If the device is not capable of controlling the screen off animation, SysUI needs to
3228         // update lock screen state in ATMS here, otherwise ATMS tries to resume activities when
3229         // enabling doze state.
3230         if (mShowing || !mPendingLock || !mDozeParameters.canControlUnlockedScreenOff()) {
3231             setShowingLocked(mShowing);
3232         }
3233     }
3234 
3235     @Override
onDozeAmountChanged(float linear, float interpolated)3236     public void onDozeAmountChanged(float linear, float interpolated) {
3237         // If we were animating the screen off, and we've completed the doze animation (doze amount
3238         // is 1f), then show the activity lock screen.
3239         if (mAnimatingScreenOff && mDozing && linear == 1f) {
3240             mAnimatingScreenOff = false;
3241             setShowingLocked(mShowing, true);
3242         }
3243     }
3244 
3245     /**
3246      * Set if the wallpaper supports ambient mode. This is used to trigger the right animation.
3247      * In case it does support it, we have to fade in the incoming app, otherwise we'll reveal it
3248      * with the light reveal scrim.
3249      */
setWallpaperSupportsAmbientMode(boolean supportsAmbientMode)3250     public void setWallpaperSupportsAmbientMode(boolean supportsAmbientMode) {
3251         mWallpaperSupportsAmbientMode = supportsAmbientMode;
3252     }
3253 
3254     private static class StartKeyguardExitAnimParams {
3255 
3256         @WindowManager.TransitionOldType int mTransit;
3257         long startTime;
3258         long fadeoutDuration;
3259         RemoteAnimationTarget[] mApps;
3260         RemoteAnimationTarget[] mWallpapers;
3261         RemoteAnimationTarget[] mNonApps;
3262         IRemoteAnimationFinishedCallback mFinishedCallback;
3263 
StartKeyguardExitAnimParams(@indowManager.TransitionOldType int transit, long startTime, long fadeoutDuration, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)3264         private StartKeyguardExitAnimParams(@WindowManager.TransitionOldType int transit,
3265                 long startTime, long fadeoutDuration,
3266                 RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers,
3267                 RemoteAnimationTarget[] nonApps,
3268                 IRemoteAnimationFinishedCallback finishedCallback) {
3269             this.mTransit = transit;
3270             this.startTime = startTime;
3271             this.fadeoutDuration = fadeoutDuration;
3272             this.mApps = apps;
3273             this.mWallpapers = wallpapers;
3274             this.mNonApps = nonApps;
3275             this.mFinishedCallback = finishedCallback;
3276         }
3277     }
3278 
setShowingLocked(boolean showing)3279     void setShowingLocked(boolean showing) {
3280         setShowingLocked(showing, false /* forceCallbacks */);
3281     }
3282 
setShowingLocked(boolean showing, boolean forceCallbacks)3283     private void setShowingLocked(boolean showing, boolean forceCallbacks) {
3284         final boolean aodShowing = mDozing && !mWakeAndUnlocking;
3285         final boolean notifyDefaultDisplayCallbacks = showing != mShowing || forceCallbacks;
3286         final boolean updateActivityLockScreenState = showing != mShowing
3287                 || aodShowing != mAodShowing || forceCallbacks;
3288         mShowing = showing;
3289         mAodShowing = aodShowing;
3290         if (notifyDefaultDisplayCallbacks) {
3291             notifyDefaultDisplayCallbacks(showing);
3292         }
3293         if (updateActivityLockScreenState) {
3294             updateActivityLockScreenState(showing, aodShowing);
3295         }
3296 
3297     }
3298 
notifyDefaultDisplayCallbacks(boolean showing)3299     private void notifyDefaultDisplayCallbacks(boolean showing) {
3300         // TODO(b/140053364)
3301         whitelistIpcs(() -> {
3302             int size = mKeyguardStateCallbacks.size();
3303             for (int i = size - 1; i >= 0; i--) {
3304                 IKeyguardStateCallback callback = mKeyguardStateCallbacks.get(i);
3305                 try {
3306                     callback.onShowingStateChanged(showing, KeyguardUpdateMonitor.getCurrentUser());
3307                 } catch (RemoteException e) {
3308                     Slog.w(TAG, "Failed to call onShowingStateChanged", e);
3309                     if (e instanceof DeadObjectException) {
3310                         mKeyguardStateCallbacks.remove(callback);
3311                     }
3312                 }
3313             }
3314         });
3315         updateInputRestrictedLocked();
3316         mUiBgExecutor.execute(() -> {
3317             mTrustManager.reportKeyguardShowingChanged();
3318         });
3319     }
3320 
notifyTrustedChangedLocked(boolean trusted)3321     private void notifyTrustedChangedLocked(boolean trusted) {
3322         int size = mKeyguardStateCallbacks.size();
3323         for (int i = size - 1; i >= 0; i--) {
3324             try {
3325                 mKeyguardStateCallbacks.get(i).onTrustedChanged(trusted);
3326             } catch (RemoteException e) {
3327                 Slog.w(TAG, "Failed to call notifyTrustedChangedLocked", e);
3328                 if (e instanceof DeadObjectException) {
3329                     mKeyguardStateCallbacks.remove(i);
3330                 }
3331             }
3332         }
3333     }
3334 
setPendingLock(boolean hasPendingLock)3335     private void setPendingLock(boolean hasPendingLock) {
3336         mPendingLock = hasPendingLock;
3337         Trace.traceCounter(Trace.TRACE_TAG_APP, "pendingLock", mPendingLock ? 1 : 0);
3338     }
3339 
addStateMonitorCallback(IKeyguardStateCallback callback)3340     public void addStateMonitorCallback(IKeyguardStateCallback callback) {
3341         synchronized (this) {
3342             mKeyguardStateCallbacks.add(callback);
3343             try {
3344                 callback.onSimSecureStateChanged(mUpdateMonitor.isSimPinSecure());
3345                 callback.onShowingStateChanged(mShowing, KeyguardUpdateMonitor.getCurrentUser());
3346                 callback.onInputRestrictedStateChanged(mInputRestricted);
3347                 callback.onTrustedChanged(mUpdateMonitor.getUserHasTrust(
3348                         KeyguardUpdateMonitor.getCurrentUser()));
3349             } catch (RemoteException e) {
3350                 Slog.w(TAG, "Failed to call to IKeyguardStateCallback", e);
3351             }
3352         }
3353     }
3354 
3355     private static class DismissMessage {
3356         private final CharSequence mMessage;
3357         private final IKeyguardDismissCallback mCallback;
3358 
DismissMessage(IKeyguardDismissCallback callback, CharSequence message)3359         DismissMessage(IKeyguardDismissCallback callback, CharSequence message) {
3360             mCallback = callback;
3361             mMessage = message;
3362         }
3363 
getCallback()3364         public IKeyguardDismissCallback getCallback() {
3365             return mCallback;
3366         }
3367 
getMessage()3368         public CharSequence getMessage() {
3369             return mMessage;
3370         }
3371     }
3372 
3373     /**
3374      * Implementation of RemoteAnimationRunner that creates a new
3375      * {@link ActivityLaunchAnimator.Runner} whenever onAnimationStart is called, delegating the
3376      * remote animation methods to that runner.
3377      */
3378     private class ActivityLaunchRemoteAnimationRunner extends IRemoteAnimationRunner.Stub {
3379 
3380         private final ActivityLaunchAnimator.Controller mActivityLaunchController;
3381         @Nullable private ActivityLaunchAnimator.Runner mRunner;
3382 
ActivityLaunchRemoteAnimationRunner(ActivityLaunchAnimator.Controller controller)3383         ActivityLaunchRemoteAnimationRunner(ActivityLaunchAnimator.Controller controller) {
3384             mActivityLaunchController = controller;
3385         }
3386 
3387         @Override
onAnimationCancelled(boolean isKeyguardOccluded)3388         public void onAnimationCancelled(boolean isKeyguardOccluded) throws RemoteException {
3389             if (mRunner != null) {
3390                 mRunner.onAnimationCancelled(isKeyguardOccluded);
3391             }
3392         }
3393 
3394         @Override
onAnimationStart(int transit, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)3395         public void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
3396                 RemoteAnimationTarget[] wallpapers,
3397                 RemoteAnimationTarget[] nonApps,
3398                 IRemoteAnimationFinishedCallback finishedCallback)
3399                 throws RemoteException {
3400             mRunner = mActivityLaunchAnimator.get().createRunner(mActivityLaunchController);
3401             mRunner.onAnimationStart(transit, apps, wallpapers, nonApps, finishedCallback);
3402         }
3403     }
3404 
3405     /**
3406      * Subclass of {@link ActivityLaunchRemoteAnimationRunner} that calls {@link #setOccluded} when
3407      * onAnimationStart is called.
3408      */
3409     private class OccludeActivityLaunchRemoteAnimationRunner
3410             extends ActivityLaunchRemoteAnimationRunner {
3411 
OccludeActivityLaunchRemoteAnimationRunner( ActivityLaunchAnimator.Controller controller)3412         OccludeActivityLaunchRemoteAnimationRunner(
3413                 ActivityLaunchAnimator.Controller controller) {
3414             super(controller);
3415         }
3416 
3417         @Override
onAnimationStart(int transit, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)3418         public void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
3419                 RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
3420                 IRemoteAnimationFinishedCallback finishedCallback) throws RemoteException {
3421             super.onAnimationStart(transit, apps, wallpapers, nonApps, finishedCallback);
3422 
3423             mInteractionJankMonitor.begin(
3424                     createInteractionJankMonitorConf(CUJ_LOCKSCREEN_OCCLUSION)
3425                             .setTag("OCCLUDE"));
3426 
3427             // This is the first signal we have from WM that we're going to be occluded. Set our
3428             // internal state to reflect that immediately, vs. waiting for the launch animator to
3429             // begin. Otherwise, calls to setShowingLocked, etc. will not know that we're about to
3430             // be occluded and might re-show the keyguard.
3431             Log.d(TAG, "OccludeAnimator#onAnimationStart. Set occluded = true.");
3432             setOccluded(true /* isOccluded */, false /* animate */);
3433         }
3434 
3435         @Override
onAnimationCancelled(boolean isKeyguardOccluded)3436         public void onAnimationCancelled(boolean isKeyguardOccluded) throws RemoteException {
3437             super.onAnimationCancelled(isKeyguardOccluded);
3438 
3439             Log.d(TAG, "Occlude animation cancelled by WM.");
3440             mInteractionJankMonitor.cancel(CUJ_LOCKSCREEN_OCCLUSION);
3441         }
3442     }
3443 }
3444