blob: b1273b64c3768c0ff9b650ea28ce9521326ff563 [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;
Sunny Goyal7185dd62018-03-14 17:51:49 -070032 private WindowStateListener mWindowStateListener;
Sunny Goyal93264612015-11-23 11:47:50 -080033
Adam Cohena6d04922014-10-23 17:28:30 -070034 public LauncherRootView(Context context, AttributeSet attrs) {
35 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070036
37 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
38 mOpaquePaint.setColor(Color.BLACK);
39 mOpaquePaint.setStyle(Paint.Style.FILL);
Sunny Goyal07b69292018-01-08 14:19:34 -080040
41 mLauncher = Launcher.getLauncher(context);
Adam Cohena6d04922014-10-23 17:28:30 -070042 }
43
44 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080045 protected void onFinishInflate() {
46 if (getChildCount() > 0) {
47 // LauncherRootView contains only one child, which should be aligned
48 // based on the horizontal insets.
49 mAlignedView = getChildAt(0);
50 }
51 super.onFinishInflate();
52 }
53
54 @TargetApi(23)
55 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070056 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal6c2975e2016-07-06 09:47:56 -070057 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080058 (!Utilities.ATLEAST_MARSHMALLOW ||
Sunny Goyal906c6b22017-08-18 00:35:52 -070059 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
60 if (mDrawSideInsetBar) {
61 mLeftInsetBarWidth = insets.left;
62 mRightInsetBarWidth = insets.right;
63 insets = new Rect(0, insets.top, 0, insets.bottom);
64 } else {
65 mLeftInsetBarWidth = mRightInsetBarWidth = 0;
66 }
Sunny Goyal07b69292018-01-08 14:19:34 -080067 mLauncher.getSystemUiController().updateUiState(
Sunny Goyal906c6b22017-08-18 00:35:52 -070068 UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080069
Sunny Goyal07b69292018-01-08 14:19:34 -080070 // Update device profile before notifying th children.
71 mLauncher.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080072 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070073 setInsets(insets);
74
75 if (mAlignedView != null) {
Sunny Goyal93264612015-11-23 11:47:50 -080076 // Apply margins on aligned view to handle left/right insets.
77 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal906c6b22017-08-18 00:35:52 -070078 if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) {
79 lp.leftMargin = mLeftInsetBarWidth;
80 lp.rightMargin = mRightInsetBarWidth;
Sunny Goyal93264612015-11-23 11:47:50 -080081 mAlignedView.setLayoutParams(lp);
82 }
83 }
Sunny Goyalce8809a2018-01-18 14:02:47 -080084 if (resetState) {
85 mLauncher.getStateManager().reapplyState();
86 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070087
Adam Cohena6d04922014-10-23 17:28:30 -070088 return true; // I'll take it from here
89 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070090
Jon Mirandade43a712018-01-18 11:35:10 -080091 @Override
92 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080093 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
94 // modifying child layout params.
95 if (!insets.equals(mInsets)) {
96 super.setInsets(insets);
97 }
Jon Mirandade43a712018-01-18 11:35:10 -080098 setBackground(insets.top == 0 ? null
99 : Themes.getAttrDrawable(getContext(), R.attr.workspaceStatusBarScrim));
100 }
101
Sunny Goyal07b69292018-01-08 14:19:34 -0800102 public void dispatchInsets() {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800103 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800104 }
105
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700106 @Override
107 protected void dispatchDraw(Canvas canvas) {
108 super.dispatchDraw(canvas);
109
110 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700111 if (mDrawSideInsetBar) {
112 if (mRightInsetBarWidth > 0) {
113 int width = getWidth();
114 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
115 }
116 if (mLeftInsetBarWidth > 0) {
117 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
118 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700119 }
120 }
Sunny Goyal7185dd62018-03-14 17:51:49 -0700121
122 public void setWindowStateListener(WindowStateListener listener) {
123 mWindowStateListener = listener;
124 }
125
126 @Override
127 public void onWindowFocusChanged(boolean hasWindowFocus) {
128 super.onWindowFocusChanged(hasWindowFocus);
129 if (mWindowStateListener != null) {
130 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
131 }
132 }
133
134 @Override
135 protected void onWindowVisibilityChanged(int visibility) {
136 super.onWindowVisibilityChanged(visibility);
137 if (mWindowStateListener != null) {
138 mWindowStateListener.onWindowVisibilityChanged(visibility);
139 }
140 }
141
142 public interface WindowStateListener {
143
144 void onWindowFocusChanged(boolean hasFocus);
145
146 void onWindowVisibilityChanged(int visibility);
147 }
Adam Cohena6d04922014-10-23 17:28:30 -0700148}