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