PanelView.java revision d7daab7b9e33cd56f2692b9ebea87ef21290c1ea
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 mInitialTouchY = y; 530 mInitialTouchX = x; 531 initVelocityTracker(); 532 trackMovement(event); 533 mTimeAnimator.cancel(); // end any outstanding animations 534 break; 535 case MotionEvent.ACTION_POINTER_UP: 536 final int upPointer = event.getPointerId(event.getActionIndex()); 537 if (mTrackingPointer == upPointer) { 538 // gesture is ongoing, find a new pointer to track 539 final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; 540 mTrackingPointer = event.getPointerId(newIndex); 541 mInitialTouchX = event.getX(newIndex); 542 mInitialTouchY = event.getY(newIndex); 543 } 544 break; 545 546 case MotionEvent.ACTION_MOVE: 547 final float h = y - mInitialTouchY; 548 trackMovement(event); 549 if (scrolledToBottom) { 550 if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { 551 mInitialOffsetOnTouch = mExpandedHeight; 552 mInitialTouchY = y; 553 mInitialTouchX = x; 554 mTracking = true; 555 onTrackingStarted(); 556 return true; 557 } 558 } 559 break; 560 } 561 return false; 562 } 563 564 private void initVelocityTracker() { 565 if (mVelocityTracker != null) { 566 mVelocityTracker.recycle(); 567 } 568 mVelocityTracker = FlingTracker.obtain(); 569 } 570 571 protected boolean isScrolledToBottom() { 572 return false; 573 } 574 575 protected float getContentHeight() { 576 return mExpandedHeight; 577 } 578 579 @Override 580 protected void onFinishInflate() { 581 super.onFinishInflate(); 582 583 loadDimens(); 584 } 585 586 public void fling(float vel, boolean always) { 587 if (DEBUG) logf("fling: vel=%.3f, this=%s", vel, this); 588 mVel = vel; 589 590 if (always||mVel != 0) { 591 animationTick(0); // begin the animation 592 } else { 593 onExpandingFinished(); 594 } 595 } 596 597 @Override 598 protected void onAttachedToWindow() { 599 super.onAttachedToWindow(); 600 mViewName = getResources().getResourceName(getId()); 601 } 602 603 public String getName() { 604 return mViewName; 605 } 606 607 // Rubberbands the panel to hold its contents. 608 @Override 609 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 610 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 611 612 if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)", 613 widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); 614 615 // Did one of our children change size? 616 int newHeight = getMeasuredHeight(); 617 if (newHeight != mMaxPanelHeight) { 618 mMaxPanelHeight = newHeight; 619 // If the user isn't actively poking us, let's rubberband to the content 620 if (!mTracking && !mTimeAnimator.isStarted() 621 && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight 622 && mMaxPanelHeight > 0) { 623 mExpandedHeight = mMaxPanelHeight; 624 } 625 } 626 setMeasuredDimension(getMeasuredWidth(), getDesiredMeasureHeight()); 627 } 628 629 protected int getDesiredMeasureHeight() { 630 return (int) mExpandedHeight; 631 } 632 633 634 public void setExpandedHeight(float height) { 635 if (DEBUG) logf("setExpandedHeight(%.1f)", height); 636 if (mTimeAnimator.isStarted()) { 637 post(mStopAnimator); 638 } 639 setExpandedHeightInternal(height); 640 mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); 641 } 642 643 @Override 644 protected void onLayout (boolean changed, int left, int top, int right, int bottom) { 645 if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom, 646 (int)mExpandedHeight, mMaxPanelHeight); 647 super.onLayout(changed, left, top, right, bottom); 648 requestPanelHeightUpdate(); 649 } 650 651 protected void requestPanelHeightUpdate() { 652 float currentMaxPanelHeight = getMaxPanelHeight(); 653 654 // If the user isn't actively poking us, let's update the height 655 if (!mTracking && !mTimeAnimator.isStarted() 656 && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) { 657 setExpandedHeightInternal(currentMaxPanelHeight); 658 } 659 } 660 661 public void setExpandedHeightInternal(float h) { 662 if (Float.isNaN(h)) { 663 // If a NaN gets in here, it will freeze the Animators. 664 if (DEBUG_NAN) { 665 Log.v(TAG, "setExpandedHeightInternal: warning: h=NaN, using 0 instead", 666 new Throwable()); 667 } 668 h = 0; 669 } 670 671 float fh = getMaxPanelHeight(); 672 if (fh == 0) { 673 // Hmm, full height hasn't been computed yet 674 } 675 676 if (h < 0) h = 0; 677 if (h > fh) h = fh; 678 679 mExpandedHeight = h; 680 681 if (DEBUG) { 682 logf("setExpansion: height=%.1f fh=%.1f tracking=%s", h, fh, 683 mTracking ? "T" : "f"); 684 } 685 686 onHeightUpdated(mExpandedHeight); 687 688// FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); 689// lp.height = (int) mExpandedHeight; 690// setLayoutParams(lp); 691 692 mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh); 693 } 694 695 protected void onHeightUpdated(float expandedHeight) { 696 requestLayout(); 697 } 698 699 /** 700 * This returns the maximum height of the panel. Children should override this if their 701 * desired height is not the full height. 702 * 703 * @return the default implementation simply returns the maximum height. 704 */ 705 protected int getMaxPanelHeight() { 706 return mMaxPanelHeight; 707 } 708 709 public void setExpandedFraction(float frac) { 710 if (Float.isNaN(frac)) { 711 // If a NaN gets in here, it will freeze the Animators. 712 if (DEBUG_NAN) { 713 Log.v(TAG, "setExpandedFraction: frac=NaN, using 0 instead", 714 new Throwable()); 715 } 716 frac = 0; 717 } 718 setExpandedHeight(getMaxPanelHeight() * frac); 719 } 720 721 public float getExpandedHeight() { 722 return mExpandedHeight; 723 } 724 725 public float getExpandedFraction() { 726 return mExpandedFraction; 727 } 728 729 public boolean isFullyExpanded() { 730 return mExpandedHeight >= getMaxPanelHeight(); 731 } 732 733 public boolean isFullyCollapsed() { 734 return mExpandedHeight <= 0; 735 } 736 737 public boolean isCollapsing() { 738 return mClosing; 739 } 740 741 public boolean isTracking() { 742 return mTracking; 743 } 744 745 public void setBar(PanelBar panelBar) { 746 mBar = panelBar; 747 } 748 749 public void collapse() { 750 // TODO: abort animation or ongoing touch 751 if (DEBUG) logf("collapse: " + this); 752 if (!isFullyCollapsed()) { 753 mTimeAnimator.cancel(); 754 mClosing = true; 755 onExpandingStarted(); 756 // collapse() should never be a rubberband, even if an animation is already running 757 fling(-mSelfCollapseVelocityPx, /*always=*/ true); 758 } 759 } 760 761 public void expand() { 762 if (DEBUG) logf("expand: " + this); 763 if (isFullyCollapsed()) { 764 mBar.startOpeningPanel(this); 765 onExpandingStarted(); 766 fling(mSelfExpandVelocityPx, /*always=*/ true); 767 } else if (DEBUG) { 768 if (DEBUG) logf("skipping expansion: is expanded"); 769 } 770 } 771 772 public void cancelPeek() { 773 if (mPeekAnimator != null && mPeekAnimator.isStarted()) { 774 mPeekAnimator.cancel(); 775 } 776 } 777 778 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 779 pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" 780 + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s" 781 + "]", 782 this.getClass().getSimpleName(), 783 getExpandedHeight(), 784 getMaxPanelHeight(), 785 mClosing?"T":"f", 786 mTracking?"T":"f", 787 mJustPeeked?"T":"f", 788 mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), 789 mTimeAnimator, ((mTimeAnimator!=null && mTimeAnimator.isStarted())?" (started)":"") 790 )); 791 } 792 793 private final OnHierarchyChangeListener mHierarchyListener = new OnHierarchyChangeListener() { 794 @Override 795 public void onChildViewAdded(View parent, View child) { 796 if (DEBUG) logf("onViewAdded: " + child); 797 } 798 799 @Override 800 public void onChildViewRemoved(View parent, View child) { 801 } 802 }; 803} 804