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