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