Register back action to close the KQS view

As the KQS view is not extending the floating view, we have to
explicitly register the back action to close the KQS view.

Bug: 379778483
Test: open KQS via alt-tab/overflow button and tap the back button
Flag: com.android.launcher3.taskbar_overflow
Change-Id: I140813d1792f0c17aee5f9f8dd9fa3ed8b40ba6c
This commit is contained in:
Wen-Chien Wang authored and Wen-chien Wang committed 2024-12-02 21:00:35 +00:00
1 parent d091b42778
commit ecc50c6d7c
2 files changed
+38

No files matched your search

@@ -35,6 +35,8 @@ import android.view.ViewTreeObserver;
import android.view.animation.Interpolator;
import android.widget.HorizontalScrollView;
import android.widget.TextView;
import android.window.OnBackInvokedDispatcher;
import android.window.WindowOnBackInvokedDispatcher;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
@@ -109,6 +111,8 @@ public class KeyboardQuickSwitchView extends ConstraintLayout {
@Nullable private AnimatorSet mOpenAnimation;
private boolean mIsBackCallbackRegistered = false;
@Nullable private KeyboardQuickSwitchViewController.ViewCallbacks mViewCallbacks;
public KeyboardQuickSwitchView(@NonNull Context context) {
@@ -158,6 +162,34 @@ public class KeyboardQuickSwitchView extends ConstraintLayout {
mIsRtl = Utilities.isRtl(resources);
}
private void registerOnBackInvokedCallback() {
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
if (isOnBackInvokedCallbackEnabled(dispatcher)
&& !mIsBackCallbackRegistered) {
dispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_OVERLAY, mViewCallbacks.onBackInvokedCallback);
mIsBackCallbackRegistered = true;
}
}
private void unregisterOnBackInvokedCallback() {
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
if (isOnBackInvokedCallbackEnabled(dispatcher)
&& mIsBackCallbackRegistered) {
dispatcher.unregisterOnBackInvokedCallback(
mViewCallbacks.onBackInvokedCallback);
mIsBackCallbackRegistered = false;
}
}
private boolean isOnBackInvokedCallbackEnabled(OnBackInvokedDispatcher dispatcher) {
return dispatcher instanceof WindowOnBackInvokedDispatcher
&& ((WindowOnBackInvokedDispatcher) dispatcher).isOnBackInvokedCallbackEnabled()
&& mViewCallbacks != null;
}
private KeyboardQuickSwitchTaskView createAndAddTaskView(
int index,
boolean isFinalView,
@@ -277,6 +309,7 @@ public class KeyboardQuickSwitchView extends ConstraintLayout {
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
registerOnBackInvokedCallback();
animateOpen(currentFocusIndexOverride);
getViewTreeObserver().removeOnGlobalLayoutListener(this);
@@ -293,6 +326,9 @@ public class KeyboardQuickSwitchView extends ConstraintLayout {
}
void resetViewCallbacks() {
// Unregister the back invoked callback after the view is closed and before the
// mViewCallbacks is reset.
unregisterOnBackInvokedCallback();
mViewCallbacks = null;
}