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