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