Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | ed2d2bc | 2018-04-19 12:34:43 -0700 | [diff] [blame] | 3 | import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV; |
| 4 | import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW; |
| 5 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 6 | import android.annotation.TargetApi; |
| 7 | import android.app.ActivityManager; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 8 | import android.content.Context; |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 9 | import android.graphics.Canvas; |
| 10 | import android.graphics.Color; |
| 11 | import android.graphics.Paint; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 12 | import android.graphics.Rect; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 13 | import android.os.Build; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 14 | import android.util.AttributeSet; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 15 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 16 | import android.view.ViewDebug; |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 17 | import android.view.WindowInsets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 18 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 22 | public class LauncherRootView extends InsettableFrameLayout { |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 23 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 24 | private final Rect mTempRect = new Rect(); |
| 25 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 26 | private final Launcher mLauncher; |
| 27 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 28 | private final Paint mOpaquePaint; |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 29 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 30 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 31 | private final Rect mConsumedInsets = new Rect(); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 32 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 33 | @ViewDebug.ExportedProperty(category = "launcher") |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 34 | private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT = |
| 35 | Collections.singletonList(new Rect()); |
| 36 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 37 | private View mAlignedView; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 38 | private WindowStateListener mWindowStateListener; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 39 | @ViewDebug.ExportedProperty(category = "launcher") |
| 40 | private boolean mDisallowBackGesture; |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 41 | @ViewDebug.ExportedProperty(category = "launcher") |
| 42 | private boolean mForceHideBackArrow; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -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 | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 46 | |
| 47 | mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 48 | mOpaquePaint.setColor(Color.BLACK); |
| 49 | mOpaquePaint.setStyle(Paint.Style.FILL); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 50 | |
| 51 | mLauncher = Launcher.getLauncher(context); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 55 | protected void onFinishInflate() { |
| 56 | if (getChildCount() > 0) { |
| 57 | // LauncherRootView contains only one child, which should be aligned |
| 58 | // based on the horizontal insets. |
| 59 | mAlignedView = getChildAt(0); |
| 60 | } |
| 61 | super.onFinishInflate(); |
| 62 | } |
| 63 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 64 | private void handleSystemWindowInsets(Rect insets) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 65 | mConsumedInsets.setEmpty(); |
| 66 | boolean drawInsetBar = false; |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 67 | if (mLauncher.isInMultiWindowMode() |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 68 | && (insets.left > 0 || insets.right > 0 || insets.bottom > 0)) { |
| 69 | mConsumedInsets.left = insets.left; |
| 70 | mConsumedInsets.right = insets.right; |
| 71 | mConsumedInsets.bottom = insets.bottom; |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 72 | insets.set(0, insets.top, 0, 0); |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 73 | drawInsetBar = true; |
| 74 | } else if ((insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 75 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 76 | mConsumedInsets.left = insets.left; |
| 77 | mConsumedInsets.right = insets.right; |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 78 | insets.set(0, insets.top, 0, insets.bottom); |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 79 | drawInsetBar = true; |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 80 | } |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 81 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 82 | mLauncher.getSystemUiController().updateUiState( |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 83 | UI_STATE_ROOT_VIEW, drawInsetBar ? FLAG_DARK_NAV : 0); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 84 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 85 | // Update device profile before notifying th children. |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 86 | mLauncher.updateInsets(insets); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 87 | boolean resetState = !insets.equals(mInsets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 88 | setInsets(insets); |
| 89 | |
| 90 | if (mAlignedView != null) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 91 | // Apply margins on aligned view to handle consumed insets. |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 92 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 93 | if (lp.leftMargin != mConsumedInsets.left || lp.rightMargin != mConsumedInsets.right || |
| 94 | lp.bottomMargin != mConsumedInsets.bottom) { |
| 95 | lp.leftMargin = mConsumedInsets.left; |
| 96 | lp.rightMargin = mConsumedInsets.right; |
| 97 | lp.topMargin = mConsumedInsets.top; |
| 98 | lp.bottomMargin = mConsumedInsets.bottom; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 99 | mAlignedView.setLayoutParams(lp); |
| 100 | } |
| 101 | } |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 102 | if (resetState) { |
Sunny Goyal | ed2d2bc | 2018-04-19 12:34:43 -0700 | [diff] [blame] | 103 | mLauncher.getStateManager().reapplyState(true /* cancelCurrentAnimation */); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 104 | } |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 105 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 106 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
| 109 | mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), |
| 110 | insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom()); |
| 111 | handleSystemWindowInsets(mTempRect); |
| 112 | if (Utilities.ATLEAST_Q) { |
| 113 | return insets.inset(mConsumedInsets.left, mConsumedInsets.top, |
| 114 | mConsumedInsets.right, mConsumedInsets.bottom); |
| 115 | } else { |
| 116 | return insets.replaceSystemWindowInsets(mTempRect); |
| 117 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 118 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 119 | |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public void setInsets(Rect insets) { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 122 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 123 | // modifying child layout params. |
| 124 | if (!insets.equals(mInsets)) { |
| 125 | super.setInsets(insets); |
| 126 | } |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 129 | public void dispatchInsets() { |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 130 | mLauncher.updateInsets(mInsets); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 131 | super.setInsets(mInsets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 134 | @Override |
| 135 | protected void dispatchDraw(Canvas canvas) { |
| 136 | super.dispatchDraw(canvas); |
| 137 | |
| 138 | // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque. |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 139 | if (mConsumedInsets.right > 0) { |
| 140 | int width = getWidth(); |
| 141 | canvas.drawRect(width - mConsumedInsets.right, 0, width, getHeight(), mOpaquePaint); |
| 142 | } |
| 143 | if (mConsumedInsets.left > 0) { |
| 144 | canvas.drawRect(0, 0, mConsumedInsets.left, getHeight(), mOpaquePaint); |
| 145 | } |
| 146 | if (mConsumedInsets.bottom > 0) { |
| 147 | int height = getHeight(); |
| 148 | canvas.drawRect(0, height - mConsumedInsets.bottom, getWidth(), height, mOpaquePaint); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 151 | |
| 152 | public void setWindowStateListener(WindowStateListener listener) { |
| 153 | mWindowStateListener = listener; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 158 | super.onWindowFocusChanged(hasWindowFocus); |
| 159 | if (mWindowStateListener != null) { |
| 160 | mWindowStateListener.onWindowFocusChanged(hasWindowFocus); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected void onWindowVisibilityChanged(int visibility) { |
| 166 | super.onWindowVisibilityChanged(visibility); |
| 167 | if (mWindowStateListener != null) { |
| 168 | mWindowStateListener.onWindowVisibilityChanged(visibility); |
| 169 | } |
| 170 | } |
| 171 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 172 | @Override |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 173 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 174 | super.onLayout(changed, l, t, r, b); |
| 175 | SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b); |
| 176 | setDisallowBackGesture(mDisallowBackGesture); |
| 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) { |
| 187 | if (!Utilities.ATLEAST_Q) { |
| 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 | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 196 | public interface WindowStateListener { |
| 197 | |
| 198 | void onWindowFocusChanged(boolean hasFocus); |
| 199 | |
| 200 | void onWindowVisibilityChanged(int visibility); |
| 201 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 202 | } |