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