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