Skip to content

Commit a67a885

Browse files
committed
[Predictive Back] Update components to use BackEventCompat
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
1 parent 9486de5 commit a67a885

19 files changed

+99
-160
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ext {
2222
targetSdkVersion = 33
2323

2424
androidXVersions = [
25+
activity : '1.8.0-alpha05',
2526
annotation : '1.2.0',
2627
appCompat : '1.6.1',
2728
cardView : '1.0.0',
@@ -86,6 +87,8 @@ private def getTransformedProjectPath(projectPath) {
8687
*/
8788
def compatibility(name) {
8889
switch (name) {
90+
case "activity":
91+
return "androidx.activity:activity:${androidXVersions.activity}"
8992
case "annotation":
9093
return "androidx.annotation:annotation:${androidXVersions.annotation}"
9194
case "appcompat":

catalog/java/io/material/catalog/bottomappbar/BottomAppBarMainDemoFragment.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import io.material.catalog.R;
2020

21-
import android.os.Build.VERSION;
22-
import android.os.Build.VERSION_CODES;
2321
import android.os.Bundle;
2422
import androidx.appcompat.app.AppCompatActivity;
2523
import androidx.appcompat.widget.Toolbar;
@@ -61,16 +59,12 @@ public class BottomAppBarMainDemoFragment extends DemoFragment {
6159
new OnBackPressedCallback(/* enabled= */ false) {
6260
@Override
6361
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
64-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
65-
bottomDrawerBehavior.startBackProgress(backEvent.toBackEvent());
66-
}
62+
bottomDrawerBehavior.startBackProgress(backEvent);
6763
}
6864

6965
@Override
7066
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
71-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
72-
bottomDrawerBehavior.updateBackProgress(backEvent.toBackEvent());
73-
}
67+
bottomDrawerBehavior.updateBackProgress(backEvent);
7468
}
7569

7670
@Override
@@ -80,9 +74,7 @@ public void handleOnBackPressed() {
8074

8175
@Override
8276
public void handleOnBackCancelled() {
83-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
84-
bottomDrawerBehavior.cancelBackProgress();
85-
}
77+
bottomDrawerBehavior.cancelBackProgress();
8678
}
8779
};
8880

catalog/java/io/material/catalog/bottomsheet/BottomSheetMainDemoFragment.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import io.material.catalog.R;
2020

2121
import android.app.Activity;
22-
import android.os.Build.VERSION;
23-
import android.os.Build.VERSION_CODES;
2422
import android.os.Bundle;
2523
import android.util.DisplayMetrics;
2624
import android.view.LayoutInflater;
@@ -52,16 +50,12 @@ public class BottomSheetMainDemoFragment extends DemoFragment {
5250

5351
@Override
5452
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
55-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
56-
persistentBottomSheetBehavior.startBackProgress(backEvent.toBackEvent());
57-
}
53+
persistentBottomSheetBehavior.startBackProgress(backEvent);
5854
}
5955

6056
@Override
6157
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
62-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
63-
persistentBottomSheetBehavior.updateBackProgress(backEvent.toBackEvent());
64-
}
58+
persistentBottomSheetBehavior.updateBackProgress(backEvent);
6559
}
6660

6761
@Override
@@ -71,9 +65,7 @@ public void handleOnBackPressed() {
7165

7266
@Override
7367
public void handleOnBackCancelled() {
74-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
75-
persistentBottomSheetBehavior.cancelBackProgress();
76-
}
68+
persistentBottomSheetBehavior.cancelBackProgress();
7769
}
7870
};
7971

