blob: f204c16e2849fb7b817726304e39784b46fd68be [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 Goyal34778042018-04-11 16:31:31 -0700103 mLauncher.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800104 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800105 }
106
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700107 @Override
108 protected void dispatchDraw(Canvas canvas) {
109 super.dispatchDraw(canvas);
110
111 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -0700112 if (mDrawSideInsetBar) {
113 if (mRightInsetBarWidth > 0) {
114 int width = getWidth();
115 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
116 }
117 if (mLeftInsetBarWidth > 0) {
118 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
119 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700120 }
121 }
Sunny Goyal7185dd62018-03-14 17:51:49 -0700122
123 public void setWindowStateListener(WindowStateListener listener) {
124 mWindowStateListener = listener;
125 }
126
127 @Override
128 public void onWindowFocusChanged(boolean hasWindowFocus) {
129 super.onWindowFocusChanged(hasWindowFocus);
130 if (mWindowStateListener != null) {
131 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
132 }
133 }
134
135 @Override
136 protected void onWindowVisibilityChanged(int visibility) {
137 super.onWindowVisibilityChanged(visibility);
138 if (mWindowStateListener != null) {
139 mWindowStateListener.onWindowVisibilityChanged(visibility);
140 }
141 }
142
143 public interface WindowStateListener {
144
145 void onWindowFocusChanged(boolean hasFocus);
146
147 void onWindowVisibilityChanged(int visibility);
148 }
Adam Cohena6d04922014-10-23 17:28:30 -0700149}