1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.statusbar.phone; 18 19 import static com.android.systemui.statusbar.notification.NotificationUtils.isHapticFeedbackDisabled; 20 21 import android.animation.Animator; 22 import android.animation.AnimatorListenerAdapter; 23 import android.animation.ObjectAnimator; 24 import android.animation.ValueAnimator; 25 import android.content.Context; 26 import android.content.res.Configuration; 27 import android.content.res.Resources; 28 import android.os.AsyncTask; 29 import android.os.SystemClock; 30 import android.os.UserHandle; 31 import android.os.VibrationEffect; 32 import android.os.Vibrator; 33 import android.provider.Settings; 34 import android.util.AttributeSet; 35 import android.util.Log; 36 import android.view.InputDevice; 37 import android.view.MotionEvent; 38 import android.view.View; 39 import android.view.ViewConfiguration; 40 import android.view.ViewTreeObserver; 41 import android.view.animation.Interpolator; 42 import android.widget.FrameLayout; 43 44 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 45 import com.android.keyguard.LatencyTracker; 46 import com.android.systemui.DejankUtils; 47 import com.android.systemui.Interpolators; 48 import com.android.systemui.R; 49 import com.android.systemui.classifier.FalsingManager; 50 import com.android.systemui.doze.DozeLog; 51 import com.android.systemui.statusbar.FlingAnimationUtils; 52 import com.android.systemui.statusbar.StatusBarState; 53 import com.android.systemui.statusbar.notification.NotificationUtils; 54 import com.android.systemui.statusbar.policy.HeadsUpManager; 55 56 import java.io.FileDescriptor; 57 import java.io.PrintWriter; 58 59 public abstract class PanelView extends FrameLayout { 60 public static final boolean DEBUG = PanelBar.DEBUG; 61 public static final String TAG = PanelView.class.getSimpleName(); 62 private static final int INITIAL_OPENING_PEEK_DURATION = 200; 63 private static final int PEEK_ANIMATION_DURATION = 360; 64 private long mDownTime; 65 private float mMinExpandHeight; 66 private LockscreenGestureLogger mLockscreenGestureLogger = new LockscreenGestureLogger(); 67 private boolean mPanelUpdateWhenAnimatorEnds; 68 private boolean mVibrateOnOpening; 69 70 private final void logf(String fmt, Object... args) { 71 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); 72 } 73 74 protected StatusBar mStatusBar; 75 protected HeadsUpManager mHeadsUpManager; 76 77 private float mPeekHeight; 78 private float mHintDistance; 79 private float mInitialOffsetOnTouch; 80 private boolean mCollapsedAndHeadsUpOnDown; 81 private float mExpandedFraction = 0; 82 protected float mExpandedHeight = 0; 83 private boolean mPanelClosedOnDown; 84 private boolean mHasLayoutedSinceDown; 85 private float mUpdateFlingVelocity; 86 private boolean mUpdateFlingOnLayout; 87 private boolean mPeekTouching; 88 private boolean mJustPeeked; 89 private boolean mClosing; 90 protected boolean mTracking; 91 private boolean mTouchSlopExceeded; 92 private int mTrackingPointer; 93 protected int mTouchSlop; 94 protected boolean mHintAnimationRunning; 95 private boolean mOverExpandedBeforeFling; 96 private boolean mTouchAboveFalsingThreshold; 97 private int mUnlockFalsingThreshold; 98 private boolean mTouchStartedInEmptyArea; 99 private boolean mMotionAborted; 100 private boolean mUpwardsWhenTresholdReached; 101 private boolean mAnimatingOnDown; 102 103 private ValueAnimator mHeightAnimator; 104 private ObjectAnimator mPeekAnimator; 105 private VelocityTrackerInterface mVelocityTracker; 106 private FlingAnimationUtils mFlingAnimationUtils; 107 private FlingAnimationUtils mFlingAnimationUtilsClosing; 108 private FlingAnimationUtils mFlingAnimationUtilsDismissing; 109 private FalsingManager mFalsingManager; 110 private final Vibrator mVibrator; 111 112 /** 113 * Whether an instant expand request is currently pending and we are just waiting for layout. 114 */ 115 private boolean mInstantExpanding; 116 private boolean mAnimateAfterExpanding; 117 118 PanelBar mBar; 119 120 private String mViewName; 121 private float mInitialTouchY; 122 private float mInitialTouchX; 123 private boolean mTouchDisabled; 124 125 /** 126 * Whether or not the PanelView can be expanded or collapsed with a drag. 127 */ 128 private boolean mNotificationsDragEnabled; 129 130 private Interpolator mBounceInterpolator; 131 protected KeyguardBottomAreaView mKeyguardBottomArea; 132 133 /** 134 * Speed-up factor to be used when {@link #mFlingCollapseRunnable} runs the next time. 135 */ 136 private float mNextCollapseSpeedUpFactor = 1.0f; 137 138 protected boolean mExpanding; 139 private boolean mGestureWaitForTouchSlop; 140 private boolean mIgnoreXTouchSlop; 141 private boolean mExpandLatencyTracking; 142 143 protected void onExpandingFinished() { 144 mBar.onExpandingFinished(); 145 } 146 147 protected void onExpandingStarted() { 148 } 149 150 private void notifyExpandingStarted() { 151 if (!mExpanding) { 152 mExpanding = true; 153 onExpandingStarted(); 154 } 155 } 156 157 protected final void notifyExpandingFinished() { 158 endClosing(); 159 if (mExpanding) { 160 mExpanding = false; 161 onExpandingFinished(); 162 } 163 } 164 165 private void runPeekAnimation(long duration, float peekHeight, boolean collapseWhenFinished) { 166 mPeekHeight = peekHeight; 167 if (DEBUG) logf("peek to height=%.1f", mPeekHeight); 168 if (mHeightAnimator != null) { 169 return; 170 } 171 if (mPeekAnimator != null) { 172 mPeekAnimator.cancel(); 173 } 174 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight) 175 .setDuration(duration); 176 mPeekAnimator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); 177 mPeekAnimator.addListener(new AnimatorListenerAdapter() { 178 private boolean mCancelled; 179 180 @Override 181 public void onAnimationCancel(Animator animation) { 182 mCancelled = true; 183 } 184 185 @Override 186 public void onAnimationEnd(Animator animation) { 187 mPeekAnimator = null; 188 if (!mCancelled && collapseWhenFinished) { 189 postOnAnimation(mPostCollapseRunnable); 190 } 191 192 } 193 }); 194 notifyExpandingStarted(); 195 mPeekAnimator.start(); 196 mJustPeeked = true; 197 } 198 199 public PanelView(Context context, AttributeSet attrs) { 200 super(context, attrs); 201 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f /* maxLengthSeconds */, 202 0.6f /* speedUpFactor */); 203 mFlingAnimationUtilsClosing = new FlingAnimationUtils(context, 0.5f /* maxLengthSeconds */, 204 0.6f /* speedUpFactor */); 205 mFlingAnimationUtilsDismissing = new FlingAnimationUtils(context, 206 0.5f /* maxLengthSeconds */, 0.2f /* speedUpFactor */, 0.6f /* x2 */, 207 0.84f /* y2 */); 208 mBounceInterpolator = new BounceInterpolator(); 209 mFalsingManager = FalsingManager.getInstance(context); 210 mNotificationsDragEnabled = 211 getResources().getBoolean(R.bool.config_enableNotificationShadeDrag); 212 mVibrator = mContext.getSystemService(Vibrator.class); 213 mVibrateOnOpening = mContext.getResources().getBoolean( 214 R.bool.config_vibrateOnIconAnimation); 215 } 216 217 protected void loadDimens() { 218 final Resources res = getContext().getResources(); 219 final ViewConfiguration configuration = ViewConfiguration.get(getContext()); 220 mTouchSlop = configuration.getScaledTouchSlop(); 221 mHintDistance = res.getDimension(R.dimen.hint_move_distance); 222 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold); 223 } 224 225 private void trackMovement(MotionEvent event) { 226 // Add movement to velocity tracker using raw screen X and Y coordinates instead 227 // of window coordinates because the window frame may be moving at the same time. 228 float deltaX = event.getRawX() - event.getX(); 229 float deltaY = event.getRawY() - event.getY(); 230 event.offsetLocation(deltaX, deltaY); 231 if (mVelocityTracker != null) mVelocityTracker.addMovement(event); 232 event.offsetLocation(-deltaX, -deltaY); 233 } 234 235 public void setTouchDisabled(boolean disabled) { 236 mTouchDisabled = disabled; 237 if (mTouchDisabled) { 238 cancelHeightAnimator(); 239 if (mTracking) { 240 onTrackingStopped(true /* expanded */); 241 } 242 notifyExpandingFinished(); 243 } 244 } 245 246 public void startExpandLatencyTracking() { 247 if (LatencyTracker.isEnabled(mContext)) { 248 LatencyTracker.getInstance(mContext).onActionStart( 249 LatencyTracker.ACTION_EXPAND_PANEL); 250 mExpandLatencyTracking = true; 251 } 252 } 253 254 @Override 255 public boolean onTouchEvent(MotionEvent event) { 256 if (mInstantExpanding || mTouchDisabled 257 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) { 258 return false; 259 } 260 261 // If dragging should not expand the notifications shade, then return false. 262 if (!mNotificationsDragEnabled) { 263 if (mTracking) { 264 // Turn off tracking if it's on or the shade can get stuck in the down position. 265 onTrackingStopped(true /* expand */); 266 } 267 return false; 268 } 269 270 // On expanding, single mouse click expands the panel instead of dragging. 271 if (isFullyCollapsed() && event.isFromSource(InputDevice.SOURCE_MOUSE)) { 272 if (event.getAction() == MotionEvent.ACTION_UP) { 273 expand(true); 274 } 275 return true; 276 } 277 278 /* 279 * We capture touch events here and update the expand height here in case according to 280 * the users fingers. This also handles multi-touch. 281 * 282 * If the user just clicks shortly, we show a quick peek of the shade. 283 * 284 * Flinging is also enabled in order to open or close the shade. 285 */ 286 287 int pointerIndex = event.findPointerIndex(mTrackingPointer); 288 if (pointerIndex < 0) { 289 pointerIndex = 0; 290 mTrackingPointer = event.getPointerId(pointerIndex); 291 } 292 final float x = event.getX(pointerIndex); 293 final float y = event.getY(pointerIndex); 294 295 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { 296 mGestureWaitForTouchSlop = isFullyCollapsed() || hasConflictingGestures(); 297 mIgnoreXTouchSlop = isFullyCollapsed() || shouldGestureIgnoreXTouchSlop(x, y); 298 } 299 300 switch (event.getActionMasked()) { 301 case MotionEvent.ACTION_DOWN: 302 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight); 303 mJustPeeked = false; 304 mMinExpandHeight = 0.0f; 305 mPanelClosedOnDown = isFullyCollapsed(); 306 mHasLayoutedSinceDown = false; 307 mUpdateFlingOnLayout = false; 308 mMotionAborted = false; 309 mPeekTouching = mPanelClosedOnDown; 310 mDownTime = SystemClock.uptimeMillis(); 311 mTouchAboveFalsingThreshold = false; 312 mCollapsedAndHeadsUpOnDown = isFullyCollapsed() 313 && mHeadsUpManager.hasPinnedHeadsUp(); 314 if (mVelocityTracker == null) { 315 initVelocityTracker(); 316 } 317 trackMovement(event); 318 if (!mGestureWaitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) 319 || mPeekAnimator != null) { 320 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning) 321 || mPeekAnimator != null; 322 cancelHeightAnimator(); 323 cancelPeek(); 324 onTrackingStarted(); 325 } 326 if (isFullyCollapsed() && !mHeadsUpManager.hasPinnedHeadsUp()) { 327 startOpening(); 328 } 329 break; 330 331 case MotionEvent.ACTION_POINTER_UP: 332 final int upPointer = event.getPointerId(event.getActionIndex()); 333 if (mTrackingPointer == upPointer) { 334 // gesture is ongoing, find a new pointer to track 335 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 336 final float newY = event.getY(newIndex); 337 final float newX = event.getX(newIndex); 338 mTrackingPointer = event.getPointerId(newIndex); 339 startExpandMotion(newX, newY, true /* startTracking */, mExpandedHeight); 340 } 341 break; 342 case MotionEvent.ACTION_POINTER_DOWN: 343 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 344 mMotionAborted = true; 345 endMotionEvent(event, x, y, true /* forceCancel */); 346 return false; 347 } 348 break; 349 case MotionEvent.ACTION_MOVE: 350 trackMovement(event); 351 float h = y - mInitialTouchY; 352 353 // If the panel was collapsed when touching, we only need to check for the 354 // y-component of the gesture, as we have no conflicting horizontal gesture. 355 if (Math.abs(h) > mTouchSlop 356 && (Math.abs(h) > Math.abs(x - mInitialTouchX) 357 || mIgnoreXTouchSlop)) { 358 mTouchSlopExceeded = true; 359 if (mGestureWaitForTouchSlop && !mTracking && !mCollapsedAndHeadsUpOnDown) { 360 if (!mJustPeeked && mInitialOffsetOnTouch != 0f) { 361 startExpandMotion(x, y, false /* startTracking */, mExpandedHeight); 362 h = 0; 363 } 364 cancelHeightAnimator(); 365 onTrackingStarted(); 366 } 367 } 368 float newHeight = Math.max(0, h + mInitialOffsetOnTouch); 369 if (newHeight > mPeekHeight) { 370 if (mPeekAnimator != null) { 371 mPeekAnimator.cancel(); 372 } 373 mJustPeeked = false; 374 } else if (mPeekAnimator == null && mJustPeeked) { 375 // The initial peek has finished, but we haven't dragged as far yet, lets 376 // speed it up by starting at the peek height. 377 mInitialOffsetOnTouch = mExpandedHeight; 378 mInitialTouchY = y; 379 mMinExpandHeight = mExpandedHeight; 380 mJustPeeked = false; 381 } 382 newHeight = Math.max(newHeight, mMinExpandHeight); 383 if (-h >= getFalsingThreshold()) { 384 mTouchAboveFalsingThreshold = true; 385 mUpwardsWhenTresholdReached = isDirectionUpwards(x, y); 386 } 387 if (!mJustPeeked && (!mGestureWaitForTouchSlop || mTracking) && 388 !isTrackingBlocked()) { 389 setExpandedHeightInternal(newHeight); 390 } 391 break; 392 393 case MotionEvent.ACTION_UP: 394 case MotionEvent.ACTION_CANCEL: 395 trackMovement(event); 396 endMotionEvent(event, x, y, false /* forceCancel */); 397 break; 398 } 399 return !mGestureWaitForTouchSlop || mTracking; 400 } 401 402 private void startOpening() {; 403 runPeekAnimation(INITIAL_OPENING_PEEK_DURATION, getOpeningHeight(), 404 false /* collapseWhenFinished */); 405 notifyBarPanelExpansionChanged(); 406 if (mVibrateOnOpening && !isHapticFeedbackDisabled(mContext)) { 407 AsyncTask.execute(() -> 408 mVibrator.vibrate(VibrationEffect.get(VibrationEffect.EFFECT_TICK, false))); 409 } 410 } 411 412 protected abstract float getOpeningHeight(); 413 414 /** 415 * @return whether the swiping direction is upwards and above a 45 degree angle compared to the 416 * horizontal direction 417 */ 418 private boolean isDirectionUpwards(float x, float y) { 419 float xDiff = x - mInitialTouchX; 420 float yDiff = y - mInitialTouchY; 421 if (yDiff >= 0) { 422 return false; 423 } 424 return Math.abs(yDiff) >= Math.abs(xDiff); 425 } 426 427 protected void startExpandingFromPeek() { 428 mStatusBar.handlePeekToExpandTransistion(); 429 } 430 431 protected void startExpandMotion(float newX, float newY, boolean startTracking, 432 float expandedHeight) { 433 mInitialOffsetOnTouch = expandedHeight; 434 mInitialTouchY = newY; 435 mInitialTouchX = newX; 436 if (startTracking) { 437 mTouchSlopExceeded = true; 438 setExpandedHeight(mInitialOffsetOnTouch); 439 onTrackingStarted(); 440 } 441 } 442 443 private void endMotionEvent(MotionEvent event, float x, float y, boolean forceCancel) { 444 mTrackingPointer = -1; 445 if ((mTracking && mTouchSlopExceeded) 446 || Math.abs(x - mInitialTouchX) > mTouchSlop 447 || Math.abs(y - mInitialTouchY) > mTouchSlop 448 || event.getActionMasked() == MotionEvent.ACTION_CANCEL 449 || forceCancel) { 450 float vel = 0f; 451 float vectorVel = 0f; 452 if (mVelocityTracker != null) { 453 mVelocityTracker.computeCurrentVelocity(1000); 454 vel = mVelocityTracker.getYVelocity(); 455 vectorVel = (float) Math.hypot( 456 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); 457 } 458 boolean expand = flingExpands(vel, vectorVel, x, y) 459 || event.getActionMasked() == MotionEvent.ACTION_CANCEL 460 || forceCancel; 461 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold, 462 mStatusBar.isFalsingThresholdNeeded(), 463 mStatusBar.isWakeUpComingFromTouch()); 464 // Log collapse gesture if on lock screen. 465 if (!expand && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 466 float displayDensity = mStatusBar.getDisplayDensity(); 467 int heightDp = (int) Math.abs((y - mInitialTouchY) / displayDensity); 468 int velocityDp = (int) Math.abs(vel / displayDensity); 469 mLockscreenGestureLogger.write( 470 MetricsEvent.ACTION_LS_UNLOCK, 471 heightDp, velocityDp); 472 } 473 fling(vel, expand, isFalseTouch(x, y)); 474 onTrackingStopped(expand); 475 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown; 476 if (mUpdateFlingOnLayout) { 477 mUpdateFlingVelocity = vel; 478 } 479 } else if (mPanelClosedOnDown && !mHeadsUpManager.hasPinnedHeadsUp() && !mTracking) { 480 long timePassed = SystemClock.uptimeMillis() - mDownTime; 481 if (timePassed < ViewConfiguration.getLongPressTimeout()) { 482 // Lets show the user that he can actually expand the panel 483 runPeekAnimation(PEEK_ANIMATION_DURATION, getPeekHeight(), true /* collapseWhenFinished */); 484 } else { 485 // We need to collapse the panel since we peeked to the small height. 486 postOnAnimation(mPostCollapseRunnable); 487 } 488 } else { 489 boolean expands = onEmptySpaceClick(mInitialTouchX); 490 onTrackingStopped(expands); 491 } 492 493 if (mVelocityTracker != null) { 494 mVelocityTracker.recycle(); 495 mVelocityTracker = null; 496 } 497 mPeekTouching = false; 498 } 499 500 protected float getCurrentExpandVelocity() { 501 if (mVelocityTracker == null) { 502 return 0; 503 } 504 mVelocityTracker.computeCurrentVelocity(1000); 505 return mVelocityTracker.getYVelocity(); 506 } 507 508 private int getFalsingThreshold() { 509 float factor = mStatusBar.isWakeUpComingFromTouch() ? 1.5f : 1.0f; 510 return (int) (mUnlockFalsingThreshold * factor); 511 } 512 513 protected abstract boolean hasConflictingGestures(); 514 515 protected abstract boolean shouldGestureIgnoreXTouchSlop(float x, float y); 516 517 protected void onTrackingStopped(boolean expand) { 518 mTracking = false; 519 mBar.onTrackingStopped(expand); 520 notifyBarPanelExpansionChanged(); 521 } 522 523 protected void onTrackingStarted() { 524 endClosing(); 525 mTracking = true; 526 mBar.onTrackingStarted(); 527 notifyExpandingStarted(); 528 notifyBarPanelExpansionChanged(); 529 } 530 531 @Override 532 public boolean onInterceptTouchEvent(MotionEvent event) { 533 if (mInstantExpanding || !mNotificationsDragEnabled || mTouchDisabled 534 || (mMotionAborted && event.getActionMasked() != MotionEvent.ACTION_DOWN)) { 535 return false; 536 } 537 538 /* 539 * If the user drags anywhere inside the panel we intercept it if the movement is 540 * upwards. This allows closing the shade from anywhere inside the panel. 541 * 542 * We only do this if the current content is scrolled to the bottom, 543 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture 544 * possible. 545 */ 546 int pointerIndex = event.findPointerIndex(mTrackingPointer); 547 if (pointerIndex < 0) { 548 pointerIndex = 0; 549 mTrackingPointer = event.getPointerId(pointerIndex); 550 } 551 final float x = event.getX(pointerIndex); 552 final float y = event.getY(pointerIndex); 553 boolean scrolledToBottom = isScrolledToBottom(); 554 555 switch (event.getActionMasked()) { 556 case MotionEvent.ACTION_DOWN: 557 mStatusBar.userActivity(); 558 mAnimatingOnDown = mHeightAnimator != null; 559 mMinExpandHeight = 0.0f; 560 mDownTime = SystemClock.uptimeMillis(); 561 if (mAnimatingOnDown && mClosing && !mHintAnimationRunning 562 || mPeekAnimator != null) { 563 cancelHeightAnimator(); 564 cancelPeek(); 565 mTouchSlopExceeded = true; 566 return true; 567 } 568 mInitialTouchY = y; 569 mInitialTouchX = x; 570 mTouchStartedInEmptyArea = !isInContentBounds(x, y); 571 mTouchSlopExceeded = false; 572 mJustPeeked = false; 573 mMotionAborted = false; 574 mPanelClosedOnDown = isFullyCollapsed(); 575 mCollapsedAndHeadsUpOnDown = false; 576 mHasLayoutedSinceDown = false; 577 mUpdateFlingOnLayout = false; 578 mTouchAboveFalsingThreshold = false; 579 initVelocityTracker(); 580 trackMovement(event); 581 break; 582 case MotionEvent.ACTION_POINTER_UP: 583 final int upPointer = event.getPointerId(event.getActionIndex()); 584 if (mTrackingPointer == upPointer) { 585 // gesture is ongoing, find a new pointer to track 586 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 587 mTrackingPointer = event.getPointerId(newIndex); 588 mInitialTouchX = event.getX(newIndex); 589 mInitialTouchY = event.getY(newIndex); 590 } 591 break; 592 case MotionEvent.ACTION_POINTER_DOWN: 593 if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 594 mMotionAborted = true; 595 if (mVelocityTracker != null) { 596 mVelocityTracker.recycle(); 597 mVelocityTracker = null; 598 } 599 } 600 break; 601 case MotionEvent.ACTION_MOVE: 602 final float h = y - mInitialTouchY; 603 trackMovement(event); 604 if (scrolledToBottom || mTouchStartedInEmptyArea || mAnimatingOnDown) { 605 float hAbs = Math.abs(h); 606 if ((h < -mTouchSlop || (mAnimatingOnDown && hAbs > mTouchSlop)) 607 && hAbs > Math.abs(x - mInitialTouchX)) { 608 cancelHeightAnimator(); 609 startExpandMotion(x, y, true /* startTracking */, mExpandedHeight); 610 return true; 611 } 612 } 613 break; 614 case MotionEvent.ACTION_CANCEL: 615 case MotionEvent.ACTION_UP: 616 if (mVelocityTracker != null) { 617 mVelocityTracker.recycle(); 618 mVelocityTracker = null; 619 } 620 break; 621 } 622 return false; 623 } 624 625 /** 626 * @return Whether a pair of coordinates are inside the visible view content bounds. 627 */ 628 protected abstract boolean isInContentBounds(float x, float y); 629 630 protected void cancelHeightAnimator() { 631 if (mHeightAnimator != null) { 632 if (mHeightAnimator.isRunning()) { 633 mPanelUpdateWhenAnimatorEnds = false; 634 } 635 mHeightAnimator.cancel(); 636 } 637 endClosing(); 638 } 639 640 private void endClosing() { 641 if (mClosing) { 642 mClosing = false; 643 onClosingFinished(); 644 } 645 } 646 647 private void initVelocityTracker() { 648 if (mVelocityTracker != null) { 649 mVelocityTracker.recycle(); 650 } 651 mVelocityTracker = VelocityTrackerFactory.obtain(getContext()); 652 } 653 654 protected boolean isScrolledToBottom() { 655 return true; 656 } 657 658 protected float getContentHeight() { 659 return mExpandedHeight; 660 } 661 662 @Override 663 protected void onFinishInflate() { 664 super.onFinishInflate(); 665 loadDimens(); 666 } 667 668 @Override 669 protected void onConfigurationChanged(Configuration newConfig) { 670 super.onConfigurationChanged(newConfig); 671 loadDimens(); 672 } 673 674 /** 675 * @param vel the current vertical velocity of the motion 676 * @param vectorVel the length of the vectorial velocity 677 * @return whether a fling should expands the panel; contracts otherwise 678 */ 679 protected boolean flingExpands(float vel, float vectorVel, float x, float y) { 680 if (isFalseTouch(x, y)) { 681 return true; 682 } 683 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) { 684 return getExpandedFraction() > 0.5f; 685 } else { 686 return vel > 0; 687 } 688 } 689 690 /** 691 * @param x the final x-coordinate when the finger was lifted 692 * @param y the final y-coordinate when the finger was lifted 693 * @return whether this motion should be regarded as a false touch 694 */ 695 private boolean isFalseTouch(float x, float y) { 696 if (!mStatusBar.isFalsingThresholdNeeded()) { 697 return false; 698 } 699 if (mFalsingManager.isClassiferEnabled()) { 700 return mFalsingManager.isFalseTouch(); 701 } 702 if (!mTouchAboveFalsingThreshold) { 703 return true; 704 } 705 if (mUpwardsWhenTresholdReached) { 706 return false; 707 } 708 return !isDirectionUpwards(x, y); 709 } 710 711 protected void fling(float vel, boolean expand) { 712 fling(vel, expand, 1.0f /* collapseSpeedUpFactor */, false); 713 } 714 715 protected void fling(float vel, boolean expand, boolean expandBecauseOfFalsing) { 716 fling(vel, expand, 1.0f /* collapseSpeedUpFactor */, expandBecauseOfFalsing); 717 } 718 719 protected void fling(float vel, boolean expand, float collapseSpeedUpFactor, 720 boolean expandBecauseOfFalsing) { 721 cancelPeek(); 722 float target = expand ? getMaxPanelHeight() : 0; 723 if (!expand) { 724 mClosing = true; 725 } 726 flingToHeight(vel, expand, target, collapseSpeedUpFactor, expandBecauseOfFalsing); 727 } 728 729 protected void flingToHeight(float vel, boolean expand, float target, 730 float collapseSpeedUpFactor, boolean expandBecauseOfFalsing) { 731 // Hack to make the expand transition look nice when clear all button is visible - we make 732 // the animation only to the last notification, and then jump to the maximum panel height so 733 // clear all just fades in and the decelerating motion is towards the last notification. 734 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible() 735 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight() 736 && !isClearAllVisible(); 737 if (clearAllExpandHack) { 738 target = getMaxPanelHeight() - getClearAllHeight(); 739 } 740 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) { 741 notifyExpandingFinished(); 742 return; 743 } 744 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f; 745 ValueAnimator animator = createHeightAnimator(target); 746 if (expand) { 747 if (expandBecauseOfFalsing && vel < 0) { 748 vel = 0; 749 } 750 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight()); 751 if (vel == 0) { 752 animator.setDuration(350); 753 } 754 } else { 755 if (shouldUseDismissingAnimation()) { 756 if (vel == 0) { 757 animator.setInterpolator(Interpolators.PANEL_CLOSE_ACCELERATED); 758 long duration = (long) (200 + mExpandedHeight / getHeight() * 100); 759 animator.setDuration(duration); 760 } else { 761 mFlingAnimationUtilsDismissing.apply(animator, mExpandedHeight, target, vel, 762 getHeight()); 763 } 764 } else { 765 mFlingAnimationUtilsClosing 766 .apply(animator, mExpandedHeight, target, vel, getHeight()); 767 } 768 769 // Make it shorter if we run a canned animation 770 if (vel == 0) { 771 animator.setDuration((long) (animator.getDuration() / collapseSpeedUpFactor)); 772 } 773 } 774 animator.addListener(new AnimatorListenerAdapter() { 775 private boolean mCancelled; 776 777 @Override 778 public void onAnimationCancel(Animator animation) { 779 mCancelled = true; 780 } 781 782 @Override 783 public void onAnimationEnd(Animator animation) { 784 if (clearAllExpandHack && !mCancelled) { 785 setExpandedHeightInternal(getMaxPanelHeight()); 786 } 787 setAnimator(null); 788 if (!mCancelled) { 789 notifyExpandingFinished(); 790 } 791 notifyBarPanelExpansionChanged(); 792 } 793 }); 794 setAnimator(animator); 795 animator.start(); 796 } 797 798 protected abstract boolean shouldUseDismissingAnimation(); 799 800 @Override 801 protected void onAttachedToWindow() { 802 super.onAttachedToWindow(); 803 mViewName = getResources().getResourceName(getId()); 804 } 805 806 public String getName() { 807 return mViewName; 808 } 809 810 public void setExpandedHeight(float height) { 811 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 812 setExpandedHeightInternal(height + getOverExpansionPixels()); 813 } 814 815 @Override 816 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 817 super.onLayout(changed, left, top, right, bottom); 818 mStatusBar.onPanelLaidOut(); 819 requestPanelHeightUpdate(); 820 mHasLayoutedSinceDown = true; 821 if (mUpdateFlingOnLayout) { 822 abortAnimations(); 823 fling(mUpdateFlingVelocity, true /* expands */); 824 mUpdateFlingOnLayout = false; 825 } 826 } 827 828 protected void requestPanelHeightUpdate() { 829 float currentMaxPanelHeight = getMaxPanelHeight(); 830 831 if (isFullyCollapsed()) { 832 return; 833 } 834 835 if (currentMaxPanelHeight == mExpandedHeight) { 836 return; 837 } 838 839 if (mPeekAnimator != null || mPeekTouching) { 840 return; 841 } 842 843 if (mTracking && !isTrackingBlocked()) { 844 return; 845 } 846 847 if (mHeightAnimator != null) { 848 mPanelUpdateWhenAnimatorEnds = true; 849 return; 850 } 851 852 setExpandedHeight(currentMaxPanelHeight); 853 } 854 855 public void setExpandedHeightInternal(float h) { 856 if (mExpandLatencyTracking && h != 0f) { 857 DejankUtils.postAfterTraversal(() -> LatencyTracker.getInstance(mContext).onActionEnd( 858 LatencyTracker.ACTION_EXPAND_PANEL)); 859 mExpandLatencyTracking = false; 860 } 861 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount(); 862 if (mHeightAnimator == null) { 863 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion); 864 if (getOverExpansionPixels() != overExpansionPixels && mTracking) { 865 setOverExpansion(overExpansionPixels, true /* isPixels */); 866 } 867 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount(); 868 } else { 869 mExpandedHeight = h; 870 if (mOverExpandedBeforeFling) { 871 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */); 872 } 873 } 874 875 // If we are closing the panel and we are almost there due to a slow decelerating 876 // interpolator, abort the animation. 877 if (mExpandedHeight < 1f && mExpandedHeight != 0f && mClosing) { 878 mExpandedHeight = 0f; 879 if (mHeightAnimator != null) { 880 mHeightAnimator.end(); 881 } 882 } 883 mExpandedFraction = Math.min(1f, 884 fhWithoutOverExpansion == 0 ? 0 : mExpandedHeight / fhWithoutOverExpansion); 885 onHeightUpdated(mExpandedHeight); 886 notifyBarPanelExpansionChanged(); 887 } 888 889 /** 890 * @return true if the panel tracking should be temporarily blocked; this is used when a 891 * conflicting gesture (opening QS) is happening 892 */ 893 protected abstract boolean isTrackingBlocked(); 894 895 protected abstract void setOverExpansion(float overExpansion, boolean isPixels); 896 897 protected abstract void onHeightUpdated(float expandedHeight); 898 899 protected abstract float getOverExpansionAmount(); 900 901 protected abstract float getOverExpansionPixels(); 902 903 /** 904 * This returns the maximum height of the panel. Children should override this if their 905 * desired height is not the full height. 906 * 907 * @return the default implementation simply returns the maximum height. 908 */ 909 protected abstract int getMaxPanelHeight(); 910 911 public void setExpandedFraction(float frac) { 912 setExpandedHeight(getMaxPanelHeight() * frac); 913 } 914 915 public float getExpandedHeight() { 916 return mExpandedHeight; 917 } 918 919 public float getExpandedFraction() { 920 return mExpandedFraction; 921 } 922 923 public boolean isFullyExpanded() { 924 return mExpandedHeight >= getMaxPanelHeight(); 925 } 926 927 public boolean isFullyCollapsed() { 928 return mExpandedFraction <= 0.0f; 929 } 930 931 public boolean isCollapsing() { 932 return mClosing; 933 } 934 935 public boolean isTracking() { 936 return mTracking; 937 } 938 939 public void setBar(PanelBar panelBar) { 940 mBar = panelBar; 941 } 942 943 public void collapse(boolean delayed, float speedUpFactor) { 944 if (DEBUG) logf("collapse: " + this); 945 if (canPanelBeCollapsed()) { 946 cancelHeightAnimator(); 947 notifyExpandingStarted(); 948 949 // Set after notifyExpandingStarted, as notifyExpandingStarted resets the closing state. 950 mClosing = true; 951 if (delayed) { 952 mNextCollapseSpeedUpFactor = speedUpFactor; 953 postDelayed(mFlingCollapseRunnable, 120); 954 } else { 955 fling(0, false /* expand */, speedUpFactor, false /* expandBecauseOfFalsing */); 956 } 957 } 958 } 959 960 public boolean canPanelBeCollapsed() { 961 return !isFullyCollapsed() && !mTracking && !mClosing; 962 } 963 964 private final Runnable mFlingCollapseRunnable = new Runnable() { 965 @Override 966 public void run() { 967 fling(0, false /* expand */, mNextCollapseSpeedUpFactor, 968 false /* expandBecauseOfFalsing */); 969 } 970 }; 971 972 public void cancelPeek() { 973 boolean cancelled = false; 974 if (mPeekAnimator != null) { 975 cancelled = true; 976 mPeekAnimator.cancel(); 977 } 978 979 if (cancelled) { 980 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also 981 // notify mBar that we might have closed ourselves. 982 notifyBarPanelExpansionChanged(); 983 } 984 } 985 986 public void expand(final boolean animate) { 987 if (!isFullyCollapsed() && !isCollapsing()) { 988 return; 989 } 990 991 mInstantExpanding = true; 992 mAnimateAfterExpanding = animate; 993 mUpdateFlingOnLayout = false; 994 abortAnimations(); 995 cancelPeek(); 996 if (mTracking) { 997 onTrackingStopped(true /* expands */); // The panel is expanded after this call. 998 } 999 if (mExpanding) { 1000 notifyExpandingFinished(); 1001 } 1002 notifyBarPanelExpansionChanged(); 1003 1004 // Wait for window manager to pickup the change, so we know the maximum height of the panel 1005 // then. 1006 getViewTreeObserver().addOnGlobalLayoutListener( 1007 new ViewTreeObserver.OnGlobalLayoutListener() { 1008 @Override 1009 public void onGlobalLayout() { 1010 if (!mInstantExpanding) { 1011 getViewTreeObserver().removeOnGlobalLayoutListener(this); 1012 return; 1013 } 1014 if (mStatusBar.getStatusBarWindow().getHeight() 1015 != mStatusBar.getStatusBarHeight()) { 1016 getViewTreeObserver().removeOnGlobalLayoutListener(this); 1017 if (mAnimateAfterExpanding) { 1018 notifyExpandingStarted(); 1019 fling(0, true /* expand */); 1020 } else { 1021 setExpandedFraction(1f); 1022 } 1023 mInstantExpanding = false; 1024 } 1025 } 1026 }); 1027 1028 // Make sure a layout really happens. 1029 requestLayout(); 1030 } 1031 1032 public void instantCollapse() { 1033 abortAnimations(); 1034 setExpandedFraction(0f); 1035 if (mExpanding) { 1036 notifyExpandingFinished(); 1037 } 1038 if (mInstantExpanding) { 1039 mInstantExpanding = false; 1040 notifyBarPanelExpansionChanged(); 1041 } 1042 } 1043 1044 private void abortAnimations() { 1045 cancelPeek(); 1046 cancelHeightAnimator(); 1047 removeCallbacks(mPostCollapseRunnable); 1048 removeCallbacks(mFlingCollapseRunnable); 1049 } 1050 1051 protected void onClosingFinished() { 1052 mBar.onClosingFinished(); 1053 } 1054 1055 1056 protected void startUnlockHintAnimation() { 1057 1058 // We don't need to hint the user if an animation is already running or the user is changing 1059 // the expansion. 1060 if (mHeightAnimator != null || mTracking) { 1061 return; 1062 } 1063 cancelPeek(); 1064 notifyExpandingStarted(); 1065 startUnlockHintAnimationPhase1(new Runnable() { 1066 @Override 1067 public void run() { 1068 notifyExpandingFinished(); 1069 onUnlockHintFinished(); 1070 mHintAnimationRunning = false; 1071 } 1072 }); 1073 onUnlockHintStarted(); 1074 mHintAnimationRunning = true; 1075 } 1076 1077 protected void onUnlockHintFinished() { 1078 mStatusBar.onHintFinished(); 1079 } 1080 1081 protected void onUnlockHintStarted() { 1082 mStatusBar.onUnlockHintStarted(); 1083 } 1084 1085 /** 1086 * Phase 1: Move everything upwards. 1087 */ 1088 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) { 1089 float target = Math.max(0, getMaxPanelHeight() - mHintDistance); 1090 ValueAnimator animator = createHeightAnimator(target); 1091 animator.setDuration(250); 1092 animator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); 1093 animator.addListener(new AnimatorListenerAdapter() { 1094 private boolean mCancelled; 1095 1096 @Override 1097 public void onAnimationCancel(Animator animation) { 1098 mCancelled = true; 1099 } 1100 1101 @Override 1102 public void onAnimationEnd(Animator animation) { 1103 if (mCancelled) { 1104 setAnimator(null); 1105 onAnimationFinished.run(); 1106 } else { 1107 startUnlockHintAnimationPhase2(onAnimationFinished); 1108 } 1109 } 1110 }); 1111 animator.start(); 1112 setAnimator(animator); 1113 1114 View[] viewsToAnimate = { 1115 mKeyguardBottomArea.getIndicationArea(), 1116 mStatusBar.getAmbientIndicationContainer()}; 1117 for (View v : viewsToAnimate) { 1118 if (v == null) { 1119 continue; 1120 } 1121 v.animate() 1122 .translationY(-mHintDistance) 1123 .setDuration(250) 1124 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN) 1125 .withEndAction(() -> v.animate() 1126 .translationY(0) 1127 .setDuration(450) 1128 .setInterpolator(mBounceInterpolator) 1129 .start()) 1130 .start(); 1131 } 1132 } 1133 1134 private void setAnimator(ValueAnimator animator) { 1135 mHeightAnimator = animator; 1136 if (animator == null && mPanelUpdateWhenAnimatorEnds) { 1137 mPanelUpdateWhenAnimatorEnds = false; 1138 requestPanelHeightUpdate(); 1139 } 1140 } 1141 1142 /** 1143 * Phase 2: Bounce down. 1144 */ 1145 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) { 1146 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight()); 1147 animator.setDuration(450); 1148 animator.setInterpolator(mBounceInterpolator); 1149 animator.addListener(new AnimatorListenerAdapter() { 1150 @Override 1151 public void onAnimationEnd(Animator animation) { 1152 setAnimator(null); 1153 onAnimationFinished.run(); 1154 notifyBarPanelExpansionChanged(); 1155 } 1156 }); 1157 animator.start(); 1158 setAnimator(animator); 1159 } 1160 1161 private ValueAnimator createHeightAnimator(float targetHeight) { 1162 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight); 1163 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 1164 @Override 1165 public void onAnimationUpdate(ValueAnimator animation) { 1166 setExpandedHeightInternal((Float) animation.getAnimatedValue()); 1167 } 1168 }); 1169 return animator; 1170 } 1171 1172 protected void notifyBarPanelExpansionChanged() { 1173 mBar.panelExpansionChanged(mExpandedFraction, mExpandedFraction > 0f 1174 || mPeekAnimator != null || mInstantExpanding || isPanelVisibleBecauseOfHeadsUp() 1175 || mTracking || mHeightAnimator != null); 1176 } 1177 1178 protected abstract boolean isPanelVisibleBecauseOfHeadsUp(); 1179 1180 /** 1181 * Gets called when the user performs a click anywhere in the empty area of the panel. 1182 * 1183 * @return whether the panel will be expanded after the action performed by this method 1184 */ 1185 protected boolean onEmptySpaceClick(float x) { 1186 if (mHintAnimationRunning) { 1187 return true; 1188 } 1189 return onMiddleClicked(); 1190 } 1191 1192 protected final Runnable mPostCollapseRunnable = new Runnable() { 1193 @Override 1194 public void run() { 1195 collapse(false /* delayed */, 1.0f /* speedUpFactor */); 1196 } 1197 }; 1198 1199 protected abstract boolean onMiddleClicked(); 1200 1201 protected abstract boolean isDozing(); 1202 1203 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1204 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 1205 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s" 1206 + "]", 1207 this.getClass().getSimpleName(), 1208 getExpandedHeight(), 1209 getMaxPanelHeight(), 1210 mClosing?"T":"f", 1211 mTracking?"T":"f", 1212 mJustPeeked?"T":"f", 1213 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 1214 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""), 1215 mTouchDisabled?"T":"f" 1216 )); 1217 } 1218 1219 public abstract void resetViews(); 1220 1221 protected abstract float getPeekHeight(); 1222 /** 1223 * @return whether "Clear all" button will be visible when the panel is fully expanded 1224 */ 1225 protected abstract boolean fullyExpandedClearAllVisible(); 1226 1227 protected abstract boolean isClearAllVisible(); 1228 1229 /** 1230 * @return the height of the clear all button, in pixels 1231 */ 1232 protected abstract int getClearAllHeight(); 1233 1234 public void setHeadsUpManager(HeadsUpManager headsUpManager) { 1235 mHeadsUpManager = headsUpManager; 1236 } 1237 } 1238