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