Skip to content

Commit

Permalink
[Predictive Back] Update components to use BackEventCompat
Browse files Browse the repository at this point in the history
Clients should prefer listening to back events using androidx.activity.OnBackPressedCallback which has been updated to forward BackEventCompat objects in androidx.activity:activity:1.8.0-alpha05. This updates component predictive back APIs to take in BackEventCompat objects instead of android.window.BackEvent objects to simplify usage - getting rid of the need for version checks and object conversions.

PiperOrigin-RevId: 540290323
  • Loading branch information
hunterstich committed Jun 14, 2023
1 parent 9486de5 commit a67a885
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 160 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ext {
targetSdkVersion = 33

androidXVersions = [
activity : '1.8.0-alpha05',
annotation : '1.2.0',
appCompat : '1.6.1',
cardView : '1.0.0',
Expand Down Expand Up @@ -86,6 +87,8 @@ private def getTransformedProjectPath(projectPath) {
*/
def compatibility(name) {
switch (name) {
case "activity":
return "androidx.activity:activity:${androidXVersions.activity}"
case "annotation":
return "androidx.annotation:annotation:${androidXVersions.annotation}"
case "appcompat":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import io.material.catalog.R;

import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Expand Down Expand Up @@ -61,16 +59,12 @@ public class BottomAppBarMainDemoFragment extends DemoFragment {
new OnBackPressedCallback(/* enabled= */ false) {
@Override
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
bottomDrawerBehavior.startBackProgress(backEvent.toBackEvent());
}
bottomDrawerBehavior.startBackProgress(backEvent);
}

@Override
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
bottomDrawerBehavior.updateBackProgress(backEvent.toBackEvent());
}
bottomDrawerBehavior.updateBackProgress(backEvent);
}

@Override
Expand All @@ -80,9 +74,7 @@ public void handleOnBackPressed() {

@Override
public void handleOnBackCancelled() {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
bottomDrawerBehavior.cancelBackProgress();
}
bottomDrawerBehavior.cancelBackProgress();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.material.catalog.R;

import android.app.Activity;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -52,16 +50,12 @@ public class BottomSheetMainDemoFragment extends DemoFragment {

@Override
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
persistentBottomSheetBehavior.startBackProgress(backEvent.toBackEvent());
}
persistentBottomSheetBehavior.startBackProgress(backEvent);
}

@Override
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
persistentBottomSheetBehavior.updateBackProgress(backEvent.toBackEvent());
}
persistentBottomSheetBehavior.updateBackProgress(backEvent);
}

@Override
Expand All @@ -71,9 +65,7 @@ public void handleOnBackPressed() {

@Override
public void handleOnBackCancelled() {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
persistentBottomSheetBehavior.cancelBackProgress();
}
persistentBottomSheetBehavior.cancelBackProgress();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.window.BackEvent;
import android.window.OnBackAnimationCallback;
import android.window.OnBackInvokedDispatcher;
import androidx.activity.BackEventCompat;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -140,19 +141,20 @@ private OnBackAnimationCallback createOnBackAnimationCallback() {

@Override
public void onBackStarted(@NonNull BackEvent backEvent) {
sideContainerBackHelper.startBackProgress(backEvent);
sideContainerBackHelper.startBackProgress(new BackEventCompat(backEvent));
}

@Override
public void onBackProgressed(@NonNull BackEvent backEvent) {
DrawerLayout.LayoutParams drawerLayoutParams =
(LayoutParams) currentDrawerView.getLayoutParams();
sideContainerBackHelper.updateBackProgress(backEvent, drawerLayoutParams.gravity);
sideContainerBackHelper.updateBackProgress(
new BackEventCompat(backEvent), drawerLayoutParams.gravity);
}

@Override
public void onBackInvoked() {
BackEvent backEvent = sideContainerBackHelper.onHandleBackInvoked();
BackEventCompat backEvent = sideContainerBackHelper.onHandleBackInvoked();
if (backEvent == null) {
drawerLayout.closeDrawers();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import static android.view.View.NO_ID;

import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Expand Down Expand Up @@ -407,16 +405,12 @@ private OnBackPressedCallback createNonModalOnBackPressedCallback(
return new OnBackPressedCallback(/* enabled= */ false) {
@Override
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
behavior.startBackProgress(backEvent.toBackEvent());
}
behavior.startBackProgress(backEvent);
}

@Override
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
behavior.updateBackProgress(backEvent.toBackEvent());
}
behavior.updateBackProgress(backEvent);
}

@Override
Expand All @@ -426,9 +420,7 @@ public void handleOnBackPressed() {

@Override
public void handleOnBackCancelled() {
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
behavior.cancelBackProgress();
}
behavior.cancelBackProgress();
}
};
}
Expand Down
11 changes: 4 additions & 7 deletions docs/components/BottomSheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,26 +486,23 @@ to see how the component behaves when a user swipes back.
### Standard (Non-Modal) Bottom Sheets

To set up Predictive Back for standard (non-modal) bottom sheets using
`BottomSheetBehavior`, create an AndroidX back callback that forwards the system
`BackEvent` objects to your `BottomSheetBehavior`:
`BottomSheetBehavior`, create an AndroidX back callback that forwards
`BackEventCompat` objects to your `BottomSheetBehavior`:

```kt
val bottomSheetBackCallback = object : OnBackPressedCallback(/* enabled= */false) {
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackStarted(backEvent: BackEvent) {
override fun handleOnBackStarted(backEvent: BackEventCompat) {
bottomSheetBehavior.startBackProgress(backEvent)
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackProgressed(backEvent: BackEvent) {
override fun handleOnBackProgressed(backEvent: BackEventCompat) {
bottomSheetBehavior.updateBackProgress(backEvent)
}

override fun handleOnBackPressed() {
bottomSheetBehavior.handleBackInvoked()
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackCancelled() {
bottomSheetBehavior.cancelBackProgress()
}
Expand Down
11 changes: 4 additions & 7 deletions docs/components/SideSheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,26 +313,23 @@ to see how the component behaves when a user swipes back.
### Standard and Coplanar (Non-Modal) Side Sheets

To set up Predictive Back for standard or coplanar (non-modal) side sheets using
`SideSheetBehavior`, create an AndroidX back callback that forwards the system
`BackEvent` objects to your `SideSheetBehavior`:
`SideSheetBehavior`, create an AndroidX back callback that forwards
`BackEventCompat` objects to your `SideSheetBehavior`:

```kt
val sideSheetBackCallback = object : OnBackPressedCallback(/* enabled= */false) {
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackStarted(backEvent: BackEvent) {
override fun handleOnBackStarted(backEvent: BackEventCompat) {
sideSheetBehavior.startBackProgress(backEvent)
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackProgressed(backEvent: BackEvent) {
override fun handleOnBackProgressed(backEvent: BackEventCompat) {
sideSheetBehavior.updateBackProgress(backEvent)
}

override fun handleOnBackPressed() {
sideSheetBehavior.handleBackInvoked()
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
override fun handleOnBackCancelled() {
sideSheetBehavior.cancelBackProgress()
}
Expand Down
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ apply plugin: 'maven-publish'
version = mdcLibraryVersion

dependencies {
api compatibility("activity")
api compatibility("annotation")
api compatibility("appcompat")
api compatibility("cardview")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import android.view.ViewParent;
import android.view.WindowInsets;
import android.view.accessibility.AccessibilityEvent;
import android.window.BackEvent;
import androidx.activity.BackEventCompat;
import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -1588,18 +1588,16 @@ boolean shouldHide(@NonNull View child, float yvel) {
return Math.abs(newTop - collapsedOffset) / (float) peek > HIDE_THRESHOLD;
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void startBackProgress(@NonNull BackEvent backEvent) {
public void startBackProgress(@NonNull BackEventCompat backEvent) {
if (bottomContainerBackHelper == null) {
return;
}
bottomContainerBackHelper.startBackProgress(backEvent);
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void updateBackProgress(@NonNull BackEvent backEvent) {
public void updateBackProgress(@NonNull BackEventCompat backEvent) {
if (bottomContainerBackHelper == null) {
return;
}
Expand All @@ -1611,7 +1609,7 @@ public void handleBackInvoked() {
if (bottomContainerBackHelper == null) {
return;
}
BackEvent backEvent = bottomContainerBackHelper.onHandleBackInvoked();
BackEventCompat backEvent = bottomContainerBackHelper.onHandleBackInvoked();
if (backEvent == null || !BuildCompat.isAtLeastU()) {
// If using traditional button system nav or if pre-U, just hide or collapse the bottom sheet.
setState(hideable ? STATE_HIDDEN : STATE_COLLAPSED);
Expand All @@ -1637,7 +1635,6 @@ public void onAnimationEnd(Animator animation) {
}
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@Override
public void cancelBackProgress() {
if (bottomContainerBackHelper == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

import android.animation.TimeInterpolator;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.view.View;
import android.window.BackEvent;
import androidx.activity.BackEventCompat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.view.animation.PathInterpolatorCompat;
Expand All @@ -50,7 +48,7 @@ public abstract class MaterialBackAnimationHelper<V extends View> {
protected final int hideDurationMin;
protected final int cancelDuration;

@Nullable private BackEvent backEvent;
@Nullable private BackEventCompat backEvent;

public MaterialBackAnimationHelper(@NonNull V view) {
this.view = view;
Expand All @@ -76,35 +74,32 @@ protected float interpolateProgress(float progress) {
return progressInterpolator.getInterpolation(progress);
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
protected void onStartBackProgress(@NonNull BackEvent backEvent) {
protected void onStartBackProgress(@NonNull BackEventCompat backEvent) {
this.backEvent = backEvent;
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
protected void onUpdateBackProgress(@NonNull BackEvent backEvent) {
protected void onUpdateBackProgress(@NonNull BackEventCompat backEvent) {
if (this.backEvent == null) {
throw new IllegalStateException("Must call startBackProgress() before updateBackProgress()");
}
this.backEvent = backEvent;
}

@Nullable
public BackEvent onHandleBackInvoked() {
BackEvent finalBackEvent = this.backEvent;
public BackEventCompat onHandleBackInvoked() {
BackEventCompat finalBackEvent = this.backEvent;
this.backEvent = null;
return finalBackEvent;
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
@CanIgnoreReturnValue
@NonNull
protected BackEvent onCancelBackProgress() {
protected BackEventCompat onCancelBackProgress() {
if (this.backEvent == null) {
throw new IllegalStateException(
"Must call startBackProgress() and updateBackProgress() before cancelBackProgress()");
}
BackEvent finalBackEvent = this.backEvent;
BackEventCompat finalBackEvent = this.backEvent;
this.backEvent = null;
return finalBackEvent;
}
Expand Down
Loading

0 comments on commit a67a885

Please sign in to comment.