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