2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.statusbar.phone;
19 import static com.android.keyguard.KeyguardSecurityModel.SecurityMode;
20 import static com.android.systemui.plugins.ActivityStarter.OnDismissAction;
22 import android.content.Context;
23 import android.content.res.ColorStateList;
24 import android.os.Handler;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.util.Log;
28 import android.util.MathUtils;
29 import android.util.Slog;
30 import android.util.StatsLog;
31 import android.view.KeyEvent;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.ViewTreeObserver;
36 import android.view.WindowInsets;
38 import com.android.internal.widget.LockPatternUtils;
39 import com.android.keyguard.KeyguardHostView;
40 import com.android.keyguard.KeyguardSecurityView;
41 import com.android.keyguard.KeyguardUpdateMonitor;
42 import com.android.keyguard.KeyguardUpdateMonitorCallback;
43 import com.android.keyguard.R;
44 import com.android.keyguard.ViewMediatorCallback;
45 import com.android.systemui.DejankUtils;
46 import com.android.systemui.keyguard.DismissCallbackRegistry;
47 import com.android.systemui.plugins.FalsingManager;
49 import java.io.PrintWriter;
52 * A class which manages the bouncer on the lockscreen.
54 public class KeyguardBouncer {
56 private static final String TAG = "KeyguardBouncer";
57 static final long BOUNCER_FACE_DELAY = 1200;
58 static final float ALPHA_EXPANSION_THRESHOLD = 0.95f;
59 static final float EXPANSION_HIDDEN = 1f;
60 static final float EXPANSION_VISIBLE = 0f;
62 protected final Context mContext;
63 protected final ViewMediatorCallback mCallback;
64 protected final LockPatternUtils mLockPatternUtils;
65 protected final ViewGroup mContainer;
66 private final FalsingManager mFalsingManager;
67 private final DismissCallbackRegistry mDismissCallbackRegistry;
68 private final Handler mHandler;
69 private final BouncerExpansionCallback mExpansionCallback;
70 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
71 private final UnlockMethodCache mUnlockMethodCache;
72 private final KeyguardUpdateMonitorCallback mUpdateMonitorCallback =
73 new KeyguardUpdateMonitorCallback() {
75 public void onStrongAuthStateChanged(int userId) {
76 mBouncerPromptReason = mCallback.getBouncerPromptReason();
79 private final Runnable mRemoveViewRunnable = this::removeView;
80 private final KeyguardBypassController mKeyguardBypassController;
81 protected KeyguardHostView mKeyguardView;
82 private final Runnable mResetRunnable = ()-> {
83 if (mKeyguardView != null) {
84 mKeyguardView.resetSecurityContainer();
88 private int mStatusBarHeight;
89 private float mExpansion = EXPANSION_HIDDEN;
90 protected ViewGroup mRoot;
91 private boolean mShowingSoon;
92 private int mBouncerPromptReason;
93 private boolean mIsAnimatingAway;
94 private boolean mIsScrimmed;
95 private ViewGroup mLockIconContainer;
97 public KeyguardBouncer(Context context, ViewMediatorCallback callback,
98 LockPatternUtils lockPatternUtils, ViewGroup container,
99 DismissCallbackRegistry dismissCallbackRegistry, FalsingManager falsingManager,
100 BouncerExpansionCallback expansionCallback, UnlockMethodCache unlockMethodCache,
101 KeyguardUpdateMonitor keyguardUpdateMonitor,
102 KeyguardBypassController keyguardBypassController, Handler handler) {
104 mCallback = callback;
105 mLockPatternUtils = lockPatternUtils;
106 mContainer = container;
107 mKeyguardUpdateMonitor = keyguardUpdateMonitor;
108 mFalsingManager = falsingManager;
109 mDismissCallbackRegistry = dismissCallbackRegistry;
110 mExpansionCallback = expansionCallback;
112 mUnlockMethodCache = unlockMethodCache;
113 mKeyguardUpdateMonitor.registerCallback(mUpdateMonitorCallback);
114 mKeyguardBypassController = keyguardBypassController;
117 public void show(boolean resetSecuritySelection) {
118 show(resetSecuritySelection, true /* scrimmed */);
124 * @param resetSecuritySelection Cleans keyguard view
125 * @param isScrimmed true when the bouncer show show scrimmed, false when the user will be
126 * dragging it and translation should be deferred.
128 public void show(boolean resetSecuritySelection, boolean isScrimmed) {
129 final int keyguardUserId = KeyguardUpdateMonitor.getCurrentUser();
130 if (keyguardUserId == UserHandle.USER_SYSTEM && UserManager.isSplitSystemUser()) {
131 // In split system user mode, we never unlock system user.
135 mIsScrimmed = isScrimmed;
137 // On the keyguard, we want to show the bouncer when the user drags up, but it's
138 // not correct to end the falsing session. We still need to verify if those touches
140 // Later, at the end of the animation, when the bouncer is at the top of the screen,
141 // onFullyShown() will be called and FalsingManager will stop recording touches.
143 setExpansion(EXPANSION_VISIBLE);
146 if (resetSecuritySelection) {
147 // showPrimarySecurityScreen() updates the current security method. This is needed in
148 // case we are already showing and the current security method changed.
149 showPrimarySecurityScreen();
152 if (mRoot.getVisibility() == View.VISIBLE || mShowingSoon) {
156 final int activeUserId = KeyguardUpdateMonitor.getCurrentUser();
157 final boolean isSystemUser =
158 UserManager.isSplitSystemUser() && activeUserId == UserHandle.USER_SYSTEM;
159 final boolean allowDismissKeyguard = !isSystemUser && activeUserId == keyguardUserId;
161 // If allowed, try to dismiss the Keyguard. If no security auth (password/pin/pattern) is
162 // set, this will dismiss the whole Keyguard. Otherwise, show the bouncer.
163 if (allowDismissKeyguard && mKeyguardView.dismiss(activeUserId)) {
167 // This condition may indicate an error on Android, so log it.
168 if (!allowDismissKeyguard) {
169 Slog.w(TAG, "User can't dismiss keyguard: " + activeUserId + " != " + keyguardUserId);
174 // Split up the work over multiple frames.
175 DejankUtils.removeCallbacks(mResetRunnable);
176 if (mUnlockMethodCache.isFaceAuthEnabled() && !needsFullscreenBouncer()
177 && !mKeyguardUpdateMonitor.userNeedsStrongAuth()
178 && !mKeyguardBypassController.getBypassEnabled()) {
179 mHandler.postDelayed(mShowRunnable, BOUNCER_FACE_DELAY);
181 DejankUtils.postAfterTraversal(mShowRunnable);
184 mCallback.onBouncerVisiblityChanged(true /* shown */);
185 mExpansionCallback.onStartingToShow();
188 public boolean isScrimmed() {
192 public ViewGroup getLockIconContainer() {
193 return mRoot == null || mRoot.getVisibility() != View.VISIBLE ? null : mLockIconContainer;
197 * This method must be called at the end of the bouncer animation when
198 * the translation is performed manually by the user, otherwise FalsingManager
199 * will never be notified and its internal state will be out of sync.
201 private void onFullyShown() {
202 mFalsingManager.onBouncerShown();
203 if (mKeyguardView == null) {
204 Log.wtf(TAG, "onFullyShown when view was null");
206 mKeyguardView.onResume();
211 * @see #onFullyShown()
213 private void onFullyHidden() {
214 cancelShowRunnable();
216 mRoot.setVisibility(View.INVISIBLE);
218 mFalsingManager.onBouncerHidden();
219 DejankUtils.postAfterTraversal(mResetRunnable);
222 private final Runnable mShowRunnable = new Runnable() {
225 mRoot.setVisibility(View.VISIBLE);
226 showPromptReason(mBouncerPromptReason);
227 final CharSequence customMessage = mCallback.consumeCustomMessage();
228 if (customMessage != null) {
229 mKeyguardView.showErrorMessage(customMessage);
231 // We might still be collapsed and the view didn't have time to layout yet or still
232 // be small, let's wait on the predraw to do the animation in that case.
233 if (mKeyguardView.getHeight() != 0 && mKeyguardView.getHeight() != mStatusBarHeight) {
234 mKeyguardView.startAppearAnimation();
236 mKeyguardView.getViewTreeObserver().addOnPreDrawListener(
237 new ViewTreeObserver.OnPreDrawListener() {
239 public boolean onPreDraw() {
240 mKeyguardView.getViewTreeObserver().removeOnPreDrawListener(this);
241 mKeyguardView.startAppearAnimation();
245 mKeyguardView.requestLayout();
247 mShowingSoon = false;
248 if (mExpansion == EXPANSION_VISIBLE) {
249 mKeyguardView.onResume();
250 mKeyguardView.resetSecurityContainer();
252 StatsLog.write(StatsLog.KEYGUARD_BOUNCER_STATE_CHANGED,
253 StatsLog.KEYGUARD_BOUNCER_STATE_CHANGED__STATE__SHOWN);
258 * Show a string explaining why the security view needs to be solved.
260 * @param reason a flag indicating which string should be shown, see
261 * {@link KeyguardSecurityView#PROMPT_REASON_NONE}
262 * and {@link KeyguardSecurityView#PROMPT_REASON_RESTART}
264 public void showPromptReason(int reason) {
265 if (mKeyguardView != null) {
266 mKeyguardView.showPromptReason(reason);
268 Log.w(TAG, "Trying to show prompt reason on empty bouncer");
272 public void showMessage(String message, ColorStateList colorState) {
273 if (mKeyguardView != null) {
274 mKeyguardView.showMessage(message, colorState);
276 Log.w(TAG, "Trying to show message on empty bouncer");
280 private void cancelShowRunnable() {
281 DejankUtils.removeCallbacks(mShowRunnable);
282 mHandler.removeCallbacks(mShowRunnable);
283 mShowingSoon = false;
286 public void showWithDismissAction(OnDismissAction r, Runnable cancelAction) {
288 mKeyguardView.setOnDismissAction(r, cancelAction);
289 show(false /* resetSecuritySelection */);
292 public void hide(boolean destroyView) {
294 StatsLog.write(StatsLog.KEYGUARD_BOUNCER_STATE_CHANGED,
295 StatsLog.KEYGUARD_BOUNCER_STATE_CHANGED__STATE__HIDDEN);
296 mDismissCallbackRegistry.notifyDismissCancelled();
299 mFalsingManager.onBouncerHidden();
300 mCallback.onBouncerVisiblityChanged(false /* shown */);
301 cancelShowRunnable();
302 if (mKeyguardView != null) {
303 mKeyguardView.cancelDismissAction();
304 mKeyguardView.cleanUp();
306 mIsAnimatingAway = false;
308 mRoot.setVisibility(View.INVISIBLE);
311 // We have a ViewFlipper that unregisters a broadcast when being detached, which may
312 // be slow because of AM lock contention during unlocking. We can delay it a bit.
313 mHandler.postDelayed(mRemoveViewRunnable, 50);
319 * See {@link StatusBarKeyguardViewManager#startPreHideAnimation}.
321 public void startPreHideAnimation(Runnable runnable) {
322 mIsAnimatingAway = true;
323 if (mKeyguardView != null) {
324 mKeyguardView.startDisappearAnimation(runnable);
325 } else if (runnable != null) {
331 * Reset the state of the view.
333 public void reset() {
334 cancelShowRunnable();
336 mFalsingManager.onBouncerHidden();
339 public void onScreenTurnedOff() {
340 if (mKeyguardView != null && mRoot != null && mRoot.getVisibility() == View.VISIBLE) {
341 mKeyguardView.onPause();
345 public boolean isShowing() {
346 return (mShowingSoon || (mRoot != null && mRoot.getVisibility() == View.VISIBLE))
347 && mExpansion == EXPANSION_VISIBLE && !isAnimatingAway();
351 * {@link #show(boolean)} was called but we're not showing yet, or being dragged.
353 public boolean inTransit() {
354 return mShowingSoon || mExpansion != EXPANSION_HIDDEN && mExpansion != EXPANSION_VISIBLE;
358 * @return {@code true} when bouncer's pre-hide animation already started but isn't completely
359 * hidden yet, {@code false} otherwise.
361 public boolean isAnimatingAway() {
362 return mIsAnimatingAway;
365 public void prepare() {
366 boolean wasInitialized = mRoot != null;
368 if (wasInitialized) {
369 showPrimarySecurityScreen();
371 mBouncerPromptReason = mCallback.getBouncerPromptReason();
374 private void showPrimarySecurityScreen() {
375 mKeyguardView.showPrimarySecurityScreen();
376 KeyguardSecurityView keyguardSecurityView = mKeyguardView.getCurrentSecurityView();
377 if (keyguardSecurityView != null) {
378 mLockIconContainer = ((ViewGroup) keyguardSecurityView)
379 .findViewById(R.id.lock_icon_container);
384 * Current notification panel expansion
385 * @param fraction 0 when notification panel is collapsed and 1 when expanded.
386 * @see StatusBarKeyguardViewManager#onPanelExpansionChanged
388 public void setExpansion(float fraction) {
389 float oldExpansion = mExpansion;
390 mExpansion = fraction;
391 if (mKeyguardView != null && !mIsAnimatingAway) {
392 float alpha = MathUtils.map(ALPHA_EXPANSION_THRESHOLD, 1, 1, 0, fraction);
393 mKeyguardView.setAlpha(MathUtils.constrain(alpha, 0f, 1f));
394 mKeyguardView.setTranslationY(fraction * mKeyguardView.getHeight());
397 if (fraction == EXPANSION_VISIBLE && oldExpansion != EXPANSION_VISIBLE) {
399 mExpansionCallback.onFullyShown();
400 } else if (fraction == EXPANSION_HIDDEN && oldExpansion != EXPANSION_HIDDEN) {
402 mExpansionCallback.onFullyHidden();
403 } else if (fraction != EXPANSION_VISIBLE && oldExpansion == EXPANSION_VISIBLE) {
404 mExpansionCallback.onStartingToHide();
408 public boolean willDismissWithAction() {
409 return mKeyguardView != null && mKeyguardView.hasDismissActions();
412 public int getTop() {
413 if (mKeyguardView == null) {
417 int top = mKeyguardView.getTop();
418 // The password view has an extra top padding that should be ignored.
419 if (mKeyguardView.getCurrentSecurityMode() == SecurityMode.Password) {
420 View messageArea = mKeyguardView.findViewById(R.id.keyguard_message_area);
421 top += messageArea.getTop();
426 protected void ensureView() {
427 // Removal of the view might be deferred to reduce unlock latency,
428 // in this case we need to force the removal, otherwise we'll
429 // end up in an unpredictable state.
430 boolean forceRemoval = mHandler.hasCallbacks(mRemoveViewRunnable);
431 if (mRoot == null || forceRemoval) {
436 protected void inflateView() {
438 mHandler.removeCallbacks(mRemoveViewRunnable);
439 mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
440 mKeyguardView = mRoot.findViewById(R.id.keyguard_host_view);
441 mKeyguardView.setLockPatternUtils(mLockPatternUtils);
442 mKeyguardView.setViewMediatorCallback(mCallback);
443 mContainer.addView(mRoot, mContainer.getChildCount());
444 mStatusBarHeight = mRoot.getResources().getDimensionPixelOffset(
445 com.android.systemui.R.dimen.status_bar_height);
446 mRoot.setVisibility(View.INVISIBLE);
447 mRoot.setAccessibilityPaneTitle(mKeyguardView.getAccessibilityTitleForCurrentMode());
449 final WindowInsets rootInsets = mRoot.getRootWindowInsets();
450 if (rootInsets != null) {
451 mRoot.dispatchApplyWindowInsets(rootInsets);
455 protected void removeView() {
456 if (mRoot != null && mRoot.getParent() == mContainer) {
457 mContainer.removeView(mRoot);
462 public boolean onBackPressed() {
463 return mKeyguardView != null && mKeyguardView.handleBackKey();
467 * @return True if and only if the security method should be shown before showing the
468 * notifications on Keyguard, like SIM PIN/PUK.
470 public boolean needsFullscreenBouncer() {
472 if (mKeyguardView != null) {
473 SecurityMode mode = mKeyguardView.getSecurityMode();
474 return mode == SecurityMode.SimPin || mode == SecurityMode.SimPuk;
480 * Like {@link #needsFullscreenBouncer}, but uses the currently visible security method, which
481 * makes this method much faster.
483 public boolean isFullscreenBouncer() {
484 if (mKeyguardView != null) {
485 SecurityMode mode = mKeyguardView.getCurrentSecurityMode();
486 return mode == SecurityMode.SimPin || mode == SecurityMode.SimPuk;
492 * WARNING: This method might cause Binder calls.
494 public boolean isSecure() {
495 return mKeyguardView == null || mKeyguardView.getSecurityMode() != SecurityMode.None;
498 public boolean shouldDismissOnMenuPressed() {
499 return mKeyguardView.shouldEnableMenuKey();
502 public boolean interceptMediaKey(KeyEvent event) {
504 return mKeyguardView.interceptMediaKey(event);
507 public void notifyKeyguardAuthenticated(boolean strongAuth) {
509 mKeyguardView.finish(strongAuth, KeyguardUpdateMonitor.getCurrentUser());
512 public void dump(PrintWriter pw) {
513 pw.println("KeyguardBouncer");
514 pw.println(" isShowing(): " + isShowing());
515 pw.println(" mStatusBarHeight: " + mStatusBarHeight);
516 pw.println(" mExpansion: " + mExpansion);
517 pw.println(" mKeyguardView; " + mKeyguardView);
518 pw.println(" mShowingSoon: " + mKeyguardView);
519 pw.println(" mBouncerPromptReason: " + mBouncerPromptReason);
520 pw.println(" mIsAnimatingAway: " + mIsAnimatingAway);
523 public interface BouncerExpansionCallback {
525 void onStartingToHide();
526 void onStartingToShow();
527 void onFullyHidden();