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 | |
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 | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 22 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 23 | private boolean mDrawSideInsetBar; |
| 24 | @ViewDebug.ExportedProperty(category = "launcher") |
| 25 | private int mLeftInsetBarWidth; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 26 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 27 | private int mRightInsetBarWidth; |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 28 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 29 | private View mAlignedView; |
| 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 | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame] | 54 | mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 55 | (!Utilities.ATLEAST_MARSHMALLOW || |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 56 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()); |
| 57 | if (mDrawSideInsetBar) { |
| 58 | mLeftInsetBarWidth = insets.left; |
| 59 | mRightInsetBarWidth = insets.right; |
| 60 | insets = new Rect(0, insets.top, 0, insets.bottom); |
| 61 | } else { |
| 62 | mLeftInsetBarWidth = mRightInsetBarWidth = 0; |
| 63 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame^] | 64 | mLauncher.getSystemUiController().updateUiState( |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 65 | UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 66 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame^] | 67 | // Update device profile before notifying th children. |
| 68 | mLauncher.getDeviceProfile().updateInsets(insets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 69 | setInsets(insets); |
| 70 | |
| 71 | if (mAlignedView != null) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 72 | // Apply margins on aligned view to handle left/right insets. |
| 73 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 74 | if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) { |
| 75 | lp.leftMargin = mLeftInsetBarWidth; |
| 76 | lp.rightMargin = mRightInsetBarWidth; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 77 | mAlignedView.setLayoutParams(lp); |
| 78 | } |
| 79 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 80 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 81 | return true; // I'll take it from here |
| 82 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame^] | 84 | public void dispatchInsets() { |
| 85 | fitSystemWindows(mInsets); |
| 86 | } |
| 87 | |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 88 | @Override |
| 89 | protected void dispatchDraw(Canvas canvas) { |
| 90 | super.dispatchDraw(canvas); |
| 91 | |
| 92 | // 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] | 93 | if (mDrawSideInsetBar) { |
| 94 | if (mRightInsetBarWidth > 0) { |
| 95 | int width = getWidth(); |
| 96 | canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint); |
| 97 | } |
| 98 | if (mLeftInsetBarWidth > 0) { |
| 99 | canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint); |
| 100 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 103 | } |