catalog/java/io/material/catalog/navigationdrawer/CustomNavigationDrawerDemoActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.window.BackEvent;
3131
import android.window.OnBackAnimationCallback;
3232
import android.window.OnBackInvokedDispatcher;
33+
import androidx.activity.BackEventCompat;
3334
import androidx.activity.OnBackPressedCallback;
3435
import androidx.annotation.NonNull;
3536
import androidx.annotation.Nullable;
@@ -140,19 +141,20 @@ private OnBackAnimationCallback createOnBackAnimationCallback() {
140141

141142
@Override
142143
public void onBackStarted(@NonNull BackEvent backEvent) {
143-
sideContainerBackHelper.startBackProgress(backEvent);
144+
sideContainerBackHelper.startBackProgress(new BackEventCompat(backEvent));
144145
}
145146

146147
@Override
147148
public void onBackProgressed(@NonNull BackEvent backEvent) {
148149
DrawerLayout.LayoutParams drawerLayoutParams =
149150
(LayoutParams) currentDrawerView.getLayoutParams();
150-
sideContainerBackHelper.updateBackProgress(backEvent, drawerLayoutParams.gravity);
151+
sideContainerBackHelper.updateBackProgress(
152+
new BackEventCompat(backEvent), drawerLayoutParams.gravity);
151153
}
152154

153155
@Override
154156
public void onBackInvoked() {
155-
BackEvent backEvent = sideContainerBackHelper.onHandleBackInvoked();
157+
BackEventCompat backEvent = sideContainerBackHelper.onHandleBackInvoked();
156158
if (backEvent == null) {
157159
drawerLayout.closeDrawers();
158160
return;

catalog/java/io/material/catalog/sidesheet/SideSheetMainDemoFragment.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import static android.view.View.NO_ID;
2222

23-
import android.os.Build.VERSION;
24-
import android.os.Build.VERSION_CODES;
2523
import android.os.Bundle;
2624
import androidx.appcompat.app.AppCompatActivity;
2725
import androidx.appcompat.widget.Toolbar;
@@ -407,16 +405,12 @@ private OnBackPressedCallback createNonModalOnBackPressedCallback(
407405
return new OnBackPressedCallback(/* enabled= */ false) {
408406
@Override
409407
public void handleOnBackStarted(@NonNull BackEventCompat backEvent) {
410-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
411-
behavior.startBackProgress(backEvent.toBackEvent());
412-
}
408+
behavior.startBackProgress(backEvent);
413409
}
414410

415411
@Override
416412
public void handleOnBackProgressed(@NonNull BackEventCompat backEvent) {
417-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
418-
behavior.updateBackProgress(backEvent.toBackEvent());
419-
}
413+
behavior.updateBackProgress(backEvent);
420414
}
421415

422416
@Override
@@ -426,9 +420,7 @@ public void handleOnBackPressed() {
426420

427421
@Override
428422
public void handleOnBackCancelled() {
429-
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
430-
behavior.cancelBackProgress();
431-
}
423+
behavior.cancelBackProgress();
432424
}
433425
};
434426
}

docs/components/BottomSheet.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,26 +486,23 @@ to see how the component behaves when a user swipes back.
486486
### Standard (Non-Modal) Bottom Sheets
487487

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

492492
```kt
493493
val bottomSheetBackCallback = object : OnBackPressedCallback(/* enabled= */false) {
494-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
495-
override fun handleOnBackStarted(backEvent: BackEvent) {
494+
override fun handleOnBackStarted(backEvent: BackEventCompat) {
496495
bottomSheetBehavior.startBackProgress(backEvent)
497496
}
498497

499-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
500-
override fun handleOnBackProgressed(backEvent: BackEvent) {
498+
override fun handleOnBackProgressed(backEvent: BackEventCompat) {
501499
bottomSheetBehavior.updateBackProgress(backEvent)
502500
}
503501

504502
override fun handleOnBackPressed() {
505503
bottomSheetBehavior.handleBackInvoked()
506504
}
507505

508-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
509506
override fun handleOnBackCancelled() {
510507
bottomSheetBehavior.cancelBackProgress()
511508
}

docs/components/SideSheet.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,26 +313,23 @@ to see how the component behaves when a user swipes back.
313313
### Standard and Coplanar (Non-Modal) Side Sheets
314314

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

319319
```kt
320320
val sideSheetBackCallback = object : OnBackPressedCallback(/* enabled= */false) {
321-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
322-
override fun handleOnBackStarted(backEvent: BackEvent) {
321+
override fun handleOnBackStarted(backEvent: BackEventCompat) {
323322
sideSheetBehavior.startBackProgress(backEvent)
324323
}
325324

326-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
327-
override fun handleOnBackProgressed(backEvent: BackEvent) {
325+
override fun handleOnBackProgressed(backEvent: BackEventCompat) {
328326
sideSheetBehavior.updateBackProgress(backEvent)
329327
}
330328

331329
override fun handleOnBackPressed() {
332330
sideSheetBehavior.handleBackInvoked()
333331
}
334332

335-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
336333
override fun handleOnBackCancelled() {
337334
sideSheetBehavior.cancelBackProgress()
338335
}

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ apply plugin: 'maven-publish'
66
version = mdcLibraryVersion
77

88
dependencies {
9+
api compatibility("activity")
910
api compatibility("annotation")
1011
api compatibility("appcompat")
1112
api compatibility("cardview")

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import android.view.ViewParent;
5050
import android.view.WindowInsets;
5151
import android.view.accessibility.AccessibilityEvent;
52-
import android.window.BackEvent;
52+
import androidx.activity.BackEventCompat;
5353
import androidx.annotation.FloatRange;
5454
import androidx.annotation.IntDef;
5555
import androidx.annotation.NonNull;
@@ -1588,18 +1588,16 @@ boolean shouldHide(@NonNull View child, float yvel) {
15881588
return Math.abs(newTop - collapsedOffset) / (float) peek > HIDE_THRESHOLD;
15891589
}
15901590

1591-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
15921591
@Override
1593-
public void startBackProgress(@NonNull BackEvent backEvent) {
1592+
public void startBackProgress(@NonNull BackEventCompat backEvent) {
15941593
if (bottomContainerBackHelper == null) {
15951594
return;
15961595
}
15971596
bottomContainerBackHelper.startBackProgress(backEvent);
15981597
}
15991598

1600-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
16011599
@Override
1602-
public void updateBackProgress(@NonNull BackEvent backEvent) {
1600+
public void updateBackProgress(@NonNull BackEventCompat backEvent) {
16031601
if (bottomContainerBackHelper == null) {
16041602
return;
16051603
}
@@ -1611,7 +1609,7 @@ public void handleBackInvoked() {
16111609
if (bottomContainerBackHelper == null) {
16121610
return;
16131611
}
1614-
BackEvent backEvent = bottomContainerBackHelper.onHandleBackInvoked();
1612+
BackEventCompat backEvent = bottomContainerBackHelper.onHandleBackInvoked();
16151613
if (backEvent == null || !BuildCompat.isAtLeastU()) {
16161614
// If using traditional button system nav or if pre-U, just hide or collapse the bottom sheet.
16171615
setState(hideable ? STATE_HIDDEN : STATE_COLLAPSED);
@@ -1637,7 +1635,6 @@ public void onAnimationEnd(Animator animation) {
16371635
}
16381636
}
16391637

1640-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
16411638
@Override
16421639
public void cancelBackProgress() {
16431640
if (bottomContainerBackHelper == null) {

lib/java/com/google/android/material/motion/MaterialBackAnimationHelper.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919

2020
import android.animation.TimeInterpolator;
2121
import android.content.Context;
22-
import android.os.Build.VERSION_CODES;
2322
import android.view.View;
24-
import android.window.BackEvent;
23+
import androidx.activity.BackEventCompat;
2524
import androidx.annotation.NonNull;
2625
import androidx.annotation.Nullable;
27-
import androidx.annotation.RequiresApi;
2826
import androidx.annotation.RestrictTo;
2927
import androidx.annotation.RestrictTo.Scope;
3028
import androidx.core.view.animation.PathInterpolatorCompat;
@@ -50,7 +48,7 @@ public abstract class MaterialBackAnimationHelper<V extends View> {
5048
protected final int hideDurationMin;
5149
protected final int cancelDuration;
5250

53-
@Nullable private BackEvent backEvent;
51+
@Nullable private BackEventCompat backEvent;
5452

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

79-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
80-
protected void onStartBackProgress(@NonNull BackEvent backEvent) {
77+
protected void onStartBackProgress(@NonNull BackEventCompat backEvent) {
8178
this.backEvent = backEvent;
8279
}
8380

84-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
85-
protected void onUpdateBackProgress(@NonNull BackEvent backEvent) {
81+
protected void onUpdateBackProgress(@NonNull BackEventCompat backEvent) {
8682
if (this.backEvent == null) {
8783
throw new IllegalStateException("Must call startBackProgress() before updateBackProgress()");
8884
}
8985
this.backEvent = backEvent;
9086
}
9187

9288
@Nullable
93-
public BackEvent onHandleBackInvoked() {
94-
BackEvent finalBackEvent = this.backEvent;
89+
public BackEventCompat onHandleBackInvoked() {
90+
BackEventCompat finalBackEvent = this.backEvent;
9591
this.backEvent = null;
9692
return finalBackEvent;
9793
}
9894

99-
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
10095
@CanIgnoreReturnValue
10196
@NonNull
102-
protected BackEvent onCancelBackProgress() {
97+
protected BackEventCompat onCancelBackProgress() {
10398
if (this.backEvent == null) {
10499
throw new IllegalStateException(
105100
"Must call startBackProgress() and updateBackProgress() before cancelBackProgress()");
106101
}
107-
BackEvent finalBackEvent = this.backEvent;
102+
BackEventCompat finalBackEvent = this.backEvent;
108103
this.backEvent = null;
109104
return finalBackEvent;
110105
}

0 commit comments

Comments
 (0)