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 | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 18 | private boolean mDrawRightInsetBar; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame^] | 19 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 20 | private int mRightInsetBarWidth; |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 21 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 22 | private View mAlignedView; |
| 23 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 24 | public LauncherRootView(Context context, AttributeSet attrs) { |
| 25 | super(context, attrs); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 26 | |
| 27 | mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 28 | mOpaquePaint.setColor(Color.BLACK); |
| 29 | mOpaquePaint.setStyle(Paint.Style.FILL); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | @Override |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 33 | protected void onFinishInflate() { |
| 34 | if (getChildCount() > 0) { |
| 35 | // LauncherRootView contains only one child, which should be aligned |
| 36 | // based on the horizontal insets. |
| 37 | mAlignedView = getChildAt(0); |
| 38 | } |
| 39 | super.onFinishInflate(); |
| 40 | } |
| 41 | |
| 42 | @TargetApi(23) |
| 43 | @Override |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 44 | protected boolean fitSystemWindows(Rect insets) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 45 | mDrawRightInsetBar = insets.right > 0 && |
| 46 | (!Utilities.ATLEAST_MARSHMALLOW || |
| 47 | getContext().getSystemService(ActivityManager.class).isLowRamDevice()); |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 48 | mRightInsetBarWidth = insets.right; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 49 | setInsets(mDrawRightInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets); |
| 50 | |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 51 | if (mAlignedView != null && mDrawRightInsetBar) { |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 52 | // Apply margins on aligned view to handle left/right insets. |
| 53 | MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams(); |
| 54 | if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) { |
| 55 | lp.leftMargin = insets.left; |
| 56 | lp.rightMargin = insets.right; |
| 57 | mAlignedView.setLayoutParams(lp); |
| 58 | } |
| 59 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 60 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 61 | return true; // I'll take it from here |
| 62 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | protected void dispatchDraw(Canvas canvas) { |
| 66 | super.dispatchDraw(canvas); |
| 67 | |
| 68 | // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque. |
| 69 | if (mDrawRightInsetBar) { |
| 70 | int width = getWidth(); |
Sunny Goyal | ecdc24f | 2016-01-12 10:35:32 -0800 | [diff] [blame] | 71 | canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint); |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 74 | } |