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