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 
13 public class LauncherRootView extends InsettableFrameLayout {
14 
15     private final Paint mOpaquePaint;
16     private boolean mDrawRightInsetBar;
17     private int mRightInsetBarWidth;
18 
19     private View mAlignedView;
20 
LauncherRootView(Context context, AttributeSet attrs)21     public LauncherRootView(Context context, AttributeSet attrs) {
22         super(context, attrs);
23 
24         mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
25         mOpaquePaint.setColor(Color.BLACK);
26         mOpaquePaint.setStyle(Paint.Style.FILL);
27     }
28 
29     @Override
onFinishInflate()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
fitSystemWindows(Rect insets)41     protected boolean fitSystemWindows(Rect insets) {
42         mDrawRightInsetBar = insets.right > 0 &&
43                 (!Utilities.ATLEAST_MARSHMALLOW ||
44                 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
45         mRightInsetBarWidth = insets.right;
46         setInsets(mDrawRightInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
47 
48         if (mAlignedView != null && mDrawRightInsetBar) {
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         }
57 
58         return true; // I'll take it from here
59     }
60 
61     @Override
dispatchDraw(Canvas canvas)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();
68             canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
69         }
70     }
71 }