1a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohenpackage com.android.launcher3;
2a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen
39326461652c36c2ddd888d1452cf7f075a391868Sunny Goyalimport android.annotation.TargetApi;
49326461652c36c2ddd888d1452cf7f075a391868Sunny Goyalimport android.app.ActivityManager;
5a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohenimport android.content.Context;
60abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyalimport android.graphics.Canvas;
70abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyalimport android.graphics.Color;
80abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyalimport android.graphics.Paint;
9a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohenimport android.graphics.Rect;
10a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohenimport android.util.AttributeSet;
119326461652c36c2ddd888d1452cf7f075a391868Sunny Goyalimport android.view.View;
124ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyalimport android.view.ViewDebug;
13a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen
14906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyalimport static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
15906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyalimport static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
16906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal
17a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohenpublic class LauncherRootView extends InsettableFrameLayout {
180abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
190abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal    private final Paint mOpaquePaint;
204ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
216c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal    private boolean mDrawSideInsetBar;
226c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
236c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal    private int mLeftInsetBarWidth;
244ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
25ecdc24f6f667319db56a16dd7d1efda16dfe662eSunny Goyal    private int mRightInsetBarWidth;
260abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
279326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal    private View mAlignedView;
289326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal
29a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen    public LauncherRootView(Context context, AttributeSet attrs) {
30a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen        super(context, attrs);
310abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
320abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
330abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        mOpaquePaint.setColor(Color.BLACK);
340abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        mOpaquePaint.setStyle(Paint.Style.FILL);
35a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen    }
36a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen
37a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen    @Override
389326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal    protected void onFinishInflate() {
399326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal        if (getChildCount() > 0) {
409326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            // LauncherRootView contains only one child, which should be aligned
419326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            // based on the horizontal insets.
429326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            mAlignedView = getChildAt(0);
439326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal        }
449326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal        super.onFinishInflate();
459326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal    }
469326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal
479326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal    @TargetApi(23)
489326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal    @Override
49a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen    protected boolean fitSystemWindows(Rect insets) {
506c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal        mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
519326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal                (!Utilities.ATLEAST_MARSHMALLOW ||
52906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal                        getContext().getSystemService(ActivityManager.class).isLowRamDevice());
53906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        if (mDrawSideInsetBar) {
54906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal            mLeftInsetBarWidth = insets.left;
55906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal            mRightInsetBarWidth = insets.right;
56906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal            insets = new Rect(0, insets.top, 0, insets.bottom);
57906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        } else {
58906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal            mLeftInsetBarWidth = mRightInsetBarWidth = 0;
59906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        }
60906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        Launcher.getLauncher(getContext()).getSystemUiController().updateUiState(
61906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal                UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0);
62906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal
63906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        boolean rawInsetsChanged = !mInsets.equals(insets);
64906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        setInsets(insets);
659326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal
66906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal        if (mAlignedView != null) {
679326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            // Apply margins on aligned view to handle left/right insets.
689326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
69906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal            if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) {
70906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal                lp.leftMargin = mLeftInsetBarWidth;
71906c6b2f6c6eab68c46a8c32fec6eb82bd057cb2Sunny Goyal                lp.rightMargin = mRightInsetBarWidth;
729326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal                mAlignedView.setLayoutParams(lp);
739326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal            }
749326461652c36c2ddd888d1452cf7f075a391868Sunny Goyal        }
750abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
761f06427266c0cb5de4561fc7c620ff542f625300Winson        if (rawInsetsChanged) {
771f06427266c0cb5de4561fc7c620ff542f625300Winson            // Update the grid again
781f06427266c0cb5de4561fc7c620ff542f625300Winson            Launcher launcher = Launcher.getLauncher(getContext());
791f06427266c0cb5de4561fc7c620ff542f625300Winson            launcher.onInsetsChanged(insets);
801f06427266c0cb5de4561fc7c620ff542f625300Winson        }
811f06427266c0cb5de4561fc7c620ff542f625300Winson
82a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen        return true; // I'll take it from here
83a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen    }
840abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
850abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal    @Override
860abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal    protected void dispatchDraw(Canvas canvas) {
870abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        super.dispatchDraw(canvas);
880abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal
890abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
906c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal        if (mDrawSideInsetBar) {
916c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal            if (mRightInsetBarWidth > 0) {
926c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal                int width = getWidth();
936c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal                canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
946c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal            }
956c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal            if (mLeftInsetBarWidth > 0) {
966c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal                canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
976c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal            }
980abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal        }
990abb36f6920733f813e22ad984bb7d48f0924698Sunny Goyal    }
100a6d0492e5f0dd4e1b43d4034a770241dc65f8f8cAdam Cohen}