Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.phone; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.AnimatorSet; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 22 | import android.animation.ArgbEvaluator; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 23 | import android.animation.ObjectAnimator; |
| 24 | import android.animation.ValueAnimator; |
| 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
| 26 | import android.content.Context; |
| 27 | import android.graphics.Canvas; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 28 | import android.graphics.Matrix; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
| 30 | import android.graphics.Rect; |
| 31 | import android.os.Handler; |
| 32 | import android.os.RemoteException; |
| 33 | import android.util.Log; |
| 34 | import android.util.Slog; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 35 | import android.view.GestureDetector; |
| 36 | import android.view.MotionEvent; |
| 37 | import android.view.View; |
| 38 | import android.view.ViewConfiguration; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 39 | import android.view.WindowManagerGlobal; |
| 40 | import android.view.animation.DecelerateInterpolator; |
| 41 | import android.view.animation.Interpolator; |
| 42 | import android.support.annotation.DimenRes; |
| 43 | import com.android.systemui.Dependency; |
| 44 | import com.android.systemui.OverviewProxyService; |
| 45 | import com.android.systemui.R; |
| 46 | import com.android.systemui.plugins.statusbar.phone.NavGesture.GestureHelper; |
| 47 | import com.android.systemui.shared.recents.IOverviewProxy; |
| 48 | import com.android.systemui.shared.recents.utilities.Utilities; |
| 49 | |
| 50 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT; |
| 51 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_BOTTOM; |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 52 | import static com.android.systemui.OverviewProxyService.DEBUG_OVERVIEW_PROXY; |
| 53 | import static com.android.systemui.OverviewProxyService.TAG_OPS; |
Winson Chung | c4e0620 | 2018-02-13 10:37:35 -0800 | [diff] [blame] | 54 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_HOME; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Class to detect gestures on the navigation bar and implement quick scrub and switch. |
| 58 | */ |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 59 | public class QuickStepController extends GestureDetector.SimpleOnGestureListener implements |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 60 | GestureHelper { |
| 61 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 62 | private static final String TAG = "QuickStepController"; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 63 | private static final int QUICK_SWITCH_FLING_VELOCITY = 0; |
| 64 | private static final int ANIM_DURATION_MS = 200; |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 65 | private static final long LONG_PRESS_DELAY_MS = 225; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * For quick step, set a damping value to allow the button to stick closer its origin position |
| 69 | * when dragging before quick scrub is active. |
| 70 | */ |
| 71 | private static final int SWITCH_STICKINESS = 4; |
| 72 | |
| 73 | private NavigationBarView mNavigationBarView; |
| 74 | private GestureDetector mGestureDetector; |
| 75 | |
| 76 | private boolean mDraggingActive; |
| 77 | private boolean mQuickScrubActive; |
Winson Chung | 4965884 | 2018-02-08 12:52:21 -0800 | [diff] [blame] | 78 | private boolean mAllowQuickSwitch; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 79 | private boolean mAllowGestureDetection; |
| 80 | private boolean mQuickStepStarted; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 81 | private float mDownOffset; |
| 82 | private float mTranslation; |
| 83 | private int mTouchDownX; |
| 84 | private int mTouchDownY; |
| 85 | private boolean mDragPositive; |
| 86 | private boolean mIsVertical; |
| 87 | private boolean mIsRTL; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 88 | private float mTrackAlpha; |
| 89 | private int mLightTrackColor; |
| 90 | private int mDarkTrackColor; |
| 91 | private float mDarkIntensity; |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 92 | private View mHomeButtonView; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 93 | |
| 94 | private final Handler mHandler = new Handler(); |
| 95 | private final Interpolator mQuickScrubEndInterpolator = new DecelerateInterpolator(); |
| 96 | private final Rect mTrackRect = new Rect(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 97 | private final Paint mTrackPaint = new Paint(); |
| 98 | private final int mScrollTouchSlop; |
| 99 | private final OverviewProxyService mOverviewEventSender; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 100 | private final int mTrackThickness; |
| 101 | private final int mTrackPadding; |
| 102 | private final ValueAnimator mTrackAnimator; |
| 103 | private final ValueAnimator mButtonAnimator; |
| 104 | private final AnimatorSet mQuickScrubEndAnimator; |
| 105 | private final Context mContext; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 106 | private final Matrix mTransformGlobalMatrix = new Matrix(); |
| 107 | private final Matrix mTransformLocalMatrix = new Matrix(); |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 108 | private final ArgbEvaluator mTrackColorEvaluator = new ArgbEvaluator(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 109 | |
| 110 | private final AnimatorUpdateListener mTrackAnimatorListener = valueAnimator -> { |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 111 | mTrackAlpha = (float) valueAnimator.getAnimatedValue(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 112 | mNavigationBarView.invalidate(); |
| 113 | }; |
| 114 | |
| 115 | private final AnimatorUpdateListener mButtonTranslationListener = animator -> { |
| 116 | int pos = (int) animator.getAnimatedValue(); |
| 117 | if (!mQuickScrubActive) { |
| 118 | pos = mDragPositive ? Math.min((int) mTranslation, pos) : Math.max((int) mTranslation, pos); |
| 119 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 120 | if (mIsVertical) { |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 121 | mHomeButtonView.setTranslationY(pos); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 122 | } else { |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 123 | mHomeButtonView.setTranslationX(pos); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 124 | } |
| 125 | }; |
| 126 | |
| 127 | private AnimatorListenerAdapter mQuickScrubEndListener = new AnimatorListenerAdapter() { |
| 128 | @Override |
| 129 | public void onAnimationEnd(Animator animation) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 130 | mQuickScrubActive = false; |
| 131 | mTranslation = 0; |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 132 | mQuickScrubEndAnimator.setCurrentPlayTime(mQuickScrubEndAnimator.getDuration()); |
| 133 | mHomeButtonView = null; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 134 | } |
| 135 | }; |
| 136 | |
| 137 | private Runnable mLongPressRunnable = this::startQuickScrub; |
| 138 | |
| 139 | private final GestureDetector.SimpleOnGestureListener mGestureListener = |
| 140 | new GestureDetector.SimpleOnGestureListener() { |
| 141 | @Override |
| 142 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velX, float velY) { |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 143 | if (!mNavigationBarView.isQuickScrubEnabled() || mQuickScrubActive |
| 144 | || !mAllowQuickSwitch |
| 145 | || mNavigationBarView.getDownHitTarget() != HIT_TARGET_HOME) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | float velocityX = mIsRTL ? -velX : velX; |
| 149 | float absVelY = Math.abs(velY); |
| 150 | final boolean isValidFling = velocityX > QUICK_SWITCH_FLING_VELOCITY && |
| 151 | mIsVertical ? (absVelY > velocityX) : (velocityX > absVelY); |
| 152 | if (isValidFling) { |
| 153 | mDraggingActive = false; |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 154 | animateEnd(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 155 | mHandler.removeCallbacks(mLongPressRunnable); |
| 156 | try { |
| 157 | final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy(); |
| 158 | overviewProxy.onQuickSwitch(); |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 159 | if (DEBUG_OVERVIEW_PROXY) { |
| 160 | Log.d(TAG_OPS, "Quick Switch"); |
| 161 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 162 | } catch (RemoteException e) { |
| 163 | Log.e(TAG, "Failed to send start of quick switch.", e); |
| 164 | } |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 165 | return true; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 166 | } |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 167 | return false; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 171 | public QuickStepController(Context context) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 172 | mContext = context; |
| 173 | mScrollTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 174 | mOverviewEventSender = Dependency.get(OverviewProxyService.class); |
| 175 | mGestureDetector = new GestureDetector(mContext, mGestureListener); |
| 176 | mTrackThickness = getDimensionPixelSize(mContext, R.dimen.nav_quick_scrub_track_thickness); |
| 177 | mTrackPadding = getDimensionPixelSize(mContext, R.dimen.nav_quick_scrub_track_edge_padding); |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 178 | mTrackPaint.setAlpha(0); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 179 | |
| 180 | mTrackAnimator = ObjectAnimator.ofFloat(); |
| 181 | mTrackAnimator.addUpdateListener(mTrackAnimatorListener); |
Matthew Ng | 55437a9 | 2018-02-23 15:02:07 -0800 | [diff] [blame] | 182 | mTrackAnimator.setFloatValues(0); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 183 | mButtonAnimator = ObjectAnimator.ofInt(); |
| 184 | mButtonAnimator.addUpdateListener(mButtonTranslationListener); |
Matthew Ng | 55437a9 | 2018-02-23 15:02:07 -0800 | [diff] [blame] | 185 | mButtonAnimator.setIntValues(0); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 186 | mQuickScrubEndAnimator = new AnimatorSet(); |
| 187 | mQuickScrubEndAnimator.playTogether(mTrackAnimator, mButtonAnimator); |
| 188 | mQuickScrubEndAnimator.setDuration(ANIM_DURATION_MS); |
| 189 | mQuickScrubEndAnimator.addListener(mQuickScrubEndListener); |
| 190 | mQuickScrubEndAnimator.setInterpolator(mQuickScrubEndInterpolator); |
| 191 | } |
| 192 | |
| 193 | public void setComponents(NavigationBarView navigationBarView) { |
| 194 | mNavigationBarView = navigationBarView; |
| 195 | } |
| 196 | |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 197 | /** |
| 198 | * @return true if we want to intercept touch events for quick scrub/switch and prevent proxying |
| 199 | * the event to the overview service. |
| 200 | */ |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 201 | @Override |
| 202 | public boolean onInterceptTouchEvent(MotionEvent event) { |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 203 | return handleTouchEvent(event); |
| 204 | } |
| 205 | |
| 206 | /** |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 207 | * @return true if we want to handle touch events for quick scrub/switch or if down event (that |
| 208 | * will get consumed and ignored). No events will be proxied to the overview service. |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 209 | */ |
| 210 | @Override |
| 211 | public boolean onTouchEvent(MotionEvent event) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 212 | // The same down event was just sent on intercept and therefore can be ignored here |
| 213 | final boolean ignoreProxyDownEvent = event.getAction() == MotionEvent.ACTION_DOWN |
| 214 | && mOverviewEventSender.getProxy() != null; |
| 215 | return ignoreProxyDownEvent || handleTouchEvent(event); |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private boolean handleTouchEvent(MotionEvent event) { |
Matthew Ng | 22cf514 | 2018-03-14 12:26:14 -0700 | [diff] [blame^] | 219 | if (!mNavigationBarView.isQuickScrubEnabled() |
| 220 | && !mNavigationBarView.isQuickStepSwipeUpEnabled()) { |
| 221 | mNavigationBarView.getHomeButton().setDelayTouchFeedback(false /* delay */); |
| 222 | return false; |
| 223 | } |
| 224 | mNavigationBarView.requestUnbufferedDispatch(event); |
| 225 | |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 226 | final ButtonDispatcher homeButton = mNavigationBarView.getHomeButton(); |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 227 | if (mGestureDetector.onTouchEvent(event)) { |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 228 | // If the fling has been handled on UP, then skip proxying the UP |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 229 | return true; |
| 230 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 231 | final boolean homePressed = mNavigationBarView.getDownHitTarget() == HIT_TARGET_HOME; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 232 | int action = event.getAction(); |
| 233 | switch (action & MotionEvent.ACTION_MASK) { |
| 234 | case MotionEvent.ACTION_DOWN: { |
| 235 | int x = (int) event.getX(); |
| 236 | int y = (int) event.getY(); |
Winson Chung | 0be8f08 | 2018-02-15 15:52:49 -0800 | [diff] [blame] | 237 | // End any existing quickscrub animations before starting the new transition |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 238 | if (mHomeButtonView != null) { |
Winson Chung | 0be8f08 | 2018-02-15 15:52:49 -0800 | [diff] [blame] | 239 | mQuickScrubEndAnimator.end(); |
| 240 | } |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 241 | mHomeButtonView = homeButton.getCurrentView(); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 242 | homeButton.setDelayTouchFeedback(true /* delay */); |
| 243 | mTouchDownX = x; |
| 244 | mTouchDownY = y; |
| 245 | if (homePressed) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 246 | mHandler.postDelayed(mLongPressRunnable, LONG_PRESS_DELAY_MS); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 247 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 248 | mTransformGlobalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 249 | mTransformLocalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 250 | mNavigationBarView.transformMatrixToGlobal(mTransformGlobalMatrix); |
| 251 | mNavigationBarView.transformMatrixToLocal(mTransformLocalMatrix); |
| 252 | mQuickStepStarted = false; |
Winson Chung | 4965884 | 2018-02-08 12:52:21 -0800 | [diff] [blame] | 253 | mAllowQuickSwitch = true; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 254 | mAllowGestureDetection = true; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | case MotionEvent.ACTION_MOVE: { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 258 | if (mQuickStepStarted || !mAllowGestureDetection){ |
| 259 | break; |
| 260 | } |
| 261 | int x = (int) event.getX(); |
| 262 | int y = (int) event.getY(); |
| 263 | int xDiff = Math.abs(x - mTouchDownX); |
| 264 | int yDiff = Math.abs(y - mTouchDownY); |
| 265 | boolean exceededTouchSlopX = xDiff > mScrollTouchSlop && xDiff > yDiff; |
| 266 | boolean exceededTouchSlopY = yDiff > mScrollTouchSlop && yDiff > xDiff; |
| 267 | boolean exceededTouchSlop, exceededPerpendicularTouchSlop; |
| 268 | int pos, touchDown, offset, trackSize; |
Matthew Ng | e0903c9 | 2018-01-17 15:32:41 -0800 | [diff] [blame] | 269 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 270 | if (mIsVertical) { |
| 271 | exceededTouchSlop = exceededTouchSlopY; |
| 272 | exceededPerpendicularTouchSlop = exceededTouchSlopX; |
| 273 | pos = y; |
| 274 | touchDown = mTouchDownY; |
| 275 | offset = pos - mTrackRect.top; |
| 276 | trackSize = mTrackRect.height(); |
| 277 | } else { |
| 278 | exceededTouchSlop = exceededTouchSlopX; |
| 279 | exceededPerpendicularTouchSlop = exceededTouchSlopY; |
| 280 | pos = x; |
| 281 | touchDown = mTouchDownX; |
| 282 | offset = pos - mTrackRect.left; |
| 283 | trackSize = mTrackRect.width(); |
| 284 | } |
| 285 | // Decide to start quickstep if dragging away from the navigation bar, otherwise in |
| 286 | // the parallel direction, decide to start quickscrub. Only one may run. |
| 287 | if (!mDraggingActive && !mQuickScrubActive && exceededPerpendicularTouchSlop) { |
| 288 | if (mNavigationBarView.isQuickStepSwipeUpEnabled()) { |
| 289 | startQuickStep(event); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 290 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 293 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 294 | // Do not handle quick scrub/switch if disabled or hit target is not home button |
| 295 | if (!homePressed || !mNavigationBarView.isQuickScrubEnabled()) { |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | if (!mDragPositive) { |
| 300 | offset -= mIsVertical ? mTrackRect.height() : mTrackRect.width(); |
| 301 | } |
| 302 | |
| 303 | // Control the button movement |
| 304 | if (!mDraggingActive && exceededTouchSlop) { |
| 305 | boolean allowDrag = !mDragPositive |
| 306 | ? offset < 0 && pos < touchDown : offset >= 0 && pos > touchDown; |
| 307 | if (allowDrag) { |
| 308 | mDownOffset = offset; |
| 309 | homeButton.abortCurrentGesture(); |
| 310 | mDraggingActive = true; |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 311 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 312 | } |
| 313 | if (mDraggingActive && (mDragPositive && offset >= 0 |
| 314 | || !mDragPositive && offset <= 0)) { |
| 315 | float scrubFraction = Utilities.clamp(Math.abs(offset) * 1f / trackSize, 0, 1); |
| 316 | mTranslation = !mDragPositive |
| 317 | ? Utilities.clamp(offset - mDownOffset, -trackSize, 0) |
| 318 | : Utilities.clamp(offset - mDownOffset, 0, trackSize); |
| 319 | if (mQuickScrubActive) { |
| 320 | try { |
| 321 | mOverviewEventSender.getProxy().onQuickScrubProgress(scrubFraction); |
| 322 | if (DEBUG_OVERVIEW_PROXY) { |
| 323 | Log.d(TAG_OPS, "Quick Scrub Progress:" + scrubFraction); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 324 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 325 | } catch (RemoteException e) { |
| 326 | Log.e(TAG, "Failed to send progress of quick scrub.", e); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 327 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 328 | } else { |
| 329 | mTranslation /= SWITCH_STICKINESS; |
| 330 | } |
| 331 | if (mIsVertical) { |
| 332 | mHomeButtonView.setTranslationY(mTranslation); |
| 333 | } else { |
| 334 | mHomeButtonView.setTranslationX(mTranslation); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | case MotionEvent.ACTION_CANCEL: |
| 340 | case MotionEvent.ACTION_UP: |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 341 | endQuickScrub(true /* animate */); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 342 | break; |
| 343 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 344 | |
| 345 | // Proxy motion events to launcher if not handled by quick scrub/switch |
| 346 | boolean handled = mDraggingActive || mQuickScrubActive; |
| 347 | if (!handled && mAllowGestureDetection) { |
| 348 | proxyMotionEvents(event); |
| 349 | } |
| 350 | return handled || mQuickStepStarted; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void onDraw(Canvas canvas) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 355 | if (mNavigationBarView.isQuickScrubEnabled()) { |
| 356 | return; |
| 357 | } |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 358 | int color = (int) mTrackColorEvaluator.evaluate(mDarkIntensity, mLightTrackColor, |
| 359 | mDarkTrackColor); |
| 360 | mTrackPaint.setColor(color); |
| 361 | mTrackPaint.setAlpha((int) (mTrackPaint.getAlpha() * mTrackAlpha)); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 362 | canvas.drawRect(mTrackRect, mTrackPaint); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 367 | final int width = right - left; |
| 368 | final int height = bottom - top; |
| 369 | final int x1, x2, y1, y2; |
| 370 | if (mIsVertical) { |
| 371 | x1 = (width - mTrackThickness) / 2; |
| 372 | x2 = x1 + mTrackThickness; |
| 373 | y1 = mDragPositive ? height / 2 : mTrackPadding; |
| 374 | y2 = y1 + height / 2 - mTrackPadding; |
| 375 | } else { |
| 376 | y1 = (height - mTrackThickness) / 2; |
| 377 | y2 = y1 + mTrackThickness; |
| 378 | x1 = mDragPositive ? width / 2 : mTrackPadding; |
| 379 | x2 = x1 + width / 2 - mTrackPadding; |
| 380 | } |
| 381 | mTrackRect.set(x1, y1, x2, y2); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public void onDarkIntensityChange(float intensity) { |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 386 | mDarkIntensity = intensity; |
| 387 | mNavigationBarView.invalidate(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 391 | public void setBarState(boolean isVertical, boolean isRTL) { |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 392 | final boolean changed = (mIsVertical != isVertical) || (mIsRTL != isRTL); |
| 393 | if (changed) { |
| 394 | // End quickscrub if the state changes mid-transition |
| 395 | endQuickScrub(false /* animate */); |
| 396 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 397 | mIsVertical = isVertical; |
| 398 | mIsRTL = isRTL; |
| 399 | try { |
| 400 | int navbarPos = WindowManagerGlobal.getWindowManagerService().getNavBarPosition(); |
| 401 | mDragPositive = navbarPos == NAV_BAR_LEFT || navbarPos == NAV_BAR_BOTTOM; |
| 402 | if (isRTL) { |
| 403 | mDragPositive = !mDragPositive; |
| 404 | } |
| 405 | } catch (RemoteException e) { |
| 406 | Slog.e(TAG, "Failed to get nav bar position.", e); |
| 407 | } |
| 408 | } |
| 409 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 410 | @Override |
| 411 | public void onNavigationButtonLongPress(View v) { |
| 412 | mAllowGestureDetection = false; |
| 413 | mHandler.removeCallbacksAndMessages(null); |
| 414 | } |
| 415 | |
| 416 | private void startQuickStep(MotionEvent event) { |
| 417 | mQuickStepStarted = true; |
| 418 | event.transform(mTransformGlobalMatrix); |
| 419 | try { |
| 420 | mOverviewEventSender.getProxy().onQuickStep(event); |
| 421 | if (DEBUG_OVERVIEW_PROXY) { |
| 422 | Log.d(TAG_OPS, "Quick Step Start"); |
| 423 | } |
| 424 | } catch (RemoteException e) { |
| 425 | Log.e(TAG, "Failed to send quick step started.", e); |
| 426 | } finally { |
| 427 | event.transform(mTransformLocalMatrix); |
| 428 | } |
| 429 | mOverviewEventSender.notifyQuickStepStarted(); |
| 430 | mNavigationBarView.getHomeButton().abortCurrentGesture(); |
| 431 | cancelQuickSwitch(); |
| 432 | mHandler.removeCallbacksAndMessages(null); |
| 433 | } |
| 434 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 435 | private void startQuickScrub() { |
Matthew Ng | fee0b5b | 2018-02-21 15:38:21 -0800 | [diff] [blame] | 436 | if (!mQuickScrubActive && mDraggingActive) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 437 | mQuickScrubActive = true; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 438 | mLightTrackColor = mContext.getColor(R.color.quick_step_track_background_light); |
| 439 | mDarkTrackColor = mContext.getColor(R.color.quick_step_track_background_dark); |
| 440 | mTrackAnimator.setFloatValues(0, 1); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 441 | mTrackAnimator.start(); |
| 442 | try { |
| 443 | mOverviewEventSender.getProxy().onQuickScrubStart(); |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 444 | if (DEBUG_OVERVIEW_PROXY) { |
| 445 | Log.d(TAG_OPS, "Quick Scrub Start"); |
| 446 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 447 | } catch (RemoteException e) { |
| 448 | Log.e(TAG, "Failed to send start of quick scrub.", e); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 453 | private void endQuickScrub(boolean animate) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 454 | mHandler.removeCallbacks(mLongPressRunnable); |
| 455 | if (mDraggingActive || mQuickScrubActive) { |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 456 | animateEnd(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 457 | try { |
| 458 | mOverviewEventSender.getProxy().onQuickScrubEnd(); |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 459 | if (DEBUG_OVERVIEW_PROXY) { |
| 460 | Log.d(TAG_OPS, "Quick Scrub End"); |
| 461 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 462 | } catch (RemoteException e) { |
| 463 | Log.e(TAG, "Failed to send end of quick scrub.", e); |
| 464 | } |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 465 | } |
| 466 | if (mHomeButtonView != null && !animate) { |
| 467 | mQuickScrubEndAnimator.end(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 468 | } |
| 469 | mDraggingActive = false; |
| 470 | } |
| 471 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 472 | private boolean proxyMotionEvents(MotionEvent event) { |
| 473 | final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy(); |
| 474 | event.transform(mTransformGlobalMatrix); |
| 475 | try { |
| 476 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 477 | overviewProxy.onPreMotionEvent(mNavigationBarView.getDownHitTarget()); |
| 478 | } |
| 479 | overviewProxy.onMotionEvent(event); |
| 480 | if (DEBUG_OVERVIEW_PROXY) { |
| 481 | Log.d(TAG_OPS, "Send MotionEvent: " + event.toString()); |
| 482 | } |
| 483 | return true; |
| 484 | } catch (RemoteException e) { |
| 485 | Log.e(TAG, "Callback failed", e); |
| 486 | } finally { |
| 487 | event.transform(mTransformLocalMatrix); |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 488 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 489 | return false; |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Winson Chung | 4965884 | 2018-02-08 12:52:21 -0800 | [diff] [blame] | 492 | public void cancelQuickSwitch() { |
| 493 | mAllowQuickSwitch = false; |
| 494 | mHandler.removeCallbacks(mLongPressRunnable); |
| 495 | } |
| 496 | |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 497 | private void animateEnd() { |
| 498 | mButtonAnimator.setIntValues((int) mTranslation, 0); |
| 499 | mTrackAnimator.setFloatValues(mTrackAlpha, 0); |
Matthew Ng | 55437a9 | 2018-02-23 15:02:07 -0800 | [diff] [blame] | 500 | mQuickScrubEndAnimator.setCurrentPlayTime(0); |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 501 | mQuickScrubEndAnimator.start(); |
| 502 | } |
| 503 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 504 | private int getDimensionPixelSize(Context context, @DimenRes int resId) { |
| 505 | return context.getResources().getDimensionPixelSize(resId); |
| 506 | } |
| 507 | } |