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; |
Tiger Huang | 44c0455 | 2018-11-29 20:06:45 +0800 | [diff] [blame] | 20 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_INVALID; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 21 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_LEFT; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 22 | import static android.view.WindowManagerPolicyConstants.NAV_BAR_RIGHT; |
| 23 | |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 24 | import static com.android.systemui.recents.OverviewProxyService.DEBUG_OVERVIEW_PROXY; |
| 25 | import static com.android.systemui.recents.OverviewProxyService.TAG_OPS; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 26 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_BACK; |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 27 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_DEAD_ZONE; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 28 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_HOME; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 29 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_NONE; |
| 30 | import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_OVERVIEW; |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 31 | import static com.android.systemui.statusbar.phone.NavigationBarView.WINDOW_TARGET_BOTTOM; |
| 32 | import static com.android.systemui.statusbar.phone.NavigationPrototypeController.EDGE_SENSITIVITY_WIDTH_SETTING; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 33 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 34 | import android.annotation.Nullable; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 36 | import android.content.res.Resources; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 37 | import android.graphics.Canvas; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 38 | import android.graphics.Matrix; |
Matthew Ng | 1cf6eac | 2018-11-27 15:06:55 -0800 | [diff] [blame] | 39 | import android.graphics.Rect; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 40 | import android.hardware.input.InputManager; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 42 | import android.os.SystemClock; |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 43 | import android.provider.Settings; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 44 | import android.util.Log; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 45 | import android.view.InputDevice; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 46 | import android.view.MotionEvent; |
| 47 | import android.view.View; |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 48 | import android.view.ViewConfiguration; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 49 | import android.view.ViewPropertyAnimator; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 50 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 51 | import com.android.systemui.Dependency; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 52 | import com.android.systemui.Interpolators; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 53 | import com.android.systemui.R; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 54 | import com.android.systemui.SysUiServiceProvider; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 55 | import com.android.systemui.plugins.statusbar.phone.NavGesture.GestureHelper; |
| 56 | import com.android.systemui.recents.OverviewProxyService; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 57 | import com.android.systemui.shared.recents.IOverviewProxy; |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 58 | import com.android.systemui.shared.recents.utilities.Utilities; |
Sunny Goyal | aac6c88 | 2019-02-11 11:57:58 -0800 | [diff] [blame] | 59 | import com.android.systemui.shared.system.InputChannelCompat.InputEventDispatcher; |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 60 | import com.android.systemui.shared.system.NavigationBarCompat; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 61 | |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 62 | import java.io.PrintWriter; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 63 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 64 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 65 | * Class to detect gestures on the navigation bar and implement quick scrub. |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 66 | * Note that the variables in this class horizontal and vertical represents horizontal always |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 67 | * aligned with along the navigation bar. |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 68 | */ |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 69 | public class QuickStepController implements GestureHelper { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 70 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 71 | private static final String TAG = "QuickStepController"; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 72 | |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 73 | /** Experiment to swipe home button left to execute a back key press */ |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 74 | private static final String HIDE_BACK_BUTTON_PROP = "quickstepcontroller_hideback"; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 75 | private static final String ENABLE_CLICK_THROUGH_NAV_PROP = "quickstepcontroller_clickthrough"; |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 76 | private static final String GESTURE_REGION_THRESHOLD_SETTING = "gesture_region_threshold"; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 77 | private static final long BACK_BUTTON_FADE_IN_ALPHA = 150; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 78 | private static final long CLICK_THROUGH_TAP_DELAY = 70; |
| 79 | private static final long CLICK_THROUGH_TAP_RESET_DELAY = 100; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 80 | |
| 81 | /** When the home-swipe-back gesture is disallowed, make it harder to pull */ |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 82 | private static final float HORIZONTAL_GESTURE_DAMPING = 0.3f; |
| 83 | private static final float VERTICAL_GESTURE_DAMPING = 0.15f; |
| 84 | private static final float HORIZONTAL_DISABLED_GESTURE_DAMPING = 0.16f; |
| 85 | private static final float VERTICAL_DISABLED_GESTURE_DAMPING = 0.06f; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 86 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 87 | private static final int ACTION_SWIPE_UP_INDEX = 0; |
| 88 | private static final int ACTION_SWIPE_DOWN_INDEX = 1; |
| 89 | private static final int ACTION_SWIPE_LEFT_INDEX = 2; |
| 90 | private static final int ACTION_SWIPE_RIGHT_INDEX = 3; |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 91 | private static final int ACTION_SWIPE_LEFT_FROM_EDGE_INDEX = 4; |
| 92 | private static final int ACTION_SWIPE_RIGHT_FROM_EDGE_INDEX = 5; |
| 93 | private static final int MAX_GESTURES = 6; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 94 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 95 | private NavigationBarView mNavigationBarView; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 96 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 97 | private boolean mAllowGestureDetection; |
Matthew Ng | 90ef063 | 2018-08-15 13:53:15 -0700 | [diff] [blame] | 98 | private boolean mNotificationsVisibleOnDown; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 99 | private int mTouchDownX; |
| 100 | private int mTouchDownY; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 101 | private boolean mDragHPositive; |
| 102 | private boolean mDragVPositive; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 103 | private boolean mIsRTL; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 104 | private int mNavBarPosition; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 105 | private float mDarkIntensity; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 106 | private ViewPropertyAnimator mDragBtnAnimator; |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 107 | private ButtonDispatcher mHitTarget; |
Matthew Ng | 20136e6 | 2018-05-30 12:20:31 -0700 | [diff] [blame] | 108 | private boolean mIsInScreenPinning; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 109 | private boolean mGestureHorizontalDragsButton; |
| 110 | private boolean mGestureVerticalDragsButton; |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 111 | private float mMaxDragLimit; |
| 112 | private float mMinDragLimit; |
| 113 | private float mDragDampeningFactor; |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 114 | private boolean mClickThroughPressed; |
| 115 | private float mClickThroughPressX; |
| 116 | private float mClickThroughPressY; |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 117 | private int mGestureRegionThreshold; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 118 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 119 | private NavigationGestureAction mCurrentAction; |
| 120 | private NavigationGestureAction[] mGestureActions = new NavigationGestureAction[MAX_GESTURES]; |
| 121 | |
Matthew Ng | 1cf6eac | 2018-11-27 15:06:55 -0800 | [diff] [blame] | 122 | private final Rect mLastLayoutRect = new Rect(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 123 | private final OverviewProxyService mOverviewEventSender; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 124 | private final Context mContext; |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 125 | private final StatusBar mStatusBar; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 126 | private final Matrix mTransformGlobalMatrix = new Matrix(); |
| 127 | private final Matrix mTransformLocalMatrix = new Matrix(); |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 128 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 129 | public QuickStepController(Context context) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 130 | mContext = context; |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 131 | mStatusBar = SysUiServiceProvider.getComponent(context, StatusBar.class); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 132 | mOverviewEventSender = Dependency.get(OverviewProxyService.class); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 135 | private final Runnable mClickThroughSendTap = new Runnable() { |
| 136 | @Override |
| 137 | public void run() { |
| 138 | sendTap(mClickThroughPressX, mClickThroughPressY); |
| 139 | mNavigationBarView.postDelayed(mClickThroughResetTap, CLICK_THROUGH_TAP_RESET_DELAY); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | private final Runnable mClickThroughResetTap = () -> { |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 144 | mNavigationBarView.setWindowTouchable(true); |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 145 | mClickThroughPressed = false; |
| 146 | }; |
| 147 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 148 | public void setComponents(NavigationBarView navigationBarView) { |
| 149 | mNavigationBarView = navigationBarView; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 150 | |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 151 | mNavigationBarView.getBackButton().setVisibility(shouldhideBackButton(mContext) |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 152 | ? View.GONE |
| 153 | : View.VISIBLE); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 156 | /** |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 157 | * Set each gesture an action. After set the gestures triggered will run the actions attached. |
| 158 | * @param swipeUpAction action after swiping up |
| 159 | * @param swipeDownAction action after swiping down |
| 160 | * @param swipeLeftAction action after swiping left |
| 161 | * @param swipeRightAction action after swiping right |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 162 | * @param swipeLeftFromEdgeAction action swiping left starting from the right side |
| 163 | * @param swipeRightFromEdgeAction action swiping right starting from the left side |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 164 | */ |
| 165 | public void setGestureActions(@Nullable NavigationGestureAction swipeUpAction, |
| 166 | @Nullable NavigationGestureAction swipeDownAction, |
| 167 | @Nullable NavigationGestureAction swipeLeftAction, |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 168 | @Nullable NavigationGestureAction swipeRightAction, |
| 169 | @Nullable NavigationGestureAction swipeLeftFromEdgeAction, |
| 170 | @Nullable NavigationGestureAction swipeRightFromEdgeAction) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 171 | mGestureActions[ACTION_SWIPE_UP_INDEX] = swipeUpAction; |
| 172 | mGestureActions[ACTION_SWIPE_DOWN_INDEX] = swipeDownAction; |
| 173 | mGestureActions[ACTION_SWIPE_LEFT_INDEX] = swipeLeftAction; |
| 174 | mGestureActions[ACTION_SWIPE_RIGHT_INDEX] = swipeRightAction; |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 175 | mGestureActions[ACTION_SWIPE_LEFT_FROM_EDGE_INDEX] = swipeLeftFromEdgeAction; |
| 176 | mGestureActions[ACTION_SWIPE_RIGHT_FROM_EDGE_INDEX] = swipeRightFromEdgeAction; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 177 | |
| 178 | // Set the current state to all actions |
| 179 | for (NavigationGestureAction action: mGestureActions) { |
| 180 | if (action != null) { |
| 181 | action.setBarState(true, mNavBarPosition, mDragHPositive, mDragVPositive); |
| 182 | action.onDarkIntensityChange(mDarkIntensity); |
Matthew Ng | 1cf6eac | 2018-11-27 15:06:55 -0800 | [diff] [blame] | 183 | action.onLayout(true /* changed */, mLastLayoutRect.left, mLastLayoutRect.top, |
| 184 | mLastLayoutRect.right, mLastLayoutRect.bottom); |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 190 | * @return true if we want to intercept touch events for quick scrub and prevent proxying the |
| 191 | * event to the overview service. |
Winson Chung | 4faf38a | 2018-02-06 08:53:37 -0800 | [diff] [blame] | 192 | */ |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 193 | @Override |
| 194 | public boolean onInterceptTouchEvent(MotionEvent event) { |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 195 | if (mStatusBar.isKeyguardShowing()) { |
| 196 | // Disallow any handling when the keyguard is showing |
| 197 | return false; |
| 198 | } |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 199 | return handleTouchEvent(event); |
| 200 | } |
| 201 | |
| 202 | /** |
Matthew Ng | fba3969 | 2018-03-13 18:08:34 -0700 | [diff] [blame] | 203 | * @return true if we want to handle touch events for quick scrub or if down event (that will |
| 204 | * 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] | 205 | */ |
| 206 | @Override |
| 207 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 208 | if (mStatusBar.isKeyguardShowing()) { |
| 209 | // Disallow any handling when the keyguard is showing |
| 210 | return false; |
| 211 | } |
| 212 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 213 | // The same down event was just sent on intercept and therefore can be ignored here |
| 214 | final boolean ignoreProxyDownEvent = event.getAction() == MotionEvent.ACTION_DOWN |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 215 | && mOverviewEventSender.getProxy() != null |
| 216 | && mNavigationBarView.getWindowTarget() == WINDOW_TARGET_BOTTOM; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 217 | return ignoreProxyDownEvent || handleTouchEvent(event); |
Winson Chung | de2a124 | 2018-02-07 15:59:43 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private boolean handleTouchEvent(MotionEvent event) { |
Matthew Ng | 472d3e4 | 2018-06-14 15:16:55 -0700 | [diff] [blame] | 221 | final boolean deadZoneConsumed = |
| 222 | mNavigationBarView.getDownHitTarget() == HIT_TARGET_DEAD_ZONE; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 223 | |
| 224 | // Requires proxy and an active gesture or able to perform any gesture to continue |
| 225 | if (mOverviewEventSender.getProxy() == null |
Matthew Ng | d04676d | 2018-12-04 14:54:48 -0800 | [diff] [blame] | 226 | || !mOverviewEventSender.shouldShowSwipeUpUI() |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 227 | || (mCurrentAction == null && !canPerformAnyAction())) { |
Matthew Ng | 2cbe8cd | 2018-08-20 11:27:40 -0700 | [diff] [blame] | 228 | return deadZoneConsumed; |
Matthew Ng | 22cf514 | 2018-03-14 12:26:14 -0700 | [diff] [blame] | 229 | } |
| 230 | mNavigationBarView.requestUnbufferedDispatch(event); |
| 231 | |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 232 | int action = event.getActionMasked(); |
| 233 | switch (action) { |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 234 | case MotionEvent.ACTION_DOWN: { |
| 235 | int x = (int) event.getX(); |
| 236 | int y = (int) event.getY(); |
Matthew Ng | 20136e6 | 2018-05-30 12:20:31 -0700 | [diff] [blame] | 237 | mIsInScreenPinning = mNavigationBarView.inScreenPinning(); |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 238 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 239 | for (NavigationGestureAction gestureAction: mGestureActions) { |
| 240 | if (gestureAction != null) { |
| 241 | gestureAction.reset(); |
| 242 | } |
Winson Chung | 0be8f08 | 2018-02-15 15:52:49 -0800 | [diff] [blame] | 243 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 244 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 245 | // Valid buttons to drag over |
| 246 | switch (mNavigationBarView.getDownHitTarget()) { |
| 247 | case HIT_TARGET_BACK: |
| 248 | mHitTarget = mNavigationBarView.getBackButton(); |
| 249 | break; |
| 250 | case HIT_TARGET_HOME: |
| 251 | mHitTarget = mNavigationBarView.getHomeButton(); |
| 252 | break; |
| 253 | case HIT_TARGET_OVERVIEW: |
| 254 | mHitTarget = mNavigationBarView.getRecentsButton(); |
| 255 | break; |
| 256 | default: |
| 257 | mHitTarget = null; |
| 258 | break; |
| 259 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 260 | if (mHitTarget != null) { |
| 261 | // Pre-emptively delay the touch feedback for the button that we just touched |
| 262 | mHitTarget.setDelayTouchFeedback(true); |
| 263 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 264 | mTouchDownX = x; |
| 265 | mTouchDownY = y; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 266 | mGestureHorizontalDragsButton = false; |
| 267 | mGestureVerticalDragsButton = false; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 268 | mTransformGlobalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 269 | mTransformLocalMatrix.set(Matrix.IDENTITY_MATRIX); |
| 270 | mNavigationBarView.transformMatrixToGlobal(mTransformGlobalMatrix); |
| 271 | mNavigationBarView.transformMatrixToLocal(mTransformLocalMatrix); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 272 | mAllowGestureDetection = true; |
Matthew Ng | 90ef063 | 2018-08-15 13:53:15 -0700 | [diff] [blame] | 273 | mNotificationsVisibleOnDown = !mNavigationBarView.isNotificationsFullyCollapsed(); |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 274 | final int defaultRegionThreshold = mContext.getResources() |
| 275 | .getDimensionPixelOffset(R.dimen.navigation_bar_default_edge_width); |
| 276 | mGestureRegionThreshold = convertDpToPixel(getIntGlobalSetting(mContext, |
| 277 | EDGE_SENSITIVITY_WIDTH_SETTING, defaultRegionThreshold)); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | case MotionEvent.ACTION_MOVE: { |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 281 | if (!mAllowGestureDetection |
| 282 | || mNavigationBarView.getWindowTarget() != WINDOW_TARGET_BOTTOM) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | int x = (int) event.getX(); |
| 286 | int y = (int) event.getY(); |
| 287 | int xDiff = Math.abs(x - mTouchDownX); |
| 288 | int yDiff = Math.abs(y - mTouchDownY); |
Matthew Ng | 9a22363 | 2018-03-30 16:47:22 -0700 | [diff] [blame] | 289 | |
Matthew Ng | f886946 | 2019-01-18 13:31:47 -0800 | [diff] [blame] | 290 | boolean exceededSwipeHorizontalTouchSlop, exceededSwipeVerticalTouchSlop, |
| 291 | exceededSwipeVerticalDragSlop; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 292 | int posH, touchDownH, posV, touchDownV; |
Matthew Ng | e0903c9 | 2018-01-17 15:32:41 -0800 | [diff] [blame] | 293 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 294 | if (isNavBarVertical()) { |
| 295 | exceededSwipeHorizontalTouchSlop = |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 296 | yDiff > NavigationBarCompat.getQuickScrubTouchSlopPx() && yDiff > xDiff; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 297 | exceededSwipeVerticalTouchSlop = |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 298 | xDiff > NavigationBarCompat.getQuickStepTouchSlopPx() && xDiff > yDiff; |
Matthew Ng | f886946 | 2019-01-18 13:31:47 -0800 | [diff] [blame] | 299 | exceededSwipeVerticalDragSlop = |
| 300 | xDiff > NavigationBarCompat.getQuickStepDragSlopPx() && xDiff > yDiff; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 301 | posH = y; |
| 302 | touchDownH = mTouchDownY; |
| 303 | posV = x; |
| 304 | touchDownV = mTouchDownX; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 305 | } else { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 306 | exceededSwipeHorizontalTouchSlop = |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 307 | xDiff > NavigationBarCompat.getQuickScrubTouchSlopPx() && xDiff > yDiff; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 308 | exceededSwipeVerticalTouchSlop = |
Matthew Ng | f29ad75 | 2018-04-26 11:24:05 -0700 | [diff] [blame] | 309 | yDiff > NavigationBarCompat.getQuickStepTouchSlopPx() && yDiff > xDiff; |
Matthew Ng | f886946 | 2019-01-18 13:31:47 -0800 | [diff] [blame] | 310 | exceededSwipeVerticalDragSlop = |
| 311 | yDiff > NavigationBarCompat.getQuickStepDragSlopPx() && yDiff > xDiff; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 312 | posH = x; |
| 313 | touchDownH = mTouchDownX; |
| 314 | posV = y; |
| 315 | touchDownV = mTouchDownY; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 316 | } |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 317 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 318 | if (mCurrentAction != null) { |
| 319 | // Gesture started, provide positions to the current action |
| 320 | mCurrentAction.onGestureMove(x, y); |
| 321 | } else { |
| 322 | // Detect gesture and try to execute an action, only one can run at a time |
Matthew Ng | f886946 | 2019-01-18 13:31:47 -0800 | [diff] [blame] | 323 | if (exceededSwipeVerticalTouchSlop || exceededSwipeVerticalDragSlop) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 324 | if (mDragVPositive ? (posV < touchDownV) : (posV > touchDownV)) { |
Matthew Ng | f886946 | 2019-01-18 13:31:47 -0800 | [diff] [blame] | 325 | // Swipe up gesture must use the larger slop |
| 326 | if (exceededSwipeVerticalTouchSlop) { |
| 327 | // Swiping up gesture |
| 328 | tryToStartGesture(mGestureActions[ACTION_SWIPE_UP_INDEX], |
| 329 | false /* alignedWithNavBar */, event); |
| 330 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 331 | } else { |
| 332 | // Swiping down gesture |
| 333 | tryToStartGesture(mGestureActions[ACTION_SWIPE_DOWN_INDEX], |
Matthew Ng | a62b6208 | 2018-12-03 15:58:52 -0800 | [diff] [blame] | 334 | false /* alignedWithNavBar */, event); |
Winson Chung | 0e490d92 | 2018-03-14 16:08:43 +0000 | [diff] [blame] | 335 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 336 | } else if (exceededSwipeHorizontalTouchSlop) { |
| 337 | if (mDragHPositive ? (posH < touchDownH) : (posH > touchDownH)) { |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 338 | // Swiping left (rtl) gesture |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 339 | tryToStartGesture(mGestureActions[ACTION_SWIPE_LEFT_INDEX], |
| 340 | true /* alignedWithNavBar */, event); |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 341 | } else { |
| 342 | // Swiping right (ltr) gesture |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 343 | tryToStartGesture(mGestureActions[ACTION_SWIPE_RIGHT_INDEX], |
| 344 | true /* alignedWithNavBar */, event); |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 347 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 348 | |
| 349 | handleDragHitTarget(mGestureHorizontalDragsButton ? posH : posV, |
| 350 | mGestureHorizontalDragsButton ? touchDownH : touchDownV); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | case MotionEvent.ACTION_CANCEL: |
| 354 | case MotionEvent.ACTION_UP: |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 355 | if (mCurrentAction != null) { |
| 356 | mCurrentAction.endGesture(); |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 357 | } else if (action == MotionEvent.ACTION_UP) { |
| 358 | if (canTriggerEdgeSwipe(event)) { |
| 359 | int index = mNavigationBarView.getWindowTarget() == NAV_BAR_LEFT |
| 360 | ? ACTION_SWIPE_RIGHT_FROM_EDGE_INDEX |
| 361 | : ACTION_SWIPE_LEFT_FROM_EDGE_INDEX; |
| 362 | tryToStartGesture(mGestureActions[index], false /* alignedWithNavBar */, |
| 363 | event); |
| 364 | if (mCurrentAction != null) { |
| 365 | mCurrentAction.endGesture(); |
| 366 | } |
| 367 | } else if (getBoolGlobalSetting(mContext, ENABLE_CLICK_THROUGH_NAV_PROP) |
| 368 | && !mClickThroughPressed) { |
| 369 | // Enable click through functionality where no gesture has been detected and |
| 370 | // not passed the drag slop so inject a touch event at the same location |
| 371 | // after making the navigation bar window untouchable. After a some time, |
| 372 | // the navigation bar will be able to take input events again |
| 373 | float diffX = Math.abs(event.getX() - mTouchDownX); |
| 374 | float diffY = Math.abs(event.getY() - mTouchDownY); |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 375 | |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 376 | if ((diffX <= NavigationBarCompat.getQuickStepDragSlopPx() |
| 377 | && diffY <= NavigationBarCompat.getQuickStepDragSlopPx())) { |
| 378 | mNavigationBarView.setWindowTouchable(false); |
| 379 | mClickThroughPressX = event.getRawX(); |
| 380 | mClickThroughPressY = event.getRawY(); |
| 381 | mClickThroughPressed = true; |
| 382 | mNavigationBarView.postDelayed(mClickThroughSendTap, |
| 383 | CLICK_THROUGH_TAP_DELAY); |
| 384 | } |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 385 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Return the hit target back to its original position |
| 389 | if (mHitTarget != null) { |
| 390 | final View button = mHitTarget.getCurrentView(); |
| 391 | if (mGestureHorizontalDragsButton || mGestureVerticalDragsButton) { |
| 392 | mDragBtnAnimator = button.animate().setDuration(BACK_BUTTON_FADE_IN_ALPHA) |
| 393 | .setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 394 | if (mGestureVerticalDragsButton ^ isNavBarVertical()) { |
| 395 | mDragBtnAnimator.translationY(0); |
| 396 | } else { |
| 397 | mDragBtnAnimator.translationX(0); |
| 398 | } |
| 399 | mDragBtnAnimator.start(); |
| 400 | } |
| 401 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 402 | break; |
| 403 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 404 | |
Matthew Ng | 90ef063 | 2018-08-15 13:53:15 -0700 | [diff] [blame] | 405 | if (shouldProxyEvents(action)) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 406 | proxyMotionEvents(event); |
| 407 | } |
Matthew Ng | a62b6208 | 2018-12-03 15:58:52 -0800 | [diff] [blame] | 408 | |
| 409 | // Clear action when gesture and event proxy finishes |
| 410 | if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { |
| 411 | mCurrentAction = null; |
| 412 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 413 | return mCurrentAction != null || deadZoneConsumed; |
| 414 | } |
| 415 | |
| 416 | private void handleDragHitTarget(int position, int touchDown) { |
| 417 | // Drag the hit target if gesture action requires it |
| 418 | if (mHitTarget != null && (mGestureVerticalDragsButton || mGestureHorizontalDragsButton)) { |
| 419 | final View button = mHitTarget.getCurrentView(); |
| 420 | if (mDragBtnAnimator != null) { |
| 421 | mDragBtnAnimator.cancel(); |
| 422 | mDragBtnAnimator = null; |
| 423 | } |
| 424 | |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 425 | // Clamp drag to the bounding box of the navigation bar |
| 426 | float diff = (position - touchDown) * mDragDampeningFactor; |
| 427 | diff = Utilities.clamp(diff, mMinDragLimit, mMaxDragLimit); |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 428 | if (mGestureVerticalDragsButton ^ isNavBarVertical()) { |
| 429 | button.setTranslationY(diff); |
| 430 | } else { |
| 431 | button.setTranslationX(diff); |
| 432 | } |
| 433 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Matthew Ng | 90ef063 | 2018-08-15 13:53:15 -0700 | [diff] [blame] | 436 | private boolean shouldProxyEvents(int action) { |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 437 | // Do not send events for side navigation bar panels |
| 438 | if (mNavigationBarView.getWindowTarget() != WINDOW_TARGET_BOTTOM) { |
| 439 | return false; |
| 440 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 441 | final boolean actionValid = (mCurrentAction == null |
Matthew Ng | a62b6208 | 2018-12-03 15:58:52 -0800 | [diff] [blame] | 442 | || !mCurrentAction.disableProxyEvents()); |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 443 | if (actionValid && !mIsInScreenPinning) { |
Matthew Ng | 90ef063 | 2018-08-15 13:53:15 -0700 | [diff] [blame] | 444 | // Allow down, cancel and up events, move and other events are passed if notifications |
| 445 | // are not showing and disabled gestures (such as long press) are not executed |
| 446 | switch (action) { |
| 447 | case MotionEvent.ACTION_DOWN: |
| 448 | case MotionEvent.ACTION_CANCEL: |
| 449 | case MotionEvent.ACTION_UP: |
| 450 | return true; |
| 451 | default: |
| 452 | return !mNotificationsVisibleOnDown && mAllowGestureDetection; |
| 453 | } |
| 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 458 | @Override |
| 459 | public void onDraw(Canvas canvas) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 460 | if (mCurrentAction != null) { |
| 461 | mCurrentAction.onDraw(canvas); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 462 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 467 | for (NavigationGestureAction action: mGestureActions) { |
| 468 | if (action != null) { |
| 469 | action.onLayout(changed, left, top, right, bottom); |
| 470 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 471 | } |
Matthew Ng | 1cf6eac | 2018-11-27 15:06:55 -0800 | [diff] [blame] | 472 | mLastLayoutRect.set(left, top, right, bottom); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public void onDarkIntensityChange(float intensity) { |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 477 | final float oldIntensity = mDarkIntensity; |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 478 | mDarkIntensity = intensity; |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 479 | |
| 480 | // When in quick scrub, invalidate gradient if changing intensity from black to white and |
| 481 | // vice-versa |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 482 | if (mCurrentAction != null && mNavigationBarView.isQuickScrubEnabled() |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 483 | && Math.round(intensity) != Math.round(oldIntensity)) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 484 | mCurrentAction.onDarkIntensityChange(mDarkIntensity); |
Matthew Ng | 4fa3a9d | 2018-06-12 16:42:11 -0700 | [diff] [blame] | 485 | } |
Matthew Ng | 7090a80 | 2018-01-19 13:36:22 -0800 | [diff] [blame] | 486 | mNavigationBarView.invalidate(); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 490 | public void setBarState(boolean isRTL, int navBarPosition) { |
| 491 | final boolean changed = (mIsRTL != isRTL) || (mNavBarPosition != navBarPosition); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 492 | mIsRTL = isRTL; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 493 | mNavBarPosition = navBarPosition; |
| 494 | |
| 495 | // Determine the drag directions depending on location of nav bar |
| 496 | switch (navBarPosition) { |
| 497 | case NAV_BAR_LEFT: |
| 498 | mDragHPositive = !isRTL; |
| 499 | mDragVPositive = false; |
| 500 | break; |
| 501 | case NAV_BAR_RIGHT: |
| 502 | mDragHPositive = isRTL; |
| 503 | mDragVPositive = true; |
| 504 | break; |
| 505 | case NAV_BAR_BOTTOM: |
| 506 | mDragHPositive = !isRTL; |
| 507 | mDragVPositive = true; |
| 508 | break; |
Tiger Huang | 44c0455 | 2018-11-29 20:06:45 +0800 | [diff] [blame] | 509 | case NAV_BAR_INVALID: |
| 510 | Log.e(TAG, "Invalid nav bar position"); |
| 511 | break; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | for (NavigationGestureAction action: mGestureActions) { |
| 515 | if (action != null) { |
| 516 | action.setBarState(changed, mNavBarPosition, mDragHPositive, mDragVPositive); |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 517 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 521 | @Override |
| 522 | public void onNavigationButtonLongPress(View v) { |
| 523 | mAllowGestureDetection = false; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 526 | @Override |
| 527 | public void dump(PrintWriter pw) { |
| 528 | pw.println("QuickStepController {"); |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 529 | pw.print(" "); pw.println("mAllowGestureDetection=" + mAllowGestureDetection); |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 530 | pw.print(" "); pw.println("mNotificationsVisibleOnDown=" + mNotificationsVisibleOnDown); |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 531 | pw.print(" "); pw.println("mNavBarPosition=" + mNavBarPosition); |
Winson Chung | 0d3815d | 2018-09-25 18:11:54 -0700 | [diff] [blame] | 532 | pw.print(" "); pw.println("mIsRTL=" + mIsRTL); |
| 533 | pw.print(" "); pw.println("mIsInScreenPinning=" + mIsInScreenPinning); |
| 534 | pw.println("}"); |
| 535 | } |
| 536 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 537 | public NavigationGestureAction getCurrentAction() { |
| 538 | return mCurrentAction; |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 541 | private void tryToStartGesture(NavigationGestureAction action, boolean alignedWithNavBar, |
Matthew Ng | a62b6208 | 2018-12-03 15:58:52 -0800 | [diff] [blame] | 542 | MotionEvent event) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 543 | if (action == null) { |
| 544 | return; |
| 545 | } |
Matthew Ng | 20136e6 | 2018-05-30 12:20:31 -0700 | [diff] [blame] | 546 | if (mIsInScreenPinning) { |
| 547 | mNavigationBarView.showPinningEscapeToast(); |
| 548 | mAllowGestureDetection = false; |
| 549 | return; |
| 550 | } |
| 551 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 552 | // Start new action from gesture if is able to start and depending on notifications |
| 553 | // visibility and starting touch down target. If the action is enabled, then also check if |
| 554 | // can perform the action so that if action requires the button to be dragged, then the |
| 555 | // gesture will have a large dampening factor and prevent action from running. |
| 556 | final boolean validHitTarget = action.requiresTouchDownHitTarget() == HIT_TARGET_NONE |
| 557 | || action.requiresTouchDownHitTarget() == mNavigationBarView.getDownHitTarget(); |
| 558 | if (mCurrentAction == null && validHitTarget && action.isEnabled() |
| 559 | && (!mNotificationsVisibleOnDown || action.canRunWhenNotificationsShowing())) { |
| 560 | if (action.canPerformAction()) { |
| 561 | mCurrentAction = action; |
| 562 | event.transform(mTransformGlobalMatrix); |
| 563 | action.startGesture(event); |
| 564 | event.transform(mTransformLocalMatrix); |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 565 | |
| 566 | // Calculate the bounding limits of drag to avoid dragging off nav bar's window |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 567 | if (action.allowHitTargetToMoveOverDrag() && mHitTarget != null) { |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 568 | final int[] buttonCenter = new int[2]; |
| 569 | View button = mHitTarget.getCurrentView(); |
| 570 | button.getLocationInWindow(buttonCenter); |
| 571 | buttonCenter[0] += button.getWidth() / 2; |
| 572 | buttonCenter[1] += button.getHeight() / 2; |
| 573 | final int x = isNavBarVertical() ? buttonCenter[1] : buttonCenter[0]; |
| 574 | final int y = isNavBarVertical() ? buttonCenter[0] : buttonCenter[1]; |
| 575 | final int iconHalfSize = mContext.getResources() |
| 576 | .getDimensionPixelSize(R.dimen.navigation_icon_size) / 2; |
| 577 | |
| 578 | if (alignedWithNavBar) { |
| 579 | mMinDragLimit = iconHalfSize - x; |
| 580 | mMaxDragLimit = -x - iconHalfSize + (isNavBarVertical() |
| 581 | ? mNavigationBarView.getHeight() : mNavigationBarView.getWidth()); |
| 582 | } else { |
| 583 | mMinDragLimit = iconHalfSize - y; |
| 584 | mMaxDragLimit = -y - iconHalfSize + (isNavBarVertical() |
| 585 | ? mNavigationBarView.getWidth() : mNavigationBarView.getHeight()); |
| 586 | } |
| 587 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 588 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 589 | |
| 590 | // Handle direction of the hit target drag from the axis that started the gesture |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 591 | // Also calculate the dampening factor, weaker dampening if there is an active action |
Matthew Ng | b9c8428 | 2018-12-06 17:15:27 -0800 | [diff] [blame] | 592 | if (action.allowHitTargetToMoveOverDrag()) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 593 | if (alignedWithNavBar) { |
| 594 | mGestureHorizontalDragsButton = true; |
| 595 | mGestureVerticalDragsButton = false; |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 596 | mDragDampeningFactor = action.isActive() |
| 597 | ? HORIZONTAL_GESTURE_DAMPING : HORIZONTAL_DISABLED_GESTURE_DAMPING; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 598 | } else { |
| 599 | mGestureVerticalDragsButton = true; |
| 600 | mGestureHorizontalDragsButton = false; |
Matthew Ng | f1f8a43 | 2018-11-08 18:03:29 -0800 | [diff] [blame] | 601 | mDragDampeningFactor = action.isActive() |
| 602 | ? VERTICAL_GESTURE_DAMPING : VERTICAL_DISABLED_GESTURE_DAMPING; |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
Winson Chung | 661d5f9 | 2018-05-21 18:41:39 -0700 | [diff] [blame] | 605 | |
| 606 | if (mHitTarget != null) { |
| 607 | mHitTarget.abortCurrentGesture(); |
| 608 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 612 | /** |
| 613 | * To trigger an edge swipe, the user must start from the left or right edges of certain height |
| 614 | * from the bottom then past the drag slope towards the center of the screen, followed by either |
| 615 | * a timed trigger for fast swipes or distance if held on the screen longer. |
| 616 | * For time, user must swipe up quickly before the Tap Timeout (typically 100ms) and for |
| 617 | * distance, the user can drag back to cancel if the touch up has not past the threshold. |
| 618 | * @param event Touch up event |
| 619 | * @return whether or not edge swipe gesture occurs |
| 620 | */ |
| 621 | private boolean canTriggerEdgeSwipe(MotionEvent event) { |
| 622 | if (mNavigationBarView.getWindowTarget() == WINDOW_TARGET_BOTTOM) { |
| 623 | return false; |
| 624 | } |
| 625 | int x = (int) event.getX(); |
| 626 | int y = (int) event.getY(); |
| 627 | int xDiff = Math.abs(x - mTouchDownX); |
| 628 | int yDiff = Math.abs(y - mTouchDownY); |
| 629 | final boolean exceededSwipeTouchSlop = xDiff > NavigationBarCompat.getQuickStepDragSlopPx() |
| 630 | && xDiff > yDiff; |
| 631 | if (exceededSwipeTouchSlop) { |
| 632 | long timeDiff = event.getEventTime() - event.getDownTime(); |
| 633 | return xDiff > mGestureRegionThreshold || timeDiff < ViewConfiguration.getTapTimeout(); |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 638 | private boolean canPerformAnyAction() { |
| 639 | for (NavigationGestureAction action: mGestureActions) { |
| 640 | if (action != null && action.isEnabled()) { |
| 641 | return true; |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 642 | } |
Matthew Ng | ed166f9 | 2018-02-20 16:22:09 -0800 | [diff] [blame] | 643 | } |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 644 | return false; |
Matthew Ng | c83b989 | 2018-08-21 16:31:13 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Matthew Ng | 11891c9 | 2019-01-15 12:12:37 -0800 | [diff] [blame] | 647 | private void sendTap(float x, float y) { |
| 648 | long now = SystemClock.uptimeMillis(); |
| 649 | injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, now, x, y, 1.0f); |
| 650 | injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_UP, now, x, y, 0.0f); |
| 651 | } |
| 652 | |
| 653 | private int getInputDeviceId(int inputSource) { |
| 654 | int[] devIds = InputDevice.getDeviceIds(); |
| 655 | for (int devId : devIds) { |
| 656 | InputDevice inputDev = InputDevice.getDevice(devId); |
| 657 | if (inputDev.supportsSource(inputSource)) { |
| 658 | return devId; |
| 659 | } |
| 660 | } |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | private void injectMotionEvent(int inputSource, int action, long when, float x, float y, |
| 665 | float pressure) { |
| 666 | final float defaultSize = 1.0f; |
| 667 | final int defaultMetaState = 0; |
| 668 | final float defaultPrecisionX = 1.0f; |
| 669 | final float defaultPrecisionY = 1.0f; |
| 670 | final int defaultEdgeFlags = 0; |
| 671 | MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, defaultSize, |
| 672 | defaultMetaState, defaultPrecisionX, defaultPrecisionY, |
| 673 | getInputDeviceId(inputSource), defaultEdgeFlags); |
| 674 | event.setSource(inputSource); |
| 675 | InputManager.getInstance().injectInputEvent(event, |
| 676 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 677 | } |
| 678 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 679 | private boolean proxyMotionEvents(MotionEvent event) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 680 | event.transform(mTransformGlobalMatrix); |
Sunny Goyal | aac6c88 | 2019-02-11 11:57:58 -0800 | [diff] [blame] | 681 | InputEventDispatcher dispatcher = mOverviewEventSender.getInputEventDispatcher(); |
| 682 | if (dispatcher != null) { |
| 683 | dispatcher.dispatch(event); |
| 684 | } |
| 685 | |
| 686 | final IOverviewProxy overviewProxy = mOverviewEventSender.getProxy(); |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 687 | try { |
| 688 | if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { |
| 689 | overviewProxy.onPreMotionEvent(mNavigationBarView.getDownHitTarget()); |
| 690 | } |
| 691 | overviewProxy.onMotionEvent(event); |
| 692 | if (DEBUG_OVERVIEW_PROXY) { |
| 693 | Log.d(TAG_OPS, "Send MotionEvent: " + event.toString()); |
| 694 | } |
| 695 | return true; |
| 696 | } catch (RemoteException e) { |
| 697 | Log.e(TAG, "Callback failed", e); |
| 698 | } finally { |
| 699 | event.transform(mTransformLocalMatrix); |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 700 | } |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 701 | return false; |
Matthew Ng | db2734c | 2018-02-16 16:02:20 -0800 | [diff] [blame] | 702 | } |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 703 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 704 | protected boolean isNavBarVertical() { |
| 705 | return mNavBarPosition == NAV_BAR_LEFT || mNavBarPosition == NAV_BAR_RIGHT; |
| 706 | } |
| 707 | |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 708 | private static int convertDpToPixel(float dp) { |
| 709 | return (int) (dp * Resources.getSystem().getDisplayMetrics().density); |
| 710 | } |
| 711 | |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 712 | static boolean getBoolGlobalSetting(Context context, String key) { |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 713 | return Settings.Global.getInt(context.getContentResolver(), key, 0) != 0; |
| 714 | } |
| 715 | |
Matthew Ng | b831fb4 | 2019-01-30 11:20:48 -0800 | [diff] [blame] | 716 | static int getIntGlobalSetting(Context context, String key, int defaultValue) { |
| 717 | return Settings.Global.getInt(context.getContentResolver(), key, defaultValue); |
| 718 | } |
| 719 | |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 720 | public static boolean shouldhideBackButton(Context context) { |
Matthew Ng | 86a436e | 2018-10-26 16:00:53 -0700 | [diff] [blame] | 721 | return getBoolGlobalSetting(context, HIDE_BACK_BUTTON_PROP); |
Matthew Ng | b687c8a | 2018-10-02 11:31:38 -0700 | [diff] [blame] | 722 | } |
Matthew Ng | a8f2426 | 2017-12-19 11:54:24 -0800 | [diff] [blame] | 723 | } |