PanelView.java revision ab1dc954b2b97b59f868c96921f2daabc3336034
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 private boolean mTouchStartedInEmptyArea; 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 endClosing(); 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 cancelHeightAnimator(); 254 cancelPeek(); 255 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning) 256 || mPeekPending || mPeekAnimator != null; 257 onTrackingStarted(); 258 } 259 if (mExpandedHeight == 0) { 260 schedulePeek(); 261 } 262 break; 263 264 case MotionEvent.ACTION_POINTER_UP: 265 final int upPointer = event.getPointerId(event.getActionIndex()); 266 if (mTrackingPointer == upPointer) { 267 // gesture is ongoing, find a new pointer to track 268 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 269 final float newY = event.getY(newIndex); 270 final float newX = event.getX(newIndex); 271 mTrackingPointer = event.getPointerId(newIndex); 272 mInitialOffsetOnTouch = mExpandedHeight; 273 mInitialTouchY = newY; 274 mInitialTouchX = newX; 275 } 276 break; 277 278 case MotionEvent.ACTION_MOVE: 279 float h = y - mInitialTouchY; 280 281 // If the panel was collapsed when touching, we only need to check for the 282 // y-component of the gesture, as we have no conflicting horizontal gesture. 283 if (Math.abs(h) > mTouchSlop 284 && (Math.abs(h) > Math.abs(x - mInitialTouchX) 285 || mInitialOffsetOnTouch == 0f)) { 286 mTouchSlopExceeded = true; 287 if (waitForTouchSlop && !mTracking) { 288 if (!mJustPeeked && mInitialOffsetOnTouch != 0f) { 289 mInitialOffsetOnTouch = mExpandedHeight; 290 mInitialTouchX = x; 291 mInitialTouchY = y; 292 h = 0; 293 } 294 cancelHeightAnimator(); 295 removeCallbacks(mPeekRunnable); 296 mPeekPending = false; 297 onTrackingStarted(); 298 } 299 } 300 final float newHeight = Math.max(0, h + mInitialOffsetOnTouch); 301 if (newHeight > mPeekHeight) { 302 if (mPeekAnimator != null) { 303 mPeekAnimator.cancel(); 304 } 305 mJustPeeked = false; 306 } 307 if (-h >= getFalsingThreshold()) { 308 mTouchAboveFalsingThreshold = true; 309 } 310 if (!mJustPeeked && (!waitForTouchSlop || mTracking) && !isTrackingBlocked()) { 311 setExpandedHeightInternal(newHeight); 312 } 313 314 trackMovement(event); 315 break; 316 317 case MotionEvent.ACTION_UP: 318 case MotionEvent.ACTION_CANCEL: 319 mTrackingPointer = -1; 320 trackMovement(event); 321 if ((mTracking && mTouchSlopExceeded) 322 || Math.abs(x - mInitialTouchX) > mTouchSlop 323 || Math.abs(y - mInitialTouchY) > mTouchSlop 324 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { 325 float vel = 0f; 326 float vectorVel = 0f; 327 if (mVelocityTracker != null) { 328 mVelocityTracker.computeCurrentVelocity(1000); 329 vel = mVelocityTracker.getYVelocity(); 330 vectorVel = (float) Math.hypot( 331 mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); 332 } 333 boolean expand = flingExpands(vel, vectorVel); 334 onTrackingStopped(expand); 335 DozeLog.traceFling(expand, mTouchAboveFalsingThreshold, 336 mStatusBar.isFalsingThresholdNeeded(), 337 mStatusBar.isScreenOnComingFromTouch()); 338 fling(vel, expand); 339 mUpdateFlingOnLayout = expand && mPanelClosedOnDown && !mHasLayoutedSinceDown; 340 if (mUpdateFlingOnLayout) { 341 mUpdateFlingVelocity = vel; 342 } 343 } else { 344 boolean expands = onEmptySpaceClick(mInitialTouchX); 345 onTrackingStopped(expands); 346 } 347 348 if (mVelocityTracker != null) { 349 mVelocityTracker.recycle(); 350 mVelocityTracker = null; 351 } 352 mPeekTouching = false; 353 break; 354 } 355 return !waitForTouchSlop || mTracking; 356 } 357 358 private int getFalsingThreshold() { 359 float factor = mStatusBar.isScreenOnComingFromTouch() ? 1.5f : 1.0f; 360 return (int) (mUnlockFalsingThreshold * factor); 361 } 362 363 protected abstract boolean hasConflictingGestures(); 364 365 protected void onTrackingStopped(boolean expand) { 366 mTracking = false; 367 mBar.onTrackingStopped(PanelView.this, expand); 368 } 369 370 protected void onTrackingStarted() { 371 endClosing(); 372 mTracking = true; 373 mCollapseAfterPeek = false; 374 mBar.onTrackingStarted(PanelView.this); 375 notifyExpandingStarted(); 376 } 377 378 @Override 379 public boolean onInterceptTouchEvent(MotionEvent event) { 380 if (mInstantExpanding) { 381 return false; 382 } 383 384 /* 385 * If the user drags anywhere inside the panel we intercept it if he moves his finger 386 * upwards. This allows closing the shade from anywhere inside the panel. 387 * 388 * We only do this if the current content is scrolled to the bottom, 389 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture 390 * possible. 391 */ 392 int pointerIndex = event.findPointerIndex(mTrackingPointer); 393 if (pointerIndex < 0) { 394 pointerIndex = 0; 395 mTrackingPointer = event.getPointerId(pointerIndex); 396 } 397 final float x = event.getX(pointerIndex); 398 final float y = event.getY(pointerIndex); 399 boolean scrolledToBottom = isScrolledToBottom(); 400 401 switch (event.getActionMasked()) { 402 case MotionEvent.ACTION_DOWN: 403 mStatusBar.userActivity(); 404 if (mHeightAnimator != null && !mHintAnimationRunning || 405 mPeekPending || mPeekAnimator != null) { 406 cancelHeightAnimator(); 407 cancelPeek(); 408 mTouchSlopExceeded = true; 409 return true; 410 } 411 mInitialTouchY = y; 412 mInitialTouchX = x; 413 mTouchStartedInEmptyArea = !isInContentBounds(x, y); 414 mTouchSlopExceeded = false; 415 mJustPeeked = false; 416 mPanelClosedOnDown = mExpandedHeight == 0.0f; 417 mHasLayoutedSinceDown = false; 418 mUpdateFlingOnLayout = false; 419 mTouchAboveFalsingThreshold = false; 420 initVelocityTracker(); 421 trackMovement(event); 422 break; 423 case MotionEvent.ACTION_POINTER_UP: 424 final int upPointer = event.getPointerId(event.getActionIndex()); 425 if (mTrackingPointer == upPointer) { 426 // gesture is ongoing, find a new pointer to track 427 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 428 mTrackingPointer = event.getPointerId(newIndex); 429 mInitialTouchX = event.getX(newIndex); 430 mInitialTouchY = event.getY(newIndex); 431 } 432 break; 433 434 case MotionEvent.ACTION_MOVE: 435 final float h = y - mInitialTouchY; 436 trackMovement(event); 437 if (scrolledToBottom || mTouchStartedInEmptyArea) { 438 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { 439 cancelHeightAnimator(); 440 mInitialOffsetOnTouch = mExpandedHeight; 441 mInitialTouchY = y; 442 mInitialTouchX = x; 443 mTracking = true; 444 mTouchSlopExceeded = true; 445 onTrackingStarted(); 446 return true; 447 } 448 } 449 break; 450 case MotionEvent.ACTION_CANCEL: 451 case MotionEvent.ACTION_UP: 452 break; 453 } 454 return false; 455 } 456 457 /** 458 * @return Whether a pair of coordinates are inside the visible view content bounds. 459 */ 460 protected abstract boolean isInContentBounds(float x, float y); 461 462 private void cancelHeightAnimator() { 463 if (mHeightAnimator != null) { 464 mHeightAnimator.cancel(); 465 } 466 endClosing(); 467 } 468 469 private void endClosing() { 470 if (mClosing) { 471 mClosing = false; 472 onClosingFinished(); 473 } 474 } 475 476 private void initVelocityTracker() { 477 if (mVelocityTracker != null) { 478 mVelocityTracker.recycle(); 479 } 480 mVelocityTracker = VelocityTrackerFactory.obtain(getContext()); 481 } 482 483 protected boolean isScrolledToBottom() { 484 return true; 485 } 486 487 protected float getContentHeight() { 488 return mExpandedHeight; 489 } 490 491 @Override 492 protected void onFinishInflate() { 493 super.onFinishInflate(); 494 loadDimens(); 495 } 496 497 @Override 498 protected void onConfigurationChanged(Configuration newConfig) { 499 super.onConfigurationChanged(newConfig); 500 loadDimens(); 501 } 502 503 /** 504 * @param vel the current vertical velocity of the motion 505 * @param vectorVel the length of the vectorial velocity 506 * @return whether a fling should expands the panel; contracts otherwise 507 */ 508 protected boolean flingExpands(float vel, float vectorVel) { 509 if (isBelowFalsingThreshold()) { 510 return true; 511 } 512 if (Math.abs(vectorVel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) { 513 return getExpandedFraction() > 0.5f; 514 } else { 515 return vel > 0; 516 } 517 } 518 519 private boolean isBelowFalsingThreshold() { 520 return !mTouchAboveFalsingThreshold && mStatusBar.isFalsingThresholdNeeded(); 521 } 522 523 protected void fling(float vel, boolean expand) { 524 cancelPeek(); 525 float target = expand ? getMaxPanelHeight() : 0.0f; 526 527 // Hack to make the expand transition look nice when clear all button is visible - we make 528 // the animation only to the last notification, and then jump to the maximum panel height so 529 // clear all just fades in and the decelerating motion is towards the last notification. 530 final boolean clearAllExpandHack = expand && fullyExpandedClearAllVisible() 531 && mExpandedHeight < getMaxPanelHeight() - getClearAllHeight() 532 && !isClearAllVisible(); 533 if (clearAllExpandHack) { 534 target = getMaxPanelHeight() - getClearAllHeight(); 535 } 536 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) { 537 notifyExpandingFinished(); 538 return; 539 } 540 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f; 541 ValueAnimator animator = createHeightAnimator(target); 542 if (expand) { 543 boolean belowFalsingThreshold = isBelowFalsingThreshold(); 544 if (belowFalsingThreshold) { 545 vel = 0; 546 } 547 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight()); 548 if (belowFalsingThreshold) { 549 animator.setDuration(350); 550 } 551 } else { 552 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel, 553 getHeight()); 554 555 // Make it shorter if we run a canned animation 556 if (vel == 0) { 557 animator.setDuration((long) 558 (animator.getDuration() * getCannedFlingDurationFactor())); 559 } 560 } 561 animator.addListener(new AnimatorListenerAdapter() { 562 private boolean mCancelled; 563 564 @Override 565 public void onAnimationCancel(Animator animation) { 566 mCancelled = true; 567 } 568 569 @Override 570 public void onAnimationEnd(Animator animation) { 571 if (clearAllExpandHack && !mCancelled) { 572 setExpandedHeightInternal(getMaxPanelHeight()); 573 } 574 mHeightAnimator = null; 575 if (!mCancelled) { 576 notifyExpandingFinished(); 577 } 578 } 579 }); 580 mHeightAnimator = animator; 581 animator.start(); 582 } 583 584 @Override 585 protected void onAttachedToWindow() { 586 super.onAttachedToWindow(); 587 mViewName = getResources().getResourceName(getId()); 588 } 589 590 public String getName() { 591 return mViewName; 592 } 593 594 public void setExpandedHeight(float height) { 595 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 596 setExpandedHeightInternal(height + getOverExpansionPixels()); 597 } 598 599 @Override 600 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 601 super.onLayout(changed, left, top, right, bottom); 602 requestPanelHeightUpdate(); 603 mHasLayoutedSinceDown = true; 604 if (mUpdateFlingOnLayout) { 605 abortAnimations(); 606 fling(mUpdateFlingVelocity, true); 607 mUpdateFlingOnLayout = false; 608 } 609 } 610 611 protected void requestPanelHeightUpdate() { 612 float currentMaxPanelHeight = getMaxPanelHeight(); 613 614 // If the user isn't actively poking us, let's update the height 615 if ((!mTracking || isTrackingBlocked()) 616 && mHeightAnimator == null 617 && mExpandedHeight > 0 618 && currentMaxPanelHeight != mExpandedHeight 619 && !mPeekPending 620 && mPeekAnimator == null 621 && !mPeekTouching) { 622 setExpandedHeight(currentMaxPanelHeight); 623 } 624 } 625 626 public void setExpandedHeightInternal(float h) { 627 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount(); 628 if (mHeightAnimator == null) { 629 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion); 630 if (getOverExpansionPixels() != overExpansionPixels && mTracking) { 631 setOverExpansion(overExpansionPixels, true /* isPixels */); 632 } 633 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount(); 634 } else { 635 mExpandedHeight = h; 636 if (mOverExpandedBeforeFling) { 637 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */); 638 } 639 } 640 641 mExpandedHeight = Math.max(0, mExpandedHeight); 642 onHeightUpdated(mExpandedHeight); 643 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0 644 ? 0 645 : mExpandedHeight / fhWithoutOverExpansion); 646 notifyBarPanelExpansionChanged(); 647 } 648 649 /** 650 * @return true if the panel tracking should be temporarily blocked; this is used when a 651 * conflicting gesture (opening QS) is happening 652 */ 653 protected abstract boolean isTrackingBlocked(); 654 655 protected abstract void setOverExpansion(float overExpansion, boolean isPixels); 656 657 protected abstract void onHeightUpdated(float expandedHeight); 658 659 protected abstract float getOverExpansionAmount(); 660 661 protected abstract float getOverExpansionPixels(); 662 663 /** 664 * This returns the maximum height of the panel. Children should override this if their 665 * desired height is not the full height. 666 * 667 * @return the default implementation simply returns the maximum height. 668 */ 669 protected abstract int getMaxPanelHeight(); 670 671 public void setExpandedFraction(float frac) { 672 setExpandedHeight(getMaxPanelHeight() * frac); 673 } 674 675 public float getExpandedHeight() { 676 return mExpandedHeight; 677 } 678 679 public float getExpandedFraction() { 680 return mExpandedFraction; 681 } 682 683 public boolean isFullyExpanded() { 684 return mExpandedHeight >= getMaxPanelHeight(); 685 } 686 687 public boolean isFullyCollapsed() { 688 return mExpandedHeight <= 0; 689 } 690 691 public boolean isCollapsing() { 692 return mClosing; 693 } 694 695 public boolean isTracking() { 696 return mTracking; 697 } 698 699 public void setBar(PanelBar panelBar) { 700 mBar = panelBar; 701 } 702 703 public void collapse(boolean delayed) { 704 if (DEBUG) logf("collapse: " + this); 705 if (mPeekPending || mPeekAnimator != null) { 706 mCollapseAfterPeek = true; 707 if (mPeekPending) { 708 709 // We know that the whole gesture is just a peek triggered by a simple click, so 710 // better start it now. 711 removeCallbacks(mPeekRunnable); 712 mPeekRunnable.run(); 713 } 714 } else if (!isFullyCollapsed() && !mTracking && !mClosing) { 715 cancelHeightAnimator(); 716 mClosing = true; 717 notifyExpandingStarted(); 718 if (delayed) { 719 postDelayed(mFlingCollapseRunnable, 120); 720 } else { 721 fling(0, false /* expand */); 722 } 723 } 724 } 725 726 private final Runnable mFlingCollapseRunnable = new Runnable() { 727 @Override 728 public void run() { 729 fling(0, false /* expand */); 730 } 731 }; 732 733 public void expand() { 734 if (DEBUG) logf("expand: " + this); 735 if (isFullyCollapsed()) { 736 mBar.startOpeningPanel(this); 737 notifyExpandingStarted(); 738 fling(0, true /* expand */); 739 } else if (DEBUG) { 740 if (DEBUG) logf("skipping expansion: is expanded"); 741 } 742 } 743 744 public void cancelPeek() { 745 if (mPeekAnimator != null) { 746 mPeekAnimator.cancel(); 747 } 748 removeCallbacks(mPeekRunnable); 749 mPeekPending = false; 750 751 // When peeking, we already tell mBar that we expanded ourselves. Make sure that we also 752 // notify mBar that we might have closed ourselves. 753 notifyBarPanelExpansionChanged(); 754 } 755 756 public void instantExpand() { 757 mInstantExpanding = true; 758 mUpdateFlingOnLayout = false; 759 abortAnimations(); 760 cancelPeek(); 761 if (mTracking) { 762 onTrackingStopped(true /* expands */); // The panel is expanded after this call. 763 } 764 if (mExpanding) { 765 notifyExpandingFinished(); 766 } 767 setVisibility(VISIBLE); 768 769 // Wait for window manager to pickup the change, so we know the maximum height of the panel 770 // then. 771 getViewTreeObserver().addOnGlobalLayoutListener( 772 new ViewTreeObserver.OnGlobalLayoutListener() { 773 @Override 774 public void onGlobalLayout() { 775 if (mStatusBar.getStatusBarWindow().getHeight() 776 != mStatusBar.getStatusBarHeight()) { 777 getViewTreeObserver().removeOnGlobalLayoutListener(this); 778 setExpandedFraction(1f); 779 mInstantExpanding = false; 780 } 781 } 782 }); 783 784 // Make sure a layout really happens. 785 requestLayout(); 786 } 787 788 public void instantCollapse() { 789 abortAnimations(); 790 setExpandedFraction(0f); 791 if (mExpanding) { 792 notifyExpandingFinished(); 793 } 794 } 795 796 private void abortAnimations() { 797 cancelPeek(); 798 cancelHeightAnimator(); 799 removeCallbacks(mPostCollapseRunnable); 800 removeCallbacks(mFlingCollapseRunnable); 801 } 802 803 protected void onClosingFinished() { 804 mBar.onClosingFinished(); 805 } 806 807 808 protected void startUnlockHintAnimation() { 809 810 // We don't need to hint the user if an animation is already running or the user is changing 811 // the expansion. 812 if (mHeightAnimator != null || mTracking) { 813 return; 814 } 815 cancelPeek(); 816 notifyExpandingStarted(); 817 startUnlockHintAnimationPhase1(new Runnable() { 818 @Override 819 public void run() { 820 notifyExpandingFinished(); 821 mStatusBar.onHintFinished(); 822 mHintAnimationRunning = false; 823 } 824 }); 825 mStatusBar.onUnlockHintStarted(); 826 mHintAnimationRunning = true; 827 } 828 829 /** 830 * Phase 1: Move everything upwards. 831 */ 832 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) { 833 float target = Math.max(0, getMaxPanelHeight() - mHintDistance); 834 ValueAnimator animator = createHeightAnimator(target); 835 animator.setDuration(250); 836 animator.setInterpolator(mFastOutSlowInInterpolator); 837 animator.addListener(new AnimatorListenerAdapter() { 838 private boolean mCancelled; 839 840 @Override 841 public void onAnimationCancel(Animator animation) { 842 mCancelled = true; 843 } 844 845 @Override 846 public void onAnimationEnd(Animator animation) { 847 if (mCancelled) { 848 mHeightAnimator = null; 849 onAnimationFinished.run(); 850 } else { 851 startUnlockHintAnimationPhase2(onAnimationFinished); 852 } 853 } 854 }); 855 animator.start(); 856 mHeightAnimator = animator; 857 mKeyguardBottomArea.getIndicationView().animate() 858 .translationY(-mHintDistance) 859 .setDuration(250) 860 .setInterpolator(mFastOutSlowInInterpolator) 861 .withEndAction(new Runnable() { 862 @Override 863 public void run() { 864 mKeyguardBottomArea.getIndicationView().animate() 865 .translationY(0) 866 .setDuration(450) 867 .setInterpolator(mBounceInterpolator) 868 .start(); 869 } 870 }) 871 .start(); 872 } 873 874 /** 875 * Phase 2: Bounce down. 876 */ 877 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) { 878 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight()); 879 animator.setDuration(450); 880 animator.setInterpolator(mBounceInterpolator); 881 animator.addListener(new AnimatorListenerAdapter() { 882 @Override 883 public void onAnimationEnd(Animator animation) { 884 mHeightAnimator = null; 885 onAnimationFinished.run(); 886 } 887 }); 888 animator.start(); 889 mHeightAnimator = animator; 890 } 891 892 private ValueAnimator createHeightAnimator(float targetHeight) { 893 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight); 894 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 895 @Override 896 public void onAnimationUpdate(ValueAnimator animation) { 897 setExpandedHeightInternal((Float) animation.getAnimatedValue()); 898 } 899 }); 900 return animator; 901 } 902 903 private void notifyBarPanelExpansionChanged() { 904 mBar.panelExpansionChanged(this, mExpandedFraction, mExpandedFraction > 0f || mPeekPending 905 || mPeekAnimator != null); 906 } 907 908 /** 909 * Gets called when the user performs a click anywhere in the empty area of the panel. 910 * 911 * @return whether the panel will be expanded after the action performed by this method 912 */ 913 protected boolean onEmptySpaceClick(float x) { 914 if (mHintAnimationRunning) { 915 return true; 916 } 917 if (x < mEdgeTapAreaWidth 918 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 919 onEdgeClicked(false /* right */); 920 return true; 921 } else if (x > getWidth() - mEdgeTapAreaWidth 922 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 923 onEdgeClicked(true /* right */); 924 return true; 925 } else { 926 return onMiddleClicked(); 927 } 928 } 929 930 private final Runnable mPostCollapseRunnable = new Runnable() { 931 @Override 932 public void run() { 933 collapse(false /* delayed */); 934 } 935 }; 936 private boolean onMiddleClicked() { 937 switch (mStatusBar.getBarState()) { 938 case StatusBarState.KEYGUARD: 939 if (!isDozing()) { 940 startUnlockHintAnimation(); 941 } 942 return true; 943 case StatusBarState.SHADE_LOCKED: 944 mStatusBar.goToKeyguard(); 945 return true; 946 case StatusBarState.SHADE: 947 948 // This gets called in the middle of the touch handling, where the state is still 949 // that we are tracking the panel. Collapse the panel after this is done. 950 post(mPostCollapseRunnable); 951 return false; 952 default: 953 return true; 954 } 955 } 956 957 protected abstract void onEdgeClicked(boolean right); 958 959 protected abstract boolean isDozing(); 960 961 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 962 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 963 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s touchDisabled=%s" 964 + "]", 965 this.getClass().getSimpleName(), 966 getExpandedHeight(), 967 getMaxPanelHeight(), 968 mClosing?"T":"f", 969 mTracking?"T":"f", 970 mJustPeeked?"T":"f", 971 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 972 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":""), 973 mTouchDisabled?"T":"f" 974 )); 975 } 976 977 public abstract void resetViews(); 978 979 protected abstract float getPeekHeight(); 980 981 protected abstract float getCannedFlingDurationFactor(); 982 983 /** 984 * @return whether "Clear all" button will be visible when the panel is fully expanded 985 */ 986 protected abstract boolean fullyExpandedClearAllVisible(); 987 988 protected abstract boolean isClearAllVisible(); 989 990 /** 991 * @return the height of the clear all button, in pixels 992 */ 993 protected abstract int getClearAllHeight(); 994} 995