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