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; |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 11 | import android.graphics.Insets; |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 12 | import android.graphics.Paint; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 13 | import android.graphics.Rect; |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 14 | import android.graphics.RectF; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 15 | import android.os.Build; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 16 | import android.util.AttributeSet; |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 17 | import android.view.MotionEvent; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 18 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 19 | import android.view.ViewDebug; |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 20 | import android.view.WindowInsets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 21 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 25 | public class LauncherRootView extends InsettableFrameLayout { |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 26 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 27 | private final Launcher mLauncher; |
| 28 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 29 | private final Paint mOpaquePaint; |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 30 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 31 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 32 | private final Rect mConsumedInsets = new Rect(); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 33 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 34 | @ViewDebug.ExportedProperty(category = "launcher") |
| 35 | private final RectF mTouchExcludeRegion = new RectF(); |
| 36 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 37 | @ViewDebug.ExportedProperty(category = "launcher") |
| 38 | private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT = |
| 39 | Collections.singletonList(new Rect()); |
| 40 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 41 | private View mAlignedView; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 42 | private WindowStateListener mWindowStateListener; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 43 | @ViewDebug.ExportedProperty(category = "launcher") |
| 44 | private boolean mDisallowBackGesture; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 45 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 46 | public LauncherRootView(Context context, AttributeSet attrs) { |
| 47 | super(context, attrs); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 48 | |
| 49 | mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 50 | mOpaquePaint.setColor(Color.BLACK); |
| 51 | mOpaquePaint.setStyle(Paint.Style.FILL); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 52 | |
| 53 | mLauncher = Launcher.getLauncher(context); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 57 | protected void onFinishInflate() { |
| 58 | if (getChildCount() > 0) { |
| 59 | // LauncherRootView contains only one child, which should be aligned |
| 60 | // based on the horizontal insets. |
| 61 | mAlignedView = getChildAt(0); |
| 62 | } |
| 63 | super.onFinishInflate(); |
| 64 | } |
| 65 | |
| 66 | @TargetApi(23) |
| 67 | @Override |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 68 | protected boolean fitSystemWindows(Rect insets) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 69 | mConsumedInsets.setEmpty(); |
| 70 | boolean drawInsetBar = false; |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 71 | if (mLauncher.isInMultiWindowMode() |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 72 | && (insets.left > 0 || insets.right > 0 || insets.bottom > 0)) { |
| 73 | mConsumedInsets.left = insets.left; |
| 74 | mConsumedInsets.right = insets.right; |
| 75 | mConsumedInsets.bottom = insets.bottom; |
| 76 | insets = new Rect(0, insets.top, 0, 0); |
| 77 | drawInsetBar = true; |
| 78 | } else if ((insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 79 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 80 | mConsumedInsets.left = insets.left; |
| 81 | mConsumedInsets.right = insets.right; |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 82 | insets = new Rect(0, insets.top, 0, insets.bottom); |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 83 | drawInsetBar = true; |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 84 | } |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 85 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 86 | mLauncher.getSystemUiController().updateUiState( |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 87 | UI_STATE_ROOT_VIEW, drawInsetBar ? FLAG_DARK_NAV : 0); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 88 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 89 | // Update device profile before notifying th children. |
| 90 | mLauncher.getDeviceProfile().updateInsets(insets); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 91 | boolean resetState = !insets.equals(mInsets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 92 | setInsets(insets); |
| 93 | |
| 94 | if (mAlignedView != null) { |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 95 | // Apply margins on aligned view to handle consumed insets. |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 96 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
Sunny Goyal | 9001b10 | 2018-05-07 11:09:13 -0700 | [diff] [blame] | 97 | if (lp.leftMargin != mConsumedInsets.left || lp.rightMargin != mConsumedInsets.right || |
| 98 | lp.bottomMargin != mConsumedInsets.bottom) { |
| 99 | lp.leftMargin = mConsumedInsets.left; |
| 100 | lp.rightMargin = mConsumedInsets.right; |
| 101 | lp.topMargin = mConsumedInsets.top; |
| 102 | lp.bottomMargin = mConsumedInsets.bottom; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 103 | mAlignedView.setLayoutParams(lp); |
| 104 | } |
| 105 | } |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 106 | if (resetState) { |
Sunny Goyal | ed2d2bc | 2018-04-19 12:34:43 -0700 | [diff] [blame] | 107 | mLauncher.getStateManager().reapplyState(true /* cancelCurrentAnimation */); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 108 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 109 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 110 | return true; // I'll take it from here |
| 111 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 112 | |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public void setInsets(Rect insets) { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 115 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 116 | // modifying child layout params. |
| 117 | if (!insets.equals(mInsets)) { |
| 118 | super.setInsets(insets); |
| 119 | } |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 122 | public void dispatchInsets() { |
Sunny Goyal | 3477804 | 2018-04-11 16:31:31 -0700 | [diff] [blame] | 123 | mLauncher.getDeviceProfile().updateInsets(mInsets); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 124 | super.setInsets(mInsets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 127 | @Override |
| 128 | protected void dispatchDraw(Canvas canvas) { |
| 129 | super.dispatchDraw(canvas); |
| 130 | |
| 131 | // 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] | 132 | if (mConsumedInsets.right > 0) { |
| 133 | int width = getWidth(); |
| 134 | canvas.drawRect(width - mConsumedInsets.right, 0, width, getHeight(), mOpaquePaint); |
| 135 | } |
| 136 | if (mConsumedInsets.left > 0) { |
| 137 | canvas.drawRect(0, 0, mConsumedInsets.left, getHeight(), mOpaquePaint); |
| 138 | } |
| 139 | if (mConsumedInsets.bottom > 0) { |
| 140 | int height = getHeight(); |
| 141 | canvas.drawRect(0, height - mConsumedInsets.bottom, getWidth(), height, mOpaquePaint); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 144 | |
| 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 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 167 | if (Utilities.ATLEAST_Q) { |
| 168 | Insets gestureInsets = insets.getMandatorySystemGestureInsets(); |
| 169 | mTouchExcludeRegion.set(gestureInsets.left, gestureInsets.top, |
| 170 | gestureInsets.right, gestureInsets.bottom); |
| 171 | } |
| 172 | return super.dispatchApplyWindowInsets(insets); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 177 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 178 | float x = ev.getX(); |
| 179 | float y = ev.getY(); |
| 180 | if (y < mTouchExcludeRegion.top |
| 181 | || x < mTouchExcludeRegion.left |
| 182 | || x > (getWidth() - mTouchExcludeRegion.right) |
| 183 | || y > (getHeight() - mTouchExcludeRegion.bottom)) { |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | return super.dispatchTouchEvent(ev); |
| 188 | } |
| 189 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 190 | @Override |
| 191 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 192 | super.onLayout(changed, l, t, r, b); |
| 193 | SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b); |
| 194 | setDisallowBackGesture(mDisallowBackGesture); |
| 195 | } |
| 196 | |
| 197 | @TargetApi(Build.VERSION_CODES.Q) |
| 198 | public void setDisallowBackGesture(boolean disallowBackGesture) { |
| 199 | if (!Utilities.ATLEAST_Q) { |
| 200 | return; |
| 201 | } |
| 202 | mDisallowBackGesture = disallowBackGesture; |
| 203 | setSystemGestureExclusionRects(mDisallowBackGesture |
| 204 | ? SYSTEM_GESTURE_EXCLUSION_RECT |
| 205 | : Collections.emptyList()); |
| 206 | } |
| 207 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 208 | public interface WindowStateListener { |
| 209 | |
| 210 | void onWindowFocusChanged(boolean hasFocus); |
| 211 | |
| 212 | void onWindowVisibilityChanged(int visibility); |
| 213 | } |
vadimt | bd0f116 | 2019-04-23 18:09:09 -0700 | [diff] [blame] | 214 | |
| 215 | @Override |
| 216 | public void requestLayout() { |
| 217 | super.requestLayout(); |
| 218 | if (com.android.launcher3.TestProtocol.sDebugTracing) { |
| 219 | android.util.Log.d(com.android.launcher3.TestProtocol.NO_DRAG_TAG, |
| 220 | "requestLayout @ " + android.util.Log.getStackTraceString( |
| 221 | new Throwable())); |
| 222 | } |
| 223 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 224 | } |