blob: 1592154c3a036ce859ee7ade8474903d49c79966 [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) {
Alex Chauccc430e2022-08-09 11:59:45 +010058 mActivity.handleConfigurationChanged(mActivity.getResources().getConfiguration());
59
Sunny Goyal187b16c2022-03-01 16:53:23 -080060 insets = WindowManagerProxy.INSTANCE.get(getContext())
61 .normalizeWindowInsets(getContext(), insets, mTempRect);
Sunny Goyal022b1822019-06-12 08:44:09 -070062 handleSystemWindowInsets(mTempRect);
Sunny Goyal786940a2020-06-02 02:31:31 -070063 return insets;
Adam Cohena6d04922014-10-23 17:28:30 -070064 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070065
Jon Mirandade43a712018-01-18 11:35:10 -080066 @Override
67 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080068 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
69 // modifying child layout params.
70 if (!insets.equals(mInsets)) {
71 super.setInsets(insets);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070072 mSysUiScrim.onInsetsChanged(insets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080073 }
Jon Mirandade43a712018-01-18 11:35:10 -080074 }
75
Sunny Goyal07b69292018-01-08 14:19:34 -080076 public void dispatchInsets() {
Sunny Goyal9d89f752020-06-19 11:09:24 -070077 mActivity.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080078 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -080079 }
80
Sunny Goyal7185dd62018-03-14 17:51:49 -070081 public void setWindowStateListener(WindowStateListener listener) {
82 mWindowStateListener = listener;
83 }
84
85 @Override
86 public void onWindowFocusChanged(boolean hasWindowFocus) {
87 super.onWindowFocusChanged(hasWindowFocus);
88 if (mWindowStateListener != null) {
89 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
90 }
91 }
92
93 @Override
94 protected void onWindowVisibilityChanged(int visibility) {
95 super.onWindowVisibilityChanged(visibility);
96 if (mWindowStateListener != null) {
97 mWindowStateListener.onWindowVisibilityChanged(visibility);
98 }
99 }
100
Adam Cohen501e1392021-02-02 16:45:07 -0800101 @Override
102 protected void dispatchDraw(Canvas canvas) {
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700103 mSysUiScrim.draw(canvas);
Adam Cohen501e1392021-02-02 16:45:07 -0800104 super.dispatchDraw(canvas);
105 }
106
Sunny Goyal745df7c2019-04-03 15:25:00 -0700107 @Override
Tony Wickham12e8a342019-04-23 11:28:54 -0700108 protected void onLayout(boolean changed, int l, int t, int r, int b) {
109 super.onLayout(changed, l, t, r, b);
110 SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
111 setDisallowBackGesture(mDisallowBackGesture);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700112 mSysUiScrim.setSize(r - l, b - t);
Tony Wickham12e8a342019-04-23 11:28:54 -0700113 }
114
115 @TargetApi(Build.VERSION_CODES.Q)
Vinit Nayak9a846212019-09-05 11:47:12 -0700116 public void setForceHideBackArrow(boolean forceHideBackArrow) {
117 this.mForceHideBackArrow = forceHideBackArrow;
118 setDisallowBackGesture(mDisallowBackGesture);
119 }
120
121 @TargetApi(Build.VERSION_CODES.Q)
Tony Wickham12e8a342019-04-23 11:28:54 -0700122 public void setDisallowBackGesture(boolean disallowBackGesture) {
Sunny Goyala314d5a2020-05-20 20:34:04 -0700123 if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) {
Tony Wickham12e8a342019-04-23 11:28:54 -0700124 return;
125 }
126 mDisallowBackGesture = disallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -0700127 setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture)
Tony Wickham12e8a342019-04-23 11:28:54 -0700128 ? SYSTEM_GESTURE_EXCLUSION_RECT
129 : Collections.emptyList());
130 }
131
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700132 public SysUiScrim getSysUiScrim() {
133 return mSysUiScrim;
134 }
135
Sunny Goyal7185dd62018-03-14 17:51:49 -0700136 public interface WindowStateListener {
137
138 void onWindowFocusChanged(boolean hasFocus);
139
140 void onWindowVisibilityChanged(int visibility);
141 }
Adam Cohen501e1392021-02-02 16:45:07 -0800142}