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