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 | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame^] | 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
Daniel Sandler | 6f7654d | 2012-11-30 15:28:38 -0500 | [diff] [blame] | 21 | import java.util.ArrayDeque; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Iterator; |
| 24 | |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 25 | import android.animation.ObjectAnimator; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 26 | import android.animation.TimeAnimator; |
| 27 | import android.animation.TimeAnimator.TimeListener; |
| 28 | import android.content.Context; |
| 29 | import android.content.res.Resources; |
| 30 | import android.util.AttributeSet; |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 31 | import android.util.Slog; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 32 | import android.view.MotionEvent; |
| 33 | import android.view.VelocityTracker; |
| 34 | import android.view.View; |
| 35 | import android.widget.FrameLayout; |
| 36 | |
| 37 | import com.android.systemui.R; |
| 38 | |
| 39 | public class PanelView extends FrameLayout { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 40 | public static final boolean DEBUG = PanelBar.DEBUG; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 41 | public static final String TAG = PanelView.class.getSimpleName(); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 42 | public final void LOG(String fmt, Object... args) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 43 | if (!DEBUG) return; |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 44 | Slog.v(TAG, (mViewName != null ? (mViewName + ": ") : "") + String.format(fmt, args)); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public static final boolean BRAKES = false; |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 48 | private boolean mRubberbandingEnabled = true; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 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 | |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 58 | private float mFlingGestureMinDistPx; |
| 59 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 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 View mHandleView; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 71 | private float mPeekHeight; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 72 | private float mTouchOffset; |
| 73 | private float mExpandedFraction = 0; |
| 74 | private float mExpandedHeight = 0; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 75 | private boolean mJustPeeked; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 76 | private boolean mClosing; |
| 77 | private boolean mRubberbanding; |
| 78 | private boolean mTracking; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 79 | |
| 80 | private TimeAnimator mTimeAnimator; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 81 | private ObjectAnimator mPeekAnimator; |
Daniel Sandler | 6f7654d | 2012-11-30 15:28:38 -0500 | [diff] [blame] | 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 | Slog.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.descendingIterator(); |
| 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 | Slog.v("FlingTracker", String.format(" [%d] dx=%.1f dy=%.1f dt=%.0f vx=%.1f vy=%.1f", |
| 130 | i, |
| 131 | dx, dy, dt, |
| 132 | (dx/dt), |
| 133 | (dy/dt) |
| 134 | )); |
| 135 | } |
| 136 | mVX += weight * dx / dt; |
| 137 | mVY += weight * dy / dt; |
| 138 | totalweight += weight; |
| 139 | weight *= DECAY; |
| 140 | } |
| 141 | last = event; |
| 142 | i++; |
| 143 | } |
| 144 | mVX /= totalweight; |
| 145 | mVY /= totalweight; |
| 146 | |
| 147 | if (FlingTracker.DEBUG) { |
| 148 | Slog.v("FlingTracker", "computed: vx=" + mVX + " vy=" + mVY); |
| 149 | } |
| 150 | } |
| 151 | public float getXVelocity() { |
| 152 | return mVX; |
| 153 | } |
| 154 | public float getYVelocity() { |
| 155 | return mVY; |
| 156 | } |
| 157 | public void recycle() { |
| 158 | mEventBuf.clear(); |
| 159 | } |
| 160 | |
| 161 | static FlingTracker sTracker; |
| 162 | static FlingTracker obtain() { |
| 163 | if (sTracker == null) { |
| 164 | sTracker = new FlingTracker(); |
| 165 | } |
| 166 | return sTracker; |
| 167 | } |
| 168 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 169 | |
| 170 | private int[] mAbsPos = new int[2]; |
| 171 | PanelBar mBar; |
| 172 | |
| 173 | private final TimeListener mAnimationCallback = new TimeListener() { |
| 174 | @Override |
| 175 | public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) { |
| 176 | animationTick(deltaTime); |
| 177 | } |
| 178 | }; |
| 179 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 180 | private final Runnable mStopAnimator = new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 183 | if (mTimeAnimator != null && mTimeAnimator.isStarted()) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 184 | mTimeAnimator.end(); |
| 185 | mRubberbanding = false; |
| 186 | mClosing = false; |
| 187 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 188 | } |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 189 | }; |
Daniel Sandler | 5a35a0d | 2012-08-16 13:50:40 -0400 | [diff] [blame] | 190 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 191 | private float mVel, mAccel; |
| 192 | private int mFullHeight = 0; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 193 | private String mViewName; |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 194 | protected float mInitialTouchY; |
| 195 | protected float mFinalTouchY; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 196 | |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 197 | public void setRubberbandingEnabled(boolean enable) { |
| 198 | mRubberbandingEnabled = enable; |
| 199 | } |
| 200 | |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 201 | private void runPeekAnimation() { |
| 202 | if (DEBUG) LOG("peek to height=%.1f", mPeekHeight); |
| 203 | if (mTimeAnimator.isStarted()) { |
| 204 | return; |
| 205 | } |
| 206 | if (mPeekAnimator == null) { |
| 207 | mPeekAnimator = ObjectAnimator.ofFloat(this, |
| 208 | "expandedHeight", mPeekHeight) |
Daniel Sandler | 3679bf5 | 2012-10-16 21:30:28 -0400 | [diff] [blame] | 209 | .setDuration(250); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 210 | } |
| 211 | mPeekAnimator.start(); |
| 212 | } |
| 213 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 214 | private void animationTick(long dtms) { |
| 215 | if (!mTimeAnimator.isStarted()) { |
| 216 | // XXX HAX to work around bug in TimeAnimator.end() not resetting its last time |
| 217 | mTimeAnimator = new TimeAnimator(); |
| 218 | mTimeAnimator.setTimeListener(mAnimationCallback); |
| 219 | |
Daniel Sandler | a801f68 | 2012-10-05 11:01:05 -0400 | [diff] [blame] | 220 | if (mPeekAnimator != null) mPeekAnimator.cancel(); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 221 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 222 | mTimeAnimator.start(); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 223 | |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 224 | mRubberbanding = mRubberbandingEnabled // is it enabled at all? |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 225 | && mExpandedHeight > getFullHeight() // are we past the end? |
| 226 | && mVel >= -mFlingGestureMinDistPx; // was this not possibly a "close" gesture? |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 227 | if (mRubberbanding) { |
| 228 | mClosing = true; |
| 229 | } else if (mVel == 0) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 230 | // if the panel is less than halfway open, close it |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 231 | mClosing = (mFinalTouchY / getFullHeight()) < 0.5f; |
| 232 | } else { |
| 233 | mClosing = mExpandedHeight > 0 && mVel < 0; |
| 234 | } |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 235 | } else if (dtms > 0) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 236 | final float dt = dtms * 0.001f; // ms -> s |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 237 | if (DEBUG) LOG("tick: v=%.2fpx/s dt=%.4fs", mVel, dt); |
| 238 | if (DEBUG) LOG("tick: before: h=%d", (int) mExpandedHeight); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 239 | |
| 240 | final float fh = getFullHeight(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 241 | boolean braking = false; |
| 242 | if (BRAKES) { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 243 | if (mClosing) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 244 | braking = mExpandedHeight <= mCollapseBrakingDistancePx; |
| 245 | mAccel = braking ? 10*mCollapseAccelPx : -mCollapseAccelPx; |
| 246 | } else { |
| 247 | braking = mExpandedHeight >= (fh-mExpandBrakingDistancePx); |
| 248 | mAccel = braking ? 10*-mExpandAccelPx : mExpandAccelPx; |
| 249 | } |
| 250 | } else { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 251 | mAccel = mClosing ? -mCollapseAccelPx : mExpandAccelPx; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | mVel += mAccel * dt; |
| 255 | |
| 256 | if (braking) { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 257 | if (mClosing && mVel > -mBrakingSpeedPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 258 | mVel = -mBrakingSpeedPx; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 259 | } else if (!mClosing && mVel < mBrakingSpeedPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 260 | mVel = mBrakingSpeedPx; |
| 261 | } |
| 262 | } else { |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 263 | if (mClosing && mVel > -mFlingCollapseMinVelocityPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 264 | mVel = -mFlingCollapseMinVelocityPx; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 265 | } else if (!mClosing && mVel > mFlingGestureMaxOutputVelocityPx) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 266 | mVel = mFlingGestureMaxOutputVelocityPx; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | float h = mExpandedHeight + mVel * dt; |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 271 | |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 272 | if (mRubberbanding && h < fh) { |
| 273 | h = fh; |
| 274 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 275 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 276 | 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] | 277 | |
| 278 | setExpandedHeightInternal(h); |
| 279 | |
| 280 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
| 281 | |
| 282 | if (mVel == 0 |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 283 | || (mClosing && mExpandedHeight == 0) |
| 284 | || ((mRubberbanding || !mClosing) && mExpandedHeight == fh)) { |
Daniel Sandler | 5a35a0d | 2012-08-16 13:50:40 -0400 | [diff] [blame] | 285 | post(mStopAnimator); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public PanelView(Context context, AttributeSet attrs) { |
| 291 | super(context, attrs); |
| 292 | |
| 293 | mTimeAnimator = new TimeAnimator(); |
| 294 | mTimeAnimator.setTimeListener(mAnimationCallback); |
| 295 | } |
| 296 | |
| 297 | private void loadDimens() { |
| 298 | final Resources res = getContext().getResources(); |
| 299 | |
| 300 | mSelfExpandVelocityPx = res.getDimension(R.dimen.self_expand_velocity); |
| 301 | mSelfCollapseVelocityPx = res.getDimension(R.dimen.self_collapse_velocity); |
| 302 | mFlingExpandMinVelocityPx = res.getDimension(R.dimen.fling_expand_min_velocity); |
| 303 | mFlingCollapseMinVelocityPx = res.getDimension(R.dimen.fling_collapse_min_velocity); |
| 304 | |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 305 | mFlingGestureMinDistPx = res.getDimension(R.dimen.fling_gesture_min_dist); |
| 306 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 307 | mCollapseMinDisplayFraction = res.getFraction(R.dimen.collapse_min_display_fraction, 1, 1); |
| 308 | mExpandMinDisplayFraction = res.getFraction(R.dimen.expand_min_display_fraction, 1, 1); |
| 309 | |
| 310 | mExpandAccelPx = res.getDimension(R.dimen.expand_accel); |
| 311 | mCollapseAccelPx = res.getDimension(R.dimen.collapse_accel); |
| 312 | |
| 313 | mFlingGestureMaxXVelocityPx = res.getDimension(R.dimen.fling_gesture_max_x_velocity); |
| 314 | |
| 315 | mFlingGestureMaxOutputVelocityPx = res.getDimension(R.dimen.fling_gesture_max_output_velocity); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 316 | |
Daniel Sandler | 3679bf5 | 2012-10-16 21:30:28 -0400 | [diff] [blame] | 317 | mPeekHeight = res.getDimension(R.dimen.peek_height) |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 318 | + getPaddingBottom() // our window might have a dropshadow |
| 319 | - (mHandleView == null ? 0 : mHandleView.getPaddingTop()); // the handle might have a topshadow |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | private void trackMovement(MotionEvent event) { |
| 323 | // Add movement to velocity tracker using raw screen X and Y coordinates instead |
| 324 | // of window coordinates because the window frame may be moving at the same time. |
| 325 | float deltaX = event.getRawX() - event.getX(); |
| 326 | float deltaY = event.getRawY() - event.getY(); |
| 327 | event.offsetLocation(deltaX, deltaY); |
Daniel Sandler | b17a726 | 2012-10-05 14:32:50 -0400 | [diff] [blame] | 328 | if (mVelocityTracker != null) mVelocityTracker.addMovement(event); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 329 | event.offsetLocation(-deltaX, -deltaY); |
| 330 | } |
| 331 | |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 332 | // Pass all touches along to the handle, allowing the user to drag the panel closed from its interior |
| 333 | @Override |
| 334 | public boolean onTouchEvent(MotionEvent event) { |
| 335 | return mHandleView.dispatchTouchEvent(event); |
| 336 | } |
| 337 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 338 | @Override |
| 339 | protected void onFinishInflate() { |
| 340 | super.onFinishInflate(); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 341 | mHandleView = findViewById(R.id.handle); |
| 342 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 343 | loadDimens(); |
| 344 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 345 | if (DEBUG) LOG("handle view: " + mHandleView); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 346 | if (mHandleView != null) { |
| 347 | mHandleView.setOnTouchListener(new View.OnTouchListener() { |
| 348 | @Override |
| 349 | public boolean onTouch(View v, MotionEvent event) { |
| 350 | final float y = event.getY(); |
| 351 | final float rawY = event.getRawY(); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 352 | 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] | 353 | MotionEvent.actionToString(event.getAction()), |
| 354 | y, rawY, mTouchOffset); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 355 | PanelView.this.getLocationOnScreen(mAbsPos); |
| 356 | |
| 357 | switch (event.getAction()) { |
| 358 | case MotionEvent.ACTION_DOWN: |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 359 | mTracking = true; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 360 | mHandleView.setPressed(true); |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 361 | postInvalidate(); // catch the press state change |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 362 | mInitialTouchY = y; |
Daniel Sandler | 6f7654d | 2012-11-30 15:28:38 -0500 | [diff] [blame] | 363 | mVelocityTracker = FlingTracker.obtain(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 364 | trackMovement(event); |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 365 | mTimeAnimator.cancel(); // end any outstanding animations |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 366 | mBar.onTrackingStarted(PanelView.this); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 367 | mTouchOffset = (rawY - mAbsPos[1]) - PanelView.this.getExpandedHeight(); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 368 | if (mExpandedHeight == 0) { |
| 369 | mJustPeeked = true; |
| 370 | runPeekAnimation(); |
| 371 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 372 | break; |
| 373 | |
| 374 | case MotionEvent.ACTION_MOVE: |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 375 | final float h = rawY - mAbsPos[1] - mTouchOffset; |
| 376 | if (h > mPeekHeight) { |
Daniel Sandler | a801f68 | 2012-10-05 11:01:05 -0400 | [diff] [blame] | 377 | if (mPeekAnimator != null && mPeekAnimator.isRunning()) { |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 378 | mPeekAnimator.cancel(); |
| 379 | } |
| 380 | mJustPeeked = false; |
| 381 | } |
| 382 | if (!mJustPeeked) { |
| 383 | PanelView.this.setExpandedHeightInternal(h); |
| 384 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
| 385 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 386 | |
| 387 | trackMovement(event); |
| 388 | break; |
| 389 | |
| 390 | case MotionEvent.ACTION_UP: |
| 391 | case MotionEvent.ACTION_CANCEL: |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 392 | mFinalTouchY = y; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 393 | mTracking = false; |
Daniel Sandler | 13522a2 | 2012-09-27 14:46:58 -0400 | [diff] [blame] | 394 | mHandleView.setPressed(false); |
Daniel Sandler | 040c2e4 | 2012-10-17 00:56:33 -0400 | [diff] [blame] | 395 | postInvalidate(); // catch the press state change |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 396 | mBar.onTrackingStopped(PanelView.this); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 397 | trackMovement(event); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 398 | |
Daniel Sandler | 9d09824 | 2012-10-07 23:27:30 -0400 | [diff] [blame] | 399 | float vel = 0, yVel = 0, xVel = 0; |
| 400 | boolean negative = false; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 401 | |
Daniel Sandler | 9d09824 | 2012-10-07 23:27:30 -0400 | [diff] [blame] | 402 | if (mVelocityTracker != null) { |
| 403 | // the velocitytracker might be null if we got a bad input stream |
| 404 | mVelocityTracker.computeCurrentVelocity(1000); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 405 | |
Daniel Sandler | 9d09824 | 2012-10-07 23:27:30 -0400 | [diff] [blame] | 406 | yVel = mVelocityTracker.getYVelocity(); |
| 407 | negative = yVel < 0; |
| 408 | |
| 409 | xVel = mVelocityTracker.getXVelocity(); |
| 410 | if (xVel < 0) { |
| 411 | xVel = -xVel; |
| 412 | } |
| 413 | if (xVel > mFlingGestureMaxXVelocityPx) { |
| 414 | xVel = mFlingGestureMaxXVelocityPx; // limit how much we care about the x axis |
| 415 | } |
| 416 | |
| 417 | vel = (float)Math.hypot(yVel, xVel); |
| 418 | if (vel > mFlingGestureMaxOutputVelocityPx) { |
| 419 | vel = mFlingGestureMaxOutputVelocityPx; |
| 420 | } |
| 421 | |
| 422 | mVelocityTracker.recycle(); |
| 423 | mVelocityTracker = null; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 424 | } |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 425 | |
| 426 | // if you've barely moved your finger, we treat the velocity as 0 |
| 427 | // preventing spurious flings due to touch screen jitter |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 428 | final float deltaY = Math.abs(mFinalTouchY - mInitialTouchY); |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 429 | if (deltaY < mFlingGestureMinDistPx |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 430 | || vel < mFlingExpandMinVelocityPx |
Daniel Sandler | be2cf32 | 2012-10-24 15:23:42 -0400 | [diff] [blame] | 431 | ) { |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 432 | vel = 0; |
| 433 | } |
| 434 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 435 | if (negative) { |
| 436 | vel = -vel; |
| 437 | } |
| 438 | |
Daniel Sandler | be2cf32 | 2012-10-24 15:23:42 -0400 | [diff] [blame] | 439 | if (DEBUG) LOG("gesture: dy=%f vel=(%f,%f) vlinear=%f", |
Daniel Sandler | 173bae2 | 2012-09-25 14:37:42 -0400 | [diff] [blame] | 440 | deltaY, |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 441 | xVel, yVel, |
| 442 | vel); |
| 443 | |
Daniel Sandler | cf591db | 2012-08-15 16:11:55 -0400 | [diff] [blame] | 444 | fling(vel, true); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 445 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | return true; |
| 449 | }}); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | public void fling(float vel, boolean always) { |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 454 | if (DEBUG) LOG("fling: vel=%.3f, this=%s", vel, this); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 455 | mVel = vel; |
| 456 | |
Daniel Sandler | cf591db | 2012-08-15 16:11:55 -0400 | [diff] [blame] | 457 | if (always||mVel != 0) { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 458 | animationTick(0); // begin the animation |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | protected void onAttachedToWindow() { |
| 464 | super.onAttachedToWindow(); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 465 | mViewName = getResources().getResourceName(getId()); |
| 466 | } |
| 467 | |
| 468 | public String getName() { |
| 469 | return mViewName; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | @Override |
| 473 | protected void onViewAdded(View child) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 474 | if (DEBUG) LOG("onViewAdded: " + child); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | public View getHandle() { |
| 478 | return mHandleView; |
| 479 | } |
| 480 | |
| 481 | // Rubberbands the panel to hold its contents. |
| 482 | @Override |
| 483 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 484 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 485 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 486 | if (DEBUG) LOG("onMeasure(%d, %d) -> (%d, %d)", |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 487 | widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 488 | |
| 489 | // Did one of our children change size? |
| 490 | int newHeight = getMeasuredHeight(); |
| 491 | if (newHeight != mFullHeight) { |
| 492 | mFullHeight = newHeight; |
| 493 | // 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] | 494 | if (!mTracking && !mRubberbanding && !mTimeAnimator.isStarted() |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 495 | && mExpandedHeight > 0 && mExpandedHeight != mFullHeight) { |
| 496 | mExpandedHeight = mFullHeight; |
| 497 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 498 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 499 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 500 | (int) mExpandedHeight, MeasureSpec.AT_MOST); // MeasureSpec.getMode(heightMeasureSpec)); |
| 501 | setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | public void setExpandedHeight(float height) { |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 506 | if (DEBUG) LOG("setExpandedHeight(%.1f)", height); |
Daniel Sandler | a801f68 | 2012-10-05 11:01:05 -0400 | [diff] [blame] | 507 | mRubberbanding = false; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 508 | if (mTimeAnimator.isRunning()) { |
| 509 | post(mStopAnimator); |
| 510 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 511 | setExpandedHeightInternal(height); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 512 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 515 | @Override |
| 516 | 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] | 517 | 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] | 518 | super.onLayout(changed, left, top, right, bottom); |
| 519 | } |
| 520 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 521 | public void setExpandedHeightInternal(float h) { |
| 522 | float fh = getFullHeight(); |
| 523 | if (fh == 0) { |
| 524 | // Hmm, full height hasn't been computed yet |
| 525 | } |
| 526 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 527 | if (h < 0) h = 0; |
Daniel Sandler | efb0faf | 2012-10-10 14:15:34 -0700 | [diff] [blame] | 528 | if (!(mRubberbandingEnabled && (mTracking || mRubberbanding)) && h > fh) h = fh; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 529 | mExpandedHeight = h; |
| 530 | |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 531 | 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] | 532 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 533 | requestLayout(); |
| 534 | // FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 535 | // lp.height = (int) mExpandedHeight; |
| 536 | // setLayoutParams(lp); |
| 537 | |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 538 | mExpandedFraction = Math.min(1f, (fh == 0) ? 0 : h / fh); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | private float getFullHeight() { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 542 | if (mFullHeight <= 0) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 543 | if (DEBUG) LOG("Forcing measure() since fullHeight=" + mFullHeight); |
| 544 | measure(MeasureSpec.makeMeasureSpec(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY), |
| 545 | MeasureSpec.makeMeasureSpec(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY)); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 546 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 547 | return mFullHeight; |
| 548 | } |
| 549 | |
| 550 | public void setExpandedFraction(float frac) { |
| 551 | setExpandedHeight(getFullHeight() * frac); |
| 552 | } |
| 553 | |
| 554 | public float getExpandedHeight() { |
| 555 | return mExpandedHeight; |
| 556 | } |
| 557 | |
| 558 | public float getExpandedFraction() { |
| 559 | return mExpandedFraction; |
| 560 | } |
| 561 | |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 562 | public boolean isFullyExpanded() { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 563 | return mExpandedHeight >= getFullHeight(); |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | public boolean isFullyCollapsed() { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 567 | return mExpandedHeight <= 0; |
| 568 | } |
| 569 | |
| 570 | public boolean isCollapsing() { |
| 571 | return mClosing; |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 574 | public void setBar(PanelBar panelBar) { |
| 575 | mBar = panelBar; |
| 576 | } |
| 577 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 578 | public void collapse() { |
| 579 | // TODO: abort animation or ongoing touch |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 580 | if (DEBUG) LOG("collapse: " + this); |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 581 | if (!isFullyCollapsed()) { |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 582 | mTimeAnimator.cancel(); |
| 583 | mClosing = true; |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 584 | // collapse() should never be a rubberband, even if an animation is already running |
| 585 | mRubberbanding = false; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 586 | fling(-mSelfCollapseVelocityPx, /*always=*/ true); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | public void expand() { |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 591 | if (DEBUG) LOG("expand: " + this); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 592 | if (isFullyCollapsed()) { |
| 593 | mBar.startOpeningPanel(this); |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 594 | fling(mSelfExpandVelocityPx, /*always=*/ true); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 595 | } else if (DEBUG) { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 596 | if (DEBUG) LOG("skipping expansion: is expanded"); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 597 | } |
| 598 | } |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame^] | 599 | |
| 600 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 601 | pw.println(String.format("[PanelView(%s): expandedHeight=%f fullHeight=%f closing=%s" |
| 602 | + " tracking=%s rubberbanding=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s" |
| 603 | + "]", |
| 604 | this.getClass().getSimpleName(), |
| 605 | getExpandedHeight(), |
| 606 | getFullHeight(), |
| 607 | mClosing?"T":"f", |
| 608 | mTracking?"T":"f", |
| 609 | mRubberbanding?"T":"f", |
| 610 | mJustPeeked?"T":"f", |
| 611 | mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), |
| 612 | mTimeAnimator, ((mTimeAnimator!=null && mTimeAnimator.isStarted())?" (started)":"") |
| 613 | )); |
| 614 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 615 | } |