blob: 3cf6d62b501c1f27d4ca362cdcbe62fbc5d4f445 [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;
13import android.util.AttributeSet;
Sunny Goyal93264612015-11-23 11:47:50 -080014import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080015import android.view.ViewDebug;
Adam Cohena6d04922014-10-23 17:28:30 -070016
17public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070018
Sunny Goyal07b69292018-01-08 14:19:34 -080019 private final Launcher mLauncher;
20
Sunny Goyal0abb36f2015-06-23 10:53:59 -070021 private final Paint mOpaquePaint;
Sunny Goyal9001b102018-05-07 11:09:13 -070022
Sunny Goyal4ffec482016-02-09 11:28:52 -080023 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal9001b102018-05-07 11:09:13 -070024 private final Rect mConsumedInsets = new Rect();
Sunny Goyal0abb36f2015-06-23 10:53:59 -070025
Sunny Goyal93264612015-11-23 11:47:50 -080026 private View mAlignedView;
Sunny Goyal7185dd62018-03-14 17:51:49 -070027 private WindowStateListener mWindowStateListener;
Sunny Goyal93264612015-11-23 11:47:50 -080028
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);
Sunny Goyal07b69292018-01-08 14:19:34 -080035
36 mLauncher = Launcher.getLauncher(context);
Adam Cohena6d04922014-10-23 17:28:30 -070037 }
38
39 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080040 protected void onFinishInflate() {
41 if (getChildCount() > 0) {
42 // LauncherRootView contains only one child, which should be aligned
43 // based on the horizontal insets.
44 mAlignedView = getChildAt(0);
45 }
46 super.onFinishInflate();
47 }
48
49 @TargetApi(23)
50 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070051 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal9001b102018-05-07 11:09:13 -070052 mConsumedInsets.setEmpty();
53 boolean drawInsetBar = false;
54 if (mLauncher.isInMultiWindowModeCompat()
55 && (insets.left > 0 || insets.right > 0 || insets.bottom > 0)) {
56 mConsumedInsets.left = insets.left;
57 mConsumedInsets.right = insets.right;
58 mConsumedInsets.bottom = insets.bottom;
59 insets = new Rect(0, insets.top, 0, 0);
60 drawInsetBar = true;
61 } else if ((insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080062 (!Utilities.ATLEAST_MARSHMALLOW ||
Sunny Goyal9001b102018-05-07 11:09:13 -070063 getContext().getSystemService(ActivityManager.class).isLowRamDevice())) {
64 mConsumedInsets.left = insets.left;
65 mConsumedInsets.right = insets.right;
Sunny Goyal906c6b22017-08-18 00:35:52 -070066 insets = new Rect(0, insets.top, 0, insets.bottom);
Sunny Goyal9001b102018-05-07 11:09:13 -070067 drawInsetBar = true;
Sunny Goyal906c6b22017-08-18 00:35:52 -070068 }
Sunny Goyal9001b102018-05-07 11:09:13 -070069
Sunny Goyal07b69292018-01-08 14:19:34 -080070 mLauncher.getSystemUiController().updateUiState(
Sunny Goyal9001b102018-05-07 11:09:13 -070071 UI_STATE_ROOT_VIEW, drawInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080072
Sunny Goyal07b69292018-01-08 14:19:34 -080073 // Update device profile before notifying th children.
74 mLauncher.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080075 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070076 setInsets(insets);
77
78 if (mAlignedView != null) {
Sunny Goyal9001b102018-05-07 11:09:13 -070079 // Apply margins on aligned view to handle consumed insets.
Sunny Goyal93264612015-11-23 11:47:50 -080080 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal9001b102018-05-07 11:09:13 -070081 if (lp.leftMargin != mConsumedInsets.left || lp.rightMargin != mConsumedInsets.right ||
82 lp.bottomMargin != mConsumedInsets.bottom) {
83 lp.leftMargin = mConsumedInsets.left;
84 lp.rightMargin = mConsumedInsets.right;
85 lp.topMargin = mConsumedInsets.top;
86 lp.bottomMargin = mConsumedInsets.bottom;
Sunny Goyal93264612015-11-23 11:47:50 -080087 mAlignedView.setLayoutParams(lp);
88 }
89 }
Sunny Goyalce8809a2018-01-18 14:02:47 -080090 if (resetState) {
Sunny Goyaled2d2bc2018-04-19 12:34:43 -070091 mLauncher.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
Sunny Goyalce8809a2018-01-18 14:02:47 -080092 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070093
Adam Cohena6d04922014-10-23 17:28:30 -070094 return true; // I'll take it from here
95 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070096
Jon Mirandade43a712018-01-18 11:35:10 -080097 @Override
98 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080099 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
100 // modifying child layout params.
101 if (!insets.equals(mInsets)) {
102 super.setInsets(insets);
103 }
Jon Mirandade43a712018-01-18 11:35:10 -0800104 }
105
Sunny Goyal07b69292018-01-08 14:19:34 -0800106 public void dispatchInsets() {
Sunny Goyal34778042018-04-11 16:31:31 -0700107 mLauncher.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800108 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -0800109 }
110
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700111 @Override
112 protected void dispatchDraw(Canvas canvas) {
113 super.dispatchDraw(canvas);
114
115 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal9001b102018-05-07 11:09:13 -0700116 if (mConsumedInsets.right > 0) {
117 int width = getWidth();
118 canvas.drawRect(width - mConsumedInsets.right, 0, width, getHeight(), mOpaquePaint);
119 }
120 if (mConsumedInsets.left > 0) {
121 canvas.drawRect(0, 0, mConsumedInsets.left, getHeight(), mOpaquePaint);
122 }
123 if (mConsumedInsets.bottom > 0) {
124 int height = getHeight();
125 canvas.drawRect(0, height - mConsumedInsets.bottom, getWidth(), height, mOpaquePaint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700126 }
127 }
Sunny Goyal7185dd62018-03-14 17:51:49 -0700128
129 public void setWindowStateListener(WindowStateListener listener) {
130 mWindowStateListener = listener;
131 }
132
133 @Override
134 public void onWindowFocusChanged(boolean hasWindowFocus) {
135 super.onWindowFocusChanged(hasWindowFocus);
136 if (mWindowStateListener != null) {
137 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
138 }
139 }
140
141 @Override
142 protected void onWindowVisibilityChanged(int visibility) {
143 super.onWindowVisibilityChanged(visibility);
144 if (mWindowStateListener != null) {
145 mWindowStateListener.onWindowVisibilityChanged(visibility);
146 }
147 }
148
149 public interface WindowStateListener {
150
151 void onWindowFocusChanged(boolean hasFocus);
152
153 void onWindowVisibilityChanged(int visibility);
154 }
Adam Cohena6d04922014-10-23 17:28:30 -0700155}