PanelView.java revision 4f0128fb8fc1e56ead8ba7bccd9fab2ff9c3b29a
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.ObjectAnimator; 20import android.animation.TimeAnimator; 21import android.animation.TimeAnimator.TimeListener; 22import android.content.Context; 23import android.content.res.Resources; 24import android.util.AttributeSet; 25import android.util.Log; 26import android.view.MotionEvent; 27import android.view.View; 28import android.view.ViewConfiguration; 29import android.widget.FrameLayout; 30 31import com.android.systemui.R; 32 33import java.io.FileDescriptor; 34import java.io.PrintWriter; 35import java.util.ArrayDeque; 36import java.util.Iterator; 37 38public class PanelView extends FrameLayout { 39 public static final boolean DEBUG = PanelBar.DEBUG; 40 public static final String TAG = PanelView.class.getSimpleName(); 41 42 public static final boolean DEBUG_NAN = true; // http://b/7686690 43 44 private final void logf(String fmt, Object... args) { 45 Log.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); 46 } 47 48 public static final boolean BRAKES = false; 49 50 private float mSelfExpandVelocityPx; // classic value: 2000px/s 51 private float mSelfCollapseVelocityPx; // classic value: 2000px/s (will be negated to collapse "up") 52 private float mFlingExpandMinVelocityPx; // classic value: 200px/s 53 private float mFlingCollapseMinVelocityPx; // classic value: 200px/s 54 private float mCollapseMinDisplayFraction; // classic value: 0.08 (25px/min(320px,480px) on G1) 55 private float mExpandMinDisplayFraction; // classic value: 0.5 (drag open halfway to expand) 56 private float mFlingGestureMaxXVelocityPx; // classic value: 150px/s 57 58 private float mFlingGestureMinDistPx; 59 60 private float mExpandAccelPx; // classic value: 2000px/s/s 61 private float mCollapseAccelPx; // classic value: 2000px/s/s (will be negated to collapse "up") 62 63 private float mFlingGestureMaxOutputVelocityPx; // how fast can it really go? (should be a little 64 // faster than mSelfCollapseVelocityPx) 65 66 private float mCollapseBrakingDistancePx = 200; // XXX Resource 67 private float mExpandBrakingDistancePx = 150; // XXX Resource 68 private float mBrakingSpeedPx = 150; // XXX Resource 69 70 private float mPeekHeight; 71 private float mInitialOffsetOnTouch; 72 private float mExpandedFraction = 0; 73 private float mExpandedHeight = 0; 74 private boolean mJustPeeked; 75 private boolean mClosing; 76 private boolean mTracking; 77 private int mTrackingPointer; 78 protected int mTouchSlop; 79 80 private TimeAnimator mTimeAnimator; 81 private ObjectAnimator mPeekAnimator; 82 private FlingTracker mVelocityTracker; 83 84 /** 85 * A very simple low-pass velocity filter for motion events; not nearly as sophisticated as 86 * VelocityTracker but optimized for the kinds of gestures we expect to see in status bar 87 * panels. 88 */ 89 private static class FlingTracker { 90 static final boolean DEBUG = false; 91 final int MAX_EVENTS = 8; 92 final float DECAY = 0.75f; 93 ArrayDeque<MotionEventCopy> mEventBuf = new ArrayDeque<MotionEventCopy>(MAX_EVENTS); 94 float mVX, mVY = 0; 95 private static class MotionEventCopy { 96 public MotionEventCopy(float x2, float y2, long eventTime) { 97 this.x = x2; 98 this.y = y2; 99 this.t = eventTime; 100 } 101 public float x, y; 102 public long t; 103 } 104 public FlingTracker() { 105 } 106 public void addMovement(MotionEvent event) { 107 if (mEventBuf.size() == MAX_EVENTS) { 108 mEventBuf.remove(); 109 } 110 mEventBuf.add(new MotionEventCopy(event.getX(), event.getY(), event.getEventTime())); 111 } 112 public void computeCurrentVelocity(long timebase) { 113 if (FlingTracker.DEBUG) { 114 Log.v("FlingTracker", "computing velocities for " + mEventBuf.size() + " events"); 115 } 116 mVX = mVY = 0; 117 MotionEventCopy last = null; 118 int i = 0; 119 float totalweight = 0f; 120 float weight = 10f; 121 for (final Iterator<MotionEventCopy> iter = mEventBuf.iterator(); 122 iter.hasNext();) { 123 final MotionEventCopy event = iter.next(); 124 if (last != null) { 125 final float dt = (float) (event.t - last.t) / timebase; 126 final float dx = (event.x - last.x); 127 final float dy = (event.y - last.y); 128 if (FlingTracker.DEBUG) { 129 Log.v("FlingTracker", String.format( 130 " [%d] (t=%d %.1f,%.1f) dx=%.1f dy=%.1f dt=%f vx=%.1f vy=%.1f", 131 i, event.t, event.x, event.y, 132 dx, dy, dt, 133 (dx/dt), 134 (dy/dt) 135 )); 136 } 137 if (event.t == last.t) { 138 // Really not sure what to do with events that happened at the same time, 139 // so we'll skip subsequent events. 140 if (DEBUG_NAN) { 141 Log.v("FlingTracker", "skipping simultaneous event at t=" + event.t); 142 } 143 continue; 144 } 145 mVX += weight * dx / dt; 146 mVY += weight * dy / dt; 147 totalweight += weight; 148 weight *= DECAY; 149 } 150 last = event; 151 i++; 152 } 153 if (totalweight > 0) { 154 mVX /= totalweight; 155 mVY /= totalweight; 156 } else { 157 if (DEBUG_NAN) { 158 Log.v("FlingTracker", "computeCurrentVelocity warning: totalweight=0", 159 new Throwable()); 160 } 161 // so as not to contaminate the velocities with NaN 162 mVX = mVY = 0; 163 } 164 165 if (FlingTracker.DEBUG) { 166 Log.v("FlingTracker", "computed: vx=" + mVX + " vy=" + mVY); 167 } 168 } 169 public float getXVelocity() { 170 if (Float.isNaN(mVX) || Float.isInfinite(mVX)) { 171 if (DEBUG_NAN) { 172 Log.v("FlingTracker", "warning: vx=" + mVX); 173 } 174 mVX = 0; 175 } 176 return mVX; 177 } 178 public float getYVelocity() { 179 if (Float.isNaN(mVY) || Float.isInfinite(mVX)) { 180 if (DEBUG_NAN) { 181 Log.v("FlingTracker", "warning: vx=" + mVY); 182 } 183 mVY = 0; 184 } 185 return mVY; 186 } 187 public void recycle() { 188 mEventBuf.clear(); 189 } 190 191 static FlingTracker sTracker; 192 static FlingTracker obtain() { 193 if (sTracker == null) { 194 sTracker = new FlingTracker(); 195 } 196 return sTracker; 197 } 198 } 199 200 PanelBar mBar; 201 202 private final TimeListener mAnimationCallback = new TimeListener() { 203 @Override 204 public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) { 205 animationTick(deltaTime); 206 } 207 }; 208 209 private final Runnable mStopAnimator = new Runnable() { 210 @Override 211 public void run() { 212 if (mTimeAnimator != null && mTimeAnimator.isStarted()) { 213 mTimeAnimator.end(); 214 mClosing = false; 215 onExpandingFinished(); 216 } 217 } 218 }; 219 220 private float mVel, mAccel; 221 protected int mMaxPanelHeight = 0; 222 private String mViewName; 223 private float mInitialTouchY; 224 private float mInitialTouchX; 225 private float mFinalTouchY; 226 227 protected void onExpandingFinished() { 228 } 229 230 protected void onExpandingStarted() { 231 } 232 233 private void runPeekAnimation() { 234 if (DEBUG) logf("peek to height=%.1f", mPeekHeight); 235 if (mTimeAnimator.isStarted()) { 236 return; 237 } 238 if (mPeekAnimator == null) { 239 mPeekAnimator = ObjectAnimator.ofFloat(this, 240 "expandedHeight", mPeekHeight) 241 .setDuration(250); 242 } 243 mPeekAnimator.start(); 244 } 245 246 private void animationTick(long dtms) { 247 if (!mTimeAnimator.isStarted()) { 248 // XXX HAX to work around bug in TimeAnimator.end() not resetting its last time 249 mTimeAnimator = new TimeAnimator(); 250 mTimeAnimator.setTimeListener(mAnimationCallback); 251 252 if (mPeekAnimator != null) mPeekAnimator.cancel(); 253 254 mTimeAnimator.start(); 255 256 if (mVel == 0) { 257 // if the panel is less than halfway open, close it 258 mClosing = (mFinalTouchY / getMaxPanelHeight()) < 0.5f; 259 } else { 260 mClosing = mExpandedHeight > 0 && mVel < 0; 261 } 262 } else if (dtms > 0) { 263 final float dt = dtms * 0.001f; // ms -> s 264 if (DEBUG) logf("tick: v=%.2fpx/s dt=%.4fs", mVel, dt); 265 if (DEBUG) logf("tick: before: h=%d", (int) mExpandedHeight); 266 267 final float fh = getMaxPanelHeight(); 268 boolean braking = false; 269 if (BRAKES) { 270 if (mClosing) { 271 braking = mExpandedHeight <= mCollapseBrakingDistancePx; 272 mAccel = braking ? 10*mCollapseAccelPx : -mCollapseAccelPx; 273 } else { 274 braking = mExpandedHeight >= (fh-mExpandBrakingDistancePx); 275 mAccel = braking ? 10*-mExpandAccelPx : mExpandAccelPx; 276 } 277 } else { 278 mAccel = mClosing ? -mCollapseAccelPx : mExpandAccelPx; 279 } 280 281 mVel += mAccel * dt; 282 283 if (braking) { 284 if (mClosing && mVel > -mBrakingSpeedPx) { 285 mVel = -mBrakingSpeedPx; 286 } else if (!mClosing && mVel < mBrakingSpeedPx) { 287 mVel = mBrakingSpeedPx; 288 } 289 } else { 290 if (mClosing && mVel > -mFlingCollapseMinVelocityPx) { 291 mVel = -mFlingCollapseMinVelocityPx; 292 } else if (!mClosing && mVel > mFlingGestureMaxOutputVelocityPx) { 293 mVel = mFlingGestureMaxOutputVelocityPx; 294 } 295 } 296 297 float h = mExpandedHeight + mVel * dt; 298 299 if (DEBUG) logf("tick: new h=%d closing=%s", (int) h, mClosing?"true":"false"); 300 301 setExpandedHeightInternal(h); 302 303 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 304 305 if (mVel == 0 306 || (mClosing && mExpandedHeight == 0) 307 || (!mClosing && mExpandedHeight == fh)) { 308 post(mStopAnimator); 309 } 310 } else { 311 Log.v(TAG, "animationTick called with dtms=" + dtms + "; nothing to do (h=" 312 + mExpandedHeight + " v=" + mVel + ")"); 313 } 314 } 315 316 public PanelView(Context context, AttributeSet attrs) { 317 super(context, attrs); 318 319 mTimeAnimator = new TimeAnimator(); 320 mTimeAnimator.setTimeListener(mAnimationCallback); 321 setOnHierarchyChangeListener(mHierarchyListener); 322 } 323 324 private void loadDimens() { 325 final Resources res = getContext().getResources(); 326 327 mSelfExpandVelocityPx = res.getDimension(R.dimen.self_expand_velocity); 328 mSelfCollapseVelocityPx = res.getDimension(R.dimen.self_collapse_velocity); 329 mFlingExpandMinVelocityPx = res.getDimension(R.dimen.fling_expand_min_velocity); 330 mFlingCollapseMinVelocityPx = res.getDimension(R.dimen.fling_collapse_min_velocity); 331 332 mFlingGestureMinDistPx = res.getDimension(R.dimen.fling_gesture_min_dist); 333 334 mCollapseMinDisplayFraction = res.getFraction(R.dimen.collapse_min_display_fraction, 1, 1); 335 mExpandMinDisplayFraction = res.getFraction(R.dimen.expand_min_display_fraction, 1, 1); 336 337 mExpandAccelPx = res.getDimension(R.dimen.expand_accel); 338 mCollapseAccelPx = res.getDimension(R.dimen.collapse_accel); 339 340 mFlingGestureMaxXVelocityPx = res.getDimension(R.dimen.fling_gesture_max_x_velocity); 341 342 mFlingGestureMaxOutputVelocityPx = res.getDimension(R.dimen.fling_gesture_max_output_velocity); 343 344 mPeekHeight = res.getDimension(R.dimen.peek_height) 345 + getPaddingBottom(); // our window might have a dropshadow 346 347 final ViewConfiguration configuration = ViewConfiguration.get(getContext()); 348 mTouchSlop = configuration.getScaledTouchSlop(); 349 } 350 351 private void trackMovement(MotionEvent event) { 352 // Add movement to velocity tracker using raw screen X and Y coordinates instead 353 // of window coordinates because the window frame may be moving at the same time. 354 float deltaX = event.getRawX() - event.getX(); 355 float deltaY = event.getRawY() - event.getY(); 356 event.offsetLocation(deltaX, deltaY); 357 if (mVelocityTracker != null) mVelocityTracker.addMovement(event); 358 event.offsetLocation(-deltaX, -deltaY); 359 } 360 361 @Override 362 public boolean onTouchEvent(MotionEvent event) { 363 364 /* 365 * We capture touch events here and update the expand height here in case according to 366 * the users fingers. This also handles multi-touch. 367 * 368 * If the user just clicks shortly, we give him a quick peek of the shade. 369 * 370 * Flinging is also enabled in order to open or close the shade. 371 */ 372 373 int pointerIndex = event.findPointerIndex(mTrackingPointer); 374 if (pointerIndex < 0) { 375 pointerIndex = 0; 376 mTrackingPointer = event.getPointerId(pointerIndex); 377 } 378 final float y = event.getY(pointerIndex); 379 final float x = event.getX(pointerIndex); 380 381 switch (event.getActionMasked()) { 382 case MotionEvent.ACTION_DOWN: 383 mTracking = true; 384 385 mInitialTouchY = y; 386 mInitialTouchX = x; 387 initVelocityTracker(); 388 trackMovement(event); 389 mTimeAnimator.cancel(); // end any outstanding animations 390 onTrackingStarted(); 391 mInitialOffsetOnTouch = mExpandedHeight; 392 if (mExpandedHeight == 0) { 393 mJustPeeked = true; 394 runPeekAnimation(); 395 } 396 break; 397 398 case MotionEvent.ACTION_POINTER_UP: 399 final int upPointer = event.getPointerId(event.getActionIndex()); 400 if (mTrackingPointer == upPointer) { 401 // gesture is ongoing, find a new pointer to track 402 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 403 final float newY = event.getY(newIndex); 404 final float newX = event.getX(newIndex); 405 mTrackingPointer = event.getPointerId(newIndex); 406 mInitialOffsetOnTouch = mExpandedHeight; 407 mInitialTouchY = newY; 408 mInitialTouchX = newX; 409 } 410 break; 411 412 case MotionEvent.ACTION_MOVE: 413 final float h = y - mInitialTouchY + mInitialOffsetOnTouch; 414 if (h > mPeekHeight) { 415 if (mPeekAnimator != null && mPeekAnimator.isStarted()) { 416 mPeekAnimator.cancel(); 417 } 418 mJustPeeked = false; 419 } 420 if (!mJustPeeked) { 421 setExpandedHeightInternal(h); 422 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 423 } 424 425 trackMovement(event); 426 break; 427 428 case MotionEvent.ACTION_UP: 429 case MotionEvent.ACTION_CANCEL: 430 mFinalTouchY = y; 431 mTracking = false; 432 mTrackingPointer = -1; 433 onTrackingStopped(); 434 trackMovement(event); 435 436 float vel = getCurrentVelocity(); 437 fling(vel, true); 438 439 if (mVelocityTracker != null) { 440 mVelocityTracker.recycle(); 441 mVelocityTracker = null; 442 } 443 break; 444 } 445 return true; 446 } 447 448 protected void onTrackingStopped() { 449 mBar.onTrackingStopped(PanelView.this); 450 } 451 452 protected void onTrackingStarted() { 453 mBar.onTrackingStarted(PanelView.this); 454 onExpandingStarted(); 455 } 456 457 private float getCurrentVelocity() { 458 float vel = 0; 459 float yVel = 0, xVel = 0; 460 boolean negative = false; 461 462 // the velocitytracker might be null if we got a bad input stream 463 if (mVelocityTracker == null) { 464 return 0; 465 } 466 467 mVelocityTracker.computeCurrentVelocity(1000); 468 469 yVel = mVelocityTracker.getYVelocity(); 470 negative = yVel < 0; 471 472 xVel = mVelocityTracker.getXVelocity(); 473 if (xVel < 0) { 474 xVel = -xVel; 475 } 476 if (xVel > mFlingGestureMaxXVelocityPx) { 477 xVel = mFlingGestureMaxXVelocityPx; // limit how much we care about the x axis 478 } 479 480 vel = (float) Math.hypot(yVel, xVel); 481 if (vel > mFlingGestureMaxOutputVelocityPx) { 482 vel = mFlingGestureMaxOutputVelocityPx; 483 } 484 485 // if you've barely moved your finger, we treat the velocity as 0 486 // preventing spurious flings due to touch screen jitter 487 final float deltaY = Math.abs(mFinalTouchY - mInitialTouchY); 488 if (deltaY < mFlingGestureMinDistPx 489 || vel < mFlingExpandMinVelocityPx 490 ) { 491 vel = 0; 492 } 493 494 if (negative) { 495 vel = -vel; 496 } 497 498 if (DEBUG) { 499 logf("gesture: dy=%f vel=(%f,%f) vlinear=%f", 500 deltaY, 501 xVel, yVel, 502 vel); 503 } 504 return vel; 505 } 506 507 @Override 508 public boolean onInterceptTouchEvent(MotionEvent event) { 509 510 /* 511 * If the user drags anywhere inside the panel we intercept it if he moves his finger 512 * upwards. This allows closing the shade from anywhere inside the panel. 513 * 514 * We only do this if the current content is scrolled to the bottom, 515 * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture 516 * possible. 517 */ 518 int pointerIndex = event.findPointerIndex(mTrackingPointer); 519 if (pointerIndex < 0) { 520 pointerIndex = 0; 521 mTrackingPointer = event.getPointerId(pointerIndex); 522 } 523 final float x = event.getX(pointerIndex); 524 final float y = event.getY(pointerIndex); 525 boolean scrolledToBottom = isScrolledToBottom(); 526 527 switch (event.getActionMasked()) { 528 case MotionEvent.ACTION_DOWN: 529 if (mTimeAnimator.isRunning()) { 530 mTimeAnimator.cancel(); // end any outstanding animations 531 return true; 532 } 533 mInitialTouchY = y; 534 mInitialTouchX = x; 535 initVelocityTracker(); 536 trackMovement(event); 537 break; 538 case MotionEvent.ACTION_POINTER_UP: 539 final int upPointer = event.getPointerId(event.getActionIndex()); 540 if (mTrackingPointer == upPointer) { 541 // gesture is ongoing, find a new pointer to track 542 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 543 mTrackingPointer = event.getPointerId(newIndex); 544 mInitialTouchX = event.getX(newIndex); 545 mInitialTouchY = event.getY(newIndex); 546 } 547 break; 548 549 case MotionEvent.ACTION_MOVE: 550 final float h = y - mInitialTouchY; 551 trackMovement(event); 552 if (scrolledToBottom) { 553 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { 554 mInitialOffsetOnTouch = mExpandedHeight; 555 mInitialTouchY = y; 556 mInitialTouchX = x; 557 mTracking = true; 558 onTrackingStarted(); 559 return true; 560 } 561 } 562 break; 563 } 564 return false; 565 } 566 567 private void initVelocityTracker() { 568 if (mVelocityTracker != null) { 569 mVelocityTracker.recycle(); 570 } 571 mVelocityTracker = FlingTracker.obtain(); 572 } 573 574 protected boolean isScrolledToBottom() { 575 return true; 576 } 577 578 protected float getContentHeight() { 579 return mExpandedHeight; 580 } 581 582 @Override 583 protected void onFinishInflate() { 584 super.onFinishInflate(); 585 586 loadDimens(); 587 } 588 589 public void fling(float vel, boolean always) { 590 if (DEBUG) logf("fling: vel=%.3f, this=%s", vel, this); 591 mVel = vel; 592 593 if (always||mVel != 0) { 594 animationTick(0); // begin the animation 595 } else { 596 onExpandingFinished(); 597 } 598 } 599 600 @Override 601 protected void onAttachedToWindow() { 602 super.onAttachedToWindow(); 603 mViewName = getResources().getResourceName(getId()); 604 } 605 606 public String getName() { 607 return mViewName; 608 } 609 610 // Rubberbands the panel to hold its contents. 611 @Override 612 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 613 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 614 615 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)", 616 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); 617 618 // Did one of our children change size? 619 int newHeight = getMeasuredHeight(); 620 if (newHeight != mMaxPanelHeight) { 621 mMaxPanelHeight = newHeight; 622 // If the user isn't actively poking us, let's rubberband to the content 623 if (!mTracking && !mTimeAnimator.isStarted() 624 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight 625 && mMaxPanelHeight > 0) { 626 mExpandedHeight = mMaxPanelHeight; 627 } 628 } 629 setMeasuredDimension(getMeasuredWidth(), getDesiredMeasureHeight()); 630 } 631 632 protected int getDesiredMeasureHeight() { 633 return (int) mExpandedHeight; 634 } 635 636 637 public void setExpandedHeight(float height) { 638 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 639 if (mTimeAnimator.isStarted()) { 640 post(mStopAnimator); 641 } 642 setExpandedHeightInternal(height); 643 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 644 } 645 646 @Override 647 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 648 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom, 649 (int)mExpandedHeight, mMaxPanelHeight); 650 super.onLayout(changed, left, top, right, bottom); 651 requestPanelHeightUpdate(); 652 } 653 654 protected void requestPanelHeightUpdate() { 655 float currentMaxPanelHeight = getMaxPanelHeight(); 656 657 // If the user isn't actively poking us, let's update the height 658 if (!mTracking && !mTimeAnimator.isStarted() 659 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) { 660 setExpandedHeightInternal(currentMaxPanelHeight); 661 } 662 } 663 664 public void setExpandedHeightInternal(float h) { 665 if (Float.isNaN(h)) { 666 // If a NaN gets in here, it will freeze the Animators. 667 if (DEBUG_NAN) { 668 Log.v(TAG, "setExpandedHeightInternal: warning: h=NaN, using 0 instead", 669 new Throwable()); 670 } 671 h = 0; 672 } 673 674 float fh = getMaxPanelHeight(); 675 if (fh == 0) { 676 // Hmm, full height hasn't been computed yet 677 } 678 679 if (h < 0) h = 0; 680 if (h > fh) h = fh; 681 682 mExpandedHeight = h; 683 684 if (DEBUG) { 685 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, 686 mTracking ? "T" : "f"); 687 } 688 689 onHeightUpdated(mExpandedHeight); 690 691// FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); 692// lp.height = (int) mExpandedHeight; 693// setLayoutParams(lp); 694 695 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh); 696 } 697 698 protected void onHeightUpdated(float expandedHeight) { 699 requestLayout(); 700 } 701 702 /** 703 * This returns the maximum height of the panel. Children should override this if their 704 * desired height is not the full height. 705 * 706 * @return the default implementation simply returns the maximum height. 707 */ 708 protected int getMaxPanelHeight() { 709 return mMaxPanelHeight; 710 } 711 712 public void setExpandedFraction(float frac) { 713 if (Float.isNaN(frac)) { 714 // If a NaN gets in here, it will freeze the Animators. 715 if (DEBUG_NAN) { 716 Log.v(TAG, "setExpandedFraction: frac=NaN, using 0 instead", 717 new Throwable()); 718 } 719 frac = 0; 720 } 721 setExpandedHeight(getMaxPanelHeight() * frac); 722 } 723 724 public float getExpandedHeight() { 725 return mExpandedHeight; 726 } 727 728 public float getExpandedFraction() { 729 return mExpandedFraction; 730 } 731 732 public boolean isFullyExpanded() { 733 return mExpandedHeight >= getMaxPanelHeight(); 734 } 735 736 public boolean isFullyCollapsed() { 737 return mExpandedHeight <= 0; 738 } 739 740 public boolean isCollapsing() { 741 return mClosing; 742 } 743 744 public boolean isTracking() { 745 return mTracking; 746 } 747 748 public void setBar(PanelBar panelBar) { 749 mBar = panelBar; 750 } 751 752 public void collapse() { 753 // TODO: abort animation or ongoing touch 754 if (DEBUG) logf("collapse: " + this); 755 if (!isFullyCollapsed()) { 756 mTimeAnimator.cancel(); 757 mClosing = true; 758 onExpandingStarted(); 759 // collapse() should never be a rubberband, even if an animation is already running 760 fling(-mSelfCollapseVelocityPx, /*always=*/ true); 761 } 762 } 763 764 public void expand() { 765 if (DEBUG) logf("expand: " + this); 766 if (isFullyCollapsed()) { 767 mBar.startOpeningPanel(this); 768 onExpandingStarted(); 769 fling(mSelfExpandVelocityPx, /*always=*/ true); 770 } else if (DEBUG) { 771 if (DEBUG) logf("skipping expansion: is expanded"); 772 } 773 } 774 775 public void cancelPeek() { 776 if (mPeekAnimator != null && mPeekAnimator.isStarted()) { 777 mPeekAnimator.cancel(); 778 } 779 } 780 781 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 782 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 783 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s" 784 + "]", 785 this.getClass().getSimpleName(), 786 getExpandedHeight(), 787 getMaxPanelHeight(), 788 mClosing?"T":"f", 789 mTracking?"T":"f", 790 mJustPeeked?"T":"f", 791 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 792 mTimeAnimator, ((mTimeAnimator!=null && mTimeAnimator.isStarted())?" (started)":"") 793 )); 794 } 795 796 private final OnHierarchyChangeListener mHierarchyListener = new OnHierarchyChangeListener() { 797 @Override 798 public void onChildViewAdded(View parent, View child) { 799 if (DEBUG) logf("onViewAdded: " + child); 800 } 801 802 @Override 803 public void onChildViewRemoved(View parent, View child) { 804 } 805 }; 806} 807