Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Alex Chau | bbe6cf3 | 2021-10-12 15:52:36 +0100 | [diff] [blame^] | 3 | import static com.android.launcher3.ResourceUtils.INVALID_RESOURCE_HANDLE; |
Sunny Goyal | a314d5a | 2020-05-20 20:34:04 -0700 | [diff] [blame] | 4 | import static com.android.launcher3.config.FeatureFlags.SEPARATE_RECENTS_ACTIVITY; |
Sunny Goyal | ed2d2bc | 2018-04-19 12:34:43 -0700 | [diff] [blame] | 5 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 6 | import android.annotation.TargetApi; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 7 | import android.content.Context; |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 8 | import android.content.res.Resources; |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 9 | import android.graphics.Canvas; |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 10 | import android.graphics.Insets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 11 | import android.graphics.Rect; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 12 | import android.os.Build; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 13 | import android.util.AttributeSet; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 14 | import android.view.ViewDebug; |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 15 | import android.view.WindowInsets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 16 | |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 17 | import androidx.annotation.RequiresApi; |
| 18 | |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 19 | import com.android.launcher3.graphics.SysUiScrim; |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 20 | import com.android.launcher3.statemanager.StatefulActivity; |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 21 | import com.android.launcher3.uioverrides.ApiWrapper; |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 22 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 23 | import java.util.Collections; |
| 24 | import java.util.List; |
| 25 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 26 | public class LauncherRootView extends InsettableFrameLayout { |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 27 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 28 | private final Rect mTempRect = new Rect(); |
| 29 | |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 30 | private final StatefulActivity mActivity; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 31 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 32 | @ViewDebug.ExportedProperty(category = "launcher") |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 33 | private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT = |
| 34 | Collections.singletonList(new Rect()); |
| 35 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 36 | private WindowStateListener mWindowStateListener; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 37 | @ViewDebug.ExportedProperty(category = "launcher") |
| 38 | private boolean mDisallowBackGesture; |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 39 | @ViewDebug.ExportedProperty(category = "launcher") |
| 40 | private boolean mForceHideBackArrow; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 41 | |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 42 | private final SysUiScrim mSysUiScrim; |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 43 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 44 | public LauncherRootView(Context context, AttributeSet attrs) { |
| 45 | super(context, attrs); |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 46 | mActivity = StatefulActivity.fromContext(context); |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 47 | mSysUiScrim = new SysUiScrim(this); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 50 | private void handleSystemWindowInsets(Rect insets) { |
Tony Wickham | 145b7fd | 2021-03-11 10:03:12 -0800 | [diff] [blame] | 51 | // Update device profile before notifying the children. |
Tony Wickham | 21970cc | 2021-09-15 15:44:05 -0700 | [diff] [blame] | 52 | mActivity.getDeviceProfile().updateInsets(insets); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 53 | boolean resetState = !insets.equals(mInsets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 54 | setInsets(insets); |
| 55 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 56 | if (resetState) { |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 57 | mActivity.getStateManager().reapplyState(true /* cancelCurrentAnimation */); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 58 | } |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 59 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 60 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 63 | if (Utilities.ATLEAST_R) { |
| 64 | insets = updateInsetsDueToTaskbar(insets); |
| 65 | Insets systemWindowInsets = insets.getInsetsIgnoringVisibility( |
| 66 | WindowInsets.Type.systemBars() | WindowInsets.Type.displayCutout()); |
| 67 | mTempRect.set(systemWindowInsets.left, systemWindowInsets.top, systemWindowInsets.right, |
| 68 | systemWindowInsets.bottom); |
| 69 | } else { |
| 70 | mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), |
| 71 | insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom()); |
| 72 | } |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 73 | handleSystemWindowInsets(mTempRect); |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 74 | return insets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 75 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 76 | |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Taskbar provides nav bar and tappable insets. However, taskbar is not attached immediately, |
| 79 | * and can be destroyed and recreated. Thus, instead of relying on taskbar being present to |
| 80 | * get its insets, we calculate them ourselves so they are stable regardless of whether taskbar |
| 81 | * is currently attached. |
| 82 | * |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 83 | * @param oldInsets The system-provided insets, which we are modifying. |
| 84 | * @return The updated insets. |
| 85 | */ |
| 86 | @RequiresApi(api = Build.VERSION_CODES.R) |
| 87 | private WindowInsets updateInsetsDueToTaskbar(WindowInsets oldInsets) { |
| 88 | if (!ApiWrapper.TASKBAR_DRAWN_IN_PROCESS) { |
| 89 | // 3P launchers based on Launcher3 should still be inset like normal. |
| 90 | return oldInsets; |
| 91 | } |
| 92 | |
| 93 | WindowInsets.Builder updatedInsetsBuilder = new WindowInsets.Builder(oldInsets); |
| 94 | |
| 95 | DeviceProfile dp = mActivity.getDeviceProfile(); |
| 96 | Resources resources = getResources(); |
| 97 | |
| 98 | Insets oldNavInsets = oldInsets.getInsets(WindowInsets.Type.navigationBars()); |
| 99 | Rect newNavInsets = new Rect(oldNavInsets.left, oldNavInsets.top, oldNavInsets.right, |
| 100 | oldNavInsets.bottom); |
Tony Wickham | 21970cc | 2021-09-15 15:44:05 -0700 | [diff] [blame] | 101 | |
| 102 | if (dp.isLandscape) { |
Alex Chau | bbe6cf3 | 2021-10-12 15:52:36 +0100 | [diff] [blame^] | 103 | boolean isGesturalMode = ResourceUtils.getIntegerByName( |
| 104 | "config_navBarInteractionMode", |
| 105 | resources, |
| 106 | INVALID_RESOURCE_HANDLE) == 2; |
| 107 | if (dp.isTablet || isGesturalMode) { |
Tony Wickham | 7eb5b53 | 2021-09-17 17:08:46 -0700 | [diff] [blame] | 108 | newNavInsets.bottom = ResourceUtils.getNavbarSize( |
| 109 | "navigation_bar_height_landscape", resources); |
| 110 | } else { |
| 111 | int navWidth = ResourceUtils.getNavbarSize("navigation_bar_width", resources); |
| 112 | if (dp.isSeascape()) { |
| 113 | newNavInsets.left = navWidth; |
| 114 | } else { |
| 115 | newNavInsets.right = navWidth; |
| 116 | } |
| 117 | } |
| 118 | } else { |
| 119 | newNavInsets.bottom = ResourceUtils.getNavbarSize("navigation_bar_height", resources); |
| 120 | } |
| 121 | updatedInsetsBuilder.setInsets(WindowInsets.Type.navigationBars(), Insets.of(newNavInsets)); |
| 122 | updatedInsetsBuilder.setInsetsIgnoringVisibility(WindowInsets.Type.navigationBars(), |
| 123 | Insets.of(newNavInsets)); |
| 124 | |
| 125 | mActivity.updateWindowInsets(updatedInsetsBuilder, oldInsets); |
| 126 | |
| 127 | return updatedInsetsBuilder.build(); |
| 128 | } |
| 129 | |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 130 | @Override |
| 131 | public void setInsets(Rect insets) { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 132 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 133 | // modifying child layout params. |
| 134 | if (!insets.equals(mInsets)) { |
| 135 | super.setInsets(insets); |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 136 | mSysUiScrim.onInsetsChanged(insets); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 137 | } |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 140 | public void dispatchInsets() { |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 141 | mActivity.getDeviceProfile().updateInsets(mInsets); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 142 | super.setInsets(mInsets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 145 | public void setWindowStateListener(WindowStateListener listener) { |
| 146 | mWindowStateListener = listener; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 151 | super.onWindowFocusChanged(hasWindowFocus); |
| 152 | if (mWindowStateListener != null) { |
| 153 | mWindowStateListener.onWindowFocusChanged(hasWindowFocus); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | protected void onWindowVisibilityChanged(int visibility) { |
| 159 | super.onWindowVisibilityChanged(visibility); |
| 160 | if (mWindowStateListener != null) { |
| 161 | mWindowStateListener.onWindowVisibilityChanged(visibility); |
| 162 | } |
| 163 | } |
| 164 | |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 165 | @Override |
| 166 | protected void dispatchDraw(Canvas canvas) { |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 167 | mSysUiScrim.draw(canvas); |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 168 | super.dispatchDraw(canvas); |
| 169 | } |
| 170 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 171 | @Override |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 172 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 173 | super.onLayout(changed, l, t, r, b); |
| 174 | SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b); |
| 175 | setDisallowBackGesture(mDisallowBackGesture); |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 176 | mSysUiScrim.setSize(r - l, b - t); |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @TargetApi(Build.VERSION_CODES.Q) |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 180 | public void setForceHideBackArrow(boolean forceHideBackArrow) { |
| 181 | this.mForceHideBackArrow = forceHideBackArrow; |
| 182 | setDisallowBackGesture(mDisallowBackGesture); |
| 183 | } |
| 184 | |
| 185 | @TargetApi(Build.VERSION_CODES.Q) |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 186 | public void setDisallowBackGesture(boolean disallowBackGesture) { |
Sunny Goyal | a314d5a | 2020-05-20 20:34:04 -0700 | [diff] [blame] | 187 | if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) { |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | mDisallowBackGesture = disallowBackGesture; |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 191 | setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture) |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 192 | ? SYSTEM_GESTURE_EXCLUSION_RECT |
| 193 | : Collections.emptyList()); |
| 194 | } |
| 195 | |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 196 | public SysUiScrim getSysUiScrim() { |
| 197 | return mSysUiScrim; |
| 198 | } |
| 199 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 200 | public interface WindowStateListener { |
| 201 | |
| 202 | void onWindowFocusChanged(boolean hasFocus); |
| 203 | |
| 204 | void onWindowVisibilityChanged(int visibility); |
| 205 | } |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 206 | } |