Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | a314d5a | 2020-05-20 20:34:04 -0700 | [diff] [blame] | 3 | import static com.android.launcher3.config.FeatureFlags.SEPARATE_RECENTS_ACTIVITY; |
Sunny Goyal | ed2d2bc | 2018-04-19 12:34:43 -0700 | [diff] [blame] | 4 | |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 5 | import android.annotation.TargetApi; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 6 | import android.content.Context; |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 7 | import android.graphics.Canvas; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 8 | import android.graphics.Rect; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 9 | import android.os.Build; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 10 | import android.util.AttributeSet; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 11 | import android.view.ViewDebug; |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 12 | import android.view.WindowInsets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 13 | |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 14 | import com.android.launcher3.graphics.SysUiScrim; |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 15 | import com.android.launcher3.statemanager.StatefulActivity; |
| 16 | |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 17 | import java.util.Collections; |
| 18 | import java.util.List; |
| 19 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 20 | public class LauncherRootView extends InsettableFrameLayout { |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 21 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 22 | private final Rect mTempRect = new Rect(); |
| 23 | |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 24 | private final StatefulActivity mActivity; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 25 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 26 | @ViewDebug.ExportedProperty(category = "launcher") |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 27 | private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT = |
| 28 | Collections.singletonList(new Rect()); |
| 29 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 30 | private WindowStateListener mWindowStateListener; |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 31 | @ViewDebug.ExportedProperty(category = "launcher") |
| 32 | private boolean mDisallowBackGesture; |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 33 | @ViewDebug.ExportedProperty(category = "launcher") |
| 34 | private boolean mForceHideBackArrow; |
Sunny Goyal | 9326461 | 2015-11-23 11:47:50 -0800 | [diff] [blame] | 35 | |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 36 | private SysUiScrim mSysUiScrim; |
| 37 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 38 | public LauncherRootView(Context context, AttributeSet attrs) { |
| 39 | super(context, attrs); |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 40 | mActivity = StatefulActivity.fromContext(context); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 43 | private void handleSystemWindowInsets(Rect insets) { |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 44 | // Update device profile before notifying th children. |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 45 | mActivity.getDeviceProfile().updateInsets(insets); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 46 | boolean resetState = !insets.equals(mInsets); |
Sunny Goyal | 906c6b2 | 2017-08-18 00:35:52 -0700 | [diff] [blame] | 47 | setInsets(insets); |
| 48 | |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 49 | if (resetState) { |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 50 | mActivity.getStateManager().reapplyState(true /* cancelCurrentAnimation */); |
Sunny Goyal | ce8809a | 2018-01-18 14:02:47 -0800 | [diff] [blame] | 51 | } |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 52 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 53 | |
Sunny Goyal | 022b182 | 2019-06-12 08:44:09 -0700 | [diff] [blame] | 54 | @Override |
| 55 | public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
| 56 | mTempRect.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), |
| 57 | insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom()); |
| 58 | handleSystemWindowInsets(mTempRect); |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 59 | return insets; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 60 | } |
Sunny Goyal | 0abb36f | 2015-06-23 10:53:59 -0700 | [diff] [blame] | 61 | |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 62 | @Override |
| 63 | public void setInsets(Rect insets) { |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 64 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 65 | // modifying child layout params. |
| 66 | if (!insets.equals(mInsets)) { |
| 67 | super.setInsets(insets); |
| 68 | } |
Jon Miranda | de43a71 | 2018-01-18 11:35:10 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 71 | public void dispatchInsets() { |
Sunny Goyal | 9d89f75 | 2020-06-19 11:09:24 -0700 | [diff] [blame] | 72 | mActivity.getDeviceProfile().updateInsets(mInsets); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 73 | super.setInsets(mInsets); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 76 | public void setWindowStateListener(WindowStateListener listener) { |
| 77 | mWindowStateListener = listener; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 82 | super.onWindowFocusChanged(hasWindowFocus); |
| 83 | if (mWindowStateListener != null) { |
| 84 | mWindowStateListener.onWindowFocusChanged(hasWindowFocus); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void onWindowVisibilityChanged(int visibility) { |
| 90 | super.onWindowVisibilityChanged(visibility); |
| 91 | if (mWindowStateListener != null) { |
| 92 | mWindowStateListener.onWindowVisibilityChanged(visibility); |
| 93 | } |
| 94 | } |
| 95 | |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 96 | public void setSysUiScrim(SysUiScrim scrim) { |
| 97 | mSysUiScrim = scrim; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void dispatchDraw(Canvas canvas) { |
| 102 | if (mSysUiScrim != null) { |
| 103 | mSysUiScrim.draw(canvas); |
| 104 | } |
| 105 | super.dispatchDraw(canvas); |
| 106 | } |
| 107 | |
Sunny Goyal | 745df7c | 2019-04-03 15:25:00 -0700 | [diff] [blame] | 108 | @Override |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 109 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 110 | super.onLayout(changed, l, t, r, b); |
| 111 | SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b); |
| 112 | setDisallowBackGesture(mDisallowBackGesture); |
| 113 | } |
| 114 | |
| 115 | @TargetApi(Build.VERSION_CODES.Q) |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 116 | public void setForceHideBackArrow(boolean forceHideBackArrow) { |
| 117 | this.mForceHideBackArrow = forceHideBackArrow; |
| 118 | setDisallowBackGesture(mDisallowBackGesture); |
| 119 | } |
| 120 | |
| 121 | @TargetApi(Build.VERSION_CODES.Q) |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 122 | public void setDisallowBackGesture(boolean disallowBackGesture) { |
Sunny Goyal | a314d5a | 2020-05-20 20:34:04 -0700 | [diff] [blame] | 123 | if (!Utilities.ATLEAST_Q || SEPARATE_RECENTS_ACTIVITY.get()) { |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | mDisallowBackGesture = disallowBackGesture; |
Vinit Nayak | 9a84621 | 2019-09-05 11:47:12 -0700 | [diff] [blame] | 127 | setSystemGestureExclusionRects((mForceHideBackArrow || mDisallowBackGesture) |
Tony Wickham | 12e8a34 | 2019-04-23 11:28:54 -0700 | [diff] [blame] | 128 | ? SYSTEM_GESTURE_EXCLUSION_RECT |
| 129 | : Collections.emptyList()); |
| 130 | } |
| 131 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 132 | public interface WindowStateListener { |
| 133 | |
| 134 | void onWindowFocusChanged(boolean hasFocus); |
| 135 | |
| 136 | void onWindowVisibilityChanged(int visibility); |
| 137 | } |
Adam Cohen | 501e139 | 2021-02-02 16:45:07 -0800 | [diff] [blame] | 138 | } |