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