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