Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -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 | |
| 17 | |
| 18 | package com.android.systemui; |
| 19 | |
Romain Guy | 8900e63 | 2012-05-25 12:08:39 -0700 | [diff] [blame] | 20 | import android.animation.Animator; |
| 21 | import android.animation.AnimatorListenerAdapter; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
| 23 | import android.content.Context; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 24 | import android.util.Log; |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 25 | import android.view.Gravity; |
Jorim Jaggi | 9ff1510 | 2015-06-10 15:58:08 -0700 | [diff] [blame] | 26 | import android.view.HapticFeedbackConstants; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 27 | import android.view.MotionEvent; |
| 28 | import android.view.ScaleGestureDetector; |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 29 | import android.view.ScaleGestureDetector.OnScaleGestureListener; |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 30 | import android.view.VelocityTracker; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 31 | import android.view.View; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 32 | import android.view.ViewConfiguration; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 33 | |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 34 | import com.android.internal.annotations.VisibleForTesting; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 35 | import com.android.systemui.statusbar.FlingAnimationUtils; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 37 | import com.android.systemui.statusbar.notification.row.ExpandableView; |
Selim Cinek | b6d85eb | 2014-03-28 20:21:01 +0100 | [diff] [blame] | 38 | import com.android.systemui.statusbar.policy.ScrollAdapter; |
| 39 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 40 | public class ExpandHelper implements Gefingerpoken { |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 41 | public interface Callback { |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 42 | ExpandableView getChildAtRawPosition(float x, float y); |
| 43 | ExpandableView getChildAtPosition(float x, float y); |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 44 | boolean canChildBeExpanded(View v); |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 45 | void setUserExpandedChild(View v, boolean userExpanded); |
| 46 | void setUserLockedChild(View v, boolean userLocked); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 47 | void expansionStateChanged(boolean isExpanding); |
Selim Cinek | 1b2a05e | 2016-04-28 14:20:39 -0700 | [diff] [blame] | 48 | int getMaxExpandHeight(ExpandableView view); |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 49 | void setExpansionCancelled(View view); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private static final String TAG = "ExpandHelper"; |
Chris Wren | e46647d | 2012-06-20 16:18:10 -0400 | [diff] [blame] | 53 | protected static final boolean DEBUG = false; |
Chris Wren | 3c148f1 | 2012-06-19 13:10:25 -0400 | [diff] [blame] | 54 | protected static final boolean DEBUG_SCALE = false; |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 55 | private static final float EXPAND_DURATION = 0.3f; |
Chris Wren | ba925e8 | 2012-04-20 16:46:43 -0400 | [diff] [blame] | 56 | |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 57 | // Set to false to disable focus-based gestures (spread-finger vertical pull). |
Chris Wren | 89139d7 | 2012-05-07 17:00:06 -0400 | [diff] [blame] | 58 | private static final boolean USE_DRAG = true; |
| 59 | // Set to false to disable scale-based gestures (both horizontal and vertical). |
| 60 | private static final boolean USE_SPAN = true; |
| 61 | // Both gestures types may be active at the same time. |
| 62 | // At least one gesture type should be active. |
| 63 | // A variant of the screwdriver gesture will emerge from either gesture type. |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 64 | |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 65 | // amount of overstretch for maximum brightness expressed in U |
| 66 | // 2f: maximum brightness is stretching a 1U to 3U, or a 4U to 6U |
| 67 | private static final float STRETCH_INTERVAL = 2f; |
| 68 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 69 | @SuppressWarnings("unused") |
| 70 | private Context mContext; |
| 71 | |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 72 | private boolean mExpanding; |
| 73 | private static final int NONE = 0; |
| 74 | private static final int BLINDS = 1<<0; |
| 75 | private static final int PULL = 1<<1; |
| 76 | private static final int STRETCH = 1<<2; |
| 77 | private int mExpansionStyle = NONE; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 78 | private boolean mWatchingForPull; |
| 79 | private boolean mHasPopped; |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 80 | private View mEventSource; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 81 | private float mOldHeight; |
| 82 | private float mNaturalHeight; |
Chris Wren | 89139d7 | 2012-05-07 17:00:06 -0400 | [diff] [blame] | 83 | private float mInitialTouchFocusY; |
Selim Cinek | 821d6a7 | 2016-03-11 17:24:19 -0800 | [diff] [blame] | 84 | private float mInitialTouchX; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 85 | private float mInitialTouchY; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 86 | private float mInitialTouchSpan; |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 87 | private float mLastFocusY; |
| 88 | private float mLastSpanY; |
Philip Quinn | 4716913 | 2020-03-23 19:04:55 -0700 | [diff] [blame] | 89 | private final int mTouchSlop; |
| 90 | private final float mSlopMultiplier; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 91 | private float mLastMotionY; |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 92 | private float mPullGestureMinXSpan; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 93 | private Callback mCallback; |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 94 | private ScaleGestureDetector mSGD; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 95 | private ViewScaler mScaler; |
Chris Wren | ba925e8 | 2012-04-20 16:46:43 -0400 | [diff] [blame] | 96 | private ObjectAnimator mScaleAnimation; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 97 | private boolean mEnabled = true; |
| 98 | private ExpandableView mResizedView; |
| 99 | private float mCurrentHeight; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 100 | |
| 101 | private int mSmallSize; |
| 102 | private int mLargeSize; |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 103 | private float mMaximumStretch; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 104 | private boolean mOnlyMovements; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 105 | |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 106 | private int mGravity; |
| 107 | |
Selim Cinek | fab078b | 2014-03-27 22:45:58 +0100 | [diff] [blame] | 108 | private ScrollAdapter mScrollAdapter; |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 109 | private FlingAnimationUtils mFlingAnimationUtils; |
| 110 | private VelocityTracker mVelocityTracker; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 111 | |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 112 | private OnScaleGestureListener mScaleGestureListener |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 113 | = new ScaleGestureDetector.SimpleOnScaleGestureListener() { |
| 114 | @Override |
| 115 | public boolean onScaleBegin(ScaleGestureDetector detector) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 116 | if (DEBUG_SCALE) Log.v(TAG, "onscalebegin()"); |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 117 | |
Jorim Jaggi | 5e8e1c6 | 2015-09-04 11:51:26 -0700 | [diff] [blame] | 118 | if (!mOnlyMovements) { |
| 119 | startExpanding(mResizedView, STRETCH); |
| 120 | } |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 121 | return mExpanding; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean onScale(ScaleGestureDetector detector) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 126 | if (DEBUG_SCALE) Log.v(TAG, "onscale() on " + mResizedView); |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onScaleEnd(ScaleGestureDetector detector) { |
| 132 | } |
| 133 | }; |
| 134 | |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 135 | @VisibleForTesting |
| 136 | ObjectAnimator getScaleAnimation() { |
| 137 | return mScaleAnimation; |
| 138 | } |
| 139 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 140 | private class ViewScaler { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 141 | ExpandableView mView; |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 142 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 143 | public ViewScaler() {} |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 144 | public void setView(ExpandableView v) { |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 145 | mView = v; |
| 146 | } |
| 147 | public void setHeight(float h) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 148 | if (DEBUG_SCALE) Log.v(TAG, "SetHeight: setting to " + h); |
Selim Cinek | eef8428 | 2015-10-30 16:28:00 -0700 | [diff] [blame] | 149 | mView.setActualHeight((int) h); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 150 | mCurrentHeight = h; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 151 | } |
| 152 | public float getHeight() { |
Selim Cinek | eef8428 | 2015-10-30 16:28:00 -0700 | [diff] [blame] | 153 | return mView.getActualHeight(); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 154 | } |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 155 | public int getNaturalHeight() { |
Selim Cinek | 1b2a05e | 2016-04-28 14:20:39 -0700 | [diff] [blame] | 156 | return mCallback.getMaxExpandHeight(mView); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 160 | /** |
| 161 | * Handle expansion gestures to expand and contract children of the callback. |
| 162 | * |
| 163 | * @param context application context |
| 164 | * @param callback the container that holds the items to be manipulated |
| 165 | * @param small the smallest allowable size for the manuipulated items. |
| 166 | * @param large the largest allowable size for the manuipulated items. |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 167 | */ |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 168 | public ExpandHelper(Context context, Callback callback, int small, int large) { |
| 169 | mSmallSize = small; |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 170 | mMaximumStretch = mSmallSize * STRETCH_INTERVAL; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 171 | mLargeSize = large; |
| 172 | mContext = context; |
| 173 | mCallback = callback; |
| 174 | mScaler = new ViewScaler(); |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 175 | mGravity = Gravity.TOP; |
Chris Wren | ba925e8 | 2012-04-20 16:46:43 -0400 | [diff] [blame] | 176 | mScaleAnimation = ObjectAnimator.ofFloat(mScaler, "height", 0f); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 177 | mPullGestureMinXSpan = mContext.getResources().getDimension(R.dimen.pull_span_min); |
Chris Wren | ba925e8 | 2012-04-20 16:46:43 -0400 | [diff] [blame] | 178 | |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 179 | final ViewConfiguration configuration = ViewConfiguration.get(mContext); |
| 180 | mTouchSlop = configuration.getScaledTouchSlop(); |
Philip Quinn | 4716913 | 2020-03-23 19:04:55 -0700 | [diff] [blame] | 181 | mSlopMultiplier = configuration.getAmbiguousGestureMultiplier(); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 182 | |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 183 | mSGD = new ScaleGestureDetector(context, mScaleGestureListener); |
Dave Mankoff | 1373fdb | 2019-12-18 14:04:37 -0500 | [diff] [blame] | 184 | mFlingAnimationUtils = new FlingAnimationUtils(mContext.getResources().getDisplayMetrics(), |
| 185 | EXPAND_DURATION); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 186 | } |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 187 | |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 188 | @VisibleForTesting |
| 189 | void updateExpansion() { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 190 | if (DEBUG_SCALE) Log.v(TAG, "updateExpansion()"); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 191 | // are we scaling or dragging? |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 192 | float span = mSGD.getCurrentSpan() - mInitialTouchSpan; |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 193 | span *= USE_SPAN ? 1f : 0f; |
| 194 | float drag = mSGD.getFocusY() - mInitialTouchFocusY; |
| 195 | drag *= USE_DRAG ? 1f : 0f; |
| 196 | drag *= mGravity == Gravity.BOTTOM ? -1f : 1f; |
| 197 | float pull = Math.abs(drag) + Math.abs(span) + 1f; |
| 198 | float hand = drag * Math.abs(drag) / pull + span * Math.abs(span) / pull; |
| 199 | float target = hand + mOldHeight; |
| 200 | float newHeight = clamp(target); |
| 201 | mScaler.setHeight(newHeight); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 202 | mLastFocusY = mSGD.getFocusY(); |
| 203 | mLastSpanY = mSGD.getCurrentSpan(); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 206 | private float clamp(float target) { |
| 207 | float out = target; |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 208 | out = out < mSmallSize ? mSmallSize : out; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 209 | out = out > mNaturalHeight ? mNaturalHeight : out; |
| 210 | return out; |
| 211 | } |
| 212 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 213 | private ExpandableView findView(float x, float y) { |
| 214 | ExpandableView v; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 215 | if (mEventSource != null) { |
| 216 | int[] location = new int[2]; |
| 217 | mEventSource.getLocationOnScreen(location); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 218 | x += location[0]; |
| 219 | y += location[1]; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 220 | v = mCallback.getChildAtRawPosition(x, y); |
| 221 | } else { |
| 222 | v = mCallback.getChildAtPosition(x, y); |
| 223 | } |
| 224 | return v; |
| 225 | } |
| 226 | |
| 227 | private boolean isInside(View v, float x, float y) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 228 | if (DEBUG) Log.d(TAG, "isinside (" + x + ", " + y + ")"); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 229 | |
| 230 | if (v == null) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 231 | if (DEBUG) Log.d(TAG, "isinside null subject"); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | if (mEventSource != null) { |
| 235 | int[] location = new int[2]; |
| 236 | mEventSource.getLocationOnScreen(location); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 237 | x += location[0]; |
| 238 | y += location[1]; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 239 | if (DEBUG) Log.d(TAG, " to global (" + x + ", " + y + ")"); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 240 | } |
| 241 | int[] location = new int[2]; |
| 242 | v.getLocationOnScreen(location); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 243 | x -= location[0]; |
| 244 | y -= location[1]; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 245 | if (DEBUG) Log.d(TAG, " to local (" + x + ", " + y + ")"); |
| 246 | if (DEBUG) Log.d(TAG, " inside (" + v.getWidth() + ", " + v.getHeight() + ")"); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 247 | boolean inside = (x > 0f && y > 0f && x < v.getWidth() & y < v.getHeight()); |
| 248 | return inside; |
| 249 | } |
| 250 | |
Chris Wren | 5de6e94 | 2012-05-16 14:22:21 -0400 | [diff] [blame] | 251 | public void setEventSource(View eventSource) { |
| 252 | mEventSource = eventSource; |
| 253 | } |
| 254 | |
Chris Wren | 9b2cd15 | 2012-06-11 10:39:36 -0400 | [diff] [blame] | 255 | public void setGravity(int gravity) { |
| 256 | mGravity = gravity; |
| 257 | } |
| 258 | |
Selim Cinek | fab078b | 2014-03-27 22:45:58 +0100 | [diff] [blame] | 259 | public void setScrollAdapter(ScrollAdapter adapter) { |
| 260 | mScrollAdapter = adapter; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Philip Quinn | 4716913 | 2020-03-23 19:04:55 -0700 | [diff] [blame] | 263 | private float getTouchSlop(MotionEvent event) { |
| 264 | // Adjust the touch slop if another gesture may be being performed. |
| 265 | return event.getClassification() == MotionEvent.CLASSIFICATION_AMBIGUOUS_GESTURE |
| 266 | ? mTouchSlop * mSlopMultiplier |
| 267 | : mTouchSlop; |
| 268 | } |
| 269 | |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 270 | @Override |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 271 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 272 | if (!isEnabled()) { |
| 273 | return false; |
| 274 | } |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 275 | trackVelocity(ev); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 276 | final int action = ev.getAction(); |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 277 | if (DEBUG_SCALE) Log.d(TAG, "intercept: act=" + MotionEvent.actionToString(action) + |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 278 | " expanding=" + mExpanding + |
| 279 | (0 != (mExpansionStyle & BLINDS) ? " (blinds)" : "") + |
| 280 | (0 != (mExpansionStyle & PULL) ? " (pull)" : "") + |
| 281 | (0 != (mExpansionStyle & STRETCH) ? " (stretch)" : "")); |
| 282 | // check for a spread-finger vertical pull gesture |
| 283 | mSGD.onTouchEvent(ev); |
| 284 | final int x = (int) mSGD.getFocusX(); |
| 285 | final int y = (int) mSGD.getFocusY(); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 286 | |
| 287 | mInitialTouchFocusY = y; |
| 288 | mInitialTouchSpan = mSGD.getCurrentSpan(); |
| 289 | mLastFocusY = mInitialTouchFocusY; |
| 290 | mLastSpanY = mInitialTouchSpan; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 291 | if (DEBUG_SCALE) Log.d(TAG, "set initial span: " + mInitialTouchSpan); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 292 | |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 293 | if (mExpanding) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 294 | mLastMotionY = ev.getRawY(); |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 295 | maybeRecycleVelocityTracker(ev); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 296 | return true; |
| 297 | } else { |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 298 | if ((action == MotionEvent.ACTION_MOVE) && 0 != (mExpansionStyle & BLINDS)) { |
| 299 | // we've begun Venetian blinds style expansion |
| 300 | return true; |
| 301 | } |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 302 | switch (action & MotionEvent.ACTION_MASK) { |
| 303 | case MotionEvent.ACTION_MOVE: { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 304 | final float xspan = mSGD.getCurrentSpanX(); |
| 305 | if (xspan > mPullGestureMinXSpan && |
| 306 | xspan > mSGD.getCurrentSpanY() && !mExpanding) { |
| 307 | // detect a vertical pulling gesture with fingers somewhat separated |
| 308 | if (DEBUG_SCALE) Log.v(TAG, "got pull gesture (xspan=" + xspan + "px)"); |
| 309 | startExpanding(mResizedView, PULL); |
| 310 | mWatchingForPull = false; |
| 311 | } |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 312 | if (mWatchingForPull) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 313 | final float yDiff = ev.getRawY() - mInitialTouchY; |
Selim Cinek | 821d6a7 | 2016-03-11 17:24:19 -0800 | [diff] [blame] | 314 | final float xDiff = ev.getRawX() - mInitialTouchX; |
Philip Quinn | 4716913 | 2020-03-23 19:04:55 -0700 | [diff] [blame] | 315 | if (yDiff > getTouchSlop(ev) && yDiff > Math.abs(xDiff)) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 316 | if (DEBUG) Log.v(TAG, "got venetian gesture (dy=" + yDiff + "px)"); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 317 | mWatchingForPull = false; |
| 318 | if (mResizedView != null && !isFullyExpanded(mResizedView)) { |
| 319 | if (startExpanding(mResizedView, BLINDS)) { |
| 320 | mLastMotionY = ev.getRawY(); |
| 321 | mInitialTouchY = ev.getRawY(); |
| 322 | mHasPopped = false; |
| 323 | } |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 324 | } |
| 325 | } |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 326 | } |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | case MotionEvent.ACTION_DOWN: |
Chris Wren | de3b1a2 | 2014-03-31 13:36:25 -0400 | [diff] [blame] | 331 | mWatchingForPull = mScrollAdapter != null && |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 332 | isInside(mScrollAdapter.getHostView(), x, y) |
| 333 | && mScrollAdapter.isScrolledToTop(); |
| 334 | mResizedView = findView(x, y); |
Selim Cinek | e53e6bb | 2015-04-13 16:14:26 -0700 | [diff] [blame] | 335 | if (mResizedView != null && !mCallback.canChildBeExpanded(mResizedView)) { |
Selim Cinek | d228115 | 2015-04-10 14:37:46 -0700 | [diff] [blame] | 336 | mResizedView = null; |
| 337 | mWatchingForPull = false; |
| 338 | } |
Selim Cinek | fe09065 | 2016-04-18 15:57:57 -0700 | [diff] [blame] | 339 | mInitialTouchY = ev.getRawY(); |
| 340 | mInitialTouchX = ev.getRawX(); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case MotionEvent.ACTION_CANCEL: |
| 344 | case MotionEvent.ACTION_UP: |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 345 | if (DEBUG) Log.d(TAG, "up/cancel"); |
Selim Cinek | c25af40 | 2017-01-12 11:57:11 -0800 | [diff] [blame] | 346 | finishExpanding(ev.getActionMasked() == MotionEvent.ACTION_CANCEL /* forceAbort */, |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 347 | getCurrentVelocity()); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 348 | clearView(); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 349 | break; |
| 350 | } |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 351 | mLastMotionY = ev.getRawY(); |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 352 | maybeRecycleVelocityTracker(ev); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 353 | return mExpanding; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 354 | } |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 357 | private void trackVelocity(MotionEvent event) { |
| 358 | int action = event.getActionMasked(); |
| 359 | switch(action) { |
| 360 | case MotionEvent.ACTION_DOWN: |
| 361 | if (mVelocityTracker == null) { |
| 362 | mVelocityTracker = VelocityTracker.obtain(); |
| 363 | } else { |
| 364 | mVelocityTracker.clear(); |
| 365 | } |
| 366 | mVelocityTracker.addMovement(event); |
| 367 | break; |
| 368 | case MotionEvent.ACTION_MOVE: |
Selim Cinek | 1b7f51e | 2014-10-28 15:20:34 +0100 | [diff] [blame] | 369 | if (mVelocityTracker == null) { |
| 370 | mVelocityTracker = VelocityTracker.obtain(); |
| 371 | } |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 372 | mVelocityTracker.addMovement(event); |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | private void maybeRecycleVelocityTracker(MotionEvent event) { |
Jorim Jaggi | c503896 | 2014-07-29 20:05:54 +0200 | [diff] [blame] | 380 | if (mVelocityTracker != null && (event.getActionMasked() == MotionEvent.ACTION_CANCEL |
| 381 | || event.getActionMasked() == MotionEvent.ACTION_UP)) { |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 382 | mVelocityTracker.recycle(); |
| 383 | mVelocityTracker = null; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | private float getCurrentVelocity() { |
| 388 | if (mVelocityTracker != null) { |
| 389 | mVelocityTracker.computeCurrentVelocity(1000); |
| 390 | return mVelocityTracker.getYVelocity(); |
| 391 | } else { |
| 392 | return 0f; |
| 393 | } |
| 394 | } |
| 395 | |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 396 | public void setEnabled(boolean enable) { |
| 397 | mEnabled = enable; |
| 398 | } |
| 399 | |
| 400 | private boolean isEnabled() { |
| 401 | return mEnabled; |
| 402 | } |
| 403 | |
| 404 | private boolean isFullyExpanded(ExpandableView underFocus) { |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 405 | return underFocus.getIntrinsicHeight() == underFocus.getMaxContentHeight() |
| 406 | && (!underFocus.isSummaryWithChildren() || underFocus.areChildrenExpanded()); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 409 | @Override |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 410 | public boolean onTouchEvent(MotionEvent ev) { |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 411 | if (!isEnabled() && !mExpanding) { |
| 412 | // In case we're expanding we still want to finish the current motion. |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 413 | return false; |
| 414 | } |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 415 | trackVelocity(ev); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 416 | final int action = ev.getActionMasked(); |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 417 | if (DEBUG_SCALE) Log.d(TAG, "touch: act=" + MotionEvent.actionToString(action) + |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 418 | " expanding=" + mExpanding + |
| 419 | (0 != (mExpansionStyle & BLINDS) ? " (blinds)" : "") + |
| 420 | (0 != (mExpansionStyle & PULL) ? " (pull)" : "") + |
| 421 | (0 != (mExpansionStyle & STRETCH) ? " (stretch)" : "")); |
| 422 | |
| 423 | mSGD.onTouchEvent(ev); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 424 | final int x = (int) mSGD.getFocusX(); |
| 425 | final int y = (int) mSGD.getFocusY(); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 426 | |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 427 | if (mOnlyMovements) { |
| 428 | mLastMotionY = ev.getRawY(); |
| 429 | return false; |
| 430 | } |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 431 | switch (action) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 432 | case MotionEvent.ACTION_DOWN: |
| 433 | mWatchingForPull = mScrollAdapter != null && |
| 434 | isInside(mScrollAdapter.getHostView(), x, y); |
| 435 | mResizedView = findView(x, y); |
Selim Cinek | fe09065 | 2016-04-18 15:57:57 -0700 | [diff] [blame] | 436 | mInitialTouchX = ev.getRawX(); |
| 437 | mInitialTouchY = ev.getRawY(); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 438 | break; |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 439 | case MotionEvent.ACTION_MOVE: { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 440 | if (mWatchingForPull) { |
| 441 | final float yDiff = ev.getRawY() - mInitialTouchY; |
Selim Cinek | 821d6a7 | 2016-03-11 17:24:19 -0800 | [diff] [blame] | 442 | final float xDiff = ev.getRawX() - mInitialTouchX; |
Philip Quinn | 4716913 | 2020-03-23 19:04:55 -0700 | [diff] [blame] | 443 | if (yDiff > getTouchSlop(ev) && yDiff > Math.abs(xDiff)) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 444 | if (DEBUG) Log.v(TAG, "got venetian gesture (dy=" + yDiff + "px)"); |
| 445 | mWatchingForPull = false; |
| 446 | if (mResizedView != null && !isFullyExpanded(mResizedView)) { |
| 447 | if (startExpanding(mResizedView, BLINDS)) { |
| 448 | mInitialTouchY = ev.getRawY(); |
| 449 | mLastMotionY = ev.getRawY(); |
| 450 | mHasPopped = false; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | if (mExpanding && 0 != (mExpansionStyle & BLINDS)) { |
| 456 | final float rawHeight = ev.getRawY() - mLastMotionY + mCurrentHeight; |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 457 | final float newHeight = clamp(rawHeight); |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 458 | boolean isFinished = false; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 459 | boolean expanded = false; |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 460 | if (rawHeight > mNaturalHeight) { |
| 461 | isFinished = true; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 462 | expanded = true; |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 463 | } |
| 464 | if (rawHeight < mSmallSize) { |
| 465 | isFinished = true; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 466 | expanded = false; |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 467 | } |
| 468 | |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 469 | if (!mHasPopped) { |
Jorim Jaggi | 9ff1510 | 2015-06-10 15:58:08 -0700 | [diff] [blame] | 470 | if (mEventSource != null) { |
| 471 | mEventSource.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
| 472 | } |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 473 | mHasPopped = true; |
Chris Wren | 86d00fb | 2012-08-01 17:03:07 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 476 | mScaler.setHeight(newHeight); |
| 477 | mLastMotionY = ev.getRawY(); |
| 478 | if (isFinished) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 479 | mCallback.expansionStateChanged(false); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 480 | } else { |
| 481 | mCallback.expansionStateChanged(true); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 482 | } |
| 483 | return true; |
| 484 | } |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 485 | |
| 486 | if (mExpanding) { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 487 | |
| 488 | // Gestural expansion is running |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 489 | updateExpansion(); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 490 | mLastMotionY = ev.getRawY(); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 494 | break; |
| 495 | } |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 496 | |
| 497 | case MotionEvent.ACTION_POINTER_UP: |
| 498 | case MotionEvent.ACTION_POINTER_DOWN: |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 499 | if (DEBUG) Log.d(TAG, "pointer change"); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 500 | mInitialTouchY += mSGD.getFocusY() - mLastFocusY; |
| 501 | mInitialTouchSpan += mSGD.getCurrentSpan() - mLastSpanY; |
| 502 | break; |
| 503 | |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 504 | case MotionEvent.ACTION_UP: |
| 505 | case MotionEvent.ACTION_CANCEL: |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 506 | if (DEBUG) Log.d(TAG, "up/cancel"); |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 507 | finishExpanding(!isEnabled() || ev.getActionMasked() == MotionEvent.ACTION_CANCEL, |
| 508 | getCurrentVelocity()); |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 509 | clearView(); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 510 | break; |
| 511 | } |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 512 | mLastMotionY = ev.getRawY(); |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 513 | maybeRecycleVelocityTracker(ev); |
Jorim Jaggi | 787a0af | 2014-06-04 18:57:14 +0200 | [diff] [blame] | 514 | return mResizedView != null; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 515 | } |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 516 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 517 | /** |
| 518 | * @return True if the view is expandable, false otherwise. |
| 519 | */ |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 520 | @VisibleForTesting |
| 521 | boolean startExpanding(ExpandableView v, int expandType) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 522 | if (!(v instanceof ExpandableNotificationRow)) { |
| 523 | return false; |
| 524 | } |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 525 | mExpansionStyle = expandType; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 526 | if (mExpanding && v == mResizedView) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 527 | return true; |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 528 | } |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 529 | mExpanding = true; |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 530 | mCallback.expansionStateChanged(true); |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 531 | if (DEBUG) Log.d(TAG, "scale type " + expandType + " beginning on view: " + v); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 532 | mCallback.setUserLockedChild(v, true); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 533 | mScaler.setView(v); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 534 | mOldHeight = mScaler.getHeight(); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 535 | mCurrentHeight = mOldHeight; |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 536 | boolean canBeExpanded = mCallback.canChildBeExpanded(v); |
| 537 | if (canBeExpanded) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 538 | if (DEBUG) Log.d(TAG, "working on an expandable child"); |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 539 | mNaturalHeight = mScaler.getNaturalHeight(); |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 540 | mSmallSize = v.getCollapsedHeight(); |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 541 | } else { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 542 | if (DEBUG) Log.d(TAG, "working on a non-expandable child"); |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 543 | mNaturalHeight = mOldHeight; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 544 | } |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 545 | if (DEBUG) Log.d(TAG, "got mOldHeight: " + mOldHeight + |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 546 | " mNaturalHeight: " + mNaturalHeight); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 547 | return true; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 550 | /** |
| 551 | * Finish the current expand motion |
| 552 | * @param forceAbort whether the expansion should be forcefully aborted and returned to the old |
| 553 | * state |
| 554 | * @param velocity the velocity this was expanded/ collapsed with |
| 555 | */ |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 556 | @VisibleForTesting |
| 557 | void finishExpanding(boolean forceAbort, float velocity) { |
Selim Cinek | 5b1591a | 2017-07-03 17:05:01 +0200 | [diff] [blame] | 558 | finishExpanding(forceAbort, velocity, true /* allowAnimation */); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Finish the current expand motion |
| 563 | * @param forceAbort whether the expansion should be forcefully aborted and returned to the old |
| 564 | * state |
| 565 | * @param velocity the velocity this was expanded/ collapsed with |
| 566 | */ |
| 567 | private void finishExpanding(boolean forceAbort, float velocity, boolean allowAnimation) { |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 568 | if (!mExpanding) return; |
| 569 | |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 570 | if (DEBUG) Log.d(TAG, "scale in finishing on view: " + mResizedView); |
Chris Wren | cea5207 | 2012-10-10 21:02:56 -0400 | [diff] [blame] | 571 | |
Chris Wren | 3ddab0d | 2012-08-02 16:52:21 -0400 | [diff] [blame] | 572 | float currentHeight = mScaler.getHeight(); |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 573 | final boolean wasClosed = (mOldHeight == mSmallSize); |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 574 | boolean nowExpanded; |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 575 | if (!forceAbort) { |
| 576 | if (wasClosed) { |
| 577 | nowExpanded = currentHeight > mOldHeight && velocity >= 0; |
| 578 | } else { |
| 579 | nowExpanded = currentHeight >= mOldHeight || velocity > 0; |
| 580 | } |
| 581 | nowExpanded |= mNaturalHeight == mSmallSize; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 582 | } else { |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 583 | nowExpanded = !wasClosed; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 584 | } |
Chris Wren | ba925e8 | 2012-04-20 16:46:43 -0400 | [diff] [blame] | 585 | if (mScaleAnimation.isRunning()) { |
| 586 | mScaleAnimation.cancel(); |
| 587 | } |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 588 | mCallback.expansionStateChanged(false); |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 589 | int naturalHeight = mScaler.getNaturalHeight(); |
Selim Cinek | 388df6d | 2015-10-22 13:25:11 -0700 | [diff] [blame] | 590 | float targetHeight = nowExpanded ? naturalHeight : mSmallSize; |
Selim Cinek | 5b1591a | 2017-07-03 17:05:01 +0200 | [diff] [blame] | 591 | if (targetHeight != currentHeight && mEnabled && allowAnimation) { |
Chris Wren | 3ddab0d | 2012-08-02 16:52:21 -0400 | [diff] [blame] | 592 | mScaleAnimation.setFloatValues(targetHeight); |
| 593 | mScaleAnimation.setupStartValues(); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 594 | final View scaledView = mResizedView; |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 595 | final boolean expand = nowExpanded; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 596 | mScaleAnimation.addListener(new AnimatorListenerAdapter() { |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 597 | public boolean mCancelled; |
| 598 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 599 | @Override |
| 600 | public void onAnimationEnd(Animator animation) { |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 601 | if (!mCancelled) { |
| 602 | mCallback.setUserExpandedChild(scaledView, expand); |
Selim Cinek | 3acdc8e7 | 2017-04-06 17:38:27 -0700 | [diff] [blame] | 603 | if (!mExpanding) { |
| 604 | mScaler.setView(null); |
| 605 | } |
Mady Mellor | b0a8246 | 2016-04-30 17:31:02 -0700 | [diff] [blame] | 606 | } else { |
| 607 | mCallback.setExpansionCancelled(scaledView); |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 608 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 609 | mCallback.setUserLockedChild(scaledView, false); |
| 610 | mScaleAnimation.removeListener(this); |
| 611 | } |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 612 | |
| 613 | @Override |
| 614 | public void onAnimationCancel(Animator animation) { |
| 615 | mCancelled = true; |
| 616 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 617 | }); |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 618 | velocity = nowExpanded == velocity >= 0 ? velocity : 0; |
Jorim Jaggi | 28c0b714 | 2014-07-26 12:36:35 +0200 | [diff] [blame] | 619 | mFlingAnimationUtils.apply(mScaleAnimation, currentHeight, targetHeight, velocity); |
Chris Wren | 3ddab0d | 2012-08-02 16:52:21 -0400 | [diff] [blame] | 620 | mScaleAnimation.start(); |
Selim Cinek | 343e6e2 | 2014-04-11 21:23:30 +0200 | [diff] [blame] | 621 | } else { |
Selim Cinek | c25af40 | 2017-01-12 11:57:11 -0800 | [diff] [blame] | 622 | if (targetHeight != currentHeight) { |
| 623 | mScaler.setHeight(targetHeight); |
| 624 | } |
Selim Cinek | d191a17 | 2016-03-11 18:25:05 -0800 | [diff] [blame] | 625 | mCallback.setUserExpandedChild(mResizedView, nowExpanded); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 626 | mCallback.setUserLockedChild(mResizedView, false); |
Selim Cinek | f306d9b | 2017-02-21 11:45:13 -0800 | [diff] [blame] | 627 | mScaler.setView(null); |
Chris Wren | 3ddab0d | 2012-08-02 16:52:21 -0400 | [diff] [blame] | 628 | } |
Daniel Sandler | 4377d14 | 2012-09-11 15:18:47 -0400 | [diff] [blame] | 629 | |
| 630 | mExpanding = false; |
| 631 | mExpansionStyle = NONE; |
| 632 | |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 633 | if (DEBUG) Log.d(TAG, "wasClosed is: " + wasClosed); |
| 634 | if (DEBUG) Log.d(TAG, "currentHeight is: " + currentHeight); |
| 635 | if (DEBUG) Log.d(TAG, "mSmallSize is: " + mSmallSize); |
| 636 | if (DEBUG) Log.d(TAG, "targetHeight is: " + targetHeight); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 637 | if (DEBUG) Log.d(TAG, "scale was finished on view: " + mResizedView); |
Chris Wren | 80a7627 | 2012-04-18 10:52:18 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | private void clearView() { |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 641 | mResizedView = null; |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 644 | /** |
Selim Cinek | 5b1591a | 2017-07-03 17:05:01 +0200 | [diff] [blame] | 645 | * Use this to abort any pending expansions in progress and force that there will be no |
| 646 | * animations. |
| 647 | */ |
| 648 | public void cancelImmediately() { |
| 649 | cancel(false /* allowAnimation */); |
| 650 | } |
| 651 | |
| 652 | /** |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 653 | * Use this to abort any pending expansions in progress. |
| 654 | */ |
| 655 | public void cancel() { |
Selim Cinek | 5b1591a | 2017-07-03 17:05:01 +0200 | [diff] [blame] | 656 | cancel(true /* allowAnimation */); |
| 657 | } |
| 658 | |
| 659 | private void cancel(boolean allowAnimation) { |
| 660 | finishExpanding(true /* forceAbort */, 0f /* velocity */, allowAnimation); |
Daniel Sandler | ac47ff7 | 2012-10-23 10:41:44 -0400 | [diff] [blame] | 661 | clearView(); |
| 662 | |
| 663 | // reset the gesture detector |
| 664 | mSGD = new ScaleGestureDetector(mContext, mScaleGestureListener); |
| 665 | } |
| 666 | |
| 667 | /** |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 668 | * Change the expansion mode to only observe movements and don't perform any resizing. |
| 669 | * This is needed when the expanding is finished and the scroller kicks in, |
| 670 | * performing an overscroll motion. We only want to shrink it again when we are not |
| 671 | * overscrolled. |
| 672 | * |
| 673 | * @param onlyMovements Should only movements be observed? |
| 674 | */ |
| 675 | public void onlyObserveMovements(boolean onlyMovements) { |
| 676 | mOnlyMovements = onlyMovements; |
| 677 | } |
Daniel Sandler | 6a858c3 | 2012-03-12 14:38:58 -0400 | [diff] [blame] | 678 | } |
Chris Wren | b4e2c48b | 2012-06-15 16:51:54 -0400 | [diff] [blame] | 679 | |