Bring back OnBackPressedHandler

- Bring back OnBackPressedHandler so we can support lower version

TODO : Recreate OnBackAnimationCallback
This commit is contained in:
MrSluffy
2023-12-16 18:59:08 +08:00
parent cc8f9828ab
commit 4276067c6c
7 changed files with 79 additions and 36 deletions

View File

@@ -168,13 +168,14 @@ public class TaskbarAllAppsSlideInView extends AbstractSlideInView<TaskbarOverla
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mActivityContext.addOnDeviceProfileChangeListener(this);
// TODO LAWNCHAIR
if (FeatureFlags.ENABLE_BACK_SWIPE_LAUNCHER_ANIMATION.get()) {
mAppsView.getAppsRecyclerViewContainer().setOutlineProvider(mViewOutlineProvider);
mAppsView.getAppsRecyclerViewContainer().setClipToOutline(true);
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
if (dispatcher != null) {
dispatcher.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, this);
OnBackInvokedDispatcher.PRIORITY_DEFAULT, null);
}
}
}
@@ -188,7 +189,7 @@ public class TaskbarAllAppsSlideInView extends AbstractSlideInView<TaskbarOverla
mAppsView.getAppsRecyclerViewContainer().setClipToOutline(false);
OnBackInvokedDispatcher dispatcher = findOnBackInvokedDispatcher();
if (dispatcher != null) {
dispatcher.unregisterOnBackInvokedCallback(this);
dispatcher.unregisterOnBackInvokedCallback(null);
}
}
}

View File

