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