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