1 package com.android.launcher3; 2 3 import android.annotation.TargetApi; 4 import android.app.ActivityManager; 5 import android.content.Context; 6 import android.graphics.Canvas; 7 import android.graphics.Color; 8 import android.graphics.Paint; 9 import android.graphics.Rect; 10 import android.util.AttributeSet; 11 import android.view.View; 12 import android.view.ViewDebug; 13 14 public class LauncherRootView extends InsettableFrameLayout { 15 16 private final Paint mOpaquePaint; 17 @ViewDebug.ExportedProperty(category = "launcher") 18 private boolean mDrawSideInsetBar; 19 @ViewDebug.ExportedProperty(category = "launcher") 20 private int mLeftInsetBarWidth; 21 @ViewDebug.ExportedProperty(category = "launcher") 22 private int mRightInsetBarWidth; 23 24 private View mAlignedView; 25 LauncherRootView(Context context, AttributeSet attrs)26 public LauncherRootView(Context context, AttributeSet attrs) { 27 super(context, attrs); 28 29 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 30 mOpaquePaint.setColor(Color.BLACK); 31 mOpaquePaint.setStyle(Paint.Style.FILL); 32 } 33 34 @Override onFinishInflate()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 fitSystemWindows(Rect insets)46 protected boolean fitSystemWindows(Rect insets) { 47 boolean rawInsetsChanged = !mInsets.equals(insets); 48 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) && 49 (!Utilities.ATLEAST_MARSHMALLOW || 50 getContext().getSystemService(ActivityManager.class).isLowRamDevice()); 51 mRightInsetBarWidth = insets.right; 52 mLeftInsetBarWidth = insets.left; 53 setInsets(mDrawSideInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets); 54 55 if (mAlignedView != null && mDrawSideInsetBar) { 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 } 64 65 if (rawInsetsChanged) { 66 // Update the grid again 67 Launcher launcher = Launcher.getLauncher(getContext()); 68 launcher.onInsetsChanged(insets); 69 } 70 71 return true; // I'll take it from here 72 } 73 74 @Override dispatchDraw(Canvas canvas)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. 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 } 87 } 88 } 89 }