blob: a5c5c02735263f700d0612e41abce995e9304ba8 [file] [log] [blame]
Adam Cohena6d04922014-10-23 17:28:30 -07001package com.android.launcher3;
2
Sunny Goyala314d5a2020-05-20 20:34:04 -07003import static com.android.launcher3.config.FeatureFlags.SEPARATE_RECENTS_ACTIVITY;
Sunny Goyaled2d2bc2018-04-19 12:34:43 -07004
Sunny Goyal93264612015-11-23 11:47:50 -08005import android.annotation.TargetApi;
Adam Cohena6d04922014-10-23 17:28:30 -07006import android.content.Context;
Adam Cohen501e1392021-02-02 16:45:07 -08007import android.graphics.Canvas;
Adam Cohena6d04922014-10-23 17:28:30 -07008import android.graphics.Rect;
Tony Wickham12e8a342019-04-23 11:28:54 -07009import android.os.Build;
Adam Cohena6d04922014-10-23 17:28:30 -070010import android.util.AttributeSet;
Sunny Goyal4ffec482016-02-09 11:28:52 -080011import android.view.ViewDebug;
Sunny Goyal022b1822019-06-12 08:44:09 -070012import android.view.WindowInsets;
Adam Cohena6d04922014-10-23 17:28:30 -070013
Adam Cohen501e1392021-02-02 16:45:07 -080014import com.android.launcher3.graphics.SysUiScrim;
Sunny Goyal9d89f752020-06-19 11:09:24 -070015import com.android.launcher3.statemanager.StatefulActivity;
Sunny Goyal187b16c2022-03-01 16:53:23 -080016import com.android.launcher3.util.window.WindowManagerProxy;
Sunny Goyal9d89f752020-06-19 11:09:24 -070017
Tony Wickham12e8a342019-04-23 11:28:54 -070018import java.util.Collections;
19import java.util.List;
20
Adam Cohena6d04922014-10-23 17:28:30 -070021public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070022
Sunny Goyal022b1822019-06-12 08:44:09 -070023 private final Rect mTempRect = new Rect();
24
Sunny Goyal9d89f752020-06-19 11:09:24 -070025 private final StatefulActivity mActivity;
Sunny Goyal07b69292018-01-08 14:19:34 -080026
Sunny Goyal745df7c2019-04-03 15:25:00 -070027 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickham12e8a342019-04-23 11:28:54 -070028 private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT =
29 Collections.singletonList(new Rect());
30
Sunny Goyal7185dd62018-03-14 17:51:49 -070031 private WindowStateListener mWindowStateListener;
Tony Wickham12e8a342019-04-23 11:28:54 -070032 @ViewDebug.ExportedProperty(category = "launcher")
33 private boolean mDisallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -070034 @ViewDebug.ExportedProperty(category = "launcher")
35 private boolean mForceHideBackArrow;
Sunny Goyal93264612015-11-23 11:47:50 -080036
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070037 private final SysUiScrim mSysUiScrim;
Adam Cohen501e1392021-02-02 16:45:07 -080038
Adam Cohena6d04922014-10-23 17:28:30 -070039 public LauncherRootView(Context context, AttributeSet attrs) {
40 super(context, attrs);
Sunny Goyal9d89f752020-06-19 11:09:24 -070041 mActivity = StatefulActivity.fromContext(context);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070042 mSysUiScrim = new SysUiScrim(this);
Adam Cohena6d04922014-10-23 17:28:30 -070043 }
44
Sunny Goyal022b1822019-06-12 08:44:09 -070045 private void handleSystemWindowInsets(Rect insets) {
Tony Wickham145b7fd2021-03-11 10:03:12 -080046 // Update device profile before notifying the children.
Tony Wickham21970cc2021-09-15 15:44:05 -070047 mActivity.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080048 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070049 setInsets(insets);
50
Sunny Goyalce8809a2018-01-18 14:02:47 -080051 if (resetState) {
Sunny Goyal9d89f752020-06-19 11:09:24 -070052 mActivity.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
Sunny Goyalce8809a2018-01-18 14:02:47 -080053 }
Sunny Goyal022b1822019-06-12 08:44:09 -070054 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070055
Sunny Goyal022b1822019-06-12 08:44:09 -070056 @Override
57 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Sunny Goyal187b16c2022-03-01 16:53:23 -080058 insets = WindowManagerProxy.INSTANCE.get(getContext())
59 .normalizeWindowInsets(getContext(), insets, mTempRect);
Sunny Goyal022b1822019-06-12 08:44:09 -070060 handleSystemWindowInsets(mTempRect);
Sunny Goyal786940a2020-06-02 02:31:31 -070061 return insets;
Adam Cohena6d04922014-10-23 17:28:30 -070062 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070063
Jon Mirandade43a712018-01-18 11:35:10 -080064 @Override
65 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080066 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
67 // modifying child layout params.
68 if (!insets.equals(mInsets)) {
69 super.setInsets(insets);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070070 mSysUiScrim.onInsetsChanged(insets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080071 }
Jon Mirandade43a712018-01-18 11:35:10 -080072 }
73
Sunny Goyal07b69292018-01-08 14:19:34 -080074 public void dispatchInsets() {
Sunny Goyal9d89f752020-06-19 11:09:24 -070075 mActivity.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080076 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -080077 }
78
Sunny Goyal7185dd62018-03-14 17:51:49 -070079 public void setWindowStateListener(WindowStateListener listener) {
80 mWindowStateListener = listener;
81 }
82
83 @Override
84 public void onWindowFocusChanged(boolean hasWindowFocus) {
85 super.onWindowFocusChanged(hasWindowFocus);
86 if (mWindowStateListener != null) {
87 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
88 }
89 }
90
91 @Override
92 protected void onWindowVisibilityChanged(int visibility) {
93 super.onWindowVisibilityChanged(visibility);
94 if (mWindowStateListener != null) {
95 mWindowStateListener.onWindowVisibilityChanged(visibility);
96 }
97 }
98
Adam Cohen501e1392021-02-02 16:45:07 -080099 @Override
100 protected void dispatchDraw(Canvas canvas) {
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700101 mSysUiScrim.draw(canvas);
Adam Cohen501e1392021-02-02 16:45:07 -0800102 super.dispatchDraw(canvas);
103 }
104
Sunny Goyal745df7c2019-04-03 15:25:00 -0700105 @Override
Tony Wickham12e8a342019-04-23 11:28:54 -0700106 protected void onLayout(boolean changed, int l, int t, int r, int b) {
107 super.onLayout(changed, l, t, r, b);
108 SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
109 setDisallowBackGesture(mDisallowBackGesture);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700110 mSysUiScrim.setSize(r - l, b - t);
Tony Wickham12e8a342019-04-23 11:28:54 -0700111 }
112
113 @TargetApi(Build.VERSION_CODES.Q)
Vinit Nayak9a846212019-09-05 11:47:12 -0700114 public void setForceHideBackArrow(boolean forceHideBackArrow) {
115 this.mForceHideBackArrow = forceHideBackArrow;
116 setDisallowBackGesture(mDisallowBackGesture);
117 }
118
119 @TargetApi(Build.VERSION_CODES.Q)
Tony Wickham12e8a342019-04-23 11:28:54 -0700120 public void setDisallowBackGesture(boolean disallowBackGesture) {
Sunny Goyala314d5a2020-05-20 20:34:04 -0700121 if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) {
Tony Wickham12e8a342019-04-23 11:28:54 -0700122 return;
123 }
124 mDisallowBackGesture = disallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -0700125 setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture)
Tony Wickham12e8a342019-04-23 11:28:54 -0700126 ? SYSTEM_GESTURE_EXCLUSION_RECT
127 : Collections.emptyList());
128 }
129
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700130 public SysUiScrim getSysUiScrim() {
131 return mSysUiScrim;
132 }
133
Sunny Goyal7185dd62018-03-14 17:51:49 -0700134 public interface WindowStateListener {
135
136 void onWindowFocusChanged(boolean hasFocus);
137
138 void onWindowVisibilityChanged(int visibility);
139 }
Adam Cohen501e1392021-02-02 16:45:07 -0800140}