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