Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2019 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 | package com.android.systemui.statusbar.phone; |
| 17 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 18 | import static android.view.Display.INVALID_DISPLAY; |
| 19 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Matthew Ng | 682ad7a | 2019-04-09 15:48:01 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 22 | import android.graphics.PixelFormat; |
| 23 | import android.graphics.Point; |
| 24 | import android.graphics.PointF; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 25 | import android.graphics.Region; |
| 26 | import android.hardware.display.DisplayManager; |
| 27 | import android.hardware.display.DisplayManager.DisplayListener; |
| 28 | import android.hardware.input.InputManager; |
| 29 | import android.os.Looper; |
| 30 | import android.os.RemoteException; |
| 31 | import android.os.SystemClock; |
Adam Cohen | a699c28 | 2019-08-07 15:29:34 -0700 | [diff] [blame] | 32 | import android.os.SystemProperties; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 34 | import android.view.ISystemGestureExclusionListener; |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 35 | import android.view.InputChannel; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 36 | import android.view.InputDevice; |
| 37 | import android.view.InputEvent; |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 38 | import android.view.InputEventReceiver; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 39 | import android.view.InputMonitor; |
| 40 | import android.view.KeyCharacterMap; |
| 41 | import android.view.KeyEvent; |
| 42 | import android.view.MotionEvent; |
| 43 | import android.view.ViewConfiguration; |
| 44 | import android.view.WindowManager; |
| 45 | import android.view.WindowManagerGlobal; |
| 46 | |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 47 | import com.android.systemui.Dependency; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 48 | import com.android.systemui.R; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 49 | import com.android.systemui.bubbles.BubbleController; |
Vinit Nayak | 59b114e6 | 2019-08-12 11:50:00 -0700 | [diff] [blame] | 50 | import com.android.systemui.model.SysUiState; |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 51 | import com.android.systemui.plugins.NavigationEdgeBackPlugin; |
| 52 | import com.android.systemui.plugins.PluginListener; |
Winson Chung | 04ff8bda | 2019-04-02 15:08:59 -0700 | [diff] [blame] | 53 | import com.android.systemui.recents.OverviewProxyService; |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 54 | import com.android.systemui.shared.plugins.PluginManager; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 55 | import com.android.systemui.shared.system.QuickStepContract; |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 56 | import com.android.systemui.shared.system.SysUiStatsLog; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 57 | |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 58 | import java.io.PrintWriter; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 59 | import java.util.concurrent.Executor; |
| 60 | |
| 61 | /** |
| 62 | * Utility class to handle edge swipes for back gesture |
| 63 | */ |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 64 | public class EdgeBackGestureHandler implements DisplayListener, |
| 65 | PluginListener<NavigationEdgeBackPlugin> { |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 66 | |
| 67 | private static final String TAG = "EdgeBackGestureHandler"; |
Adam Cohen | a699c28 | 2019-08-07 15:29:34 -0700 | [diff] [blame] | 68 | private static final int MAX_LONG_PRESS_TIMEOUT = SystemProperties.getInt( |
| 69 | "gestures.back_timeout", 250); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 70 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 71 | private ISystemGestureExclusionListener mGestureExclusionListener = |
| 72 | new ISystemGestureExclusionListener.Stub() { |
| 73 | @Override |
| 74 | public void onSystemGestureExclusionChanged(int displayId, |
Adrian Roos | 5f2c9a1 | 2019-07-03 18:31:46 +0200 | [diff] [blame] | 75 | Region systemGestureExclusion, Region unrestrictedOrNull) { |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 76 | if (displayId == mDisplayId) { |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 77 | mMainExecutor.execute(() -> { |
| 78 | mExcludeRegion.set(systemGestureExclusion); |
| 79 | mUnrestrictedExcludeRegion.set(unrestrictedOrNull != null |
| 80 | ? unrestrictedOrNull : systemGestureExclusion); |
| 81 | }); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | private final Context mContext; |
Winson Chung | 04ff8bda | 2019-04-02 15:08:59 -0700 | [diff] [blame] | 87 | private final OverviewProxyService mOverviewProxyService; |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 88 | private PluginManager mPluginManager; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 89 | |
| 90 | private final Point mDisplaySize = new Point(); |
| 91 | private final int mDisplayId; |
| 92 | |
| 93 | private final Executor mMainExecutor; |
| 94 | |
| 95 | private final Region mExcludeRegion = new Region(); |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 96 | private final Region mUnrestrictedExcludeRegion = new Region(); |
| 97 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 98 | // The edge width where touch down is allowed |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 99 | private int mEdgeWidth; |
Winson Chung | 77b2d1d | 2019-12-20 15:18:42 -0800 | [diff] [blame] | 100 | // The bottom gesture area height |
| 101 | private int mBottomGestureHeight; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 102 | // The slop to distinguish between horizontal and vertical motion |
| 103 | private final float mTouchSlop; |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 104 | // Duration after which we consider the event as longpress. |
| 105 | private final int mLongPressTimeout; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 106 | |
| 107 | private final PointF mDownPoint = new PointF(); |
| 108 | private boolean mThresholdCrossed = false; |
Winson Chung | e2a686d | 2019-04-17 16:54:23 -0700 | [diff] [blame] | 109 | private boolean mAllowGesture = false; |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 110 | private boolean mInRejectedExclusion = false; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 111 | private boolean mIsOnLeftEdge; |
| 112 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 113 | private boolean mIsAttached; |
| 114 | private boolean mIsGesturalModeEnabled; |
| 115 | private boolean mIsEnabled; |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 116 | private boolean mIsNavBarShownTransiently; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 117 | |
| 118 | private InputMonitor mInputMonitor; |
| 119 | private InputEventReceiver mInputEventReceiver; |
| 120 | |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 121 | private NavigationEdgeBackPlugin mEdgeBackPlugin; |
Selim Cinek | e50b121 | 2019-06-04 18:40:31 -0700 | [diff] [blame] | 122 | private int mLeftInset; |
| 123 | private int mRightInset; |
Vinit Nayak | 59b114e6 | 2019-08-12 11:50:00 -0700 | [diff] [blame] | 124 | private int mSysUiFlags; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 125 | |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 126 | private final NavigationEdgeBackPlugin.BackCallback mBackCallback = |
| 127 | new NavigationEdgeBackPlugin.BackCallback() { |
| 128 | @Override |
| 129 | public void triggerBack() { |
| 130 | sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK); |
| 131 | sendEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK); |
| 132 | |
| 133 | mOverviewProxyService.notifyBackAction(true, (int) mDownPoint.x, |
| 134 | (int) mDownPoint.y, false /* isButton */, !mIsOnLeftEdge); |
| 135 | int backtype = (mInRejectedExclusion |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 136 | ? SysUiStatsLog.BACK_GESTURE__TYPE__COMPLETED_REJECTED : |
| 137 | SysUiStatsLog.BACK_GESTURE__TYPE__COMPLETED); |
| 138 | SysUiStatsLog.write(SysUiStatsLog.BACK_GESTURE_REPORTED_REPORTED, backtype, |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 139 | (int) mDownPoint.y, mIsOnLeftEdge |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 140 | ? SysUiStatsLog.BACK_GESTURE__X_LOCATION__LEFT : |
| 141 | SysUiStatsLog.BACK_GESTURE__X_LOCATION__RIGHT); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void cancelBack() { |
| 146 | mOverviewProxyService.notifyBackAction(false, (int) mDownPoint.x, |
| 147 | (int) mDownPoint.y, false /* isButton */, !mIsOnLeftEdge); |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 148 | int backtype = SysUiStatsLog.BACK_GESTURE__TYPE__INCOMPLETE; |
| 149 | SysUiStatsLog.write(SysUiStatsLog.BACK_GESTURE_REPORTED_REPORTED, backtype, |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 150 | (int) mDownPoint.y, mIsOnLeftEdge |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 151 | ? SysUiStatsLog.BACK_GESTURE__X_LOCATION__LEFT : |
| 152 | SysUiStatsLog.BACK_GESTURE__X_LOCATION__RIGHT); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 153 | } |
| 154 | }; |
| 155 | |
Vinit Nayak | 59b114e6 | 2019-08-12 11:50:00 -0700 | [diff] [blame] | 156 | public EdgeBackGestureHandler(Context context, OverviewProxyService overviewProxyService, |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 157 | SysUiState sysUiFlagContainer, PluginManager pluginManager) { |
Matthew Ng | 682ad7a | 2019-04-09 15:48:01 -0700 | [diff] [blame] | 158 | final Resources res = context.getResources(); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 159 | mContext = context; |
| 160 | mDisplayId = context.getDisplayId(); |
| 161 | mMainExecutor = context.getMainExecutor(); |
Winson Chung | 04ff8bda | 2019-04-02 15:08:59 -0700 | [diff] [blame] | 162 | mOverviewProxyService = overviewProxyService; |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 163 | mPluginManager = pluginManager; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 164 | |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 165 | // Reduce the default touch slop to ensure that we can intercept the gesture |
| 166 | // before the app starts to react to it. |
| 167 | // TODO(b/130352502) Tune this value and extract into a constant |
| 168 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop() * 0.75f; |
Winson Chung | 05d4be3 | 2019-06-19 13:12:10 -0700 | [diff] [blame] | 169 | mLongPressTimeout = Math.min(MAX_LONG_PRESS_TIMEOUT, |
| 170 | ViewConfiguration.getLongPressTimeout()); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 171 | |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 172 | updateCurrentUserResources(res); |
Vinit Nayak | 59b114e6 | 2019-08-12 11:50:00 -0700 | [diff] [blame] | 173 | sysUiFlagContainer.addCallback(sysUiFlags -> mSysUiFlags = sysUiFlags); |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public void updateCurrentUserResources(Resources res) { |
| 177 | mEdgeWidth = res.getDimensionPixelSize( |
| 178 | com.android.internal.R.dimen.config_backGestureInset); |
Winson Chung | 77b2d1d | 2019-12-20 15:18:42 -0800 | [diff] [blame] | 179 | mBottomGestureHeight = res.getDimensionPixelSize( |
| 180 | com.android.internal.R.dimen.navigation_bar_gesture_height); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @see NavigationBarView#onAttachedToWindow() |
| 185 | */ |
| 186 | public void onNavBarAttached() { |
| 187 | mIsAttached = true; |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 188 | updateIsEnabled(); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @see NavigationBarView#onDetachedFromWindow() |
| 193 | */ |
| 194 | public void onNavBarDetached() { |
| 195 | mIsAttached = false; |
| 196 | updateIsEnabled(); |
| 197 | } |
| 198 | |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 199 | public void onNavigationModeChanged(int mode, Context currentUserContext) { |
Winson Chung | 1aa24b9 | 2019-04-24 15:17:33 -0700 | [diff] [blame] | 200 | mIsGesturalModeEnabled = QuickStepContract.isGesturalMode(mode); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 201 | updateIsEnabled(); |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 202 | updateCurrentUserResources(currentUserContext.getResources()); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 205 | public void onNavBarTransientStateChanged(boolean isTransient) { |
| 206 | mIsNavBarShownTransiently = isTransient; |
Winson Chung | 3d6c080 | 2019-10-04 10:37:12 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 209 | private void disposeInputChannel() { |
| 210 | if (mInputEventReceiver != null) { |
| 211 | mInputEventReceiver.dispose(); |
| 212 | mInputEventReceiver = null; |
| 213 | } |
| 214 | if (mInputMonitor != null) { |
| 215 | mInputMonitor.dispose(); |
| 216 | mInputMonitor = null; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | private void updateIsEnabled() { |
| 221 | boolean isEnabled = mIsAttached && mIsGesturalModeEnabled; |
| 222 | if (isEnabled == mIsEnabled) { |
| 223 | return; |
| 224 | } |
| 225 | mIsEnabled = isEnabled; |
| 226 | disposeInputChannel(); |
| 227 | |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 228 | if (mEdgeBackPlugin != null) { |
| 229 | mEdgeBackPlugin.onDestroy(); |
| 230 | mEdgeBackPlugin = null; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (!mIsEnabled) { |
Sunny Goyal | 1904969 | 2019-04-08 11:56:07 -0700 | [diff] [blame] | 234 | mContext.getSystemService(DisplayManager.class).unregisterDisplayListener(this); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 235 | mPluginManager.removePluginListener(this); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 236 | |
| 237 | try { |
| 238 | WindowManagerGlobal.getWindowManagerService() |
| 239 | .unregisterSystemGestureExclusionListener( |
| 240 | mGestureExclusionListener, mDisplayId); |
| 241 | } catch (RemoteException e) { |
| 242 | Log.e(TAG, "Failed to unregister window manager callbacks", e); |
| 243 | } |
| 244 | |
| 245 | } else { |
| 246 | updateDisplaySize(); |
Sunny Goyal | 1904969 | 2019-04-08 11:56:07 -0700 | [diff] [blame] | 247 | mContext.getSystemService(DisplayManager.class).registerDisplayListener(this, |
| 248 | mContext.getMainThreadHandler()); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 249 | |
| 250 | try { |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 251 | WindowManagerGlobal.getWindowManagerService() |
| 252 | .registerSystemGestureExclusionListener( |
| 253 | mGestureExclusionListener, mDisplayId); |
| 254 | } catch (RemoteException e) { |
| 255 | Log.e(TAG, "Failed to register window manager callbacks", e); |
| 256 | } |
| 257 | |
| 258 | // Register input event receiver |
| 259 | mInputMonitor = InputManager.getInstance().monitorGestureInput( |
| 260 | "edge-swipe", mDisplayId); |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 261 | mInputEventReceiver = new SysUiInputEventReceiver( |
| 262 | mInputMonitor.getInputChannel(), Looper.getMainLooper()); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 263 | |
| 264 | // Add a nav bar panel window |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 265 | setEdgeBackPlugin(new NavigationBarEdgePanel(mContext)); |
| 266 | mPluginManager.addPluginListener( |
| 267 | this, NavigationEdgeBackPlugin.class, /*allowMultiple=*/ false); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 271 | @Override |
| 272 | public void onPluginConnected(NavigationEdgeBackPlugin plugin, Context context) { |
| 273 | setEdgeBackPlugin(plugin); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void onPluginDisconnected(NavigationEdgeBackPlugin plugin) { |
| 278 | setEdgeBackPlugin(new NavigationBarEdgePanel(mContext)); |
| 279 | } |
| 280 | |
| 281 | private void setEdgeBackPlugin(NavigationEdgeBackPlugin edgeBackPlugin) { |
| 282 | if (mEdgeBackPlugin != null) { |
| 283 | mEdgeBackPlugin.onDestroy(); |
| 284 | } |
| 285 | mEdgeBackPlugin = edgeBackPlugin; |
| 286 | mEdgeBackPlugin.setBackCallback(mBackCallback); |
| 287 | mEdgeBackPlugin.setLayoutParams(createLayoutParams()); |
| 288 | updateDisplaySize(); |
| 289 | } |
| 290 | |
| 291 | private WindowManager.LayoutParams createLayoutParams() { |
| 292 | Resources resources = mContext.getResources(); |
| 293 | WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( |
| 294 | resources.getDimensionPixelSize(R.dimen.navigation_edge_panel_width), |
| 295 | resources.getDimensionPixelSize(R.dimen.navigation_edge_panel_height), |
| 296 | WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL, |
| 297 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 298 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 299 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 300 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, |
| 301 | PixelFormat.TRANSLUCENT); |
| 302 | layoutParams.privateFlags |= |
| 303 | WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS; |
| 304 | layoutParams.setTitle(TAG + mContext.getDisplayId()); |
| 305 | layoutParams.accessibilityTitle = mContext.getString(R.string.nav_bar_edge_panel); |
| 306 | layoutParams.windowAnimations = 0; |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 307 | layoutParams.setFitInsetsTypes(0 /* types */); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 308 | return layoutParams; |
| 309 | } |
| 310 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 311 | private void onInputEvent(InputEvent ev) { |
| 312 | if (ev instanceof MotionEvent) { |
| 313 | onMotionEvent((MotionEvent) ev); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private boolean isWithinTouchRegion(int x, int y) { |
Winson Chung | 3d6c080 | 2019-10-04 10:37:12 -0700 | [diff] [blame] | 318 | // Disallow if too far from the edge |
Selim Cinek | e50b121 | 2019-06-04 18:40:31 -0700 | [diff] [blame] | 319 | if (x > mEdgeWidth + mLeftInset && x < (mDisplaySize.x - mEdgeWidth - mRightInset)) { |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 320 | return false; |
| 321 | } |
Winson Chung | 3d6c080 | 2019-10-04 10:37:12 -0700 | [diff] [blame] | 322 | |
Winson Chung | 77b2d1d | 2019-12-20 15:18:42 -0800 | [diff] [blame] | 323 | // Disallow if we are in the bottom gesture area |
| 324 | if (y >= (mDisplaySize.y - mBottomGestureHeight)) { |
| 325 | return false; |
| 326 | } |
| 327 | |
Winson Chung | 3d6c080 | 2019-10-04 10:37:12 -0700 | [diff] [blame] | 328 | // Always allow if the user is in a transient sticky immersive state |
Jorim Jaggi | 956ca41 | 2019-01-07 14:49:14 +0100 | [diff] [blame] | 329 | if (mIsNavBarShownTransiently) { |
Winson Chung | 3d6c080 | 2019-10-04 10:37:12 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
Winson Chung | 51787a1 | 2019-04-10 15:33:31 -0700 | [diff] [blame] | 333 | boolean isInExcludedRegion = mExcludeRegion.contains(x, y); |
| 334 | if (isInExcludedRegion) { |
| 335 | mOverviewProxyService.notifyBackAction(false /* completed */, -1, -1, |
| 336 | false /* isButton */, !mIsOnLeftEdge); |
Muhammad Qureshi | 9bced7d | 2020-01-16 15:22:12 -0800 | [diff] [blame] | 337 | SysUiStatsLog.write(SysUiStatsLog.BACK_GESTURE_REPORTED_REPORTED, |
| 338 | SysUiStatsLog.BACK_GESTURE__TYPE__INCOMPLETE_EXCLUDED, y, |
| 339 | mIsOnLeftEdge ? SysUiStatsLog.BACK_GESTURE__X_LOCATION__LEFT : |
| 340 | SysUiStatsLog.BACK_GESTURE__X_LOCATION__RIGHT); |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 341 | } else { |
| 342 | mInRejectedExclusion = mUnrestrictedExcludeRegion.contains(x, y); |
Winson Chung | 51787a1 | 2019-04-10 15:33:31 -0700 | [diff] [blame] | 343 | } |
| 344 | return !isInExcludedRegion; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 347 | private void cancelGesture(MotionEvent ev) { |
| 348 | // Send action cancel to reset all the touch events |
| 349 | mAllowGesture = false; |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 350 | mInRejectedExclusion = false; |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 351 | MotionEvent cancelEv = MotionEvent.obtain(ev); |
| 352 | cancelEv.setAction(MotionEvent.ACTION_CANCEL); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 353 | mEdgeBackPlugin.onMotionEvent(cancelEv); |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 354 | cancelEv.recycle(); |
| 355 | } |
| 356 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 357 | private void onMotionEvent(MotionEvent ev) { |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 358 | int action = ev.getActionMasked(); |
| 359 | if (action == MotionEvent.ACTION_DOWN) { |
Winson Chung | e2a686d | 2019-04-17 16:54:23 -0700 | [diff] [blame] | 360 | // Verify if this is in within the touch region and we aren't in immersive mode, and |
| 361 | // either the bouncer is showing or the notification panel is hidden |
Selim Cinek | e50b121 | 2019-06-04 18:40:31 -0700 | [diff] [blame] | 362 | mIsOnLeftEdge = ev.getX() <= mEdgeWidth + mLeftInset; |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 363 | mInRejectedExclusion = false; |
Vinit Nayak | 59b114e6 | 2019-08-12 11:50:00 -0700 | [diff] [blame] | 364 | mAllowGesture = !QuickStepContract.isBackGestureDisabled(mSysUiFlags) |
Winson Chung | e2a686d | 2019-04-17 16:54:23 -0700 | [diff] [blame] | 365 | && isWithinTouchRegion((int) ev.getX(), (int) ev.getY()); |
| 366 | if (mAllowGesture) { |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 367 | mEdgeBackPlugin.setIsLeftPanel(mIsOnLeftEdge); |
| 368 | mEdgeBackPlugin.onMotionEvent(ev); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 369 | |
| 370 | mDownPoint.set(ev.getX(), ev.getY()); |
| 371 | mThresholdCrossed = false; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 372 | } |
Winson Chung | e2a686d | 2019-04-17 16:54:23 -0700 | [diff] [blame] | 373 | } else if (mAllowGesture) { |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 374 | if (!mThresholdCrossed) { |
| 375 | if (action == MotionEvent.ACTION_POINTER_DOWN) { |
| 376 | // We do not support multi touch for back gesture |
| 377 | cancelGesture(ev); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 378 | return; |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 379 | } else if (action == MotionEvent.ACTION_MOVE) { |
| 380 | if ((ev.getEventTime() - ev.getDownTime()) > mLongPressTimeout) { |
| 381 | cancelGesture(ev); |
| 382 | return; |
| 383 | } |
| 384 | float dx = Math.abs(ev.getX() - mDownPoint.x); |
| 385 | float dy = Math.abs(ev.getY() - mDownPoint.y); |
| 386 | if (dy > dx && dy > mTouchSlop) { |
| 387 | cancelGesture(ev); |
| 388 | return; |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 389 | |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 390 | } else if (dx > dy && dx > mTouchSlop) { |
| 391 | mThresholdCrossed = true; |
| 392 | // Capture inputs |
| 393 | mInputMonitor.pilferPointers(); |
| 394 | } |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 395 | } |
Sunny Goyal | 3c317f3 | 2019-05-21 11:49:11 -0700 | [diff] [blame] | 396 | |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // forward touch |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 400 | mEdgeBackPlugin.onMotionEvent(ev); |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void onDisplayAdded(int displayId) { } |
| 406 | |
| 407 | @Override |
| 408 | public void onDisplayRemoved(int displayId) { } |
| 409 | |
| 410 | @Override |
| 411 | public void onDisplayChanged(int displayId) { |
| 412 | if (displayId == mDisplayId) { |
| 413 | updateDisplaySize(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private void updateDisplaySize() { |
| 418 | mContext.getSystemService(DisplayManager.class) |
Sunny Goyal | 1904969 | 2019-04-08 11:56:07 -0700 | [diff] [blame] | 419 | .getDisplay(mDisplayId) |
| 420 | .getRealSize(mDisplaySize); |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 421 | if (mEdgeBackPlugin != null) { |
| 422 | mEdgeBackPlugin.setDisplaySize(mDisplaySize); |
| 423 | } |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | private void sendEvent(int action, int code) { |
| 427 | long when = SystemClock.uptimeMillis(); |
| 428 | final KeyEvent ev = new KeyEvent(when, when, action, code, 0 /* repeat */, |
| 429 | 0 /* metaState */, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /* scancode */, |
| 430 | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 431 | InputDevice.SOURCE_KEYBOARD); |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 432 | |
| 433 | // Bubble controller will give us a valid display id if it should get the back event |
| 434 | BubbleController bubbleController = Dependency.get(BubbleController.class); |
| 435 | int bubbleDisplayId = bubbleController.getExpandedDisplayId(mContext); |
| 436 | if (code == KeyEvent.KEYCODE_BACK && bubbleDisplayId != INVALID_DISPLAY) { |
| 437 | ev.setDisplayId(bubbleDisplayId); |
| 438 | } |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 439 | InputManager.getInstance().injectInputEvent(ev, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 440 | } |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 441 | |
Selim Cinek | e50b121 | 2019-06-04 18:40:31 -0700 | [diff] [blame] | 442 | public void setInsets(int leftInset, int rightInset) { |
| 443 | mLeftInset = leftInset; |
| 444 | mRightInset = rightInset; |
Andy Wickham | 2029701 | 2019-11-06 14:56:26 -0800 | [diff] [blame] | 445 | if (mEdgeBackPlugin != null) { |
| 446 | mEdgeBackPlugin.setInsets(leftInset, rightInset); |
| 447 | } |
Selim Cinek | e50b121 | 2019-06-04 18:40:31 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 450 | public void dump(PrintWriter pw) { |
| 451 | pw.println("EdgeBackGestureHandler:"); |
| 452 | pw.println(" mIsEnabled=" + mIsEnabled); |
| 453 | pw.println(" mAllowGesture=" + mAllowGesture); |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 454 | pw.println(" mInRejectedExclusion" + mInRejectedExclusion); |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 455 | pw.println(" mExcludeRegion=" + mExcludeRegion); |
Hyunyoung Song | 36d7a2f | 2019-08-05 17:19:54 -0700 | [diff] [blame] | 456 | pw.println(" mUnrestrictedExcludeRegion=" + mUnrestrictedExcludeRegion); |
Winson Chung | 316f673 | 2019-06-14 09:31:46 -0700 | [diff] [blame] | 457 | pw.println(" mIsAttached=" + mIsAttached); |
| 458 | pw.println(" mEdgeWidth=" + mEdgeWidth); |
| 459 | } |
| 460 | |
Siarhei Vishniakou | faa8799 | 2019-05-29 09:12:18 -0700 | [diff] [blame] | 461 | class SysUiInputEventReceiver extends InputEventReceiver { |
| 462 | SysUiInputEventReceiver(InputChannel channel, Looper looper) { |
| 463 | super(channel, looper); |
| 464 | } |
| 465 | |
| 466 | public void onInputEvent(InputEvent event) { |
| 467 | EdgeBackGestureHandler.this.onInputEvent(event); |
| 468 | finishInputEvent(event, true); |
| 469 | } |
| 470 | } |
Sunny Goyal | a6a5812 | 2019-04-02 10:20:29 -0700 | [diff] [blame] | 471 | } |