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