Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | package com.android.systemui.statusbar; |
| 18 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 23 | import android.content.Context; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 24 | import android.view.MotionEvent; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewConfiguration; |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 27 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 28 | import com.android.systemui.ExpandHelper; |
| 29 | import com.android.systemui.Gefingerpoken; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 30 | import com.android.systemui.Interpolators; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 31 | import com.android.systemui.R; |
Dave Mankoff | dde5ee6 | 2019-05-02 17:36:11 -0400 | [diff] [blame] | 32 | import com.android.systemui.classifier.FalsingManagerFactory; |
Dave Mankoff | 468d4f6 | 2019-05-08 14:56:29 -0400 | [diff] [blame] | 33 | import com.android.systemui.plugins.FalsingManager; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.row.ExpandableView; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 35 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 36 | /** |
| 37 | * A utility class to enable the downward swipe on the lockscreen to go to the full shade and expand |
| 38 | * the notification where the drag started. |
| 39 | */ |
| 40 | public class DragDownHelper implements Gefingerpoken { |
| 41 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 42 | private static final float RUBBERBAND_FACTOR_EXPANDABLE = 0.5f; |
| 43 | private static final float RUBBERBAND_FACTOR_STATIC = 0.15f; |
| 44 | |
| 45 | private static final int SPRING_BACK_ANIMATION_LENGTH_MS = 375; |
| 46 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 47 | private int mMinDragDistance; |
| 48 | private ExpandHelper.Callback mCallback; |
| 49 | private float mInitialTouchX; |
| 50 | private float mInitialTouchY; |
| 51 | private boolean mDraggingDown; |
| 52 | private float mTouchSlop; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 53 | private DragDownCallback mDragDownCallback; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 54 | private View mHost; |
| 55 | private final int[] mTemp2 = new int[2]; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 56 | private boolean mDraggedFarEnough; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 57 | private ExpandableView mStartingChild; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 58 | private float mLastHeight; |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 59 | private FalsingManager mFalsingManager; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 60 | |
| 61 | public DragDownHelper(Context context, View host, ExpandHelper.Callback callback, |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 62 | DragDownCallback dragDownCallback) { |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 63 | mMinDragDistance = context.getResources().getDimensionPixelSize( |
| 64 | R.dimen.keyguard_drag_down_min_distance); |
| 65 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
| 66 | mCallback = callback; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 67 | mDragDownCallback = dragDownCallback; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 68 | mHost = host; |
Dave Mankoff | dde5ee6 | 2019-05-02 17:36:11 -0400 | [diff] [blame] | 69 | mFalsingManager = FalsingManagerFactory.getInstance(context); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 74 | final float x = event.getX(); |
| 75 | final float y = event.getY(); |
| 76 | |
| 77 | switch (event.getActionMasked()) { |
| 78 | case MotionEvent.ACTION_DOWN: |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 79 | mDraggedFarEnough = false; |
| 80 | mDraggingDown = false; |
| 81 | mStartingChild = null; |
| 82 | mInitialTouchY = y; |
| 83 | mInitialTouchX = x; |
| 84 | break; |
| 85 | |
| 86 | case MotionEvent.ACTION_MOVE: |
| 87 | final float h = y - mInitialTouchY; |
| 88 | if (h > mTouchSlop && h > Math.abs(x - mInitialTouchX)) { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 89 | mFalsingManager.onNotificatonStartDraggingDown(); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 90 | mDraggingDown = true; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 91 | captureStartingChild(mInitialTouchX, mInitialTouchY); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 92 | mInitialTouchY = y; |
| 93 | mInitialTouchX = x; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 94 | mDragDownCallback.onTouchSlopExceeded(); |
Selim Cinek | 5454a0d | 2019-07-30 17:14:50 -0700 | [diff] [blame] | 95 | return mStartingChild != null || mDragDownCallback.isDragDownAnywhereEnabled(); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 96 | } |
| 97 | break; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean onTouchEvent(MotionEvent event) { |
| 104 | if (!mDraggingDown) { |
| 105 | return false; |
| 106 | } |
| 107 | final float x = event.getX(); |
| 108 | final float y = event.getY(); |
| 109 | |
| 110 | switch (event.getActionMasked()) { |
| 111 | case MotionEvent.ACTION_MOVE: |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 112 | mLastHeight = y - mInitialTouchY; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 113 | captureStartingChild(mInitialTouchX, mInitialTouchY); |
| 114 | if (mStartingChild != null) { |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 115 | handleExpansion(mLastHeight, mStartingChild); |
| 116 | } else { |
| 117 | mDragDownCallback.setEmptyDragAmount(mLastHeight); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 118 | } |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 119 | if (mLastHeight > mMinDragDistance) { |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 120 | if (!mDraggedFarEnough) { |
| 121 | mDraggedFarEnough = true; |
Selim Cinek | 177fd43 | 2015-11-18 11:53:47 -0800 | [diff] [blame] | 122 | mDragDownCallback.onCrossedThreshold(true); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 123 | } |
| 124 | } else { |
| 125 | if (mDraggedFarEnough) { |
| 126 | mDraggedFarEnough = false; |
Selim Cinek | 177fd43 | 2015-11-18 11:53:47 -0800 | [diff] [blame] | 127 | mDragDownCallback.onCrossedThreshold(false); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | case MotionEvent.ACTION_UP: |
Dave Mankoff | c88d622 | 2018-10-25 15:31:20 -0400 | [diff] [blame] | 132 | if (!mFalsingManager.isUnlockingDisabled() && !isFalseTouch() |
| 133 | && mDragDownCallback.onDraggedDown(mStartingChild, |
Christoph Studer | b018399 | 2014-12-22 21:02:26 +0100 | [diff] [blame] | 134 | (int) (y - mInitialTouchY))) { |
Jorim Jaggi | dbc3dce | 2014-08-01 01:16:36 +0200 | [diff] [blame] | 135 | if (mStartingChild == null) { |
Lucas Dupin | 55c6e80 | 2018-09-27 18:07:36 -0700 | [diff] [blame] | 136 | cancelExpansion(); |
Selim Cinek | 5243c12 | 2015-10-21 14:00:33 -0700 | [diff] [blame] | 137 | } else { |
| 138 | mCallback.setUserLockedChild(mStartingChild, false); |
Selim Cinek | f306d9b | 2017-02-21 11:45:13 -0800 | [diff] [blame] | 139 | mStartingChild = null; |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 140 | } |
Jorim Jaggi | 98fb09c | 2014-05-01 22:40:56 +0200 | [diff] [blame] | 141 | mDraggingDown = false; |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 142 | } else { |
| 143 | stopDragging(); |
| 144 | return false; |
| 145 | } |
| 146 | break; |
| 147 | case MotionEvent.ACTION_CANCEL: |
| 148 | stopDragging(); |
| 149 | return false; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 154 | private boolean isFalseTouch() { |
Selim Cinek | 980a44e | 2017-08-23 12:21:23 -0700 | [diff] [blame] | 155 | if (!mDragDownCallback.isFalsingCheckNeeded()) { |
| 156 | return false; |
| 157 | } |
Blazej Magnowski | 52af6b6 | 2015-09-30 15:46:17 -0700 | [diff] [blame] | 158 | return mFalsingManager.isFalseTouch() || !mDraggedFarEnough; |
Blazej Magnowski | 6dc59b4 | 2015-09-22 15:14:20 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 161 | private void captureStartingChild(float x, float y) { |
| 162 | if (mStartingChild == null) { |
| 163 | mStartingChild = findView(x, y); |
| 164 | if (mStartingChild != null) { |
Selim Cinek | 5454a0d | 2019-07-30 17:14:50 -0700 | [diff] [blame] | 165 | if (mDragDownCallback.isDragDownEnabledForView(mStartingChild)) { |
| 166 | mCallback.setUserLockedChild(mStartingChild, true); |
| 167 | } else { |
| 168 | mStartingChild = null; |
| 169 | } |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private void handleExpansion(float heightDelta, ExpandableView child) { |
| 175 | if (heightDelta < 0) { |
| 176 | heightDelta = 0; |
| 177 | } |
| 178 | boolean expandable = child.isContentExpandable(); |
| 179 | float rubberbandFactor = expandable |
| 180 | ? RUBBERBAND_FACTOR_EXPANDABLE |
| 181 | : RUBBERBAND_FACTOR_STATIC; |
| 182 | float rubberband = heightDelta * rubberbandFactor; |
Selim Cinek | 2c58461 | 2016-02-29 16:14:25 -0800 | [diff] [blame] | 183 | if (expandable |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 184 | && (rubberband + child.getCollapsedHeight()) > child.getMaxContentHeight()) { |
Selim Cinek | 2c58461 | 2016-02-29 16:14:25 -0800 | [diff] [blame] | 185 | float overshoot = |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 186 | (rubberband + child.getCollapsedHeight()) - child.getMaxContentHeight(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 187 | overshoot *= (1 - RUBBERBAND_FACTOR_STATIC); |
| 188 | rubberband -= overshoot; |
| 189 | } |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 190 | child.setActualHeight((int) (child.getCollapsedHeight() + rubberband)); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private void cancelExpansion(final ExpandableView child) { |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 194 | if (child.getActualHeight() == child.getCollapsedHeight()) { |
Selim Cinek | 5243c12 | 2015-10-21 14:00:33 -0700 | [diff] [blame] | 195 | mCallback.setUserLockedChild(child, false); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 196 | return; |
| 197 | } |
Selim Cinek | eef8428 | 2015-10-30 16:28:00 -0700 | [diff] [blame] | 198 | ObjectAnimator anim = ObjectAnimator.ofInt(child, "actualHeight", |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 199 | child.getActualHeight(), child.getCollapsedHeight()); |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 200 | anim.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 201 | anim.setDuration(SPRING_BACK_ANIMATION_LENGTH_MS); |
| 202 | anim.addListener(new AnimatorListenerAdapter() { |
| 203 | @Override |
| 204 | public void onAnimationEnd(Animator animation) { |
| 205 | mCallback.setUserLockedChild(child, false); |
| 206 | } |
| 207 | }); |
| 208 | anim.start(); |
| 209 | } |
| 210 | |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 211 | private void cancelExpansion() { |
| 212 | ValueAnimator anim = ValueAnimator.ofFloat(mLastHeight, 0); |
Selim Cinek | c18010f | 2016-01-20 13:41:30 -0800 | [diff] [blame] | 213 | anim.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 214 | anim.setDuration(SPRING_BACK_ANIMATION_LENGTH_MS); |
Lucas Dupin | 55c6e80 | 2018-09-27 18:07:36 -0700 | [diff] [blame] | 215 | anim.addUpdateListener(animation -> { |
| 216 | mDragDownCallback.setEmptyDragAmount((Float) animation.getAnimatedValue()); |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 217 | }); |
| 218 | anim.start(); |
| 219 | } |
| 220 | |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 221 | private void stopDragging() { |
Blazej Magnowski | 0e2ffbd | 2015-09-10 14:37:17 -0700 | [diff] [blame] | 222 | mFalsingManager.onNotificatonStopDraggingDown(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 223 | if (mStartingChild != null) { |
| 224 | cancelExpansion(mStartingChild); |
Selim Cinek | f306d9b | 2017-02-21 11:45:13 -0800 | [diff] [blame] | 225 | mStartingChild = null; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 226 | } else { |
| 227 | cancelExpansion(); |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 228 | } |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 229 | mDraggingDown = false; |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 230 | mDragDownCallback.onDragDownReset(); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 233 | private ExpandableView findView(float x, float y) { |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 234 | mHost.getLocationOnScreen(mTemp2); |
| 235 | x += mTemp2[0]; |
| 236 | y += mTemp2[1]; |
| 237 | return mCallback.getChildAtRawPosition(x, y); |
| 238 | } |
| 239 | |
Selim Cinek | 740c111 | 2016-12-22 16:39:54 +0100 | [diff] [blame] | 240 | public boolean isDraggingDown() { |
| 241 | return mDraggingDown; |
| 242 | } |
| 243 | |
Selim Cinek | 5454a0d | 2019-07-30 17:14:50 -0700 | [diff] [blame] | 244 | public boolean isDragDownEnabled() { |
| 245 | return mDragDownCallback.isDragDownEnabledForView(null); |
| 246 | } |
| 247 | |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 248 | public interface DragDownCallback { |
| 249 | |
| 250 | /** |
| 251 | * @return true if the interaction is accepted, false if it should be cancelled |
| 252 | */ |
Christoph Studer | b018399 | 2014-12-22 21:02:26 +0100 | [diff] [blame] | 253 | boolean onDraggedDown(View startingChild, int dragLengthY); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 254 | void onDragDownReset(); |
Selim Cinek | 177fd43 | 2015-11-18 11:53:47 -0800 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * The user has dragged either above or below the threshold |
| 258 | * @param above whether he dragged above it |
| 259 | */ |
| 260 | void onCrossedThreshold(boolean above); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 261 | void onTouchSlopExceeded(); |
Jorim Jaggi | 48bc36a | 2014-07-25 23:16:04 +0200 | [diff] [blame] | 262 | void setEmptyDragAmount(float amount); |
Selim Cinek | 980a44e | 2017-08-23 12:21:23 -0700 | [diff] [blame] | 263 | boolean isFalsingCheckNeeded(); |
Selim Cinek | 5454a0d | 2019-07-30 17:14:50 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Is dragging down enabled on a given view |
| 267 | * @param view The view to check or {@code null} to check if it's enabled at all |
| 268 | */ |
| 269 | boolean isDragDownEnabledForView(ExpandableView view); |
| 270 | |
| 271 | /** |
| 272 | * @return if drag down is enabled anywhere, not just on selected views. |
| 273 | */ |
| 274 | boolean isDragDownAnywhereEnabled(); |
Jorim Jaggi | ecbab36 | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 275 | } |
| 276 | } |