Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 1 | package com.android.systemui.statusbar.phone; |
| 2 | |
| 3 | import android.animation.TimeAnimator; |
| 4 | import android.animation.TimeAnimator.TimeListener; |
| 5 | import android.content.Context; |
| 6 | import android.content.res.Resources; |
| 7 | import android.util.AttributeSet; |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 8 | import android.util.Slog; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 9 | import android.view.MotionEvent; |
| 10 | import android.view.VelocityTracker; |
| 11 | import android.view.View; |
| 12 | import android.widget.FrameLayout; |
| 13 | |
| 14 | import com.android.systemui.R; |
| 15 | |
| 16 | public class PanelView extends FrameLayout { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 17 | public static final boolean DEBUG = PanelBar.DEBUG; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 18 | public static final String TAG = PanelView.class.getSimpleName(); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 19 | public final void LOG(String fmt, Object... args) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 20 | if (!DEBUG) return; |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 21 | Slog.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | public static final boolean BRAKES = false; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 25 | private static final boolean STRETCH_PAST_CONTENTS = true; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 26 | |
| 27 | private float mSelfExpandVelocityPx; // classic value: 2000px/s |
| 28 | private float mSelfCollapseVelocityPx; // classic value: 2000px/s (will be negated to collapse "up") |
| 29 | private float mFlingExpandMinVelocityPx; // classic value: 200px/s |
| 30 | private float mFlingCollapseMinVelocityPx; // classic value: 200px/s |
| 31 | private float mCollapseMinDisplayFraction; // classic value: 0.08 (25px/min(320px,480px) on G1) |
| 32 | private float mExpandMinDisplayFraction; // classic value: 0.5 (drag open halfway to expand) |
| 33 | private float mFlingGestureMaxXVelocityPx; // classic value: 150px/s |
| 34 | |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 35 | private float mFlingGestureMinDistPx; |
| 36 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 37 | private float mExpandAccelPx; // classic value: 2000px/s/s |
| 38 | private float mCollapseAccelPx; // classic value: 2000px/s/s (will be negated to collapse "up") |
| 39 | |
| 40 | private float mFlingGestureMaxOutputVelocityPx; // how fast can it really go? (should be a little |
| 41 | // faster than mSelfCollapseVelocityPx) |
| 42 | |
| 43 | private float mCollapseBrakingDistancePx = 200; // XXX Resource |
| 44 | private float mExpandBrakingDistancePx = 150; // XXX Resource |
| 45 | private float mBrakingSpeedPx = 150; // XXX Resource |
| 46 | |
| 47 | private View mHandleView; |
| 48 | private float mTouchOffset; |
| 49 | private float mExpandedFraction = 0; |
| 50 | private float mExpandedHeight = 0; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 51 | private boolean mClosing; |
| 52 | private boolean mRubberbanding; |
| 53 | private boolean mTracking; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 54 | |
| 55 | private TimeAnimator mTimeAnimator; |
| 56 | private VelocityTracker mVelocityTracker; |
| 57 | |
| 58 | private int[] mAbsPos = new int[2]; |
| 59 | PanelBar mBar; |
| 60 | |
| 61 | private final TimeListener mAnimationCallback = new TimeListener() { |
| 62 | @Override |
| 63 | public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) { |
| 64 | animationTick(deltaTime); |
| 65 | } |
| 66 | }; |
| 67 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 68 | private final Runnable mStopAnimator = new Runnable() { |
| 69 | @Override |
| 70 | public void run() { |
| 71 | if (mTimeAnimator.isStarted()) { |
| 72 | mTimeAnimator.end(); |
| 73 | mRubberbanding = false; |
| 74 | mClosing = false; |
| 75 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 76 | } |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 77 | }; |
Daniel Sandler | 5a35a0d | 2012-08-16 13:50:40 -0400 | [diff] [blame] | 78 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 79 | private float mVel, mAccel; |
| 80 | private int mFullHeight = 0; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 81 | private String mViewName; |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 82 | protected float mInitialTouchY; |
| 83 | protected float mFinalTouchY; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 84 | |
| 85 | private void animationTick(long dtms) { |
| 86 | if (!mTimeAnimator.isStarted()) { |
| 87 | // XXX HAX to work around bug in TimeAnimator.end() not resetting its last time |
| 88 | mTimeAnimator = new TimeAnimator(); |
| 89 | mTimeAnimator.setTimeListener(mAnimationCallback); |
| 90 | |
| 91 | mTimeAnimator.start(); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 92 | |
| 93 | mRubberbanding = STRETCH_PAST_CONTENTS // is it enabled at all? |
| 94 | && mExpandedHeight > getFullHeight() // are we past the end? |
| 95 | && mVel >= -mFlingGestureMinDistPx; // was this not possibly a "close" gesture? |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 96 | if (mRubberbanding) { |
| 97 | mClosing = true; |
| 98 | } else if (mVel == 0) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 99 | // if the panel is less than halfway open, close it |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 100 | mClosing = (mFinalTouchY / getFullHeight()) < 0.5f; |
| 101 | } else { |
| 102 | mClosing = mExpandedHeight > 0 && mVel < 0; |
| 103 | } |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 104 | } else if (dtms > 0) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 105 | final float dt = dtms * 0.001f; // ms -> s |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 106 | if (DEBUG) LOG("tick: v=%.2fpx/s dt=%.4fs", mVel, dt); |
| 107 | if (DEBUG) LOG("tick: before: h=%d", (int) mExpandedHeight); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 108 | |
| 109 | final float fh = getFullHeight(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 110 | boolean braking = false; |
| 111 | if (BRAKES) { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 112 | if (mClosing) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 113 | braking = mExpandedHeight <= mCollapseBrakingDistancePx; |
| 114 | mAccel = braking ? 10*mCollapseAccelPx : -mCollapseAccelPx; |
| 115 | } else { |
| 116 | braking = mExpandedHeight >= (fh-mExpandBrakingDistancePx); |
| 117 | mAccel = braking ? 10*-mExpandAccelPx : mExpandAccelPx; |
| 118 | } |
| 119 | } else { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 120 | mAccel = mClosing ? -mCollapseAccelPx : mExpandAccelPx; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | mVel += mAccel * dt; |
| 124 | |
| 125 | if (braking) { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 126 | if (mClosing && mVel > -mBrakingSpeedPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 127 | mVel = -mBrakingSpeedPx; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 128 | } else if (!mClosing && mVel < mBrakingSpeedPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 129 | mVel = mBrakingSpeedPx; |
| 130 | } |
| 131 | } else { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 132 | if (mClosing && mVel > -mFlingCollapseMinVelocityPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 133 | mVel = -mFlingCollapseMinVelocityPx; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 134 | } else if (!mClosing && mVel > mFlingGestureMaxOutputVelocityPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 135 | mVel = mFlingGestureMaxOutputVelocityPx; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | float h = mExpandedHeight + mVel * dt; |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 140 | |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 141 | if (mRubberbanding && h < fh) { |
| 142 | h = fh; |
| 143 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 144 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 145 | if (DEBUG) LOG("tick: new h=%d closing=%s", (int) h, mClosing?"true":"false"); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 146 | |
| 147 | setExpandedHeightInternal(h); |
| 148 | |
| 149 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
| 150 | |
| 151 | if (mVel == 0 |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 152 | || (mClosing && mExpandedHeight == 0) |
| 153 | || ((mRubberbanding || !mClosing) && mExpandedHeight == fh)) { |
Daniel Sandler | 5a35a0d | 2012-08-16 13:50:40 -0400 | [diff] [blame] | 154 | post(mStopAnimator); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | public PanelView(Context context, AttributeSet attrs) { |
| 160 | super(context, attrs); |
| 161 | |
| 162 | mTimeAnimator = new TimeAnimator(); |
| 163 | mTimeAnimator.setTimeListener(mAnimationCallback); |
| 164 | } |
| 165 | |
| 166 | private void loadDimens() { |
| 167 | final Resources res = getContext().getResources(); |
| 168 | |
| 169 | mSelfExpandVelocityPx = res.getDimension(R.dimen.self_expand_velocity); |
| 170 | mSelfCollapseVelocityPx = res.getDimension(R.dimen.self_collapse_velocity); |
| 171 | mFlingExpandMinVelocityPx = res.getDimension(R.dimen.fling_expand_min_velocity); |
| 172 | mFlingCollapseMinVelocityPx = res.getDimension(R.dimen.fling_collapse_min_velocity); |
| 173 | |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 174 | mFlingGestureMinDistPx = res.getDimension(R.dimen.fling_gesture_min_dist); |
| 175 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 176 | mCollapseMinDisplayFraction = res.getFraction(R.dimen.collapse_min_display_fraction, 1, 1); |
| 177 | mExpandMinDisplayFraction = res.getFraction(R.dimen.expand_min_display_fraction, 1, 1); |
| 178 | |
| 179 | mExpandAccelPx = res.getDimension(R.dimen.expand_accel); |
| 180 | mCollapseAccelPx = res.getDimension(R.dimen.collapse_accel); |
| 181 | |
| 182 | mFlingGestureMaxXVelocityPx = res.getDimension(R.dimen.fling_gesture_max_x_velocity); |
| 183 | |
| 184 | mFlingGestureMaxOutputVelocityPx = res.getDimension(R.dimen.fling_gesture_max_output_velocity); |
| 185 | } |
| 186 | |
| 187 | private void trackMovement(MotionEvent event) { |
| 188 | // Add movement to velocity tracker using raw screen X and Y coordinates instead |
| 189 | // of window coordinates because the window frame may be moving at the same time. |
| 190 | float deltaX = event.getRawX() - event.getX(); |
| 191 | float deltaY = event.getRawY() - event.getY(); |
| 192 | event.offsetLocation(deltaX, deltaY); |
| 193 | mVelocityTracker.addMovement(event); |
| 194 | event.offsetLocation(-deltaX, -deltaY); |
| 195 | } |
| 196 | |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 197 | // Pass all touches along to the handle, allowing the user to drag the panel closed from its interior |
| 198 | @Override |
| 199 | public boolean onTouchEvent(MotionEvent event) { |
| 200 | return mHandleView.dispatchTouchEvent(event); |
| 201 | } |
| 202 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 203 | @Override |
| 204 | protected void onFinishInflate() { |
| 205 | super.onFinishInflate(); |
| 206 | loadDimens(); |
| 207 | |
| 208 | mHandleView = findViewById(R.id.handle); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 209 | if (DEBUG) LOG("handle view: " + mHandleView); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 210 | if (mHandleView != null) { |
| 211 | mHandleView.setOnTouchListener(new View.OnTouchListener() { |
| 212 | @Override |
| 213 | public boolean onTouch(View v, MotionEvent event) { |
| 214 | final float y = event.getY(); |
| 215 | final float rawY = event.getRawY(); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 216 | if (DEBUG) LOG("handle.onTouch: a=%s y=%.1f rawY=%.1f off=%.1f", |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 217 | MotionEvent.actionToString(event.getAction()), |
| 218 | y, rawY, mTouchOffset); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 219 | PanelView.this.getLocationOnScreen(mAbsPos); |
| 220 | |
| 221 | switch (event.getAction()) { |
| 222 | case MotionEvent.ACTION_DOWN: |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 223 | mTracking = true; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 224 | mHandleView.setPressed(true); |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 225 | mInitialTouchY = y; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 226 | mVelocityTracker = VelocityTracker.obtain(); |
| 227 | trackMovement(event); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 228 | mTimeAnimator.cancel(); // end any outstanding animations |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 229 | mBar.onTrackingStarted(PanelView.this); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 230 | mTouchOffset = (rawY - mAbsPos[1]) - PanelView.this.getExpandedHeight(); |
| 231 | break; |
| 232 | |
| 233 | case MotionEvent.ACTION_MOVE: |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 234 | PanelView.this.setExpandedHeightInternal(rawY - mAbsPos[1] - mTouchOffset); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 235 | |
| 236 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
| 237 | |
| 238 | trackMovement(event); |
| 239 | break; |
| 240 | |
| 241 | case MotionEvent.ACTION_UP: |
| 242 | case MotionEvent.ACTION_CANCEL: |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 243 | mFinalTouchY = y; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 244 | mTracking = false; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 245 | mHandleView.setPressed(false); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 246 | mBar.onTrackingStopped(PanelView.this); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 247 | trackMovement(event); |
| 248 | mVelocityTracker.computeCurrentVelocity(1000); |
| 249 | |
| 250 | float yVel = mVelocityTracker.getYVelocity(); |
| 251 | boolean negative = yVel < 0; |
| 252 | |
| 253 | float xVel = mVelocityTracker.getXVelocity(); |
| 254 | if (xVel < 0) { |
| 255 | xVel = -xVel; |
| 256 | } |
| 257 | if (xVel > mFlingGestureMaxXVelocityPx) { |
| 258 | xVel = mFlingGestureMaxXVelocityPx; // limit how much we care about the x axis |
| 259 | } |
| 260 | |
| 261 | float vel = (float)Math.hypot(yVel, xVel); |
| 262 | if (vel > mFlingGestureMaxOutputVelocityPx) { |
| 263 | vel = mFlingGestureMaxOutputVelocityPx; |
| 264 | } |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 265 | |
| 266 | // if you've barely moved your finger, we treat the velocity as 0 |
| 267 | // preventing spurious flings due to touch screen jitter |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 268 | final float deltaY = Math.abs(mFinalTouchY - mInitialTouchY); |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 269 | if (deltaY < mFlingGestureMinDistPx |
| 270 | || vel < mFlingGestureMinDistPx) { |
| 271 | vel = 0; |
| 272 | } |
| 273 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 274 | if (negative) { |
| 275 | vel = -vel; |
| 276 | } |
| 277 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 278 | if (DEBUG) LOG("gesture: dy=%f vraw=(%f,%f) vnorm=(%f,%f) vlinear=%f", |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 279 | deltaY, |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 280 | mVelocityTracker.getXVelocity(), |
| 281 | mVelocityTracker.getYVelocity(), |
| 282 | xVel, yVel, |
| 283 | vel); |
| 284 | |
Daniel Sandler | cf591db | 2012-08-15 16:11:55 -0400 | [diff] [blame] | 285 | fling(vel, true); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 286 | |
| 287 | mVelocityTracker.recycle(); |
| 288 | mVelocityTracker = null; |
| 289 | |
| 290 | break; |
| 291 | } |
| 292 | return true; |
| 293 | }}); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | public void fling(float vel, boolean always) { |
| 298 | mVel = vel; |
| 299 | |
Daniel Sandler | cf591db | 2012-08-15 16:11:55 -0400 | [diff] [blame] | 300 | if (always||mVel != 0) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 301 | animationTick(0); // begin the animation |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | protected void onAttachedToWindow() { |
| 307 | super.onAttachedToWindow(); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 308 | mViewName = getResources().getResourceName(getId()); |
| 309 | } |
| 310 | |
| 311 | public String getName() { |
| 312 | return mViewName; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | protected void onViewAdded(View child) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 317 | if (DEBUG) LOG("onViewAdded: " + child); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | public View getHandle() { |
| 321 | return mHandleView; |
| 322 | } |
| 323 | |
| 324 | // Rubberbands the panel to hold its contents. |
| 325 | @Override |
| 326 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 327 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 328 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 329 | if (DEBUG) LOG("onMeasure(%d, %d) -> (%d, %d)", |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 330 | widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 331 | |
| 332 | // Did one of our children change size? |
| 333 | int newHeight = getMeasuredHeight(); |
| 334 | if (newHeight != mFullHeight) { |
| 335 | mFullHeight = newHeight; |
| 336 | // If the user isn't actively poking us, let's rubberband to the content |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 337 | if (!mTracking && !mRubberbanding && !mTimeAnimator.isStarted() |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 338 | && mExpandedHeight > 0 && mExpandedHeight != mFullHeight) { |
| 339 | mExpandedHeight = mFullHeight; |
| 340 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 341 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 342 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 343 | (int) mExpandedHeight, MeasureSpec.AT_MOST); // MeasureSpec.getMode(heightMeasureSpec)); |
| 344 | setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | public void setExpandedHeight(float height) { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 349 | mTracking = mRubberbanding = false; |
Daniel Sandler | 5a35a0d | 2012-08-16 13:50:40 -0400 | [diff] [blame] | 350 | post(mStopAnimator); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 351 | setExpandedHeightInternal(height); |
| 352 | } |
| 353 | |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 354 | @Override |
| 355 | protected void onLayout (boolean changed, int left, int top, int right, int bottom) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 356 | if (DEBUG) LOG("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom, (int)mExpandedHeight, mFullHeight); |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 357 | super.onLayout(changed, left, top, right, bottom); |
| 358 | } |
| 359 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 360 | public void setExpandedHeightInternal(float h) { |
| 361 | float fh = getFullHeight(); |
| 362 | if (fh == 0) { |
| 363 | // Hmm, full height hasn't been computed yet |
| 364 | } |
| 365 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 366 | if (h < 0) h = 0; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 367 | if (!(STRETCH_PAST_CONTENTS && (mTracking || mRubberbanding)) && h > fh) h = fh; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 368 | mExpandedHeight = h; |
| 369 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 370 | if (DEBUG) LOG("setExpansion: height=%.1f fh=%.1f tracking=%s rubber=%s", h, fh, mTracking?"T":"f", mRubberbanding?"T":"f"); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 371 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 372 | requestLayout(); |
| 373 | // FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 374 | // lp.height = (int) mExpandedHeight; |
| 375 | // setLayoutParams(lp); |
| 376 | |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 377 | mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | private float getFullHeight() { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 381 | if (mFullHeight <= 0) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 382 | if (DEBUG) LOG("Forcing measure() since fullHeight=" + mFullHeight); |
| 383 | measure(MeasureSpec.makeMeasureSpec(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY), |
| 384 | MeasureSpec.makeMeasureSpec(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY)); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 385 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 386 | return mFullHeight; |
| 387 | } |
| 388 | |
| 389 | public void setExpandedFraction(float frac) { |
| 390 | setExpandedHeight(getFullHeight() * frac); |
| 391 | } |
| 392 | |
| 393 | public float getExpandedHeight() { |
| 394 | return mExpandedHeight; |
| 395 | } |
| 396 | |
| 397 | public float getExpandedFraction() { |
| 398 | return mExpandedFraction; |
| 399 | } |
| 400 | |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 401 | public boolean isFullyExpanded() { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 402 | return mExpandedHeight >= getFullHeight(); |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | public boolean isFullyCollapsed() { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 406 | return mExpandedHeight <= 0; |
| 407 | } |
| 408 | |
| 409 | public boolean isCollapsing() { |
| 410 | return mClosing; |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 413 | public void setBar(PanelBar panelBar) { |
| 414 | mBar = panelBar; |
| 415 | } |
| 416 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 417 | public void collapse() { |
| 418 | // TODO: abort animation or ongoing touch |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 419 | if (!isFullyCollapsed()) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 420 | // collapse() should never be a rubberband, even if an animation is already running |
| 421 | mRubberbanding = false; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 422 | fling(-mSelfCollapseVelocityPx, /*always=*/ true); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | public void expand() { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 427 | if (isFullyCollapsed()) { |
| 428 | mBar.startOpeningPanel(this); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 429 | if (DEBUG) LOG("expand: calling fling(%s, true)", mSelfExpandVelocityPx); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 430 | fling (mSelfExpandVelocityPx, /*always=*/ true); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 431 | } else if (DEBUG) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame^] | 432 | if (DEBUG) LOG("skipping expansion: is expanded"); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |