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