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