@@ -93,6 +93,7 @@ import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherState;
import com.android.launcher3.OnBackPressedHandler;
import com.android.launcher3.QuickstepAccessibilityDelegate;
import com.android.launcher3.QuickstepTransitionManager;
import com.android.launcher3.R;
@@ -755,51 +756,49 @@ public class QuickstepLauncher extends Launcher {
new OnBackAnimationCallback() {
@Nullable
OnBackAnimationCallback mActiveOnBackAnimationCallback;
OnBackPressedHandler mActiveOnBackPressedHandler;
@Override
public void onBackStarted(@NonNull BackEvent backEvent) {
if (mActiveOnBackAnimationCallback != null) {
mActiveOnBackAnimationCallback.onBackCancelled();
if (mActiveOnBackPressedHandler != null) {
mActiveOnBackPressedHandler.onBackCancelled();
}
mActiveOnBackAnimationCallback = getOnBackAnimationCallback();
mActiveOnBackAnimationCallback.onBackStarted(backEvent);
mActiveOnBackPressedHandler = getOnBackPressedHandler();
mActiveOnBackPressedHandler.onBackStarted();
}
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void onBackInvoked() {
// Recreate mActiveOnBackAnimationCallback if necessary to avoid NPE
// Recreate mActiveOnBackPressedHandler if necessary to avoid NPE
// because:
// 1. b/260636433: In 3-button-navigation mode, onBackStarted() is not
// called on ACTION_DOWN before onBackInvoked() is called in ACTION_UP.
// 2. Launcher#onBackPressed() will call onBackInvoked() without calling
// onBackInvoked() beforehand.
if (mActiveOnBackAnimationCallback == null) {
mActiveOnBackAnimationCallback = getOnBackAnimationCallback();
if (mActiveOnBackPressedHandler == null) {
mActiveOnBackPressedHandler = getOnBackPressedHandler();
}
mActiveOnBackAnimationCallback.onBackInvoked();
mActiveOnBackAnimationCallback = null;
mActiveOnBackPressedHandler.onBackInvoked();
mActiveOnBackPressedHandler = null;
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked");
}
@Override
public void onBackProgressed(@NonNull BackEvent backEvent) {
if (!FeatureFlags.IS_STUDIO_BUILD
&& mActiveOnBackAnimationCallback == null) {
&& mActiveOnBackPressedHandler == null) {
return;
}
mActiveOnBackAnimationCallback.onBackProgressed(backEvent);
mActiveOnBackPressedHandler.onBackProgressed(backEvent.getProgress());
}
@Override
public void onBackCancelled() {
if (!FeatureFlags.IS_STUDIO_BUILD
&& mActiveOnBackAnimationCallback == null) {
&& mActiveOnBackPressedHandler == null) {
return;
}
mActiveOnBackAnimationCallback.onBackCancelled();
mActiveOnBackAnimationCallback = null;
mActiveOnBackPressedHandler.onBackCancelled();
mActiveOnBackPressedHandler = null;
}
});
}

View File

@@ -34,8 +34,6 @@ import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.window.OnBackAnimationCallback;
import androidx.annotation.IntDef;
import com.android.launcher3.anim.PendingAnimation;
@@ -51,7 +49,7 @@ import java.lang.annotation.RetentionPolicy;
*/
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public abstract class AbstractFloatingView extends LinearLayout implements TouchController,
OnBackAnimationCallback {
OnBackPressedHandler {
@IntDef(flag = true, value = {
TYPE_COMPOSE_VIEW,

View File

@@ -131,6 +131,7 @@ import android.window.BackEvent;
import android.window.OnBackAnimationCallback;
import androidx.annotation.CallSuper;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
@@ -636,8 +637,7 @@ public class Launcher extends StatefulActivity<LauncherState>
* previous 3 don't.
*/
@NonNull
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
protected OnBackAnimationCallback getOnBackAnimationCallback() {
protected OnBackPressedHandler getOnBackPressedHandler() {
// #1 auto cancel action mode handler
if (isInAutoCancelActionMode()) {
return this::finishAutoCancelActionMode;
@@ -655,16 +655,17 @@ public class Launcher extends StatefulActivity<LauncherState>
}
// #4 state handler
return new OnBackAnimationCallback() {
return new OnBackPressedHandler() {
@Override
public void onBackInvoked() {
onStateBack();
}
@Override
public void onBackProgressed(@NonNull BackEvent backEvent) {
public void onBackProgressed(
@FloatRange(from = 0.0, to = 1.0) float backProgress) {
mStateManager.getState().onBackProgressed(
Launcher.this, backEvent.getProgress());
Launcher.this, backProgress);
}
@Override
@@ -2198,7 +2199,7 @@ public class Launcher extends StatefulActivity<LauncherState>
@Override
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void onBackPressed() {
getOnBackAnimationCallback().onBackInvoked();
getOnBackPressedHandler().onBackInvoked();
}
protected void onStateBack() {

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3;
import androidx.annotation.FloatRange;
/**
* Interface that mimics {@link android.window.OnBackInvokedCallback} without dependencies on U's
* API such as {@link android.window.BackEvent}.
*
* <p> Impl can assume below order during a back gesture:
* <ol>
* <li> [optional] one {@link #onBackStarted()} will be called to start the gesture
* <li> zero or multiple {@link #onBackProgressed(float)} will be called during swipe gesture
* <li> either one of {@link #onBackInvoked()} or {@link #onBackCancelled()} will be called to end
* the gesture
*/
public interface OnBackPressedHandler{
/** Called when back has started. */
default void onBackStarted() {}
/** Called when back is committed. */
void onBackInvoked();
/** Called with back gesture's progress. */
default void onBackProgressed(@FloatRange(from = 0.0, to = 1.0) float backProgress) {}
/** Called when user drops the back gesture. */
default void onBackCancelled() {}
}

View File

@@ -40,6 +40,7 @@ import android.view.ViewOutlineProvider;
import android.view.animation.Interpolator;
import android.window.BackEvent;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
@@ -294,11 +295,9 @@ public abstract class AbstractSlideInView<T extends Context & ActivityContext>
}
@Override
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void onBackProgressed(BackEvent backEvent) {
final float progress = backEvent.getProgress();
float deceleratedProgress = Interpolators.PREDICTIVE_BACK_DECELERATED_EASE.getInterpolation(progress);
mIsBackProgressing = progress > 0f;
public void onBackProgressed(float backProgress) {
float deceleratedProgress = Interpolators.PREDICTIVE_BACK_DECELERATED_EASE.getInterpolation(backProgress);
mIsBackProgressing = backProgress > 0f;
mSlideInViewScale.updateValue(PREDICTIVE_BACK_MIN_SCALE
+ (1 - PREDICTIVE_BACK_MIN_SCALE) * (1 - deceleratedProgress));
}

View File

@@ -305,9 +305,9 @@ public class WidgetsFullSheet extends BaseWidgetSheet
@Override
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
public void onBackProgressed(@NonNull BackEvent backEvent) {
super.onBackProgressed(backEvent);
mFastScroller.setVisibility(backEvent.getProgress() > 0 ? View.INVISIBLE : View.VISIBLE);
public void onBackProgressed(float backProgress) {
super.onBackProgressed(backProgress);
mFastScroller.setVisibility(backProgress > 0 ? View.INVISIBLE : View.VISIBLE);
}
private void attachScrollbarToRecyclerView(WidgetsRecyclerView recyclerView) {