blob: fc4de2d32de1dc8b17e2473bc9b42722feeacab0 [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
Jon Mirandade43a712018-01-18 11:35:10 -080014import com.android.launcher3.util.Themes;
15
Sunny Goyal906c6b22017-08-18 00:35:52 -070016import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
17import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
18
Adam Cohena6d04922014-10-23 17:28:30 -070019public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070020
Sunny Goyal07b69292018-01-08 14:19:34 -080021 private final Launcher mLauncher;
22
Sunny Goyal0abb36f2015-06-23 10:53:59 -070023 private final Paint mOpaquePaint;
Sunny Goyal4ffec482016-02-09 11:28:52 -080024 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal6c2975e2016-07-06 09:47:56 -070025 private boolean mDrawSideInsetBar;
26 @ViewDebug.ExportedProperty(category = "launcher")
27 private int mLeftInsetBarWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080028 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalecdc24f2016-01-12 10:35:32 -080029 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070030
Sunny Goyal93264612015-11-23 11:47:50 -080031 private View mAlignedView;
32
Adam Cohena6d04922014-10-23 17:28:30 -070033 public LauncherRootView(Context context, AttributeSet attrs) {
34 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070035
36 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
37 mOpaquePaint.setColor(Color.BLACK);
38 mOpaquePaint.setStyle(Paint.Style.FILL);
Sunny Goyal07b69292018-01-08 14:19:34 -080039
40 mLauncher = Launcher.getLauncher(context);
Adam Cohena6d04922014-10-23 17:28:30 -070041 }
42
43 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080044 protected void onFinishInflate() {
45 if (getChildCount() > 0) {
46 // LauncherRootView contains only one child, which should be aligned
47 // based on the horizontal insets.
48 mAlignedView = getChildAt(0);
49 }
50 super.onFinishInflate();
51 }
52
53 @TargetApi(23)
54 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070055 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal6c2975e2016-07-06 09:47:56 -070056 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080057 (!Utilities.ATLEAST_MARSHMALLOW ||
Sunny Goyal906c6b22017-08-18 00:35:52 -070058 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
59 if (mDrawSideInsetBar) {
60 mLeftInsetBarWidth = insets.left;
61 mRightInsetBarWidth = insets.right;
62 insets = new Rect(0, insets.top, 0, insets.bottom);
63 } else {
64 mLeftInsetBarWidth = mRightInsetBarWidth = 0;
65 }
Sunny Goyal07b69292018-01-08 14:19:34 -080066 mLauncher.getSystemUiController().updateUiState(
Sunny Goyal906c6b22017-08-18 00:35:52 -070067 UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080068
Sunny Goyal07b69292018-01-08 14:19:34 -080069 // Update device profile before notifying th children.
70 mLauncher.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080071 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070072 setInsets(insets);
73
74 if (mAlignedView != null) {
Sunny Goyal93264612015-11-23 11:47:50 -080075 // Apply margins on aligned view to handle left/right insets.
76 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal906c6b22017-08-18 00:35:52 -070077 if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) {
78 lp.leftMargin = mLeftInsetBarWidth;
79 lp.rightMargin = mRightInsetBarWidth;
Sunny Goyal93264612015-11-23 11:47:50 -080080 mAlignedView.setLayoutParams(lp);
81 }
82 }
Sunny Goyalce8809a2018-01-18 14:02:47 -080083 if (resetState) {
84 mLauncher.getStateManager().reapplyState();
85 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070086
Adam Cohena6d04922014-10-23 17:28:30 -070087 return true; // I'll take it from here
88 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070089
Jon Mirandade43a712018-01-18 11:35:10 -080090 @Override
91 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080092 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
93 // modifying child layout params.
94 if (!insets.equals(mInsets)) {
95 super.setInsets(insets);
96 }
Jon Mirandade43a712018-01-18 11:35:10 -080097 setBackground(insets.top == 0 ? null
98 : Themes.getAttrDrawable(getContext(), R.attr.workspaceStatusBarScrim));
99 }
100
Sunny Goyal07b69292018-01-08 14:19:34 -0800101 public void dispatchInsets() {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800102 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800103 }
104
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700105 @Override
106 protected void dispatchDraw(Canvas canvas) {
107 super.dispatchDraw(canvas);
108
109 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700110 if (mDrawSideInsetBar) {
111 if (mRightInsetBarWidth > 0) {
112 int width = getWidth();
113 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
114 }
115 if (mLeftInsetBarWidth > 0) {
116 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
117 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700118 }
119 }
Adam Cohena6d04922014-10-23 17:28:30 -0700120}