blob: b4fbbc327953583bbbbfd602d29d100d2e161043 [file] [log] [blame]
Adam Cohena6d04922014-10-23 17:28:30 -07001package com.android.launcher3;
2
Sunny Goyaled2d2bc2018-04-19 12:34:43 -07003import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
4import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
5
Sunny Goyal93264612015-11-23 11:47:50 -08006import android.annotation.TargetApi;
7import android.app.ActivityManager;
Adam Cohena6d04922014-10-23 17:28:30 -07008import android.content.Context;
Sunny Goyal0abb36f2015-06-23 10:53:59 -07009import android.graphics.Canvas;
10import android.graphics.Color;
11import android.graphics.Paint;
Adam Cohena6d04922014-10-23 17:28:30 -070012import android.graphics.Rect;
Tony Wickham12e8a342019-04-23 11:28:54 -070013import android.os.Build;
Adam Cohena6d04922014-10-23 17:28:30 -070014import android.util.AttributeSet;
Sunny Goyal93264612015-11-23 11:47:50 -080015import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080016import android.view.ViewDebug;
Sunny Goyal022b1822019-06-12 08:44:09 -070017import android.view.WindowInsets;
Adam Cohena6d04922014-10-23 17:28:30 -070018
Tony Wickham12e8a342019-04-23 11:28:54 -070019import java.util.Collections;
20import java.util.List;
21
Adam Cohena6d04922014-10-23 17:28:30 -070022public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070023
Sunny Goyal022b1822019-06-12 08:44:09 -070024 private final Rect mTempRect = new Rect();
25
Sunny Goyal07b69292018-01-08 14:19:34 -080026 private final Launcher mLauncher;
27
Sunny Goyal0abb36f2015-06-23 10:53:59 -070028 private final Paint mOpaquePaint;
Sunny Goyal9001b102018-05-07 11:09:13 -070029
Sunny Goyal4ffec482016-02-09 11:28:52 -080030 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal9001b102018-05-07 11:09:13 -070031 private final Rect mConsumedInsets = new Rect();
Sunny Goyal0abb36f2015-06-23 10:53:59 -070032
Sunny Goyal745df7c2019-04-03 15:25:00 -070033 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickham12e8a342019-04-23 11:28:54 -070034 private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT =
35 Collections.singletonList(new Rect());
36
Sunny Goyal93264612015-11-23 11:47:50 -080037 private View mAlignedView;
Sunny Goyal7185dd62018-03-14 17:51:49 -070038 private WindowStateListener mWindowStateListener;
Tony Wickham12e8a342019-04-23 11:28:54 -070039 @ViewDebug.ExportedProperty(category = "launcher")
40 private boolean mDisallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -070041 @ViewDebug.ExportedProperty(category = "launcher")
42 private boolean mForceHideBackArrow;
Sunny Goyal93264612015-11-23 11:47:50 -080043
Adam Cohena6d04922014-10-23 17:28:30 -070044 public LauncherRootView(Context context, AttributeSet attrs) {
45 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070046
47 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
48 mOpaquePaint.setColor(Color.BLACK);
49 mOpaquePaint.setStyle(Paint.Style.FILL);
Sunny Goyal07b69292018-01-08 14:19:34 -080050
51 mLauncher = Launcher.getLauncher(context);
Adam Cohena6d04922014-10-23 17:28:30 -070052 }
53
54 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080055 protected void onFinishInflate() {
56 if (getChildCount() > 0) {
57 // LauncherRootView contains only one child, which should be aligned
58 // based on the horizontal insets.
59 mAlignedView = getChildAt(0);
60 }
61 super.onFinishInflate();
62 }
63
Sunny Goyal022b1822019-06-12 08:44:09 -070064 private void handleSystemWindowInsets(Rect insets) {
Sunny Goyal9001b102018-05-07 11:09:13 -070065 mConsumedInsets.setEmpty();
66 boolean drawInsetBar = false;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080067 if (mLauncher.isInMultiWindowMode()
Sunny Goyal9001b102018-05-07 11:09:13 -070068 && (insets.left > 0 || insets.right > 0 || insets.bottom > 0)) {
69 mConsumedInsets.left = insets.left;
70 mConsumedInsets.right = insets.right;
71 mConsumedInsets.bottom = insets.bottom;
Sunny Goyal022b1822019-06-12 08:44:09 -070072 insets.set(0, insets.top, 0, 0);
Sunny Goyal9001b102018-05-07 11:09:13 -070073 drawInsetBar = true;
74 } else if ((insets.right > 0 || insets.left > 0) &&
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080075 getContext().getSystemService(ActivityManager.class).isLowRamDevice()) {
Sunny Goyal9001b102018-05-07 11:09:13 -070076 mConsumedInsets.left = insets.left;
77 mConsumedInsets.right = insets.right;
Sunny Goyal022b1822019-06-12 08:44:09 -070078 insets.set(0, insets.top, 0, insets.bottom);
Sunny Goyal9001b102018-05-07 11:09:13 -070079 drawInsetBar = true;
Sunny Goyal906c6b22017-08-18 00:35:52 -070080 }
Sunny Goyal9001b102018-05-07 11:09:13 -070081
Sunny Goyal07b69292018-01-08 14:19:34 -080082 mLauncher.getSystemUiController().updateUiState(
Sunny Goyal9001b102018-05-07 11:09:13 -070083 UI_STATE_ROOT_VIEW, drawInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080084
Sunny Goyal07b69292018-01-08 14:19:34 -080085 // Update device profile before notifying th children.
Sunny Goyalc4d32012020-04-03 17:10:11 -070086 mLauncher.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080087 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070088 setInsets(insets);
89
90 if (mAlignedView != null) {
Sunny Goyal9001b102018-05-07 11:09:13 -070091 // Apply margins on aligned view to handle consumed insets.
Sunny Goyal93264612015-11-23 11:47:50 -080092 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal9001b102018-05-07 11:09:13 -070093 if (lp.leftMargin != mConsumedInsets.left || lp.rightMargin != mConsumedInsets.right ||
94 lp.bottomMargin != mConsumedInsets.bottom) {
95 lp.leftMargin = mConsumedInsets.left;
96 lp.rightMargin = mConsumedInsets.right;
97 lp.topMargin = mConsumedInsets.top;
98 lp.bottomMargin = mConsumedInsets.bottom;
Sunny Goyal93264612015-11-23 11:47:50 -080099 mAlignedView.setLayoutParams(lp);
100 }
101 }
Sunny Goyalce8809a2018-01-18 14:02:47 -0800102 if (resetState) {
Sunny Goyaled2d2bc2018-04-19 12:34:43 -0700103 mLauncher.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
Sunny Goyalce8809a2018-01-18 14:02:47 -0800104 }
Sunny Goyal022b1822019-06-12 08:44:09 -0700105 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700106
Sunny Goyal022b1822019-06-12 08:44:09 -0700107 @Override
108 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
109 mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
110 insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
111 handleSystemWindowInsets(mTempRect);
112 if (Utilities.ATLEAST_Q) {
113 return insets.inset(mConsumedInsets.left, mConsumedInsets.top,
114 mConsumedInsets.right, mConsumedInsets.bottom);
115 } else {
116 return insets.replaceSystemWindowInsets(mTempRect);
117 }
Adam Cohena6d04922014-10-23 17:28:30 -0700118 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700119
Jon Mirandade43a712018-01-18 11:35:10 -0800120 @Override
121 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800122 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
123 // modifying child layout params.
124 if (!insets.equals(mInsets)) {
125 super.setInsets(insets);
126 }
Jon Mirandade43a712018-01-18 11:35:10 -0800127 }
128
Sunny Goyal07b69292018-01-08 14:19:34 -0800129 public void dispatchInsets() {
Sunny Goyalc4d32012020-04-03 17:10:11 -0700130 mLauncher.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800131 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800132 }
133
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700134 @Override
135 protected void dispatchDraw(Canvas canvas) {
136 super.dispatchDraw(canvas);
137
138 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal9001b102018-05-07 11:09:13 -0700139 if (mConsumedInsets.right > 0) {
140 int width = getWidth();
141 canvas.drawRect(width - mConsumedInsets.right, 0, width, getHeight(), mOpaquePaint);
142 }
143 if (mConsumedInsets.left > 0) {
144 canvas.drawRect(0, 0, mConsumedInsets.left, getHeight(), mOpaquePaint);
145 }
146 if (mConsumedInsets.bottom > 0) {
147 int height = getHeight();
148 canvas.drawRect(0, height - mConsumedInsets.bottom, getWidth(), height, mOpaquePaint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700149 }
150 }
Sunny Goyal7185dd62018-03-14 17:51:49 -0700151
152 public void setWindowStateListener(WindowStateListener listener) {
153 mWindowStateListener = listener;
154 }
155
156 @Override
157 public void onWindowFocusChanged(boolean hasWindowFocus) {
158 super.onWindowFocusChanged(hasWindowFocus);
159 if (mWindowStateListener != null) {
160 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
161 }
162 }
163
164 @Override
165 protected void onWindowVisibilityChanged(int visibility) {
166 super.onWindowVisibilityChanged(visibility);
167 if (mWindowStateListener != null) {
168 mWindowStateListener.onWindowVisibilityChanged(visibility);
169 }
170 }
171
Sunny Goyal745df7c2019-04-03 15:25:00 -0700172 @Override
Tony Wickham12e8a342019-04-23 11:28:54 -0700173 protected void onLayout(boolean changed, int l, int t, int r, int b) {
174 super.onLayout(changed, l, t, r, b);
175 SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
176 setDisallowBackGesture(mDisallowBackGesture);
177 }
178
179 @TargetApi(Build.VERSION_CODES.Q)
Vinit Nayak9a846212019-09-05 11:47:12 -0700180 public void setForceHideBackArrow(boolean forceHideBackArrow) {
181 this.mForceHideBackArrow = forceHideBackArrow;
182 setDisallowBackGesture(mDisallowBackGesture);
183 }
184
185 @TargetApi(Build.VERSION_CODES.Q)
Tony Wickham12e8a342019-04-23 11:28:54 -0700186 public void setDisallowBackGesture(boolean disallowBackGesture) {
187 if (!Utilities.ATLEAST_Q) {
188 return;
189 }
190 mDisallowBackGesture = disallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -0700191 setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture)
Tony Wickham12e8a342019-04-23 11:28:54 -0700192 ? SYSTEM_GESTURE_EXCLUSION_RECT
193 : Collections.emptyList());
194 }
195
Sunny Goyal7185dd62018-03-14 17:51:49 -0700196 public interface WindowStateListener {
197
198 void onWindowFocusChanged(boolean hasFocus);
199
200 void onWindowVisibilityChanged(int visibility);
201 }
Adam Cohena6d04922014-10-23 17:28:30 -0700202}