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 | |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 23 | import android.content.Context; |
Selim Cinek | b84a107 | 2014-05-15 19:10:18 +0200 | [diff] [blame] | 24 | import android.content.res.Configuration; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 25 | import android.content.res.Resources; |
| 26 | import android.util.AttributeSet; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 27 | import android.util.Log; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 29 | import android.view.ViewConfiguration; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 30 | import android.view.animation.AnimationUtils; |
| 31 | import android.view.animation.Interpolator; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
| 33 | |
| 34 | import com.android.systemui.R; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 35 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 36 | import com.android.systemui.statusbar.StatusBarState; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 37 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
| 39 | import java.io.PrintWriter; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 40 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 41 | public abstract class PanelView extends FrameLayout { |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 42 | public static final boolean DEBUG = PanelBar.DEBUG; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 43 | public static final String TAG = PanelView.class.getSimpleName(); |
Daniel Sandler | e7c5bbb | 2013-03-05 13:36:21 -0500 | [diff] [blame] | 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 | |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 49 | protected PhoneStatusBar mStatusBar; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 50 | private float mPeekHeight; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 51 | private float mHintDistance; |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 52 | private int mEdgeTapAreaWidth; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 53 | private float mInitialOffsetOnTouch; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 54 | private float mExpandedFraction = 0; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 55 | protected float mExpandedHeight = 0; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 56 | private boolean mJustPeeked; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 57 | private boolean mClosing; |
Jorim Jaggi | 8dd95e0 | 2014-06-03 16:19:33 +0200 | [diff] [blame] | 58 | protected boolean mTracking; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 59 | private boolean mTouchSlopExceeded; |
John Spurlock | 48fa91a | 2013-08-15 09:29:31 -0400 | [diff] [blame] | 60 | private int mTrackingPointer; |
Jorim Jaggi | d7daab7 | 2014-05-06 22:22:20 +0200 | [diff] [blame] | 61 | protected int mTouchSlop; |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 62 | protected boolean mHintAnimationRunning; |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 63 | private boolean mOverExpandedBeforeFling; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 64 | |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 65 | private ValueAnimator mHeightAnimator; |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 66 | private ObjectAnimator mPeekAnimator; |
Jorim Jaggi | b7b61dd | 2014-05-21 15:45:07 +0200 | [diff] [blame] | 67 | private VelocityTrackerInterface mVelocityTracker; |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 68 | private FlingAnimationUtils mFlingAnimationUtils; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 69 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 70 | PanelBar mBar; |
| 71 | |
Selim Cinek | b84a107 | 2014-05-15 19:10:18 +0200 | [diff] [blame] | 72 | protected int mMaxPanelHeight = -1; |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 73 | private String mViewName; |
Jorim Jaggi | d7daab7 | 2014-05-06 22:22:20 +0200 | [diff] [blame] | 74 | private float mInitialTouchY; |
| 75 | private float mInitialTouchX; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 76 | |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 77 | private Interpolator mLinearOutSlowInInterpolator; |
| 78 | private Interpolator mBounceInterpolator; |
| 79 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 80 | protected void onExpandingFinished() { |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 81 | mBar.onExpandingFinished(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected void onExpandingStarted() { |
| 85 | } |
| 86 | |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 87 | private void runPeekAnimation() { |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 88 | if (DEBUG) logf("peek to height=%.1f", mPeekHeight); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 89 | if (mHeightAnimator != null) { |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | if (mPeekAnimator == null) { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 93 | mPeekAnimator = ObjectAnimator.ofFloat(this, |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 94 | "expandedHeight", mPeekHeight) |
Daniel Sandler | 3679bf5 | 2012-10-16 21:30:28 -0400 | [diff] [blame] | 95 | .setDuration(250); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 96 | } |
| 97 | mPeekAnimator.start(); |
| 98 | } |
| 99 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 100 | public PanelView(Context context, AttributeSet attrs) { |
| 101 | super(context, attrs); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 102 | mFlingAnimationUtils = new FlingAnimationUtils(context, 0.6f); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 103 | mLinearOutSlowInInterpolator = |
| 104 | AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in); |
| 105 | mBounceInterpolator = new BounceInterpolator(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Jorim Jaggi | 069cd03 | 2014-05-15 03:09:01 +0200 | [diff] [blame] | 108 | protected void loadDimens() { |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 109 | final Resources res = getContext().getResources(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 110 | mPeekHeight = res.getDimension(R.dimen.peek_height) |
John Spurlock | 5072883 | 2014-04-17 19:05:28 -0400 | [diff] [blame] | 111 | + getPaddingBottom(); // our window might have a dropshadow |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 112 | |
| 113 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 114 | mTouchSlop = configuration.getScaledTouchSlop(); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 115 | mHintDistance = res.getDimension(R.dimen.hint_move_distance); |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 116 | mEdgeTapAreaWidth = res.getDimensionPixelSize(R.dimen.edge_tap_area_width); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private void trackMovement(MotionEvent event) { |
| 120 | // Add movement to velocity tracker using raw screen X and Y coordinates instead |
| 121 | // of window coordinates because the window frame may be moving at the same time. |
| 122 | float deltaX = event.getRawX() - event.getX(); |
| 123 | float deltaY = event.getRawY() - event.getY(); |
| 124 | event.offsetLocation(deltaX, deltaY); |
Daniel Sandler | b17a726 | 2012-10-05 14:32:50 -0400 | [diff] [blame] | 125 | if (mVelocityTracker != null) mVelocityTracker.addMovement(event); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 126 | event.offsetLocation(-deltaX, -deltaY); |
| 127 | } |
| 128 | |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 129 | @Override |
| 130 | public boolean onTouchEvent(MotionEvent event) { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * We capture touch events here and update the expand height here in case according to |
| 134 | * the users fingers. This also handles multi-touch. |
| 135 | * |
| 136 | * If the user just clicks shortly, we give him a quick peek of the shade. |
| 137 | * |
| 138 | * Flinging is also enabled in order to open or close the shade. |
| 139 | */ |
| 140 | |
| 141 | int pointerIndex = event.findPointerIndex(mTrackingPointer); |
| 142 | if (pointerIndex < 0) { |
| 143 | pointerIndex = 0; |
| 144 | mTrackingPointer = event.getPointerId(pointerIndex); |
| 145 | } |
| 146 | final float y = event.getY(pointerIndex); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 147 | final float x = event.getX(pointerIndex); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 148 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 149 | boolean waitForTouchSlop = hasConflictingGestures(); |
| 150 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 151 | switch (event.getActionMasked()) { |
| 152 | case MotionEvent.ACTION_DOWN: |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 153 | mInitialTouchY = y; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 154 | mInitialTouchX = x; |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 155 | mInitialOffsetOnTouch = mExpandedHeight; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 156 | mTouchSlopExceeded = false; |
Jorim Jaggi | b7b61dd | 2014-05-21 15:45:07 +0200 | [diff] [blame] | 157 | if (mVelocityTracker == null) { |
| 158 | initVelocityTracker(); |
| 159 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 160 | trackMovement(event); |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 161 | if (!waitForTouchSlop || (mHeightAnimator != null && !mHintAnimationRunning)) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 162 | if (mHeightAnimator != null) { |
| 163 | mHeightAnimator.cancel(); // end any outstanding animations |
| 164 | } |
Jorim Jaggi | 787a0af | 2014-06-04 18:57:14 +0200 | [diff] [blame] | 165 | mTouchSlopExceeded = (mHeightAnimator != null && !mHintAnimationRunning); |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 166 | onTrackingStarted(); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 167 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 168 | if (mExpandedHeight == 0) { |
| 169 | mJustPeeked = true; |
| 170 | runPeekAnimation(); |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | case MotionEvent.ACTION_POINTER_UP: |
| 175 | final int upPointer = event.getPointerId(event.getActionIndex()); |
| 176 | if (mTrackingPointer == upPointer) { |
| 177 | // gesture is ongoing, find a new pointer to track |
| 178 | final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; |
| 179 | final float newY = event.getY(newIndex); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 180 | final float newX = event.getX(newIndex); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 181 | mTrackingPointer = event.getPointerId(newIndex); |
| 182 | mInitialOffsetOnTouch = mExpandedHeight; |
| 183 | mInitialTouchY = newY; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 184 | mInitialTouchX = newX; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 185 | } |
| 186 | break; |
| 187 | |
| 188 | case MotionEvent.ACTION_MOVE: |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 189 | float h = y - mInitialTouchY; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 190 | if (Math.abs(h) > mTouchSlop && Math.abs(h) > Math.abs(x - mInitialTouchX)) { |
| 191 | mTouchSlopExceeded = true; |
| 192 | if (waitForTouchSlop && !mTracking) { |
| 193 | mInitialOffsetOnTouch = mExpandedHeight; |
| 194 | mInitialTouchX = x; |
| 195 | mInitialTouchY = y; |
| 196 | if (mHeightAnimator != null) { |
| 197 | mHeightAnimator.cancel(); // end any outstanding animations |
| 198 | } |
| 199 | onTrackingStarted(); |
| 200 | h = 0; |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 201 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 202 | } |
| 203 | final float newHeight = h + mInitialOffsetOnTouch; |
| 204 | if (newHeight > mPeekHeight) { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 205 | if (mPeekAnimator != null && mPeekAnimator.isStarted()) { |
| 206 | mPeekAnimator.cancel(); |
| 207 | } |
| 208 | mJustPeeked = false; |
| 209 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 210 | if (!mJustPeeked && (!waitForTouchSlop || mTracking)) { |
| 211 | setExpandedHeightInternal(newHeight); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 212 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
| 213 | } |
| 214 | |
| 215 | trackMovement(event); |
| 216 | break; |
| 217 | |
| 218 | case MotionEvent.ACTION_UP: |
| 219 | case MotionEvent.ACTION_CANCEL: |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 220 | mTrackingPointer = -1; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 221 | trackMovement(event); |
Jorim Jaggi | 787a0af | 2014-06-04 18:57:14 +0200 | [diff] [blame] | 222 | if ((mTracking && mTouchSlopExceeded) |
| 223 | || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 224 | float vel = getCurrentVelocity(); |
| 225 | boolean expand = flingExpands(vel); |
| 226 | onTrackingStopped(expand); |
| 227 | fling(vel, expand); |
| 228 | } else { |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 229 | boolean expands = onEmptySpaceClick(mInitialTouchX); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 230 | onTrackingStopped(expands); |
| 231 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 232 | if (mVelocityTracker != null) { |
| 233 | mVelocityTracker.recycle(); |
| 234 | mVelocityTracker = null; |
| 235 | } |
| 236 | break; |
| 237 | } |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 238 | return !waitForTouchSlop || mTracking; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 241 | protected abstract boolean hasConflictingGestures(); |
| 242 | |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 243 | protected void onTrackingStopped(boolean expand) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 244 | mTracking = false; |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 245 | mBar.onTrackingStopped(PanelView.this, expand); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | protected void onTrackingStarted() { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 249 | mTracking = true; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 250 | mBar.onTrackingStarted(PanelView.this); |
| 251 | onExpandingStarted(); |
| 252 | } |
| 253 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 254 | private float getCurrentVelocity() { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 255 | |
| 256 | // the velocitytracker might be null if we got a bad input stream |
| 257 | if (mVelocityTracker == null) { |
| 258 | return 0; |
| 259 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 260 | mVelocityTracker.computeCurrentVelocity(1000); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 261 | return mVelocityTracker.getYVelocity(); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 266 | |
| 267 | /* |
| 268 | * If the user drags anywhere inside the panel we intercept it if he moves his finger |
| 269 | * upwards. This allows closing the shade from anywhere inside the panel. |
| 270 | * |
| 271 | * We only do this if the current content is scrolled to the bottom, |
| 272 | * i.e isScrolledToBottom() is true and therefore there is no conflicting scrolling gesture |
| 273 | * possible. |
| 274 | */ |
| 275 | int pointerIndex = event.findPointerIndex(mTrackingPointer); |
| 276 | if (pointerIndex < 0) { |
| 277 | pointerIndex = 0; |
| 278 | mTrackingPointer = event.getPointerId(pointerIndex); |
| 279 | } |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 280 | final float x = event.getX(pointerIndex); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 281 | final float y = event.getY(pointerIndex); |
| 282 | boolean scrolledToBottom = isScrolledToBottom(); |
| 283 | |
| 284 | switch (event.getActionMasked()) { |
| 285 | case MotionEvent.ACTION_DOWN: |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 286 | if (mHeightAnimator != null && !mHintAnimationRunning) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 287 | mHeightAnimator.cancel(); // end any outstanding animations |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 288 | mTouchSlopExceeded = true; |
Selim Cinek | 172e914 | 2014-05-07 19:38:00 +0200 | [diff] [blame] | 289 | return true; |
| 290 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 291 | mInitialTouchY = y; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 292 | mInitialTouchX = x; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 293 | mTouchSlopExceeded = false; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 294 | initVelocityTracker(); |
| 295 | trackMovement(event); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 296 | break; |
| 297 | case MotionEvent.ACTION_POINTER_UP: |
| 298 | final int upPointer = event.getPointerId(event.getActionIndex()); |
| 299 | if (mTrackingPointer == upPointer) { |
| 300 | // gesture is ongoing, find a new pointer to track |
| 301 | final int newIndex = event.getPointerId(0) != upPointer ? 0 : 1; |
| 302 | mTrackingPointer = event.getPointerId(newIndex); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 303 | mInitialTouchX = event.getX(newIndex); |
| 304 | mInitialTouchY = event.getY(newIndex); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 305 | } |
| 306 | break; |
| 307 | |
| 308 | case MotionEvent.ACTION_MOVE: |
| 309 | final float h = y - mInitialTouchY; |
| 310 | trackMovement(event); |
| 311 | if (scrolledToBottom) { |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 312 | if (h < -mTouchSlop && h < -Math.abs(x - mInitialTouchX)) { |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 313 | if (mHeightAnimator != null) { |
| 314 | mHeightAnimator.cancel(); |
| 315 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 316 | mInitialOffsetOnTouch = mExpandedHeight; |
| 317 | mInitialTouchY = y; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 318 | mInitialTouchX = x; |
Selim Cinek | 91c39ef | 2014-04-07 21:18:09 +0200 | [diff] [blame] | 319 | mTracking = true; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 320 | mTouchSlopExceeded = true; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 321 | onTrackingStarted(); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | private void initVelocityTracker() { |
| 331 | if (mVelocityTracker != null) { |
| 332 | mVelocityTracker.recycle(); |
| 333 | } |
Jorim Jaggi | b7b61dd | 2014-05-21 15:45:07 +0200 | [diff] [blame] | 334 | mVelocityTracker = VelocityTrackerFactory.obtain(getContext()); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | protected boolean isScrolledToBottom() { |
Selim Cinek | 172e914 | 2014-05-07 19:38:00 +0200 | [diff] [blame] | 338 | return true; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | protected float getContentHeight() { |
| 342 | return mExpandedHeight; |
Daniel Sandler | bf526d1 | 2012-09-04 22:56:44 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 345 | @Override |
| 346 | protected void onFinishInflate() { |
| 347 | super.onFinishInflate(); |
| 348 | loadDimens(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Jorim Jaggi | 069cd03 | 2014-05-15 03:09:01 +0200 | [diff] [blame] | 351 | @Override |
| 352 | protected void onConfigurationChanged(Configuration newConfig) { |
| 353 | super.onConfigurationChanged(newConfig); |
| 354 | loadDimens(); |
| 355 | mMaxPanelHeight = -1; |
| 356 | } |
| 357 | |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 358 | /** |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 359 | * @param vel the current velocity of the motion |
| 360 | * @return whether a fling should expands the panel; contracts otherwise |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 361 | */ |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 362 | private boolean flingExpands(float vel) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 363 | if (Math.abs(vel) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) { |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 364 | return getExpandedFraction() > 0.5f; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 365 | } else { |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 366 | return vel > 0; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 367 | } |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | protected void fling(float vel, boolean expand) { |
| 371 | cancelPeek(); |
| 372 | float target = expand ? getMaxPanelHeight() : 0.0f; |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 373 | if (target == mExpandedHeight || getOverExpansionAmount() > 0f && expand) { |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 374 | onExpandingFinished(); |
Jorim Jaggi | e29b2db | 2014-05-30 23:17:03 +0200 | [diff] [blame] | 375 | mBar.panelExpansionChanged(this, mExpandedFraction); |
Jorim Jaggi | 2fbad7b | 2014-05-26 22:38:00 +0200 | [diff] [blame] | 376 | return; |
| 377 | } |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 378 | mOverExpandedBeforeFling = getOverExpansionAmount() > 0f; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 379 | ValueAnimator animator = createHeightAnimator(target); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 380 | if (expand) { |
| 381 | mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight()); |
| 382 | } else { |
| 383 | mFlingAnimationUtils.applyDismissing(animator, mExpandedHeight, target, vel, |
| 384 | getHeight()); |
| 385 | |
| 386 | // Make it shorter if we run a canned animation |
| 387 | if (vel == 0) { |
| 388 | animator.setDuration((long) (animator.getDuration() / 1.75f)); |
| 389 | } |
| 390 | } |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 391 | animator.addListener(new AnimatorListenerAdapter() { |
| 392 | @Override |
| 393 | public void onAnimationEnd(Animator animation) { |
| 394 | mHeightAnimator = null; |
| 395 | onExpandingFinished(); |
| 396 | } |
| 397 | }); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 398 | mHeightAnimator = animator; |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 399 | animator.start(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
| 403 | protected void onAttachedToWindow() { |
| 404 | super.onAttachedToWindow(); |
Daniel Sandler | 978f853 | 2012-08-15 15:48:16 -0400 | [diff] [blame] | 405 | mViewName = getResources().getResourceName(getId()); |
| 406 | } |
| 407 | |
| 408 | public String getName() { |
| 409 | return mViewName; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 412 | @Override |
| 413 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 414 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 415 | |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 416 | if (DEBUG) logf("onMeasure(%d, %d) -> (%d, %d)", |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 417 | widthMeasureSpec, heightMeasureSpec, getMeasuredWidth(), getMeasuredHeight()); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 418 | |
| 419 | // Did one of our children change size? |
| 420 | int newHeight = getMeasuredHeight(); |
Selim Cinek | b84a107 | 2014-05-15 19:10:18 +0200 | [diff] [blame] | 421 | if (newHeight > mMaxPanelHeight) { |
| 422 | // we only adapt the max height if it's bigger |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 423 | mMaxPanelHeight = newHeight; |
Jorim Jaggi | 03c701e | 2014-04-02 12:39:51 +0200 | [diff] [blame] | 424 | // If the user isn't actively poking us, let's rubberband to the content |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 425 | if (!mTracking && mHeightAnimator == null |
Jorim Jaggi | 03c701e | 2014-04-02 12:39:51 +0200 | [diff] [blame] | 426 | && mExpandedHeight > 0 && mExpandedHeight != mMaxPanelHeight |
| 427 | && mMaxPanelHeight > 0) { |
| 428 | mExpandedHeight = mMaxPanelHeight; |
| 429 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 430 | } |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 433 | public void setExpandedHeight(float height) { |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 434 | if (DEBUG) logf("setExpandedHeight(%.1f)", height); |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 435 | setExpandedHeightInternal(height + getOverExpansionPixels()); |
Daniel Sandler | 0c1b75c | 2012-10-04 12:08:54 -0400 | [diff] [blame] | 436 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 439 | @Override |
| 440 | 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] | 441 | if (DEBUG) logf("onLayout: changed=%s, bottom=%d eh=%d fh=%d", changed?"T":"f", bottom, |
| 442 | (int)mExpandedHeight, mMaxPanelHeight); |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 443 | super.onLayout(changed, left, top, right, bottom); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 444 | requestPanelHeightUpdate(); |
| 445 | } |
| 446 | |
| 447 | protected void requestPanelHeightUpdate() { |
| 448 | float currentMaxPanelHeight = getMaxPanelHeight(); |
| 449 | |
| 450 | // If the user isn't actively poking us, let's update the height |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 451 | if (!mTracking && mHeightAnimator == null |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 452 | && mExpandedHeight > 0 && currentMaxPanelHeight != mExpandedHeight) { |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 453 | setExpandedHeight(currentMaxPanelHeight); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 454 | } |
Daniel Sandler | 5050813 | 2012-08-16 14:10:53 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 457 | public void setExpandedHeightInternal(float h) { |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 458 | float fhWithoutOverExpansion = getMaxPanelHeight() - getOverExpansionAmount(); |
| 459 | if (mHeightAnimator == null) { |
| 460 | float overExpansionPixels = Math.max(0, h - fhWithoutOverExpansion); |
| 461 | if (getOverExpansionPixels() != overExpansionPixels && mTracking) { |
| 462 | setOverExpansion(overExpansionPixels, true /* isPixels */); |
| 463 | } |
| 464 | mExpandedHeight = Math.min(h, fhWithoutOverExpansion) + getOverExpansionAmount(); |
| 465 | } else { |
| 466 | mExpandedHeight = h; |
| 467 | if (mOverExpandedBeforeFling) { |
| 468 | setOverExpansion(Math.max(0, h - fhWithoutOverExpansion), false /* isPixels */); |
| 469 | } |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 470 | } |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 471 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 472 | onHeightUpdated(mExpandedHeight); |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 473 | mExpandedFraction = Math.min(1f, fhWithoutOverExpansion == 0 |
| 474 | ? 0 |
| 475 | : mExpandedHeight / fhWithoutOverExpansion); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 478 | protected abstract void setOverExpansion(float overExpansion, boolean isPixels); |
Selim Cinek | 24120a5 | 2014-05-26 10:05:42 +0200 | [diff] [blame] | 479 | |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 480 | protected abstract void onHeightUpdated(float expandedHeight); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 481 | |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 482 | protected abstract float getOverExpansionAmount(); |
| 483 | |
| 484 | protected abstract float getOverExpansionPixels(); |
| 485 | |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 486 | /** |
| 487 | * This returns the maximum height of the panel. Children should override this if their |
| 488 | * desired height is not the full height. |
| 489 | * |
| 490 | * @return the default implementation simply returns the maximum height. |
| 491 | */ |
| 492 | protected int getMaxPanelHeight() { |
Selim Cinek | b84a107 | 2014-05-15 19:10:18 +0200 | [diff] [blame] | 493 | mMaxPanelHeight = Math.max(mMaxPanelHeight, getHeight()); |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 494 | return mMaxPanelHeight; |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | public void setExpandedFraction(float frac) { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 498 | setExpandedHeight(getMaxPanelHeight() * frac); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | public float getExpandedHeight() { |
| 502 | return mExpandedHeight; |
| 503 | } |
| 504 | |
| 505 | public float getExpandedFraction() { |
| 506 | return mExpandedFraction; |
| 507 | } |
| 508 | |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 509 | public boolean isFullyExpanded() { |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 510 | return mExpandedHeight >= getMaxPanelHeight(); |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | public boolean isFullyCollapsed() { |
Daniel Sandler | 67eab79 | 2012-10-02 17:08:23 -0400 | [diff] [blame] | 514 | return mExpandedHeight <= 0; |
| 515 | } |
| 516 | |
| 517 | public boolean isCollapsing() { |
| 518 | return mClosing; |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 519 | } |
| 520 | |
John Spurlock | a4b70af | 2013-08-17 14:05:49 -0400 | [diff] [blame] | 521 | public boolean isTracking() { |
| 522 | return mTracking; |
| 523 | } |
| 524 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 525 | public void setBar(PanelBar panelBar) { |
| 526 | mBar = panelBar; |
| 527 | } |
| 528 | |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 529 | public void collapse() { |
| 530 | // TODO: abort animation or ongoing touch |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 531 | if (DEBUG) logf("collapse: " + this); |
Daniel Sandler | b4e56ed | 2012-09-12 23:07:44 -0700 | [diff] [blame] | 532 | if (!isFullyCollapsed()) { |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 533 | if (mHeightAnimator != null) { |
| 534 | mHeightAnimator.cancel(); |
| 535 | } |
Daniel Sandler | 750bb9b | 2012-10-03 16:24:00 -0400 | [diff] [blame] | 536 | mClosing = true; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 537 | onExpandingStarted(); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 538 | fling(0, false /* expand */); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | public void expand() { |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 543 | if (DEBUG) logf("expand: " + this); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 544 | if (isFullyCollapsed()) { |
| 545 | mBar.startOpeningPanel(this); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 546 | onExpandingStarted(); |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 547 | fling(0, true /* expand */); |
Daniel Sandler | 198a030 | 2012-08-17 16:04:31 -0400 | [diff] [blame] | 548 | } else if (DEBUG) { |
John Spurlock | 9764218 | 2013-07-29 17:58:39 -0400 | [diff] [blame] | 549 | if (DEBUG) logf("skipping expansion: is expanded"); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | public void cancelPeek() { |
| 554 | if (mPeekAnimator != null && mPeekAnimator.isStarted()) { |
| 555 | mPeekAnimator.cancel(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 556 | } |
| 557 | } |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 558 | |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 559 | protected void startUnlockHintAnimation() { |
| 560 | |
| 561 | // We don't need to hint the user if an animation is already running or the user is changing |
| 562 | // the expansion. |
| 563 | if (mHeightAnimator != null || mTracking) { |
| 564 | return; |
| 565 | } |
| 566 | cancelPeek(); |
| 567 | onExpandingStarted(); |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 568 | startUnlockHintAnimationPhase1(new Runnable() { |
| 569 | @Override |
| 570 | public void run() { |
| 571 | onExpandingFinished(); |
| 572 | mStatusBar.onHintFinished(); |
| 573 | mHintAnimationRunning = false; |
| 574 | } |
| 575 | }); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 576 | mStatusBar.onUnlockHintStarted(); |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 577 | mHintAnimationRunning = true; |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Phase 1: Move everything upwards. |
| 582 | */ |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 583 | private void startUnlockHintAnimationPhase1(final Runnable onAnimationFinished) { |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 584 | float target = Math.max(0, getMaxPanelHeight() - mHintDistance); |
| 585 | ValueAnimator animator = createHeightAnimator(target); |
| 586 | animator.setDuration(250); |
| 587 | animator.setInterpolator(mLinearOutSlowInInterpolator); |
| 588 | animator.addListener(new AnimatorListenerAdapter() { |
| 589 | private boolean mCancelled; |
| 590 | |
| 591 | @Override |
| 592 | public void onAnimationCancel(Animator animation) { |
| 593 | mCancelled = true; |
| 594 | } |
| 595 | |
| 596 | @Override |
| 597 | public void onAnimationEnd(Animator animation) { |
| 598 | if (mCancelled) { |
| 599 | mHeightAnimator = null; |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 600 | onAnimationFinished.run(); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 601 | } else { |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 602 | startUnlockHintAnimationPhase2(onAnimationFinished); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | }); |
| 606 | animator.start(); |
| 607 | mHeightAnimator = animator; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Phase 2: Bounce down. |
| 612 | */ |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 613 | private void startUnlockHintAnimationPhase2(final Runnable onAnimationFinished) { |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 614 | ValueAnimator animator = createHeightAnimator(getMaxPanelHeight()); |
| 615 | animator.setDuration(450); |
| 616 | animator.setInterpolator(mBounceInterpolator); |
| 617 | animator.addListener(new AnimatorListenerAdapter() { |
| 618 | @Override |
| 619 | public void onAnimationEnd(Animator animation) { |
| 620 | mHeightAnimator = null; |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 621 | onAnimationFinished.run(); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 622 | } |
| 623 | }); |
| 624 | animator.start(); |
| 625 | mHeightAnimator = animator; |
| 626 | } |
| 627 | |
| 628 | private ValueAnimator createHeightAnimator(float targetHeight) { |
| 629 | ValueAnimator animator = ValueAnimator.ofFloat(mExpandedHeight, targetHeight); |
| 630 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 631 | @Override |
| 632 | public void onAnimationUpdate(ValueAnimator animation) { |
Jorim Jaggi | 47c85a3 | 2014-06-05 17:25:40 +0200 | [diff] [blame^] | 633 | setExpandedHeightInternal((Float) animation.getAnimatedValue()); |
| 634 | mBar.panelExpansionChanged(PanelView.this, mExpandedFraction); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 635 | } |
| 636 | }); |
| 637 | return animator; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Gets called when the user performs a click anywhere in the empty area of the panel. |
| 642 | * |
| 643 | * @return whether the panel will be expanded after the action performed by this method |
| 644 | */ |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 645 | private boolean onEmptySpaceClick(float x) { |
| 646 | if (mHintAnimationRunning) { |
| 647 | return true; |
| 648 | } |
Jorim Jaggi | 6539a83 | 2014-06-03 23:33:09 +0200 | [diff] [blame] | 649 | if (x < mEdgeTapAreaWidth |
| 650 | && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 651 | onEdgeClicked(false /* right */); |
| 652 | return true; |
Jorim Jaggi | 6539a83 | 2014-06-03 23:33:09 +0200 | [diff] [blame] | 653 | } else if (x > getWidth() - mEdgeTapAreaWidth |
| 654 | && mStatusBar.getBarState() == StatusBarState.KEYGUARD) { |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 655 | onEdgeClicked(true /* right */); |
| 656 | return true; |
| 657 | } else { |
| 658 | return onMiddleClicked(); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | private boolean onMiddleClicked() { |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 663 | switch (mStatusBar.getBarState()) { |
| 664 | case StatusBarState.KEYGUARD: |
| 665 | startUnlockHintAnimation(); |
| 666 | return true; |
| 667 | case StatusBarState.SHADE_LOCKED: |
Jorim Jaggi | 6539a83 | 2014-06-03 23:33:09 +0200 | [diff] [blame] | 668 | mStatusBar.goToKeyguard(); |
Jorim Jaggi | 9012958 | 2014-06-02 14:44:49 +0200 | [diff] [blame] | 669 | return true; |
| 670 | case StatusBarState.SHADE: |
| 671 | collapse(); |
| 672 | return false; |
| 673 | default: |
| 674 | return true; |
| 675 | } |
| 676 | } |
| 677 | |
Jorim Jaggi | b3f0a2f | 2014-06-02 19:29:39 +0200 | [diff] [blame] | 678 | protected abstract void onEdgeClicked(boolean right); |
| 679 | |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 680 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Selim Cinek | a3e5bcb | 2014-04-07 20:07:22 +0200 | [diff] [blame] | 681 | pw.println(String.format("[PanelView(%s): expandedHeight=%f maxPanelHeight=%d closing=%s" |
John Spurlock | 5072883 | 2014-04-17 19:05:28 -0400 | [diff] [blame] | 682 | + " tracking=%s justPeeked=%s peekAnim=%s%s timeAnim=%s%s" |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 683 | + "]", |
| 684 | this.getClass().getSimpleName(), |
| 685 | getExpandedHeight(), |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 686 | getMaxPanelHeight(), |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 687 | mClosing?"T":"f", |
| 688 | mTracking?"T":"f", |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 689 | mJustPeeked?"T":"f", |
| 690 | mPeekAnimator, ((mPeekAnimator!=null && mPeekAnimator.isStarted())?" (started)":""), |
Jorim Jaggi | 1d48069 | 2014-05-20 19:41:58 +0200 | [diff] [blame] | 691 | mHeightAnimator, ((mHeightAnimator !=null && mHeightAnimator.isStarted())?" (started)":"") |
Daniel Sandler | 37a38aa | 2013-02-13 17:15:57 -0500 | [diff] [blame] | 692 | )); |
| 693 | } |
Selim Cinek | 3c4635c | 2014-05-29 02:12:47 +0200 | [diff] [blame] | 694 | |
| 695 | public abstract void resetViews(); |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 696 | } |