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 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 19 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_BOTTOM; |
| 20 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT; |
| 21 | import static com.android.systemui.Interpolators.ALPHA_IN; |
| 22 | import static com.android.systemui.Interpolators.ALPHA_OUT; |
| 23 | import static com.android.systemui.OverviewProxyService.DEBUG_OVERVIEW_PROXY; |
| 24 | import static com.android.systemui.OverviewProxyService.TAG_OPS; |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 25 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_DEAD_ZONE; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 26 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 27 | import android.animation.Animator; |
| 28 | import android.animation.AnimatorListenerAdapter; |
| 29 | import android.animation.AnimatorSet; |
| 30 | import android.animation.ObjectAnimator; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 31 | import android.animation.PropertyValuesHolder; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 32 | import android.content.Context; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 34 | import android.graphics.Canvas; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 35 | import android.graphics.Matrix; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 36 | import android.graphics.Paint; |
| 37 | import android.graphics.RadialGradient; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 38 | import android.graphics.Rect; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 39 | import android.graphics.Shader; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.RemoteException; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 42 | import android.util.FloatProperty; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | import android.util.Slog; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 45 | import android.view.MotionEvent; |
| 46 | import android.view.View; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 47 | import android.view.WindowManagerGlobal; |
| 48 | import android.view.animation.DecelerateInterpolator; |
| 49 | import android.view.animation.Interpolator; |
| 50 | import android.support.annotation.DimenRes; |
| 51 | import com.android.systemui.Dependency; |
| 52 | import com.android.systemui.OverviewProxyService; |
| 53 | import com.android.systemui.R; |
| 54 | import com.android.systemui.plugins.statusbar.phone.NavGesture.GestureHelper; |
| 55 | import com.android.systemui.shared.recents.IOverviewProxy; |
| 56 | import com.android.systemui.shared.recents.utilities.Utilities; |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 57 | import com.android.systemui.shared.system.NavigationBarCompat; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 58 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 59 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 60 | * Class to detect gestures on the navigation bar and implement quick scrub. |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 61 | */ |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 62 | public class QuickStepController implements GestureHelper { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 63 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 64 | private static final String TAG = "QuickStepController"; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 65 | private static final int ANIM_IN_DURATION_MS = 150; |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 66 | private static final int ANIM_OUT_DURATION_MS = 134; |
| 67 | private static final float TRACK_SCALE = 0.95f; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 68 | private static final float GRADIENT_WIDTH = .75f; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 69 | |
| 70 | private NavigationBarView mNavigationBarView; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 71 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 72 | private boolean mQuickScrubActive; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 73 | private boolean mAllowGestureDetection; |
| 74 | private boolean mQuickStepStarted; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 75 | private int mTouchDownX; |
| 76 | private int mTouchDownY; |
| 77 | private boolean mDragPositive; |
| 78 | private boolean mIsVertical; |
| 79 | private boolean mIsRTL; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 80 | private float mTrackAlpha; |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 81 | private float mTrackScale = TRACK_SCALE; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 82 | private float mDarkIntensity; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 83 | private RadialGradient mHighlight; |
| 84 | private float mHighlightCenter; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 85 | private AnimatorSet mTrackAnimator; |
| 86 | private ButtonDispatcher mHitTarget; |
| 87 | private View mCurrentNavigationBarView; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 88 | |
| 89 | private final Handler mHandler = new Handler(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 90 | private final Rect mTrackRect = new Rect(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 91 | private final OverviewProxyService mOverviewEventSender; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 92 | private final int mTrackThickness; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 93 | private final int mTrackEndPadding; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 94 | private final Context mContext; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 95 | private final Matrix mTransformGlobalMatrix = new Matrix(); |
| 96 | private final Matrix mTransformLocalMatrix = new Matrix(); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 97 | private final Paint mTrackPaint = new Paint(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 98 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 99 | private final FloatProperty<QuickStepController> mTrackAlphaProperty = |
| 100 | new FloatProperty<QuickStepController>("TrackAlpha") { |
| 101 | @Override |
| 102 | public void setValue(QuickStepController controller, float alpha) { |
| 103 | mTrackAlpha = alpha; |
| 104 | mNavigationBarView.invalidate(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Float get(QuickStepController controller) { |
| 109 | return mTrackAlpha; |
| 110 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 113 | private final FloatProperty<QuickStepController> mTrackScaleProperty = |
| 114 | new FloatProperty<QuickStepController>("TrackScale") { |
| 115 | @Override |
| 116 | public void setValue(QuickStepController controller, float scale) { |
| 117 | mTrackScale = scale; |
| 118 | mNavigationBarView.invalidate(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Float get(QuickStepController controller) { |
| 123 | return mTrackScale; |
| 124 | } |
| 125 | }; |
| 126 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 127 | private final FloatProperty<QuickStepController> mNavBarAlphaProperty = |
| 128 | new FloatProperty<QuickStepController>("NavBarAlpha") { |
| 129 | @Override |
| 130 | public void setValue(QuickStepController controller, float alpha) { |
| 131 | if (mCurrentNavigationBarView != null) { |
| 132 | mCurrentNavigationBarView.setAlpha(alpha); |
| 133 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 134 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | public Float get(QuickStepController controller) { |
| 138 | if (mCurrentNavigationBarView != null) { |
| 139 | return mCurrentNavigationBarView.getAlpha(); |
| 140 | } |
| 141 | return 1f; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | private AnimatorListenerAdapter mQuickScrubEndListener = new AnimatorListenerAdapter() { |
| 146 | @Override |
| 147 | public void onAnimationEnd(Animator animation) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 148 | resetQuickScrub(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 152 | public QuickStepController(Context context) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 153 | final Resources res = context.getResources(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 154 | mContext = context; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 155 | mOverviewEventSender = Dependency.get(OverviewProxyService.class); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 156 | mTrackThickness = res.getDimensionPixelSize(R.dimen.nav_quick_scrub_track_thickness); |
| 157 | mTrackEndPadding = res.getDimensionPixelSize(R.dimen.nav_quick_scrub_track_edge_padding); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 158 | mTrackPaint.setAntiAlias(true); |
| 159 | mTrackPaint.setDither(true); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | public void setComponents(NavigationBarView navigationBarView) { |
| 163 | mNavigationBarView = navigationBarView; |
| 164 | } |
| 165 | |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 166 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 167 | * @return true if we want to intercept touch events for quick scrub and prevent proxying the |
| 168 | * event to the overview service. |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 169 | */ |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 170 | @Override |
| 171 | public boolean onInterceptTouchEvent(MotionEvent event) { |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 172 | return handleTouchEvent(event); |
| 173 | } |
| 174 | |
| 175 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 176 | * @return true if we want to handle touch events for quick scrub or if down event (that will |
| 177 | * 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] | 178 | */ |
| 179 | @Override |
| 180 | public boolean onTouchEvent(MotionEvent event) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 181 | // The same down event was just sent on intercept and therefore can be ignored here |
| 182 | final boolean ignoreProxyDownEvent = event.getAction() == MotionEvent.ACTION_DOWN |
| 183 | && mOverviewEventSender.getProxy() != null; |
| 184 | return ignoreProxyDownEvent || handleTouchEvent(event); |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | private boolean handleTouchEvent(MotionEvent event) { |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 188 | final boolean deadZoneConsumed = |
| 189 | mNavigationBarView.getDownHitTarget() == HIT_TARGET_DEAD_ZONE; |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 190 | if (mOverviewEventSender.getProxy() == null || (!mNavigationBarView.isQuickScrubEnabled() |
| 191 | && !mNavigationBarView.isQuickStepSwipeUpEnabled())) { |
Matthew Ng | 22cf514 | 2018-03-14 12:26:14 -0700 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | mNavigationBarView.requestUnbufferedDispatch(event); |
| 195 | |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 196 | int action = event.getActionMasked(); |
| 197 | switch (action) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 198 | case MotionEvent.ACTION_DOWN: { |
| 199 | int x = (int) event.getX(); |
| 200 | int y = (int) event.getY(); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 201 | |
Winson Chung | 0be8f08 | 2018-02-15 15:52:49 -0800 | [diff] [blame] | 202 | // End any existing quickscrub animations before starting the new transition |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 203 | if (mTrackAnimator != null) { |
| 204 | mTrackAnimator.end(); |
| 205 | mTrackAnimator = null; |
Winson Chung | 0be8f08 | 2018-02-15 15:52:49 -0800 | [diff] [blame] | 206 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 207 | |
| 208 | mCurrentNavigationBarView = mNavigationBarView.getCurrentView(); |
| 209 | mHitTarget = mNavigationBarView.getButtonAtPosition(x, y); |
| 210 | if (mHitTarget != null) { |
| 211 | // Pre-emptively delay the touch feedback for the button that we just touched |
| 212 | mHitTarget.setDelayTouchFeedback(true); |
| 213 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 214 | mTouchDownX = x; |
| 215 | mTouchDownY = y; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 216 | mTransformGlobalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 217 | mTransformLocalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 218 | mNavigationBarView.transformMatrixToGlobal(mTransformGlobalMatrix); |
| 219 | mNavigationBarView.transformMatrixToLocal(mTransformLocalMatrix); |
| 220 | mQuickStepStarted = false; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 221 | mAllowGestureDetection = true; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | case MotionEvent.ACTION_MOVE: { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 225 | if (mQuickStepStarted || !mAllowGestureDetection){ |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | int x = (int) event.getX(); |
| 229 | int y = (int) event.getY(); |
| 230 | int xDiff = Math.abs(x - mTouchDownX); |
| 231 | int yDiff = Math.abs(y - mTouchDownY); |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 232 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 233 | boolean exceededScrubTouchSlop, exceededSwipeUpTouchSlop; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 234 | int pos, touchDown, offset, trackSize; |
Matthew Ng | e0903c9 | 2018-01-17 15:32:41 -0800 | [diff] [blame] | 235 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 236 | if (mIsVertical) { |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 237 | exceededScrubTouchSlop = |
| 238 | yDiff > NavigationBarCompat.getQuickScrubTouchSlopPx() && yDiff > xDiff; |
| 239 | exceededSwipeUpTouchSlop = |
| 240 | xDiff > NavigationBarCompat.getQuickStepTouchSlopPx() && xDiff > yDiff; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 241 | pos = y; |
| 242 | touchDown = mTouchDownY; |
| 243 | offset = pos - mTrackRect.top; |
| 244 | trackSize = mTrackRect.height(); |
| 245 | } else { |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 246 | exceededScrubTouchSlop = |
| 247 | xDiff > NavigationBarCompat.getQuickScrubTouchSlopPx() && xDiff > yDiff; |
| 248 | exceededSwipeUpTouchSlop = |
| 249 | yDiff > NavigationBarCompat.getQuickStepTouchSlopPx() && yDiff > xDiff; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 250 | pos = x; |
| 251 | touchDown = mTouchDownX; |
| 252 | offset = pos - mTrackRect.left; |
| 253 | trackSize = mTrackRect.width(); |
| 254 | } |
| 255 | // Decide to start quickstep if dragging away from the navigation bar, otherwise in |
| 256 | // the parallel direction, decide to start quickscrub. Only one may run. |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 257 | if (!mQuickScrubActive && exceededSwipeUpTouchSlop) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 258 | if (mNavigationBarView.isQuickStepSwipeUpEnabled()) { |
| 259 | startQuickStep(event); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 260 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 261 | break; |
| 262 | } |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 263 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 264 | // Do not handle quick scrub if disabled |
| 265 | if (!mNavigationBarView.isQuickScrubEnabled()) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | |
| 269 | if (!mDragPositive) { |
| 270 | offset -= mIsVertical ? mTrackRect.height() : mTrackRect.width(); |
| 271 | } |
| 272 | |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 273 | final boolean allowDrag = !mDragPositive |
| 274 | ? offset < 0 && pos < touchDown : offset >= 0 && pos > touchDown; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 275 | float scrubFraction = Utilities.clamp(Math.abs(offset) * 1f / trackSize, 0, 1); |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 276 | if (allowDrag) { |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 277 | // Passing the drag slop then touch slop will start quick step |
| 278 | if (!mQuickScrubActive && exceededScrubTouchSlop) { |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 279 | startQuickScrub(); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 280 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 281 | } |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 282 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 283 | if (mQuickScrubActive && (mDragPositive && offset >= 0 |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 284 | || !mDragPositive && offset <= 0)) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 285 | try { |
| 286 | mOverviewEventSender.getProxy().onQuickScrubProgress(scrubFraction); |
| 287 | if (DEBUG_OVERVIEW_PROXY) { |
| 288 | Log.d(TAG_OPS, "Quick Scrub Progress:" + scrubFraction); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 289 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 290 | } catch (RemoteException e) { |
| 291 | Log.e(TAG, "Failed to send progress of quick scrub.", e); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 292 | } |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 293 | mHighlightCenter = x; |
| 294 | mNavigationBarView.invalidate(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case MotionEvent.ACTION_CANCEL: |
| 299 | case MotionEvent.ACTION_UP: |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 300 | endQuickScrub(true /* animate */); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 301 | break; |
| 302 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 303 | |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 304 | // Proxy motion events to launcher if not handled by quick scrub |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 305 | // Proxy motion events up/cancel that would be sent after long press on any nav button |
| 306 | if (!mQuickScrubActive && (mAllowGestureDetection || action == MotionEvent.ACTION_CANCEL |
| 307 | || action == MotionEvent.ACTION_UP)) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 308 | proxyMotionEvents(event); |
| 309 | } |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 310 | return mQuickScrubActive || mQuickStepStarted || deadZoneConsumed; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void onDraw(Canvas canvas) { |
Matthew Ng | f781bbd | 2018-03-21 14:58:55 -0700 | [diff] [blame] | 315 | if (!mNavigationBarView.isQuickScrubEnabled()) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 318 | mTrackPaint.setAlpha(Math.round(255f * mTrackAlpha)); |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 319 | |
| 320 | // Scale the track, but apply the inverse scale from the nav bar |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 321 | final float radius = mTrackRect.height() / 2; |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 322 | canvas.save(); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 323 | float translate = Utilities.clamp(mHighlightCenter, mTrackRect.left, mTrackRect.right); |
| 324 | canvas.translate(translate, 0); |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 325 | canvas.scale(mTrackScale / mNavigationBarView.getScaleX(), |
| 326 | 1f / mNavigationBarView.getScaleY(), |
| 327 | mTrackRect.centerX(), mTrackRect.centerY()); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 328 | canvas.drawRoundRect(mTrackRect.left - translate, mTrackRect.top, |
| 329 | mTrackRect.right - translate, mTrackRect.bottom, radius, radius, mTrackPaint); |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 330 | canvas.restore(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 335 | final int paddingLeft = mNavigationBarView.getPaddingLeft(); |
| 336 | final int paddingTop = mNavigationBarView.getPaddingTop(); |
| 337 | final int paddingRight = mNavigationBarView.getPaddingRight(); |
| 338 | final int paddingBottom = mNavigationBarView.getPaddingBottom(); |
| 339 | final int width = (right - left) - paddingRight - paddingLeft; |
| 340 | final int height = (bottom - top) - paddingBottom - paddingTop; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 341 | final int x1, x2, y1, y2; |
| 342 | if (mIsVertical) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 343 | x1 = (width - mTrackThickness) / 2 + paddingLeft; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 344 | x2 = x1 + mTrackThickness; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 345 | y1 = paddingTop + mTrackEndPadding; |
| 346 | y2 = y1 + height - 2 * mTrackEndPadding; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 347 | } else { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 348 | y1 = (height - mTrackThickness) / 2 + paddingTop; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 349 | y2 = y1 + mTrackThickness; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 350 | x1 = mNavigationBarView.getPaddingStart() + mTrackEndPadding; |
| 351 | x2 = x1 + width - 2 * mTrackEndPadding; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 352 | } |
| 353 | mTrackRect.set(x1, y1, x2, y2); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 354 | updateHighlight(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public void onDarkIntensityChange(float intensity) { |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 359 | final float oldIntensity = mDarkIntensity; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 360 | mDarkIntensity = intensity; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 361 | |
| 362 | // When in quick scrub, invalidate gradient if changing intensity from black to white and |
| 363 | // vice-versa |
| 364 | if (mNavigationBarView.isQuickScrubEnabled() |
| 365 | && Math.round(intensity) != Math.round(oldIntensity)) { |
| 366 | updateHighlight(); |
| 367 | } |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 368 | mNavigationBarView.invalidate(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 372 | public void setBarState(boolean isVertical, boolean isRTL) { |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 373 | final boolean changed = (mIsVertical != isVertical) || (mIsRTL != isRTL); |
| 374 | if (changed) { |
| 375 | // End quickscrub if the state changes mid-transition |
| 376 | endQuickScrub(false /* animate */); |
| 377 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 378 | mIsVertical = isVertical; |
| 379 | mIsRTL = isRTL; |
| 380 | try { |
| 381 | int navbarPos = WindowManagerGlobal.getWindowManagerService().getNavBarPosition(); |
| 382 | mDragPositive = navbarPos == NAV_BAR_LEFT || navbarPos == NAV_BAR_BOTTOM; |
| 383 | if (isRTL) { |
| 384 | mDragPositive = !mDragPositive; |
| 385 | } |
| 386 | } catch (RemoteException e) { |
| 387 | Slog.e(TAG, "Failed to get nav bar position.", e); |
| 388 | } |
| 389 | } |
| 390 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 391 | @Override |
| 392 | public void onNavigationButtonLongPress(View v) { |
| 393 | mAllowGestureDetection = false; |
| 394 | mHandler.removeCallbacksAndMessages(null); |
| 395 | } |
| 396 | |
| 397 | private void startQuickStep(MotionEvent event) { |
| 398 | mQuickStepStarted = true; |
| 399 | event.transform(mTransformGlobalMatrix); |
| 400 | try { |
| 401 | mOverviewEventSender.getProxy().onQuickStep(event); |
| 402 | if (DEBUG_OVERVIEW_PROXY) { |
| 403 | Log.d(TAG_OPS, "Quick Step Start"); |
| 404 | } |
| 405 | } catch (RemoteException e) { |
| 406 | Log.e(TAG, "Failed to send quick step started.", e); |
| 407 | } finally { |
| 408 | event.transform(mTransformLocalMatrix); |
| 409 | } |
| 410 | mOverviewEventSender.notifyQuickStepStarted(); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 411 | mHandler.removeCallbacksAndMessages(null); |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 412 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 413 | if (mHitTarget != null) { |
| 414 | mHitTarget.abortCurrentGesture(); |
| 415 | } |
| 416 | |
| 417 | if (mQuickScrubActive) { |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 418 | animateEnd(); |
| 419 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 422 | private void startQuickScrub() { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 423 | if (!mQuickScrubActive) { |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 424 | updateHighlight(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 425 | mQuickScrubActive = true; |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 426 | ObjectAnimator trackAnimator = ObjectAnimator.ofPropertyValuesHolder(this, |
| 427 | PropertyValuesHolder.ofFloat(mTrackAlphaProperty, 1f), |
| 428 | PropertyValuesHolder.ofFloat(mTrackScaleProperty, 1f)); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 429 | trackAnimator.setInterpolator(ALPHA_IN); |
| 430 | trackAnimator.setDuration(ANIM_IN_DURATION_MS); |
| 431 | ObjectAnimator navBarAnimator = ObjectAnimator.ofFloat(this, mNavBarAlphaProperty, 0f); |
| 432 | navBarAnimator.setInterpolator(ALPHA_OUT); |
| 433 | navBarAnimator.setDuration(ANIM_OUT_DURATION_MS); |
| 434 | mTrackAnimator = new AnimatorSet(); |
| 435 | mTrackAnimator.playTogether(trackAnimator, navBarAnimator); |
| 436 | mTrackAnimator.start(); |
Matthew Ng | 6607c3d | 2018-04-26 15:23:27 -0700 | [diff] [blame] | 437 | |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 438 | // Disable slippery for quick scrub to not cancel outside the nav bar |
| 439 | mNavigationBarView.updateSlippery(); |
| 440 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 441 | try { |
| 442 | mOverviewEventSender.getProxy().onQuickScrubStart(); |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 443 | if (DEBUG_OVERVIEW_PROXY) { |
| 444 | Log.d(TAG_OPS, "Quick Scrub Start"); |
| 445 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 446 | } catch (RemoteException e) { |
| 447 | Log.e(TAG, "Failed to send start of quick scrub.", e); |
| 448 | } |
Tracy Zhou | 2759905 | 2018-04-16 15:47:29 -0700 | [diff] [blame] | 449 | mOverviewEventSender.notifyQuickScrubStarted(); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 450 | |
| 451 | if (mHitTarget != null) { |
| 452 | mHitTarget.abortCurrentGesture(); |
| 453 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Winson Chung | d10ca30 | 2018-02-14 10:13:41 -0800 | [diff] [blame] | 457 | private void endQuickScrub(boolean animate) { |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 458 | if (mQuickScrubActive) { |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 459 | animateEnd(); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 460 | try { |
| 461 | mOverviewEventSender.getProxy().onQuickScrubEnd(); |
| 462 | if (DEBUG_OVERVIEW_PROXY) { |
| 463 | Log.d(TAG_OPS, "Quick Scrub End"); |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 464 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 465 | } catch (RemoteException e) { |
| 466 | Log.e(TAG, "Failed to send end of quick scrub.", e); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 467 | } |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 468 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 469 | if (!animate) { |
| 470 | if (mTrackAnimator != null) { |
| 471 | mTrackAnimator.end(); |
| 472 | mTrackAnimator = null; |
| 473 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 474 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 477 | private void animateEnd() { |
| 478 | if (mTrackAnimator != null) { |
| 479 | mTrackAnimator.cancel(); |
| 480 | } |
| 481 | |
Winson Chung | 4baf124 | 2018-05-24 14:21:57 -0700 | [diff] [blame] | 482 | ObjectAnimator trackAnimator = ObjectAnimator.ofPropertyValuesHolder(this, |
| 483 | PropertyValuesHolder.ofFloat(mTrackAlphaProperty, 0f), |
| 484 | PropertyValuesHolder.ofFloat(mTrackScaleProperty, TRACK_SCALE)); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 485 | trackAnimator.setInterpolator(ALPHA_OUT); |
| 486 | trackAnimator.setDuration(ANIM_OUT_DURATION_MS); |
| 487 | ObjectAnimator navBarAnimator = ObjectAnimator.ofFloat(this, mNavBarAlphaProperty, 1f); |
| 488 | navBarAnimator.setInterpolator(ALPHA_IN); |
| 489 | navBarAnimator.setDuration(ANIM_IN_DURATION_MS); |
| 490 | mTrackAnimator = new AnimatorSet(); |
| 491 | mTrackAnimator.playTogether(trackAnimator, navBarAnimator); |
| 492 | mTrackAnimator.addListener(mQuickScrubEndListener); |
| 493 | mTrackAnimator.start(); |
| 494 | } |
| 495 | |
| 496 | private void resetQuickScrub() { |
| 497 | mQuickScrubActive = false; |
| 498 | mAllowGestureDetection = false; |
| 499 | mCurrentNavigationBarView = null; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 500 | updateHighlight(); |
| 501 | } |
| 502 | |
| 503 | private void updateHighlight() { |
Matthew Ng | 908111d | 2018-06-26 15:22:56 -0700 | [diff] [blame] | 504 | if (mTrackRect.isEmpty()) { |
| 505 | return; |
| 506 | } |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 507 | int colorBase, colorGrad; |
| 508 | if (mDarkIntensity > 0.5f) { |
| 509 | colorBase = mContext.getColor(R.color.quick_step_track_background_background_dark); |
| 510 | colorGrad = mContext.getColor(R.color.quick_step_track_background_foreground_dark); |
| 511 | } else { |
| 512 | colorBase = mContext.getColor(R.color.quick_step_track_background_background_light); |
| 513 | colorGrad = mContext.getColor(R.color.quick_step_track_background_foreground_light); |
| 514 | } |
| 515 | mHighlight = new RadialGradient(0, mTrackRect.height() / 2, |
| 516 | mTrackRect.width() * GRADIENT_WIDTH, colorGrad, colorBase, |
| 517 | Shader.TileMode.CLAMP); |
| 518 | mTrackPaint.setShader(mHighlight); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 521 | private boolean proxyMotionEvents(MotionEvent event) { |
| 522 | final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy(); |
| 523 | event.transform(mTransformGlobalMatrix); |
| 524 | try { |
| 525 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 526 | overviewProxy.onPreMotionEvent(mNavigationBarView.getDownHitTarget()); |
| 527 | } |
| 528 | overviewProxy.onMotionEvent(event); |
| 529 | if (DEBUG_OVERVIEW_PROXY) { |
| 530 | Log.d(TAG_OPS, "Send MotionEvent: " + event.toString()); |
| 531 | } |
| 532 | return true; |
| 533 | } catch (RemoteException e) { |
| 534 | Log.e(TAG, "Callback failed", e); |
| 535 | } finally { |
| 536 | event.transform(mTransformLocalMatrix); |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 537 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 538 | return false; |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 539 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 540 | } |