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 | |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame^] | 14 | import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV; |
| 15 | import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW; |
| 16 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 17 | public class LauncherRootView extends InsettableFrameLayout { |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 18 | |
| 19 | private final Paint mOpaquePaint; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 20 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 21 | private boolean mDrawSideInsetBar; |
| 22 | @ViewDebug.ExportedProperty(category = "launcher") |
| 23 | private int mLeftInsetBarWidth; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 24 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 25 | private int mRightInsetBarWidth; |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 26 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 27 | private View mAlignedView; |
| 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); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Override |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 38 | protected void onFinishInflate() { |
| 39 | if (getChildCount() > 0) { |
| 40 | // LauncherRootView contains only one child, which should be aligned |
| 41 | // based on the horizontal insets. |
| 42 | mAlignedView = getChildAt(0); |
| 43 | } |
| 44 | super.onFinishInflate(); |
| 45 | } |
| 46 | |
| 47 | @TargetApi(23) |
| 48 | @Override |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 49 | protected boolean fitSystemWindows(Rect insets) { |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 50 | mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 51 | (!Utilities.ATLEAST_MARSHMALLOW || |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame^] | 52 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()); |
| 53 | if (mDrawSideInsetBar) { |
| 54 | mLeftInsetBarWidth = insets.left; |
| 55 | mRightInsetBarWidth = insets.right; |
| 56 | insets = new Rect(0, insets.top, 0, insets.bottom); |
| 57 | } else { |
| 58 | mLeftInsetBarWidth = mRightInsetBarWidth = 0; |
| 59 | } |
| 60 | Launcher.getLauncher(getContext()).getSystemUiController().updateUiState( |
| 61 | UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 62 | |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame^] | 63 | boolean rawInsetsChanged = !mInsets.equals(insets); |
| 64 | setInsets(insets); |
| 65 | |
| 66 | if (mAlignedView != null) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 67 | // Apply margins on aligned view to handle left/right insets. |
| 68 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame^] | 69 | if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) { |
| 70 | lp.leftMargin = mLeftInsetBarWidth; |
| 71 | lp.rightMargin = mRightInsetBarWidth; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 72 | mAlignedView.setLayoutParams(lp); |
| 73 | } |
| 74 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 75 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 76 | if (rawInsetsChanged) { |
| 77 | // Update the grid again |
| 78 | Launcher launcher = Launcher.getLauncher(getContext()); |
| 79 | launcher.onInsetsChanged(insets); |
| 80 | } |
| 81 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 82 | return true; // I'll take it from here |
| 83 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | protected void dispatchDraw(Canvas canvas) { |
| 87 | super.dispatchDraw(canvas); |
| 88 | |
| 89 | // 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] | 90 | if (mDrawSideInsetBar) { |
| 91 | if (mRightInsetBarWidth > 0) { |
| 92 | int width = getWidth(); |
| 93 | canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint); |
| 94 | } |
| 95 | if (mLeftInsetBarWidth > 0) { |
| 96 | canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint); |
| 97 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 100 | } |