blob: a8143236333bacb9107ff8600dc684754720033b [file] [log] [blame]
Adam Cohena6d04922014-10-23 17:28:30 -07001package com.android.launcher3;
2
Sunny Goyal93264612015-11-23 11:47:50 -08003import android.annotation.TargetApi;
4import android.app.ActivityManager;
Adam Cohena6d04922014-10-23 17:28:30 -07005import android.content.Context;
Sunny Goyal0abb36f2015-06-23 10:53:59 -07006import android.graphics.Canvas;
7import android.graphics.Color;
8import android.graphics.Paint;
Adam Cohena6d04922014-10-23 17:28:30 -07009import android.graphics.Rect;
10import android.util.AttributeSet;
Sunny Goyal93264612015-11-23 11:47:50 -080011import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080012import android.view.ViewDebug;
Adam Cohena6d04922014-10-23 17:28:30 -070013
Sunny Goyal906c6b22017-08-18 00:35:52 -070014import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
15import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
16
Adam Cohena6d04922014-10-23 17:28:30 -070017public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070018
19 private final Paint mOpaquePaint;
Sunny Goyal4ffec482016-02-09 11:28:52 -080020 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal6c2975e2016-07-06 09:47:56 -070021 private boolean mDrawSideInsetBar;
22 @ViewDebug.ExportedProperty(category = "launcher")
23 private int mLeftInsetBarWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080024 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalecdc24f2016-01-12 10:35:32 -080025 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070026
Sunny Goyal93264612015-11-23 11:47:50 -080027 private View mAlignedView;
28
Adam Cohena6d04922014-10-23 17:28:30 -070029 public LauncherRootView(Context context, AttributeSet attrs) {
30 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070031
32 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
33 mOpaquePaint.setColor(Color.BLACK);
34 mOpaquePaint.setStyle(Paint.Style.FILL);
Adam Cohena6d04922014-10-23 17:28:30 -070035 }
36
37 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080038 protected void onFinishInflate() {
39 if (getChildCount() > 0) {
40 // LauncherRootView contains only one child, which should be aligned
41 // based on the horizontal insets.
42 mAlignedView = getChildAt(0);
43 }
44 super.onFinishInflate();
45 }
46
47 @TargetApi(23)
48 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070049 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal6c2975e2016-07-06 09:47:56 -070050 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080051 (!Utilities.ATLEAST_MARSHMALLOW ||
Sunny Goyal906c6b22017-08-18 00:35:52 -070052 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
53 if (mDrawSideInsetBar) {
54 mLeftInsetBarWidth = insets.left;
55 mRightInsetBarWidth = insets.right;
56 insets = new Rect(0, insets.top, 0, insets.bottom);
57 } else {
58 mLeftInsetBarWidth = mRightInsetBarWidth = 0;
59 }
60 Launcher.getLauncher(getContext()).getSystemUiController().updateUiState(
61 UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080062
Sunny Goyal906c6b22017-08-18 00:35:52 -070063 boolean rawInsetsChanged = !mInsets.equals(insets);
64 setInsets(insets);
65
66 if (mAlignedView != null) {
Sunny Goyal93264612015-11-23 11:47:50 -080067 // Apply margins on aligned view to handle left/right insets.
68 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal906c6b22017-08-18 00:35:52 -070069 if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) {
70 lp.leftMargin = mLeftInsetBarWidth;
71 lp.rightMargin = mRightInsetBarWidth;
Sunny Goyal93264612015-11-23 11:47:50 -080072 mAlignedView.setLayoutParams(lp);
73 }
74 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070075
Winson1f064272016-07-18 17:18:02 -070076 if (rawInsetsChanged) {
77 // Update the grid again
78 Launcher launcher = Launcher.getLauncher(getContext());
79 launcher.onInsetsChanged(insets);
80 }
81
Adam Cohena6d04922014-10-23 17:28:30 -070082 return true; // I'll take it from here
83 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070084
85 @Override
86 protected void dispatchDraw(Canvas canvas) {
87 super.dispatchDraw(canvas);
88
89 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -070090 if (mDrawSideInsetBar) {
91 if (mRightInsetBarWidth > 0) {
92 int width = getWidth();
93 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
94 }
95 if (mLeftInsetBarWidth > 0) {
96 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
97 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070098 }
99 }
Adam Cohena6d04922014-10-23 17:28:30 -0700100}