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; |
| 32 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 33 | public LauncherRootView(Context context, AttributeSet attrs) { |
| 34 | super(context, attrs); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 35 | |
| 36 | mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 37 | mOpaquePaint.setColor(Color.BLACK); |
| 38 | mOpaquePaint.setStyle(Paint.Style.FILL); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 39 | |
| 40 | mLauncher = Launcher.getLauncher(context); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 44 | protected void onFinishInflate() { |
| 45 | if (getChildCount() > 0) { |
| 46 | // LauncherRootView contains only one child, which should be aligned |
| 47 | // based on the horizontal insets. |
| 48 | mAlignedView = getChildAt(0); |
| 49 | } |
| 50 | super.onFinishInflate(); |
| 51 | } |
| 52 | |
| 53 | @TargetApi(23) |
| 54 | @Override |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 55 | protected boolean fitSystemWindows(Rect insets) { |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 56 | mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 57 | (!Utilities.ATLEAST_MARSHMALLOW || |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 58 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()); |
| 59 | if (mDrawSideInsetBar) { |
| 60 | mLeftInsetBarWidth = insets.left; |
| 61 | mRightInsetBarWidth = insets.right; |
| 62 | insets = new Rect(0, insets.top, 0, insets.bottom); |
| 63 | } else { |
| 64 | mLeftInsetBarWidth = mRightInsetBarWidth = 0; |
| 65 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 66 | mLauncher.getSystemUiController().updateUiState( |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 67 | UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 68 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 69 | // Update device profile before notifying th children. |
| 70 | mLauncher.getDeviceProfile().updateInsets(insets); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 71 | boolean resetState = !insets.equals(mInsets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 72 | setInsets(insets); |
| 73 | |
| 74 | if (mAlignedView != null) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 75 | // Apply margins on aligned view to handle left/right insets. |
| 76 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 77 | if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) { |
| 78 | lp.leftMargin = mLeftInsetBarWidth; |
| 79 | lp.rightMargin = mRightInsetBarWidth; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 80 | mAlignedView.setLayoutParams(lp); |
| 81 | } |
| 82 | } |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 83 | if (resetState) { |
| 84 | mLauncher.getStateManager().reapplyState(); |
| 85 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 86 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 87 | return true; // I'll take it from here |
| 88 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 89 | |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 90 | @Override |
| 91 | public void setInsets(Rect insets) { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame^] | 92 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 93 | // modifying child layout params. |
| 94 | if (!insets.equals(mInsets)) { |
| 95 | super.setInsets(insets); |
| 96 | } |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 97 | setBackground(insets.top == 0 ? null |
| 98 | : Themes.getAttrDrawable(getContext(), R.attr.workspaceStatusBarScrim)); |
| 99 | } |
| 100 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 101 | public void dispatchInsets() { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame^] | 102 | super.setInsets(mInsets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 105 | @Override |
| 106 | protected void dispatchDraw(Canvas canvas) { |
| 107 | super.dispatchDraw(canvas); |
| 108 | |
| 109 | // 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] | 110 | if (mDrawSideInsetBar) { |
| 111 | if (mRightInsetBarWidth > 0) { |
| 112 | int width = getWidth(); |
| 113 | canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint); |
| 114 | } |
| 115 | if (mLeftInsetBarWidth > 0) { |
| 116 | canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint); |
| 117 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 120 | } |