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