blob: f26cfe872e8fe78f72f1258b6fd200569f3821ed [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;
16
Tony Wickham12e8a342019-04-23 11:28:54 -070017import java.util.Collections;
18import java.util.List;
19
Adam Cohena6d04922014-10-23 17:28:30 -070020public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070021
Sunny Goyal022b1822019-06-12 08:44:09 -070022 private final Rect mTempRect = new Rect();
23
Sunny Goyal9d89f752020-06-19 11:09:24 -070024 private final StatefulActivity mActivity;
Sunny Goyal07b69292018-01-08 14:19:34 -080025
Sunny Goyal745df7c2019-04-03 15:25:00 -070026 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickham12e8a342019-04-23 11:28:54 -070027 private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT =
28 Collections.singletonList(new Rect());
29
Sunny Goyal7185dd62018-03-14 17:51:49 -070030 private WindowStateListener mWindowStateListener;
Tony Wickham12e8a342019-04-23 11:28:54 -070031 @ViewDebug.ExportedProperty(category = "launcher")
32 private boolean mDisallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -070033 @ViewDebug.ExportedProperty(category = "launcher")
34 private boolean mForceHideBackArrow;
Sunny Goyal93264612015-11-23 11:47:50 -080035
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070036 private final SysUiScrim mSysUiScrim;
Adam Cohen501e1392021-02-02 16:45:07 -080037
Adam Cohena6d04922014-10-23 17:28:30 -070038 public LauncherRootView(Context context, AttributeSet attrs) {
39 super(context, attrs);
Sunny Goyal9d89f752020-06-19 11:09:24 -070040 mActivity = StatefulActivity.fromContext(context);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070041 mSysUiScrim = new SysUiScrim(this);
Adam Cohena6d04922014-10-23 17:28:30 -070042 }
43
Sunny Goyal022b1822019-06-12 08:44:09 -070044 private void handleSystemWindowInsets(Rect insets) {
Tony Wickham145b7fd2021-03-11 10:03:12 -080045 DeviceProfile dp = mActivity.getDeviceProfile();
46
47 // Taskbar provides insets, but we don't want that for most Launcher elements so remove it.
48 mTempRect.set(insets);
49 insets = mTempRect;
50 insets.bottom = Math.max(0, insets.bottom - dp.nonOverlappingTaskbarInset);
51
52 // Update device profile before notifying the children.
53 dp.updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080054 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070055 setInsets(insets);
56
Sunny Goyalce8809a2018-01-18 14:02:47 -080057 if (resetState) {
Sunny Goyal9d89f752020-06-19 11:09:24 -070058 mActivity.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
Sunny Goyalce8809a2018-01-18 14:02:47 -080059 }
Sunny Goyal022b1822019-06-12 08:44:09 -070060 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070061
Sunny Goyal022b1822019-06-12 08:44:09 -070062 @Override
63 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
64 mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
65 insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
66 handleSystemWindowInsets(mTempRect);
Sunny Goyal786940a2020-06-02 02:31:31 -070067 return insets;
Adam Cohena6d04922014-10-23 17:28:30 -070068 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070069
Jon Mirandade43a712018-01-18 11:35:10 -080070 @Override
71 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080072 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
73 // modifying child layout params.
74 if (!insets.equals(mInsets)) {
75 super.setInsets(insets);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070076 mSysUiScrim.onInsetsChanged(insets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080077 }
Jon Mirandade43a712018-01-18 11:35:10 -080078 }
79
Sunny Goyal07b69292018-01-08 14:19:34 -080080 public void dispatchInsets() {
Sunny Goyal9d89f752020-06-19 11:09:24 -070081 mActivity.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080082 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -080083 }
84
Sunny Goyal7185dd62018-03-14 17:51:49 -070085 public void setWindowStateListener(WindowStateListener listener) {
86 mWindowStateListener = listener;
87 }
88
89 @Override
90 public void onWindowFocusChanged(boolean hasWindowFocus) {
91 super.onWindowFocusChanged(hasWindowFocus);
92 if (mWindowStateListener != null) {
93 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
94 }
95 }
96
97 @Override
98 protected void onWindowVisibilityChanged(int visibility) {
99 super.onWindowVisibilityChanged(visibility);
100 if (mWindowStateListener != null) {
101 mWindowStateListener.onWindowVisibilityChanged(visibility);
102 }
103 }
104
Adam Cohen501e1392021-02-02 16:45:07 -0800105 @Override
106 protected void dispatchDraw(Canvas canvas) {
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700107 mSysUiScrim.draw(canvas);
Adam Cohen501e1392021-02-02 16:45:07 -0800108 super.dispatchDraw(canvas);
109 }
110
Sunny Goyal745df7c2019-04-03 15:25:00 -0700111 @Override
Tony Wickham12e8a342019-04-23 11:28:54 -0700112 protected void onLayout(boolean changed, int l, int t, int r, int b) {
113 super.onLayout(changed, l, t, r, b);
114 SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
115 setDisallowBackGesture(mDisallowBackGesture);
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700116 mSysUiScrim.setSize(r - l, b - t);
Tony Wickham12e8a342019-04-23 11:28:54 -0700117 }
118
119 @TargetApi(Build.VERSION_CODES.Q)
Vinit Nayak9a846212019-09-05 11:47:12 -0700120 public void setForceHideBackArrow(boolean forceHideBackArrow) {
121 this.mForceHideBackArrow = forceHideBackArrow;
122 setDisallowBackGesture(mDisallowBackGesture);
123 }
124
125 @TargetApi(Build.VERSION_CODES.Q)
Tony Wickham12e8a342019-04-23 11:28:54 -0700126 public void setDisallowBackGesture(boolean disallowBackGesture) {
Sunny Goyala314d5a2020-05-20 20:34:04 -0700127 if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) {
Tony Wickham12e8a342019-04-23 11:28:54 -0700128 return;
129 }
130 mDisallowBackGesture = disallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -0700131 setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture)
Tony Wickham12e8a342019-04-23 11:28:54 -0700132 ? SYSTEM_GESTURE_EXCLUSION_RECT
133 : Collections.emptyList());
134 }
135
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700136 public SysUiScrim getSysUiScrim() {
137 return mSysUiScrim;
138 }
139
Sunny Goyal7185dd62018-03-14 17:51:49 -0700140 public interface WindowStateListener {
141
142 void onWindowFocusChanged(boolean hasFocus);
143
144 void onWindowVisibilityChanged(int visibility);
145 }
Adam Cohen501e1392021-02-02 16:45:07 -0800146}