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