blob: 51504ce6a4f0801da62670dde537d44e94dd3f05 [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;
7import android.graphics.Rect;
Tony Wickham12e8a342019-04-23 11:28:54 -07008import android.os.Build;
Adam Cohena6d04922014-10-23 17:28:30 -07009import android.util.AttributeSet;
Sunny Goyal4ffec482016-02-09 11:28:52 -080010import android.view.ViewDebug;
Sunny Goyal022b1822019-06-12 08:44:09 -070011import android.view.WindowInsets;
Adam Cohena6d04922014-10-23 17:28:30 -070012
Sunny Goyal9d89f752020-06-19 11:09:24 -070013import com.android.launcher3.statemanager.StatefulActivity;
14
Tony Wickham12e8a342019-04-23 11:28:54 -070015import java.util.Collections;
16import java.util.List;
17
Adam Cohena6d04922014-10-23 17:28:30 -070018public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070019
Sunny Goyal022b1822019-06-12 08:44:09 -070020 private final Rect mTempRect = new Rect();
21
Sunny Goyal9d89f752020-06-19 11:09:24 -070022 private final StatefulActivity mActivity;
Sunny Goyal07b69292018-01-08 14:19:34 -080023
Sunny Goyal745df7c2019-04-03 15:25:00 -070024 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickham12e8a342019-04-23 11:28:54 -070025 private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT =
26 Collections.singletonList(new Rect());
27
Sunny Goyal7185dd62018-03-14 17:51:49 -070028 private WindowStateListener mWindowStateListener;
Tony Wickham12e8a342019-04-23 11:28:54 -070029 @ViewDebug.ExportedProperty(category = "launcher")
30 private boolean mDisallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -070031 @ViewDebug.ExportedProperty(category = "launcher")
32 private boolean mForceHideBackArrow;
Sunny Goyal93264612015-11-23 11:47:50 -080033
Adam Cohena6d04922014-10-23 17:28:30 -070034 public LauncherRootView(Context context, AttributeSet attrs) {
35 super(context, attrs);
Sunny Goyal9d89f752020-06-19 11:09:24 -070036 mActivity = StatefulActivity.fromContext(context);
Adam Cohena6d04922014-10-23 17:28:30 -070037 }
38
Sunny Goyal022b1822019-06-12 08:44:09 -070039 private void handleSystemWindowInsets(Rect insets) {
Sunny Goyal07b69292018-01-08 14:19:34 -080040 // Update device profile before notifying th children.
Sunny Goyal9d89f752020-06-19 11:09:24 -070041 mActivity.getDeviceProfile().updateInsets(insets);
Sunny Goyalce8809a2018-01-18 14:02:47 -080042 boolean resetState = !insets.equals(mInsets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070043 setInsets(insets);
44
Sunny Goyalce8809a2018-01-18 14:02:47 -080045 if (resetState) {
Sunny Goyal9d89f752020-06-19 11:09:24 -070046 mActivity.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
Sunny Goyalce8809a2018-01-18 14:02:47 -080047 }
Sunny Goyal022b1822019-06-12 08:44:09 -070048 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070049
Sunny Goyal022b1822019-06-12 08:44:09 -070050 @Override
51 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
52 mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
53 insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
54 handleSystemWindowInsets(mTempRect);
Sunny Goyal786940a2020-06-02 02:31:31 -070055 return insets;
Adam Cohena6d04922014-10-23 17:28:30 -070056 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070057
Jon Mirandade43a712018-01-18 11:35:10 -080058 @Override
59 public void setInsets(Rect insets) {
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080060 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
61 // modifying child layout params.
62 if (!insets.equals(mInsets)) {
63 super.setInsets(insets);
64 }
Jon Mirandade43a712018-01-18 11:35:10 -080065 }
66
Sunny Goyal07b69292018-01-08 14:19:34 -080067 public void dispatchInsets() {
Sunny Goyal9d89f752020-06-19 11:09:24 -070068 mActivity.getDeviceProfile().updateInsets(mInsets);
Sunny Goyalf8d56fc2018-01-31 15:18:11 -080069 super.setInsets(mInsets);
Sunny Goyal07b69292018-01-08 14:19:34 -080070 }
71
Sunny Goyal7185dd62018-03-14 17:51:49 -070072 public void setWindowStateListener(WindowStateListener listener) {
73 mWindowStateListener = listener;
74 }
75
76 @Override
77 public void onWindowFocusChanged(boolean hasWindowFocus) {
78 super.onWindowFocusChanged(hasWindowFocus);
79 if (mWindowStateListener != null) {
80 mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
81 }
82 }
83
84 @Override
85 protected void onWindowVisibilityChanged(int visibility) {
86 super.onWindowVisibilityChanged(visibility);
87 if (mWindowStateListener != null) {
88 mWindowStateListener.onWindowVisibilityChanged(visibility);
89 }
90 }
91
Sunny Goyal745df7c2019-04-03 15:25:00 -070092 @Override
Tony Wickham12e8a342019-04-23 11:28:54 -070093 protected void onLayout(boolean changed, int l, int t, int r, int b) {
94 super.onLayout(changed, l, t, r, b);
95 SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
96 setDisallowBackGesture(mDisallowBackGesture);
97 }
98
99 @TargetApi(Build.VERSION_CODES.Q)
Vinit Nayak9a846212019-09-05 11:47:12 -0700100 public void setForceHideBackArrow(boolean forceHideBackArrow) {
101 this.mForceHideBackArrow = forceHideBackArrow;
102 setDisallowBackGesture(mDisallowBackGesture);
103 }
104
105 @TargetApi(Build.VERSION_CODES.Q)
Tony Wickham12e8a342019-04-23 11:28:54 -0700106 public void setDisallowBackGesture(boolean disallowBackGesture) {
Sunny Goyala314d5a2020-05-20 20:34:04 -0700107 if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) {
Tony Wickham12e8a342019-04-23 11:28:54 -0700108 return;
109 }
110 mDisallowBackGesture = disallowBackGesture;
Vinit Nayak9a846212019-09-05 11:47:12 -0700111 setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture)
Tony Wickham12e8a342019-04-23 11:28:54 -0700112 ? SYSTEM_GESTURE_EXCLUSION_RECT
113 : Collections.emptyList());
114 }
115
Sunny Goyal7185dd62018-03-14 17:51:49 -0700116 public interface WindowStateListener {
117
118 void onWindowFocusChanged(boolean hasFocus);
119
120 void onWindowVisibilityChanged(int visibility);
121 }
Adam Cohena6d04922014-10-23 17:28:30 -0700122}