blob: ae276a86124391f2159f5dd51d11ac7b4adf52aa [file] [log] [blame]
Matthew Nga8f24262017-12-19 11:54:24 -08001/*
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
17package com.android.systemui.statusbar.phone;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
Matthew Ng7090a802018-01-19 13:36:22 -080022import android.animation.ArgbEvaluator;
Matthew Nga8f24262017-12-19 11:54:24 -080023import android.animation.ObjectAnimator;
24import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
26import android.content.Context;
27import android.graphics.Canvas;
28import android.graphics.Paint;
29import android.graphics.Rect;
30import android.os.Handler;
31import android.os.RemoteException;
Matthew Ngf41f7c32018-01-22 17:57:05 -080032import android.os.SystemProperties;
Matthew Nga8f24262017-12-19 11:54:24 -080033import android.util.Log;
34import android.util.Slog;
35import android.view.Display;
36import android.view.GestureDetector;
37import android.view.MotionEvent;
38import android.view.View;
39import android.view.ViewConfiguration;
40import android.view.WindowManager;
41import android.view.WindowManagerGlobal;
42import android.view.animation.DecelerateInterpolator;
43import android.view.animation.Interpolator;
44import android.support.annotation.DimenRes;
45import com.android.systemui.Dependency;
46import com.android.systemui.OverviewProxyService;
47import com.android.systemui.R;
48import com.android.systemui.plugins.statusbar.phone.NavGesture.GestureHelper;
49import com.android.systemui.shared.recents.IOverviewProxy;
50import com.android.systemui.shared.recents.utilities.Utilities;
51
52import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT;
53import static android.view.WindowManagerPolicyConstants.NAV_BAR_BOTTOM;
Matthew Ngbd824572018-01-17 16:25:56 -080054import static com.android.systemui.OverviewProxyService.DEBUG_OVERVIEW_PROXY;
55import static com.android.systemui.OverviewProxyService.TAG_OPS;
Matthew Nga8f24262017-12-19 11:54:24 -080056
57/**
58 * Class to detect gestures on the navigation bar and implement quick scrub and switch.
59 */
60public class QuickScrubController extends GestureDetector.SimpleOnGestureListener implements
61 GestureHelper {
62
63 private static final String TAG = "QuickScrubController";
64 private static final int QUICK_SWITCH_FLING_VELOCITY = 0;
65 private static final int ANIM_DURATION_MS = 200;
Winson Chungde2a1242018-02-07 15:59:43 -080066 private static final long LONG_PRESS_DELAY_MS = 225;
Matthew Nga8f24262017-12-19 11:54:24 -080067
68 /**
69 * For quick step, set a damping value to allow the button to stick closer its origin position
70 * when dragging before quick scrub is active.
71 */
72 private static final int SWITCH_STICKINESS = 4;
73
74 private NavigationBarView mNavigationBarView;
75 private GestureDetector mGestureDetector;
76
77 private boolean mDraggingActive;
78 private boolean mQuickScrubActive;
79 private float mDownOffset;
80 private float mTranslation;
81 private int mTouchDownX;
82 private int mTouchDownY;
83 private boolean mDragPositive;
84 private boolean mIsVertical;
85 private boolean mIsRTL;
Matthew Ng7090a802018-01-19 13:36:22 -080086 private float mTrackAlpha;
87 private int mLightTrackColor;
88 private int mDarkTrackColor;
89 private float mDarkIntensity;
Matthew Nga8f24262017-12-19 11:54:24 -080090
91 private final Handler mHandler = new Handler();
92 private final Interpolator mQuickScrubEndInterpolator = new DecelerateInterpolator();
93 private final Rect mTrackRect = new Rect();
94 private final Rect mHomeButtonRect = new Rect();
95 private final Paint mTrackPaint = new Paint();
96 private final int mScrollTouchSlop;
97 private final OverviewProxyService mOverviewEventSender;
Matthew Nga8f24262017-12-19 11:54:24 -080098 private final int mTrackThickness;
99 private final int mTrackPadding;
100 private final ValueAnimator mTrackAnimator;
101 private final ValueAnimator mButtonAnimator;
102 private final AnimatorSet mQuickScrubEndAnimator;
103 private final Context mContext;
Matthew Ng7090a802018-01-19 13:36:22 -0800104 private final ArgbEvaluator mTrackColorEvaluator = new ArgbEvaluator();
Matthew Nga8f24262017-12-19 11:54:24 -0800105
106 private final AnimatorUpdateListener mTrackAnimatorListener = valueAnimator -> {
Matthew Ng7090a802018-01-19 13:36:22 -0800107 mTrackAlpha = (float) valueAnimator.getAnimatedValue();
Matthew Nga8f24262017-12-19 11:54:24 -0800108 mNavigationBarView.invalidate();
109 };
110
111 private final AnimatorUpdateListener mButtonTranslationListener = animator -> {
112 int pos = (int) animator.getAnimatedValue();
113 if (!mQuickScrubActive) {
114 pos = mDragPositive ? Math.min((int) mTranslation, pos) : Math.max((int) mTranslation, pos);
115 }
116 final View homeView = mNavigationBarView.getHomeButton().getCurrentView();
117 if (mIsVertical) {
118 homeView.setTranslationY(pos);
119 } else {
120 homeView.setTranslationX(pos);
121 }
122 };
123
124 private AnimatorListenerAdapter mQuickScrubEndListener = new AnimatorListenerAdapter() {
125 @Override
126 public void onAnimationEnd(Animator animation) {
127 mNavigationBarView.getHomeButton().setClickable(true);
128 mQuickScrubActive = false;
129 mTranslation = 0;
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) {
Winson Chungde2a1242018-02-07 15:59:43 -0800139 if (!isQuickScrubEnabled() || mQuickScrubActive ||
140 !mHomeButtonRect.contains(mTouchDownX, mTouchDownY)) {
Matthew Nga8f24262017-12-19 11:54:24 -0800141 return false;
142 }
143 float velocityX = mIsRTL ? -velX : velX;
144 float absVelY = Math.abs(velY);
145 final boolean isValidFling = velocityX > QUICK_SWITCH_FLING_VELOCITY &&
146 mIsVertical ? (absVelY > velocityX) : (velocityX > absVelY);
147 if (isValidFling) {
148 mDraggingActive = false;
149 mButtonAnimator.setIntValues((int) mTranslation, 0);
150 mButtonAnimator.start();
151 mHandler.removeCallbacks(mLongPressRunnable);
152 try {
153 final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy();
154 overviewProxy.onQuickSwitch();
Matthew Ngbd824572018-01-17 16:25:56 -0800155 if (DEBUG_OVERVIEW_PROXY) {
156 Log.d(TAG_OPS, "Quick Switch");
157 }
Matthew Nga8f24262017-12-19 11:54:24 -0800158 } catch (RemoteException e) {
159 Log.e(TAG, "Failed to send start of quick switch.", e);
160 }
Winson Chung4faf38a2018-02-06 08:53:37 -0800161 return true;
Matthew Nga8f24262017-12-19 11:54:24 -0800162 }
Winson Chung4faf38a2018-02-06 08:53:37 -0800163 return false;
Matthew Nga8f24262017-12-19 11:54:24 -0800164 }
165 };
166
167 public QuickScrubController(Context context) {
168 mContext = context;
169 mScrollTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Matthew Nga8f24262017-12-19 11:54:24 -0800170 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 Ng7090a802018-01-19 13:36:22 -0800174 mTrackPaint.setAlpha(0);
Matthew Nga8f24262017-12-19 11:54:24 -0800175
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 Chung4faf38a2018-02-06 08:53:37 -0800191 /**
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 Nga8f24262017-12-19 11:54:24 -0800195 @Override
196 public boolean onInterceptTouchEvent(MotionEvent event) {
197 final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy();
198 final ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
199 if (overviewProxy == null) {
200 homeButton.setDelayTouchFeedback(false);
201 return false;
202 }
Winson Chungde2a1242018-02-07 15:59:43 -0800203
204 return handleTouchEvent(event);
205 }
206
207 /**
208 * @return true if we want to handle touch events for quick scrub/switch and prevent proxying
209 * the event to the overview service.
210 */
211 @Override
212 public boolean onTouchEvent(MotionEvent event) {
213 return handleTouchEvent(event);
214 }
215
216 private boolean handleTouchEvent(MotionEvent event) {
217 final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy();
218 final ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
Winson Chung4faf38a2018-02-06 08:53:37 -0800219 if (mGestureDetector.onTouchEvent(event)) {
Winson Chungde2a1242018-02-07 15:59:43 -0800220 // If the fling has been handled on UP, then skip proxying the UP
Winson Chung4faf38a2018-02-06 08:53:37 -0800221 return true;
222 }
Matthew Nga8f24262017-12-19 11:54:24 -0800223 int action = event.getAction();
224 switch (action & MotionEvent.ACTION_MASK) {
225 case MotionEvent.ACTION_DOWN: {
226 int x = (int) event.getX();
227 int y = (int) event.getY();
Matthew Ngf41f7c32018-01-22 17:57:05 -0800228 if (isQuickScrubEnabled() && mHomeButtonRect.contains(x, y)) {
Matthew Nga8f24262017-12-19 11:54:24 -0800229 mTouchDownX = x;
230 mTouchDownY = y;
231 homeButton.setDelayTouchFeedback(true);
232 mHandler.postDelayed(mLongPressRunnable, LONG_PRESS_DELAY_MS);
233 } else {
Matthew Ngf41f7c32018-01-22 17:57:05 -0800234 homeButton.setDelayTouchFeedback(false);
Matthew Nga8f24262017-12-19 11:54:24 -0800235 mTouchDownX = mTouchDownY = -1;
236 }
237 break;
238 }
239 case MotionEvent.ACTION_MOVE: {
240 if (mTouchDownX != -1) {
241 int x = (int) event.getX();
242 int y = (int) event.getY();
243 int xDiff = Math.abs(x - mTouchDownX);
244 int yDiff = Math.abs(y - mTouchDownY);
Matthew Nge0903c92018-01-17 15:32:41 -0800245 boolean exceededTouchSlopX = xDiff > mScrollTouchSlop && xDiff > yDiff;
246 boolean exceededTouchSlopY = yDiff > mScrollTouchSlop && yDiff > xDiff;
247 boolean exceededTouchSlop, exceededPerpendicularTouchSlop;
Matthew Nga8f24262017-12-19 11:54:24 -0800248 int pos, touchDown, offset, trackSize;
Matthew Nge0903c92018-01-17 15:32:41 -0800249
Matthew Nga8f24262017-12-19 11:54:24 -0800250 if (mIsVertical) {
Matthew Nge0903c92018-01-17 15:32:41 -0800251 exceededTouchSlop = exceededTouchSlopY;
252 exceededPerpendicularTouchSlop = exceededTouchSlopX;
Matthew Nga8f24262017-12-19 11:54:24 -0800253 pos = y;
254 touchDown = mTouchDownY;
255 offset = pos - mTrackRect.top;
256 trackSize = mTrackRect.height();
257 } else {
Matthew Nge0903c92018-01-17 15:32:41 -0800258 exceededTouchSlop = exceededTouchSlopX;
259 exceededPerpendicularTouchSlop = exceededTouchSlopY;
Matthew Nga8f24262017-12-19 11:54:24 -0800260 pos = x;
261 touchDown = mTouchDownX;
262 offset = pos - mTrackRect.left;
263 trackSize = mTrackRect.width();
264 }
Winson Chung4faf38a2018-02-06 08:53:37 -0800265 // Do not start scrubbing when dragging in the perpendicular direction if we
266 // haven't already started quickscrub
267 if (!mDraggingActive && !mQuickScrubActive && exceededPerpendicularTouchSlop) {
Matthew Nge0903c92018-01-17 15:32:41 -0800268 mHandler.removeCallbacksAndMessages(null);
269 return false;
270 }
Matthew Nga8f24262017-12-19 11:54:24 -0800271 if (!mDragPositive) {
272 offset -= mIsVertical ? mTrackRect.height() : mTrackRect.width();
273 }
274
275 // Control the button movement
276 if (!mDraggingActive && exceededTouchSlop) {
277 boolean allowDrag = !mDragPositive
278 ? offset < 0 && pos < touchDown : offset >= 0 && pos > touchDown;
279 if (allowDrag) {
280 mDownOffset = offset;
281 homeButton.setClickable(false);
282 mDraggingActive = true;
283 }
284 }
285 if (mDraggingActive && (mDragPositive && offset >= 0
286 || !mDragPositive && offset <= 0)) {
287 float scrubFraction =
288 Utilities.clamp(Math.abs(offset) * 1f / trackSize, 0, 1);
289 mTranslation = !mDragPositive
290 ? Utilities.clamp(offset - mDownOffset, -trackSize, 0)
291 : Utilities.clamp(offset - mDownOffset, 0, trackSize);
292 if (mQuickScrubActive) {
293 try {
294 overviewProxy.onQuickScrubProgress(scrubFraction);
Matthew Ngbd824572018-01-17 16:25:56 -0800295 if (DEBUG_OVERVIEW_PROXY) {
296 Log.d(TAG_OPS, "Quick Scrub Progress:" + scrubFraction);
297 }
Matthew Nga8f24262017-12-19 11:54:24 -0800298 } catch (RemoteException e) {
299 Log.e(TAG, "Failed to send progress of quick scrub.", e);
300 }
301 } else {
302 mTranslation /= SWITCH_STICKINESS;
303 }
304 if (mIsVertical) {
305 homeButton.getCurrentView().setTranslationY(mTranslation);
306 } else {
307 homeButton.getCurrentView().setTranslationX(mTranslation);
308 }
309 }
310 }
311 break;
312 }
313 case MotionEvent.ACTION_CANCEL:
314 case MotionEvent.ACTION_UP:
315 endQuickScrub();
316 break;
317 }
318 return mDraggingActive || mQuickScrubActive;
319 }
320
321 @Override
322 public void onDraw(Canvas canvas) {
Matthew Ng7090a802018-01-19 13:36:22 -0800323 int color = (int) mTrackColorEvaluator.evaluate(mDarkIntensity, mLightTrackColor,
324 mDarkTrackColor);
325 mTrackPaint.setColor(color);
326 mTrackPaint.setAlpha((int) (mTrackPaint.getAlpha() * mTrackAlpha));
Matthew Nga8f24262017-12-19 11:54:24 -0800327 canvas.drawRect(mTrackRect, mTrackPaint);
328 }
329
330 @Override
331 public void onLayout(boolean changed, int left, int top, int right, int bottom) {
332 final int width = right - left;
333 final int height = bottom - top;
334 final int x1, x2, y1, y2;
335 if (mIsVertical) {
336 x1 = (width - mTrackThickness) / 2;
337 x2 = x1 + mTrackThickness;
338 y1 = mDragPositive ? height / 2 : mTrackPadding;
339 y2 = y1 + height / 2 - mTrackPadding;
340 } else {
341 y1 = (height - mTrackThickness) / 2;
342 y2 = y1 + mTrackThickness;
343 x1 = mDragPositive ? width / 2 : mTrackPadding;
344 x2 = x1 + width / 2 - mTrackPadding;
345 }
346 mTrackRect.set(x1, y1, x2, y2);
347
348 // Get the touch rect of the home button location
349 View homeView = mNavigationBarView.getHomeButton().getCurrentView();
Matthew Nga5612a02018-01-17 12:28:41 -0800350 if (homeView != null) {
351 int[] globalHomePos = homeView.getLocationOnScreen();
352 int[] globalNavBarPos = mNavigationBarView.getLocationOnScreen();
353 int homeX = globalHomePos[0] - globalNavBarPos[0];
354 int homeY = globalHomePos[1] - globalNavBarPos[1];
355 mHomeButtonRect.set(homeX, homeY, homeX + homeView.getMeasuredWidth(),
356 homeY + homeView.getMeasuredHeight());
357 }
Matthew Nga8f24262017-12-19 11:54:24 -0800358 }
359
360 @Override
361 public void onDarkIntensityChange(float intensity) {
Matthew Ng7090a802018-01-19 13:36:22 -0800362 mDarkIntensity = intensity;
363 mNavigationBarView.invalidate();
Matthew Nga8f24262017-12-19 11:54:24 -0800364 }
365
366 @Override
Matthew Nga8f24262017-12-19 11:54:24 -0800367 public void setBarState(boolean isVertical, boolean isRTL) {
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
Matthew Ngf41f7c32018-01-22 17:57:05 -0800381 boolean isQuickScrubEnabled() {
382 return SystemProperties.getBoolean("persist.quickstep.scrub.enabled", false);
383 }
384
Matthew Nga8f24262017-12-19 11:54:24 -0800385 private void startQuickScrub() {
386 if (!mQuickScrubActive) {
387 mQuickScrubActive = true;
Matthew Ng7090a802018-01-19 13:36:22 -0800388 mLightTrackColor = mContext.getColor(R.color.quick_step_track_background_light);
389 mDarkTrackColor = mContext.getColor(R.color.quick_step_track_background_dark);
390 mTrackAnimator.setFloatValues(0, 1);
Matthew Nga8f24262017-12-19 11:54:24 -0800391 mTrackAnimator.start();
392 try {
393 mOverviewEventSender.getProxy().onQuickScrubStart();
Matthew Ngbd824572018-01-17 16:25:56 -0800394 if (DEBUG_OVERVIEW_PROXY) {
395 Log.d(TAG_OPS, "Quick Scrub Start");
396 }
Matthew Nga8f24262017-12-19 11:54:24 -0800397 } catch (RemoteException e) {
398 Log.e(TAG, "Failed to send start of quick scrub.", e);
399 }
400 }
401 }
402
403 private void endQuickScrub() {
404 mHandler.removeCallbacks(mLongPressRunnable);
405 if (mDraggingActive || mQuickScrubActive) {
406 mButtonAnimator.setIntValues((int) mTranslation, 0);
Matthew Ng7090a802018-01-19 13:36:22 -0800407 mTrackAnimator.setFloatValues(mTrackAlpha, 0);
Matthew Nga8f24262017-12-19 11:54:24 -0800408 mQuickScrubEndAnimator.start();
409 try {
410 mOverviewEventSender.getProxy().onQuickScrubEnd();
Matthew Ngbd824572018-01-17 16:25:56 -0800411 if (DEBUG_OVERVIEW_PROXY) {
412 Log.d(TAG_OPS, "Quick Scrub End");
413 }
Matthew Nga8f24262017-12-19 11:54:24 -0800414 } catch (RemoteException e) {
415 Log.e(TAG, "Failed to send end of quick scrub.", e);
416 }
417 }
418 mDraggingActive = false;
419 }
420
421 private int getDimensionPixelSize(Context context, @DimenRes int resId) {
422 return context.getResources().getDimensionPixelSize(resId);
423 }
424}