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) { |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame^] | 47 | mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) && |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 48 | (!Utilities.ATLEAST_MARSHMALLOW || |
| 49 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()); |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 50 | mRightInsetBarWidth = insets.right; |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame^] | 51 | mLeftInsetBarWidth = insets.left; |
| 52 | setInsets(mDrawSideInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets); |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 53 | |
Sunny Goyal | 6c2975e | 2016-07-06 09:47:56 -0700 | [diff] [blame^] | 54 | if (mAlignedView != null && mDrawSideInsetBar) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 55 | // Apply margins on aligned view to handle left/right insets. |
| 56 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
| 57 | if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) { |
| 58 | lp.leftMargin = insets.left; |
| 59 | lp.rightMargin = insets.right; |
| 60 | mAlignedView.setLayoutParams(lp); |
| 61 | } |
| 62 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 63 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 64 | return true; // I'll take it from here |
| 65 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | protected void dispatchDraw(Canvas canvas) { |
| 69 | super.dispatchDraw(canvas); |
| 70 | |
| 71 | // 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^] | 72 | if (mDrawSideInsetBar) { |
| 73 | if (mRightInsetBarWidth > 0) { |
| 74 | int width = getWidth(); |
| 75 | canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint); |
| 76 | } |
| 77 | if (mLeftInsetBarWidth > 0) { |
| 78 | canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint); |
| 79 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 82 | } |