PanelView.java revision 47c85a3525dcd0bbd3168632830e8ab491d18462
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.animation.AnimationUtils; 31import android.view.animation.Interpolator; 32import android.widget.FrameLayout; 33 34import com.android.systemui.R; 35import com.android.systemui.statusbar.FlingAnimationUtils; 36import com.android.systemui.statusbar.StatusBarState; 37 38import java.io.FileDescriptor; 39import java.io.PrintWriter; 40 41public abstract class PanelView extends FrameLayout { 42 public static final boolean DEBUG = PanelBar.DEBUG; 43 public static final String TAG = PanelView.class.getSimpleName(); 44 45 private final void logf(String fmt, Object... args) { 46 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); 47 } 48 49 protected PhoneStatusBar mStatusBar; 50 private float mPeekHeight; 51 private float mHintDistance; 52 private int mEdgeTapAreaWidth; 53 private float mInitialOffsetOnTouch; 54 private float mExpandedFraction = 0; 55 protected float mExpandedHeight = 0; 56 private boolean mJustPeeked; 57 private boolean mClosing; 58 protected boolean mTracking; 59 private boolean mTouchSlopExceeded; 60 private int mTrackingPointer; 61 protected int mTouchSlop; 62 protected boolean mHintAnimationRunning; 63 private boolean mOverExpandedBeforeFling; 64 65 private ValueAnimator mHeightAnimator; 66 private ObjectAnimator mPeekAnimator; 67 private VelocityTrackerInterface mVelocityTracker; 68 private FlingAnimationUtils mFlingAnimationUtils; 69 70 PanelBar mBar; 71 72 protected int mMaxPanelHeight = -1; 73 private String mViewName; 74 private float mInitialTouchY; 75 private float mInitialTouchX; 76 77 private Interpolator mLinearOutSlowInInterpolator; 78 private Interpolator mBounceInterpolator; 79 80 protected void onExpandingFinished() { 81 mBar.onExpandingFinished(); 82 } 83 84 protected void onExpandingStarted() { 85 } 86 87 private void runPeekAnimation() { 88 if (DEBUG) logf("peek to height=%.1f", mPeekHeight); 89 if (mHeightAnimator != null) { 90 return; 91 } 92 if (mPeekAnimator == null) { 93 mPeekAnimator = ObjectAnimator.ofFloat(this, 94 "expandedHeight", mPeekHeight) 95 .setDuration(250); 96 } 97 mPeekAnimator.start(); 98 } 99 100 public PanelView(Context context, AttributeSet attrs) { 101 super(context, attrs); 102 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f); 103 mLinearOutSlowInInterpolator = 104 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in); 105 mBounceInterpolator = new BounceInterpolator(); 106 } 107 108 protected void loadDimens() { 109 final Resources res = getContext().getResources(); 110 mPeekHeight = res.getDimension(R.dimen.peek_height) 111 + getPaddingBottom(); // our window might have a dropshadow 112 113 final ViewConfiguration configuration = ViewConfiguration.get(getContext()); 114 mTouchSlop = configuration.getScaledTouchSlop(); 115 mHintDistance = res.getDimension(R.dimen.hint_move_distance); 116 mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width); 117 } 118 119 private void trackMovement(MotionEvent event) { 120 // Add movement to velocity tracker using raw screen X and Y coordinates instead 121 // of window coordinates because the window frame may be moving at the same time. 122 float deltaX = event.getRawX() - event.getX(); 123 float deltaY = event.getRawY() - event.getY(); 124 event.offsetLocation(deltaX, deltaY); 125 if (mVelocityTracker != null) mVelocityTracker.addMovement(event); 126 event.offsetLocation(-deltaX, -deltaY); 127 } 128 129 @Override 130 public boolean onTouchEvent(MotionEvent event) { 131 132 /* 133 * We capture touch events here and update the expand height here in case according to 134 * the users fingers. This also handles multi-touch. 135 * 136 * If the user just clicks shortly, we give him a quick peek of the shade. 137 * 138 * Flinging is also enabled in order to open or close the shade. 139 */ 140 141 int pointerIndex = event.findPointerIndex(mTrackingPointer); 142 if (pointerIndex < 0) { 143 pointerIndex = 0; 144 mTrackingPointer = event.getPointerId(pointerIndex); 145 } 146 final float y = event.getY(pointerIndex); 147 final float x = event.getX(pointerIndex); 148 149 boolean waitForTouchSlop = hasConflictingGestures(); 150 151 switch (event.getActionMasked()) { 152 case MotionEvent.ACTION_DOWN: 153 mInitialTouchY = y; 154 mInitialTouchX = x; 155 mInitialOffsetOnTouch = mExpandedHeight; 156 mTouchSlopExceeded = false; 157 if (mVelocityTracker == null) { 158 initVelocityTracker(); 159 } 160 trackMovement(event); 161 if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning)) { 162 if (mHeightAnimator != null) { 163 mHeightAnimator.cancel(); // end any outstanding animations 164 } 165 mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning); 166 onTrackingStarted(); 167 } 168 if (mExpandedHeight == 0) { 169 mJustPeeked = true; 170 runPeekAnimation(); 171 } 172 break; 173 174 case MotionEvent.ACTION_POINTER_UP: 175 final int upPointer = event.getPointerId(event.getActionIndex()); 176 if (mTrackingPointer == upPointer) { 177 // gesture is ongoing, find a new pointer to track 178 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 179 final float newY = event.getY(newIndex); 180 final float newX = event.getX(newIndex); 181 mTrackingPointer = event.getPointerId(newIndex); 182 mInitialOffsetOnTouch = mExpandedHeight; 183 mInitialTouchY = newY; 184 mInitialTouchX = newX; 185 } 186 break; 187 188 case MotionEvent.ACTION_MOVE: 189 float h = y - mInitialTouchY; 190 if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) { 191 mTouchSlopExceeded = true; 192 if (waitForTouchSlop && !mTracking) { 193 mInitialOffsetOnTouch = mExpandedHeight; 194 mInitialTouchX = x; 195 mInitialTouchY = y; 196 if (mHeightAnimator != null) { 197 mHeightAnimator.cancel(); // end any outstanding animations 198 } 199 onTrackingStarted(); 200 h = 0; 201 } 202 } 203 final float newHeight = h + mInitialOffsetOnTouch; 204 if (newHeight > mPeekHeight) { 205 if (mPeekAnimator != null && mPeekAnimator.isStarted()) { 206 mPeekAnimator.cancel(); 207 } 208 mJustPeeked = false; 209 } 210 if (!mJustPeeked && (!waitForTouchSlop || mTracking)) { 211 setExpandedHeightInternal(newHeight); 212 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 213 } 214 215 trackMovement(event); 216 break; 217 218 case MotionEvent.ACTION_UP: 219 case MotionEvent.ACTION_CANCEL: 220 mTrackingPointer = -1; 221 trackMovement(event); 222 if ((mTracking && mTouchSlopExceeded) 223 || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { 224 float vel = getCurrentVelocity(); 225 boolean expand = flingExpands(vel); 226 onTrackingStopped(expand); 227 fling(vel, expand); 228 } else { 229 boolean expands = onEmptySpaceClick(mInitialTouchX); 230 onTrackingStopped(expands); 231 } 232 if (mVelocityTracker != null) { 233 mVelocityTracker.recycle(); 234 mVelocityTracker = null; 235 } 236 break; 237 } 238 return !waitForTouchSlop || mTracking; 239 } 240 241 protected abstract boolean hasConflictingGestures(); 242 243 protected void onTrackingStopped(boolean expand) { 244 mTracking = false; 245 mBar.onTrackingStopped(PanelView.this, expand); 246 } 247 248 protected void onTrackingStarted() { 249 mTracking = true; 250 mBar.onTrackingStarted(PanelView.this); 251 onExpandingStarted(); 252 } 253 254 private float getCurrentVelocity() { 255 256 // the velocitytracker might be null if we got a bad input stream 257 if (mVelocityTracker == null) { 258 return 0; 259 } 260 mVelocityTracker.computeCurrentVelocity(1000); 261 return mVelocityTracker.getYVelocity(); 262 } 263 264 @Override 265 public boolean onInterceptTouchEvent(MotionEvent event) { 266 267 /* 268 * If the user drags anywhere inside the panel we intercept it if he moves his finger 269 * upwards. This allows closing the shade from anywhere inside the panel. 270 * 271 * We only do this if the current content is scrolled to the bottom, 272 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture 273 * possible. 274 */ 275 int pointerIndex = event.findPointerIndex(mTrackingPointer); 276 if (pointerIndex < 0) { 277 pointerIndex = 0; 278 mTrackingPointer = event.getPointerId(pointerIndex); 279 } 280 final float x = event.getX(pointerIndex); 281 final float y = event.getY(pointerIndex); 282 boolean scrolledToBottom = isScrolledToBottom(); 283 284 switch (event.getActionMasked()) { 285 case MotionEvent.ACTION_DOWN: 286 if (mHeightAnimator != null && !mHintAnimationRunning) { 287 mHeightAnimator.cancel(); // end any outstanding animations 288 mTouchSlopExceeded = true; 289 return true; 290 } 291 mInitialTouchY = y; 292 mInitialTouchX = x; 293 mTouchSlopExceeded = false; 294 initVelocityTracker(); 295 trackMovement(event); 296 break; 297 case MotionEvent.ACTION_POINTER_UP: 298 final int upPointer = event.getPointerId(event.getActionIndex()); 299 if (mTrackingPointer == upPointer) { 300 // gesture is ongoing, find a new pointer to track 301 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 302 mTrackingPointer = event.getPointerId(newIndex); 303 mInitialTouchX = event.getX(newIndex); 304 mInitialTouchY = event.getY(newIndex); 305 } 306 break; 307 308 case MotionEvent.ACTION_MOVE: 309 final float h = y - mInitialTouchY; 310 trackMovement(event); 311 if (scrolledToBottom) { 312 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { 313 if (mHeightAnimator != null) { 314 mHeightAnimator.cancel(); 315 } 316 mInitialOffsetOnTouch = mExpandedHeight; 317 mInitialTouchY = y; 318 mInitialTouchX = x; 319 mTracking = true; 320 mTouchSlopExceeded = true; 321 onTrackingStarted(); 322 return true; 323 } 324 } 325 break; 326 } 327 return false; 328 } 329 330 private void initVelocityTracker() { 331 if (mVelocityTracker != null) { 332 mVelocityTracker.recycle(); 333 } 334 mVelocityTracker = VelocityTrackerFactory.obtain(getContext()); 335 } 336 337 protected boolean isScrolledToBottom() { 338 return true; 339 } 340 341 protected float getContentHeight() { 342 return mExpandedHeight; 343 } 344 345 @Override 346 protected void onFinishInflate() { 347 super.onFinishInflate(); 348 loadDimens(); 349 } 350 351 @Override 352 protected void onConfigurationChanged(Configuration newConfig) { 353 super.onConfigurationChanged(newConfig); 354 loadDimens(); 355 mMaxPanelHeight = -1; 356 } 357 358 /** 359 * @param vel the current velocity of the motion 360 * @return whether a fling should expands the panel; contracts otherwise 361 */ 362 private boolean flingExpands(float vel) { 363 if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) { 364 return getExpandedFraction() > 0.5f; 365 } else { 366 return vel > 0; 367 } 368 } 369 370 protected void fling(float vel, boolean expand) { 371 cancelPeek(); 372 float target = expand ? getMaxPanelHeight() : 0.0f; 373 if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) { 374 onExpandingFinished(); 375 mBar.panelExpansionChanged(this, mExpandedFraction); 376 return; 377 } 378 mOverExpandedBeforeFling = getOverExpansionAmount() > 0f; 379 ValueAnimator animator = createHeightAnimator(target); 380 if (expand) { 381 mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight()); 382 } else { 383 mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel, 384 getHeight()); 385 386 // Make it shorter if we run a canned animation 387 if (vel == 0) { 388 animator.setDuration((long) (animator.getDuration() / 1.75f)); 389 } 390 } 391 animator.addListener(new AnimatorListenerAdapter() { 392 @Override 393 public void onAnimationEnd(Animator animation) { 394 mHeightAnimator = null; 395 onExpandingFinished(); 396 } 397 }); 398 mHeightAnimator = animator; 399 animator.start(); 400 } 401 402 @Override 403 protected void onAttachedToWindow() { 404 super.onAttachedToWindow(); 405 mViewName = getResources().getResourceName(getId()); 406 } 407 408 public String getName() { 409 return mViewName; 410 } 411 412 @Override 413 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 414 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 415 416 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)", 417 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); 418 419 // Did one of our children change size? 420 int newHeight = getMeasuredHeight(); 421 if (newHeight > mMaxPanelHeight) { 422 // we only adapt the max height if it's bigger 423 mMaxPanelHeight = newHeight; 424 // If the user isn't actively poking us, let's rubberband to the content 425 if (!mTracking && mHeightAnimator == null 426 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight 427 && mMaxPanelHeight > 0) { 428 mExpandedHeight = mMaxPanelHeight; 429 } 430 } 431 } 432 433 public void setExpandedHeight(float height) { 434 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 435 setExpandedHeightInternal(height + getOverExpansionPixels()); 436 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 437 } 438 439 @Override 440 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 441 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom, 442 (int)mExpandedHeight, mMaxPanelHeight); 443 super.onLayout(changed, left, top, right, bottom); 444 requestPanelHeightUpdate(); 445 } 446 447 protected void requestPanelHeightUpdate() { 448 float currentMaxPanelHeight = getMaxPanelHeight(); 449 450 // If the user isn't actively poking us, let's update the height 451 if (!mTracking && mHeightAnimator == null 452 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) { 453 setExpandedHeight(currentMaxPanelHeight); 454 } 455 } 456 457 public void setExpandedHeightInternal(float h) { 458 float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount(); 459 if (mHeightAnimator == null) { 460 float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion); 461 if (getOverExpansionPixels() != overExpansionPixels && mTracking) { 462 setOverExpansion(overExpansionPixels, true /* isPixels */); 463 } 464 mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount(); 465 } else { 466 mExpandedHeight = h; 467 if (mOverExpandedBeforeFling) { 468 setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */); 469 } 470 } 471 472 onHeightUpdated(mExpandedHeight); 473 mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0 474 ? 0 475 : mExpandedHeight / fhWithoutOverExpansion); 476 } 477 478 protected abstract void setOverExpansion(float overExpansion, boolean isPixels); 479 480 protected abstract void onHeightUpdated(float expandedHeight); 481 482 protected abstract float getOverExpansionAmount(); 483 484 protected abstract float getOverExpansionPixels(); 485 486 /** 487 * This returns the maximum height of the panel. Children should override this if their 488 * desired height is not the full height. 489 * 490 * @return the default implementation simply returns the maximum height. 491 */ 492 protected int getMaxPanelHeight() { 493 mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight()); 494 return mMaxPanelHeight; 495 } 496 497 public void setExpandedFraction(float frac) { 498 setExpandedHeight(getMaxPanelHeight() * frac); 499 } 500 501 public float getExpandedHeight() { 502 return mExpandedHeight; 503 } 504 505 public float getExpandedFraction() { 506 return mExpandedFraction; 507 } 508 509 public boolean isFullyExpanded() { 510 return mExpandedHeight >= getMaxPanelHeight(); 511 } 512 513 public boolean isFullyCollapsed() { 514 return mExpandedHeight <= 0; 515 } 516 517 public boolean isCollapsing() { 518 return mClosing; 519 } 520 521 public boolean isTracking() { 522 return mTracking; 523 } 524 525 public void setBar(PanelBar panelBar) { 526 mBar = panelBar; 527 } 528 529 public void collapse() { 530 // TODO: abort animation or ongoing touch 531 if (DEBUG) logf("collapse: " + this); 532 if (!isFullyCollapsed()) { 533 if (mHeightAnimator != null) { 534 mHeightAnimator.cancel(); 535 } 536 mClosing = true; 537 onExpandingStarted(); 538 fling(0, false /* expand */); 539 } 540 } 541 542 public void expand() { 543 if (DEBUG) logf("expand: " + this); 544 if (isFullyCollapsed()) { 545 mBar.startOpeningPanel(this); 546 onExpandingStarted(); 547 fling(0, true /* expand */); 548 } else if (DEBUG) { 549 if (DEBUG) logf("skipping expansion: is expanded"); 550 } 551 } 552 553 public void cancelPeek() { 554 if (mPeekAnimator != null && mPeekAnimator.isStarted()) { 555 mPeekAnimator.cancel(); 556 } 557 } 558 559 protected void startUnlockHintAnimation() { 560 561 // We don't need to hint the user if an animation is already running or the user is changing 562 // the expansion. 563 if (mHeightAnimator != null || mTracking) { 564 return; 565 } 566 cancelPeek(); 567 onExpandingStarted(); 568 startUnlockHintAnimationPhase1(new Runnable() { 569 @Override 570 public void run() { 571 onExpandingFinished(); 572 mStatusBar.onHintFinished(); 573 mHintAnimationRunning = false; 574 } 575 }); 576 mStatusBar.onUnlockHintStarted(); 577 mHintAnimationRunning = true; 578 } 579 580 /** 581 * Phase 1: Move everything upwards. 582 */ 583 private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) { 584 float target = Math.max(0, getMaxPanelHeight() - mHintDistance); 585 ValueAnimator animator = createHeightAnimator(target); 586 animator.setDuration(250); 587 animator.setInterpolator(mLinearOutSlowInInterpolator); 588 animator.addListener(new AnimatorListenerAdapter() { 589 private boolean mCancelled; 590 591 @Override 592 public void onAnimationCancel(Animator animation) { 593 mCancelled = true; 594 } 595 596 @Override 597 public void onAnimationEnd(Animator animation) { 598 if (mCancelled) { 599 mHeightAnimator = null; 600 onAnimationFinished.run(); 601 } else { 602 startUnlockHintAnimationPhase2(onAnimationFinished); 603 } 604 } 605 }); 606 animator.start(); 607 mHeightAnimator = animator; 608 } 609 610 /** 611 * Phase 2: Bounce down. 612 */ 613 private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) { 614 ValueAnimator animator = createHeightAnimator(getMaxPanelHeight()); 615 animator.setDuration(450); 616 animator.setInterpolator(mBounceInterpolator); 617 animator.addListener(new AnimatorListenerAdapter() { 618 @Override 619 public void onAnimationEnd(Animator animation) { 620 mHeightAnimator = null; 621 onAnimationFinished.run(); 622 } 623 }); 624 animator.start(); 625 mHeightAnimator = animator; 626 } 627 628 private ValueAnimator createHeightAnimator(float targetHeight) { 629 ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight); 630 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 631 @Override 632 public void onAnimationUpdate(ValueAnimator animation) { 633 setExpandedHeightInternal((Float) animation.getAnimatedValue()); 634 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 635 } 636 }); 637 return animator; 638 } 639 640 /** 641 * Gets called when the user performs a click anywhere in the empty area of the panel. 642 * 643 * @return whether the panel will be expanded after the action performed by this method 644 */ 645 private boolean onEmptySpaceClick(float x) { 646 if (mHintAnimationRunning) { 647 return true; 648 } 649 if (x < mEdgeTapAreaWidth 650 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 651 onEdgeClicked(false /* right */); 652 return true; 653 } else if (x > getWidth() - mEdgeTapAreaWidth 654 && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { 655 onEdgeClicked(true /* right */); 656 return true; 657 } else { 658 return onMiddleClicked(); 659 } 660 } 661 662 private boolean onMiddleClicked() { 663 switch (mStatusBar.getBarState()) { 664 case StatusBarState.KEYGUARD: 665 startUnlockHintAnimation(); 666 return true; 667 case StatusBarState.SHADE_LOCKED: 668 mStatusBar.goToKeyguard(); 669 return true; 670 case StatusBarState.SHADE: 671 collapse(); 672 return false; 673 default: 674 return true; 675 } 676 } 677 678 protected abstract void onEdgeClicked(boolean right); 679 680 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 681 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 682 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s" 683 + "]", 684 this.getClass().getSimpleName(), 685 getExpandedHeight(), 686 getMaxPanelHeight(), 687 mClosing?"T":"f", 688 mTracking?"T":"f", 689 mJustPeeked?"T":"f", 690 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 691 mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"") 692 )); 693 } 694 695 public abstract void resetViews(); 696} 697