PanelView.java revision d7912d2f106053bf4965d137b436e0b5b863f184
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 17package com.android.systemui.statusbar.phone; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.ObjectAnimator; 22import android.animation.ValueAnimator; 23import android.content.Context; 24import android.content.res.Configuration; 25import android.content.res.Resources; 26import android.util.AttributeSet; 27import android.util.Log; 28import android.view.MotionEvent; 29import android.view.ViewConfiguration; 30import android.view.ViewTreeObserver; 31import android.view.animation.AnimationUtils; 32import android.view.animation.Interpolator; 33import android.widget.FrameLayout; 34 35import com.android.systemui.R; 36import com.android.systemui.doze.DozeLog; 37import com.android.systemui.statusbar.FlingAnimationUtils; 38import com.android.systemui.statusbar.StatusBarState; 39 40import java.io.FileDescriptor; 41import java.io.PrintWriter; 42 43public abstract class PanelView extends FrameLayout { 44 public static final boolean DEBUG = PanelBar.DEBUG; 45 public static final String TAG = PanelView.class.getSimpleName(); 46 47 private final void logf(String fmt, Object... args) { 48 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); 49 } 50 51 protected PhoneStatusBar mStatusBar; 52 private float mPeekHeight; 53 private float mHintDistance; 54 private int mEdgeTapAreaWidth; 55 private float mInitialOffsetOnTouch; 56 private float mExpandedFraction = 0; 57 protected float mExpandedHeight = 0; 58 private boolean mPanelClosedOnDown; 59 private boolean mHasLayoutedSinceDown; 60 private float mUpdateFlingVelocity; 61 private boolean mUpdateFlingOnLayout; 62 private boolean mPeekTouching; 63 private boolean mJustPeeked; 64 private boolean mClosing; 65 protected boolean mTracking; 66 private boolean mTouchSlopExceeded; 67 private int mTrackingPointer; 68 protected int mTouchSlop; 69 protected boolean mHintAnimationRunning; 70 private boolean mOverExpandedBeforeFling; 71 private float mOriginalIndicationY; 72 private boolean mTouchAboveFalsingThreshold; 73 private int mUnlockFalsingThreshold; 74 75 private ValueAnimator mHeightAnimator; 76 private ObjectAnimator mPeekAnimator; 77 private VelocityTrackerInterface mVelocityTracker; 78 private FlingAnimationUtils mFlingAnimationUtils; 79 80 /** 81 * Whether an instant expand request is currently pending and we are just waiting for layout. 82 */ 83 private boolean mInstantExpanding; 84 85 PanelBar mBar; 86 87 private String mViewName; 88 private float mInitialTouchY; 89 private float mInitialTouchX; 90 private boolean mTouchDisabled; 91 92 private Interpolator mLinearOutSlowInInterpolator; 93 private Interpolator mFastOutSlowInInterpolator; 94 private Interpolator mBounceInterpolator; 95 protected KeyguardBottomAreaView mKeyguardBottomArea; 96 97 private boolean mPeekPending; 98 private boolean mCollapseAfterPeek; 99 private boolean mExpanding; 100 private boolean mGestureWaitForTouchSlop; 101 private Runnable mPeekRunnable = new Runnable() { 102 @Override 103 public void run() { 104 mPeekPending = false; 105 runPeekAnimation(); 106 } 107 }; 108 109 protected void onExpandingFinished() { 110 mClosing = false; 111 mBar.onExpandingFinished(); 112 } 113 114 protected void onExpandingStarted() { 115 } 116 117 private void notifyExpandingStarted() { 118 if (!mExpanding) { 119 mExpanding = true; 120 onExpandingStarted(); 121 } 122 } 123 124 private void notifyExpandingFinished() { 125 if (mExpanding) { 126 mExpanding = false; 127 onExpandingFinished(); 128 } 129 } 130 131 private void schedulePeek() { 132 mPeekPending = true; 133 long timeout = ViewConfiguration.getTapTimeout(); 134 postOnAnimationDelayed(mPeekRunnable, timeout); 135 notifyBarPanelExpansionChanged(); 136 } 137 138 private void runPeekAnimation() { 139 mPeekHeight = getPeekHeight(); 140 if (DEBUG) logf("peek to height=%.1f", mPeekHeight); 141 if (mHeightAnimator != null) { 142 return; 143 } 144 mPeekAnimator = ObjectAnimator.ofFloat(this, "expandedHeight", mPeekHeight) 145 .setDuration(250); 146 mPeekAnimator.setInterpolator(mLinearOutSlowInInterpolator); 147 mPeekAnimator.addListener(new AnimatorListenerAdapter() { 148 private boolean mCancelled; 149 150 @Override 151 public void onAnimationCancel(Animator animation) { 152 mCancelled = true; 153 } 154 155 @Override 156 public void onAnimationEnd(Animator animation) { 157 mPeekAnimator = null; 158 if (mCollapseAfterPeek && !mCancelled) { 159 postOnAnimation(new Runnable() { 160 @Override 161 public void run() { 162 collapse(false /* delayed */); 163 } 164 }); 165 } 166 mCollapseAfterPeek = false; 167 } 168 }); 169 notifyExpandingStarted(); 170 mPeekAnimator.start(); 171 mJustPeeked = true; 172 } 173 174 public PanelView(Context context, AttributeSet attrs) { 175 super(context, attrs); 176 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f); 177 mFastOutSlowInInterpolator = 178 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in); 179 mLinearOutSlowInInterpolator = 180 AnimationUtils.loadInterpolator(context, android.R.interpolator.linear_out_slow_in); 181 mBounceInterpolator = new BounceInterpolator(); 182 } 183 184 protected void loadDimens() { 185 final Resources res = getContext().getResources(); 186 final ViewConfiguration configuration = ViewConfiguration.get(getContext()); 187 mTouchSlop = configuration.getScaledTouchSlop(); 188 mHintDistance = res.getDimension(R.dimen.hint_move_distance); 189 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width); 190 mUnlockFalsingThreshold = res.getDimensionPixelSize(R.dimen.unlock_falsing_threshold); 191 } 192 193 private void trackMovement(MotionEvent event) { 194 // Add movement to velocity tracker using raw screen X and Y coordinates instead 195 // of window coordinates because the window frame may be moving at the same time. 196 float deltaX = event.getRawX() - event.getX(); 197 float deltaY = event.getRawY() - event.getY(); 198 event.offsetLocation(deltaX, deltaY); 199 if (mVelocityTracker != null) mVelocityTracker.addMovement(event); 200 event.offsetLocation(-deltaX, -deltaY); 201 } 202 203 public void setTouchDisabled(boolean disabled) { 204 mTouchDisabled = disabled; 205 } 206 207 @Override 208 public boolean onTouchEvent(MotionEvent event) { 209 if (mInstantExpanding || mTouchDisabled) { 210 return false; 211 } 212 213 /* 214 * We capture touch events here and update the expand height here in case according to 215 * the users fingers. This also handles multi-touch. 216 * 217 * If the user just clicks shortly, we give him a quick peek of the shade. 218 * 219 * Flinging is also enabled in order to open or close the shade. 220 */ 221 222 int pointerIndex = event.findPointerIndex(mTrackingPointer); 223 if (pointerIndex < 0) { 224 pointerIndex = 0; 225 mTrackingPointer = event.getPointerId(pointerIndex); 226 } 227 final float y = event.getY(pointerIndex); 228 final float x = event.getX(pointerIndex); 229 230 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { 231 mGestureWaitForTouchSlop = mExpandedHeight == 0f; 232 } 233 boolean waitForTouchSlop = hasConflictingGestures() || mGestureWaitForTouchSlop; 234 235 switch (event.getActionMasked()) { 236 case MotionEvent.ACTION_DOWN: 237 mInitialTouchY = y; 238 mInitialTouchX = x; 239 mInitialOffsetOnTouch = mExpandedHeight; 240 mTouchSlopExceeded = false; 241 mJustPeeked = false; 242 mPanelClosedOnDown = mExpandedHeight == 0.0f; 243 mHasLayoutedSinceDown = false; 244 mUpdateFlingOnLayout = false; 245 mPeekTouching = mPanelClosedOnDown; 246 mTouchAboveFalsingThreshold = false; 247 if (mVelocityTracker == null) { 248 initVelocityTracker(); 249 } 250 trackMovement(event); 251 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning) || 252 mPeekPending || mPeekAnimator != null) { 253 if (mHeightAnimator != null) { 254 mHeightAnimator.cancel(); // end any outstanding animations 255 } 256 cancelPeek(); 257 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning) 258 || mPeekPending || mPeekAnimator != null; 259 onTrackingStarted(); 260 } 261 if (mExpandedHeight == 0) { 262 schedulePeek(); 263 } 264 break; 265 266 case MotionEvent.ACTION_POINTER_UP: 267 final int upPointer = event.getPointerId(event.getActionIndex()); 268 if (mTrackingPointer == upPointer) { 269 // gesture is ongoing, find a new pointer to track 270 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 271 final float newY = event.getY(newIndex); 272 final float newX = event.getX(newIndex); 273 mTrackingPointer = event.getPointerId(newIndex); 274 mInitialOffsetOnTouch = mExpandedHeight; 275 mInitialTouchY = newY; 276 mInitialTouchX = newX; 277 } 278 break; 279 280 case MotionEvent.ACTION_MOVE: 281 float h = y - mInitialTouchY; 282 283 // If the panel was collapsed when touching, we only need to check for the 284 // y-component of the gesture, as we have no conflicting horizontal gesture. 285 if (Math.abs(h) > mTouchSlop 286 && (Math.abs(h) > Math.abs(x - mInitialTouchX) 287 || mInitialOffsetOnTouch == 0f)) { 288 mTouchSlopExceeded = true; 289 if (waitForTouchSlop && !mTracking) { 290 if (!mJustPeeked) { 291 mInitialOffsetOnTouch = mExpandedHeight; 292 mInitialTouchX = x; 293 mInitialTouchY = y; 294 h = 0; 295 } 296 if (mHeightAnimator != null) { 297 mHeightAnimator.cancel(); // end any outstanding animations 298 } 299 removeCallbacks(mPeekRunnable); 300 mPeekPending = false; 301 onTrackingStarted(); 302 } 303 } 304 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch); 305 if (newHeight > mPeekHeight) { 306 if (mPeekAnimator != null) { 307 mPeekAnimator.cancel(); 308 } 309 mJustPeeked = false; 310 } 311 if (-h >= getFalsingThreshold()) { 312 mTouchAboveFalsingThreshold = true; 313 } 314 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) { 315 setExpandedHeightInternal(newHeight); 316 } 317 318 trackMovement(event); 319 break; 320 321 case MotionEvent.ACTION_UP: 322 case MotionEvent.ACTION_CANCEL: 323 mTrackingPointer = -1; 324 trackMovement(event); 325 if ((mTracking && mTouchSlopExceeded) 326 || Math.abs(x - mInitialTouchX) > mTouchSlop 327 || Math.abs(y - mInitialTouchY) > mTouchSlop 328 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { 329 float vel = 0f; 330 float vectorVel = 0f; 331 if (mVelocityTracker != null) { 332 mVelocityTracker.computeCurrentVelocity(1000); 333 vel = mVelocityTracker.getYVelocity(); 334 vectorVel = (float) Math.hypot( 335 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); 336 } 337 boolean expand = flingExpands(vel, vectorVel); 338 onTrackingStopped(expand); 339 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold, 340 mStatusBar.isFalsingThresholdNeeded()); 341 fling(vel, expand); 342 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown; 343 if (mUpdateFlingOnLayout) { 344 mUpdateFlingVelocity = vel; 345 } 346 } else { 347 boolean expands = onEmptySpaceClick(mInitialTouchX); 348 onTrackingStopped(expands); 349 } 350 351 if (mVelocityTracker != null) { 352 mVelocityTracker.recycle(); 353 mVelocityTracker = null; 354 } 355 mPeekTouching = false; 356 break; 357 } 358 return !waitForTouchSlop || mTracking; 359 } 360 361 private int getFalsingThreshold() { 362 float factor = mStatusBar.isScreenOnComingFromTouch() ? 1.5f : 1.0f; 363 return (int) (mUnlockFalsingThreshold * factor); 364 } 365 366 protected abstract boolean hasConflictingGestures(); 367 368 protected void onTrackingStopped(boolean expand) { 369 mTracking = false; 370 mBar.onTrackingStopped(PanelView.this, expand); 371 } 372 373 protected void onTrackingStarted() { 374 mTracking = true; 375 mCollapseAfterPeek = false; 376 mBar.onTrackingStarted(PanelView.this); 377 notifyExpandingStarted(); 378 } 379 380 @Override 381 public boolean onInterceptTouchEvent(MotionEvent event) { 382 if (mInstantExpanding) { 383 return false; 384 } 385 386 /* 387 * If the user drags anywhere inside the panel we intercept it if he moves his finger 388 * upwards. This allows closing the shade from anywhere inside the panel. 389 * 390 * We only do this if the current content is scrolled to the bottom, 391 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture 392 * possible. 393 */ 394 int pointerIndex = event.findPointerIndex(mTrackingPointer); 395 if (pointerIndex < 0) { 396 pointerIndex = 0; 397 mTrackingPointer = event.getPointerId(pointerIndex); 398 } 399 final float x = event.getX(pointerIndex); 400 final float y = event.getY(pointerIndex); 401 boolean scrolledToBottom = isScrolledToBottom(); 402 403 switch (event.getActionMasked()) { 404 case MotionEvent.ACTION_DOWN: 405 mStatusBar.userActivity(); 406 if (mHeightAnimator != null && !mHintAnimationRunning || 407 mPeekPending || mPeekAnimator != null) { 408 if (mHeightAnimator != null) { 409 mHeightAnimator.cancel(); // end any outstanding animations 410 } 411 cancelPeek(); 412 mTouchSlopExceeded = true; 413 return true; 414 } 415 mInitialTouchY = y; 416 mInitialTouchX = x; 417 mTouchSlopExceeded = false; 418 mJustPeeked = false; 419 mPanelClosedOnDown = mExpandedHeight == 0.0f; 420 mHasLayoutedSinceDown = false; 421 mUpdateFlingOnLayout = false; 422 mTouchAboveFalsingThreshold = false; 423 initVelocityTracker(); 424 trackMovement(event); 425 break; 426 case MotionEvent.ACTION_POINTER_UP: 427 final int upPointer = event.getPointerId(event.getActionIndex()); 428 if (mTrackingPointer == upPointer) { 429 // gesture is ongoing, find a new pointer to track 430 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 431 mTrackingPointer = event.getPointerId(newIndex); 432 mInitialTouchX = event.getX(newIndex); 433 mInitialTouchY = event.getY(newIndex); 434 } 435 break; 436 437 case MotionEvent.ACTION_MOVE: 438 final float h = y - mInitialTouchY; 439 trackMovement(event); 440 if (scrolledToBottom) { 441 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { 442 if (mHeightAnimator != null) { 443 mHeightAnimator.cancel(); 444 } 445 mInitialOffsetOnTouch = mExpandedHeight; 446 mInitialTouchY = y; 447 mInitialTouchX = x; 448 mTracking = true; 449 mTouchSlopExceeded = true; 450 onTrackingStarted(); 451 return true; 452 } 453 } 454 break; 455 case MotionEvent.ACTION_CANCEL: 456 case MotionEvent.ACTION_UP: 457 break; 458 } 459 return false; 460 } 461 462 private void initVelocityTracker() { 463 if (mVelocityTracker != null) { 464 mVelocityTracker.recycle(); 465 } 466 mVelocityTracker = VelocityTrackerFactory.obtain(getContext()); 467 } 468 469 protected boolean isScrolledToBottom() { 470 return true; 471 } 472 473 protected float getContentHeight() { 474 return mExpandedHeight; 475 } 476 477 @Override 478 protected void onFinishInflate() { 479 super.onFinishInflate(); 480 loadDimens(); 481 } 482 483 @Override 484 protected void onConfigurationChanged(Configuration newConfig) { 485 super.onConfigurationChanged(newConfig); 486 loadDimens(); 487 } 488 489 /** 490 * @param vel the current vertical velocity of the motion 491 * @param vectorVel the length of the vectorial velocity 492 * @return whether a fling should expands the panel; contracts otherwise 493 */ 494 protected boolean flingExpands(float vel, float vectorVel) { 495 if (isBelowFalsingThreshold()) { 496 return true; 497 } 498 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) { 499 return getExpandedFraction() > 0.5f; 500 } else { 501 return vel > 0; 502 } 503 } 504 505 private boolean isBelowFalsingThreshold() { 506 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded(); 507 } 508 509 protected void fling(float vel, boolean expand) { 510 cancelPeek(); 511 float target = expand ? getMaxPanelHeight() : 0.0f; 512 513 // Hack to make the expand transition look nice when clear all button is visible - we make 514 // the animation only to the last notification, and then jump to the maximum panel height so 515 // clear all just fades in and the decelerating motion is towards the last notification. 516 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible() 517 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight() 518 && !isClearAllVisible(); 519 if (clearAllExpandHack) { 520 target = getMaxPanelHeight() - getClearAllHeight(); 521 } 522 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) { 523 notifyExpandingFinished(); 524 return; 525 } 526 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f; 527 ValueAnimator animator = createHeightAnimator(target); 528 if (expand) { 529 boolean belowFalsingThreshold = isBelowFalsingThreshold(); 530 if (belowFalsingThreshold) { 531 vel = 0; 532 } 533 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight()); 534 if (belowFalsingThreshold) { 535 animator.setDuration(350); 536 } 537 } else { 538 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel, 539 getHeight()); 540 541 // Make it shorter if we run a canned animation 542 if (vel == 0) { 543 animator.setDuration((long) 544 (animator.getDuration() * getCannedFlingDurationFactor())); 545 } 546 } 547 animator.addListener(new AnimatorListenerAdapter() { 548 private boolean mCancelled; 549 550 @Override 551 public void onAnimationCancel(Animator animation) { 552 mCancelled = true; 553 } 554 555 @Override 556 public void onAnimationEnd(Animator animation) { 557 if (clearAllExpandHack && !mCancelled) { 558 setExpandedHeightInternal(getMaxPanelHeight()); 559 } 560 mHeightAnimator = null; 561 if (!mCancelled) { 562 notifyExpandingFinished(); 563 } 564 } 565 }); 566 mHeightAnimator = animator; 567 animator.start(); 568 } 569 570 @Override 571 protected void onAttachedToWindow() { 572 super.onAttachedToWindow(); 573 mViewName = getResources().getResourceName(getId()); 574 } 575 576 public String getName() { 577 return mViewName; 578 } 579 580 public void setExpandedHeight(float height) { 581 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 582 setExpandedHeightInternal(height + getOverExpansionPixels()); 583 } 584 585 @Override 586 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 587 super.onLayout(changed, left, top, right, bottom); 588 requestPanelHeightUpdate(); 589 mHasLayoutedSinceDown = true; 590 if (mUpdateFlingOnLayout) { 591 abortAnimations(); 592 fling(mUpdateFlingVelocity, true); 593 mUpdateFlingOnLayout = false; 594 } 595 } 596 597 protected void requestPanelHeightUpdate() { 598 float currentMaxPanelHeight = getMaxPanelHeight(); 599 600 // If the user isn't actively poking us, let's update the height 601 if ((!mTracking || isTrackingBlocked()) 602 && mHeightAnimator == null 603 && mExpandedHeight > 0 604 && currentMaxPanelHeight != mExpandedHeight 605 && !mPeekPending 606 && mPeekAnimator == null 607 && !mPeekTouching) { 608 setExpandedHeight(currentMaxPanelHeight); 609 } 610 } 611 612 public void setExpandedHeightInternal(float h) { 613 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount(); 614 if (mHeightAnimator == null) { 615 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion); 616 if (getOverExpansionPixels() != overExpansionPixels && mTracking) { 617 setOverExpansion(overExpansionPixels, true /* isPixels */); 618 } 619 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount(); 620 } else { 621 mExpandedHeight = h; 622 if (mOverExpandedBeforeFling) { 623 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */); 624 } 625 } 626 627 mExpandedHeight = Math.max(0, mExpandedHeight); 628 onHeightUpdated(mExpandedHeight); 629 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0 630 ? 0 631 : mExpandedHeight / fhWithoutOverExpansion); 632 notifyBarPanelExpansionChanged(); 633 } 634 635 /** 636 * @return true if the panel tracking should be temporarily blocked; this is used when a 637 * conflicting gesture (opening QS) is happening 638 */ 639 protected abstract boolean isTrackingBlocked(); 640 641 protected abstract void setOverExpansion(float overExpansion, boolean isPixels); 642 643 protected abstract void onHeightUpdated(float expandedHeight); 644 645 protected abstract float getOverExpansionAmount(); 646 647 protected abstract float getOverExpansionPixels(); 648 649 /** 650 * This returns the maximum height of the panel. Children should override this if their 651 * desired height is not the full height. 652 * 653 * @return the default implementation simply returns the maximum height. 654 */ 655 protected abstract int getMaxPanelHeight(); 656 657 public void setExpandedFraction(float frac) { 658 setExpandedHeight(getMaxPanelHeight() * frac); 659 } 660 661 public float getExpandedHeight() { 662 return mExpandedHeight; 663 } 664 665 public float getExpandedFraction() { 666 return mExpandedFraction; 667 } 668 669 public boolean isFullyExpanded() { 670 return mExpandedHeight >= getMaxPanelHeight(); 671 } 672 673 public boolean isFullyCollapsed() { 674 return mExpandedHeight <= 0; 675 } 676 677 public boolean isCollapsing() { 678 return mClosing; 679 } 680 681 public boolean isTracking() { 682 return mTracking; 683 } 684 685 public void setBar(PanelBar panelBar) { 686 mBar = panelBar; 687 } 688 689 public void collapse(boolean delayed) { 690 if (DEBUG) logf("collapse: " + this); 691 if (mPeekPending || mPeekAnimator != null) { 692 mCollapseAfterPeek = true; 693 if (mPeekPending) { 694 695 // We know that the whole gesture is just a peek triggered by a simple click, so 696 // better start it now. 697 removeCallbacks(mPeekRunnable); 698 mPeekRunnable.run(); 699 } 700 } else if (!isFullyCollapsed() && !mTracking && !mClosing) { 701 if (mHeightAnimator != null) { 702 mHeightAnimator.cancel(); 703 } 704 mClosing = true; 705 notifyExpandingStarted(); 706 if (delayed) { 707 postDelayed(mFlingCollapseRunnable, 120); 708 } else { 709 fling(0, false /* expand */); 710 } 711 } 712 } 713 714 private final Runnable mFlingCollapseRunnable = new Runnable() { 715 @Override 716 public void run() { 717 fling(0, false /* expand */); 718 } 719 }; 720 721 public void expand() { 722 if (DEBUG) logf("expand: " + this); 723 if (isFullyCollapsed()) { 724 mBar.startOpeningPanel(this); 725 notifyExpandingStarted(); 726 fling(0, true /* expand */); 727 } else if (DEBUG) { 728 if (DEBUG) logf("skipping expansion: is expanded"); 729 } 730 } 731 732 public void cancelPeek() { 733 if (mPeekAnimator != null) { 734 mPeekAnimator.cancel(); 735 } 736 removeCallbacks(mPeekRunnable); 737 mPeekPending = false; 738 739 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also 740 // notify mBar that we might have closed ourselves. 741 notifyBarPanelExpansionChanged(); 742 } 743 744 public void instantExpand() { 745 mInstantExpanding = true; 746 mUpdateFlingOnLayout = false; 747 abortAnimations(); 748 cancelPeek(); 749 if (mTracking) { 750 onTrackingStopped(true /* expands */); // The panel is expanded after this call. 751 } 752 if (mExpanding) { 753 notifyExpandingFinished(); 754 } 755 setVisibility(VISIBLE); 756 757 // Wait for window manager to pickup the change, so we know the maximum height of the panel 758 // then. 759 getViewTreeObserver().addOnGlobalLayoutListener( 760 new ViewTreeObserver.OnGlobalLayoutListener() { 761 @Override 762 public void onGlobalLayout() { 763 if (mStatusBar.getStatusBarWindow().getHeight() 764 != mStatusBar.getStatusBarHeight()) { 765 getViewTreeObserver().removeOnGlobalLayoutListener(this); 766 setExpandedFraction(1f); 767 mInstantExpanding = false; 768 } 769 } 770 }); 771 772 // Make sure a layout really happens. 773 requestLayout(); 774 } 775 776 private void abortAnimations() { 777 cancelPeek(); 778 if (mHeightAnimator != null) { 779 mHeightAnimator.cancel(); 780 } 781 removeCallbacks(mPostCollapseRunnable); 782 removeCallbacks(mFlingCollapseRunnable); 783 } 784 785 protected void startUnlockHintAnimation() { 786 787 // We don't need to hint the user if an animation is already running or the user is changing 788 // the expansion. 789 if (mHeightAnimator != null || mTracking) { 790 return; 791 } 792 cancelPeek(); 793 notifyExpandingStarted(); 794 startUnlockHintAnimationPhase1(new Runnable() { 795 @Override 796 public void run() { 797 notifyExpandingFinished(); 798 mStatusBar.onHintFinished(); 799 mHintAnimationRunning = false; 800 } 801 }); 802 mStatusBar.onUnlockHintStarted(); 803 mHintAnimationRunning = true; 804 } 805 806 /** 807 * Phase 1: Move everything upwards. 808 */ 809 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) { 810 float target = Math.max(0, getMaxPanelHeight() - mHintDistance); 811 ValueAnimator animator = createHeightAnimator(target); 812 animator.setDuration(250); 813 animator.setInterpolator(mFastOutSlowInInterpolator); 814 animator.addListener(new AnimatorListenerAdapter() { 815 private boolean mCancelled; 816 817 @Override 818 public void onAnimationCancel(Animator animation) { 819 mCancelled = true; 820 } 821 822 @Override 823 public void onAnimationEnd(Animator animation) { 824 if (mCancelled) { 825 mHeightAnimator = null; 826 onAnimationFinished.run(); 827 } else { 828 startUnlockHintAnimationPhase2(onAnimationFinished); 829 } 830 } 831 }); 832 animator.start(); 833 mHeightAnimator = animator; 834 mOriginalIndicationY = mKeyguardBottomArea.getIndicationView().getY(); 835 mKeyguardBottomArea.getIndicationView().animate() 836 .y(mOriginalIndicationY - mHintDistance) 837 .setDuration(250) 838 .setInterpolator(mFastOutSlowInInterpolator) 839 .withEndAction(new Runnable() { 840 @Override 841 public void run() { 842 mKeyguardBottomArea.getIndicationView().animate() 843 .y(mOriginalIndicationY) 844 .setDuration(450) 845 .setInterpolator(mBounceInterpolator) 846 .start(); 847 } 848 }) 849 .start(); 850 } 851 852 /** 853 * Phase 2: Bounce down. 854 */ 855 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) { 856 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight()); 857 animator.setDuration(450); 858 animator.setInterpolator(mBounceInterpolator); 859 animator.addListener(new AnimatorListenerAdapter() { 860 @Override 861 public void onAnimationEnd(Animator animation) { 862 mHeightAnimator = null; 863 onAnimationFinished.run(); 864 } 865 }); 866 animator.start(); 867 mHeightAnimator = animator; 868 } 869 870 private ValueAnimator createHeightAnimator(float targetHeight) { 871 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight); 872 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 873 @Override 874 public void onAnimationUpdate(ValueAnimator animation) { 875 setExpandedHeightInternal((Float) animation.getAnimatedValue()); 876 } 877 }); 878 return animator; 879 } 880 881 private void notifyBarPanelExpansionChanged() { 882 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending 883 || mPeekAnimator != null); 884 } 885 886 /** 887 * Gets called when the user performs a click anywhere in the empty area of the panel. 888 * 889 * @return whether the panel will be expanded after the action performed by this method 890 */ 891 private boolean onEmptySpaceClick(float x) { 892 if (mHintAnimationRunning) { 893 return true; 894 } 895 if (x < mEdgeTapAreaWidth 896 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 897 onEdgeClicked(false /* right */); 898 return true; 899 } else if (x > getWidth() - mEdgeTapAreaWidth 900 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 901 onEdgeClicked(true /* right */); 902 return true; 903 } else { 904 return onMiddleClicked(); 905 } 906 } 907 908 private final Runnable mPostCollapseRunnable = new Runnable() { 909 @Override 910 public void run() { 911 collapse(false /* delayed */); 912 } 913 }; 914 private boolean onMiddleClicked() { 915 switch (mStatusBar.getBarState()) { 916 case StatusBarState.KEYGUARD: 917 if (!isDozing()) { 918 startUnlockHintAnimation(); 919 } 920 return true; 921 case StatusBarState.SHADE_LOCKED: 922 mStatusBar.goToKeyguard(); 923 return true; 924 case StatusBarState.SHADE: 925 926 // This gets called in the middle of the touch handling, where the state is still 927 // that we are tracking the panel. Collapse the panel after this is done. 928 post(mPostCollapseRunnable); 929 return false; 930 default: 931 return true; 932 } 933 } 934 935 protected abstract void onEdgeClicked(boolean right); 936 937 protected abstract boolean isDozing(); 938 939 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 940 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 941 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s" 942 + "]", 943 this.getClass().getSimpleName(), 944 getExpandedHeight(), 945 getMaxPanelHeight(), 946 mClosing?"T":"f", 947 mTracking?"T":"f", 948 mJustPeeked?"T":"f", 949 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 950 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""), 951 mTouchDisabled?"T":"f" 952 )); 953 } 954 955 public abstract void resetViews(); 956 957 protected abstract float getPeekHeight(); 958 959 protected abstract float getCannedFlingDurationFactor(); 960 961 /** 962 * @return whether "Clear all" button will be visible when the panel is fully expanded 963 */ 964 protected abstract boolean fullyExpandedClearAllVisible(); 965 966 protected abstract boolean isClearAllVisible(); 967 968 /** 969 * @return the height of the clear all button, in pixels 970 */ 971 protected abstract int getClearAllHeight(); 972} 973