Skip to content

Commit

Permalink
[Predictive Back] Fix custom nav drawer Catalog demo pre-T crash due …
Browse files Browse the repository at this point in the history
…to OnBackAnimationCallback class not found

PiperOrigin-RevId: 527348628
  • Loading branch information
dsn5ft authored and leticiarossi committed Apr 26, 2023
1 parent f335a50 commit d8c5c2c
Showing 1 changed file with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,6 @@
@SuppressWarnings("RestrictTo")
public class CustomNavigationDrawerDemoActivity extends DemoActivity {

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
private final OnBackAnimationCallback drawerOnBackAnimationCallback =
new OnBackAnimationCallback() {
@Override
public void onBackStarted(@NonNull BackEvent backEvent) {
sideContainerBackHelper.startBackProgress(backEvent);
}

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

@Override
public void onBackInvoked() {
BackEvent backEvent = sideContainerBackHelper.onHandleBackInvoked();
if (backEvent == null) {
drawerLayout.closeDrawers();
return;
}

DrawerLayout.LayoutParams drawerLayoutParams =
(LayoutParams) currentDrawerView.getLayoutParams();
int gravity = drawerLayoutParams.gravity;
AnimatorListener scrimCloseAnimatorListener =
DrawerLayoutUtils.getScrimCloseAnimatorListener(drawerLayout, currentDrawerView);
AnimatorUpdateListener scrimCloseAnimatorUpdateListener =
DrawerLayoutUtils.getScrimCloseAnimatorUpdateListener(drawerLayout);

sideContainerBackHelper.finishBackProgress(
backEvent, gravity, scrimCloseAnimatorListener, scrimCloseAnimatorUpdateListener);
}

@Override
public void onBackCancelled() {
sideContainerBackHelper.cancelBackProgress();
}
};

private final OnBackPressedCallback drawerOnBackPressedCallback =
new OnBackPressedCallback(/* enabled= */ true) {
@Override
Expand All @@ -95,6 +54,8 @@ public void handleOnBackPressed() {
}
};

@Nullable private OnBackAnimationCallback drawerOnBackAnimationCallback;

private DrawerLayout drawerLayout;
private View currentDrawerView;
private MaterialSideContainerBackHelper sideContainerBackHelper;
Expand Down Expand Up @@ -128,6 +89,9 @@ public void onDrawerOpened(@NonNull View drawerView) {
sideContainerBackHelper = new MaterialSideContainerBackHelper(drawerView);

if (BuildCompat.isAtLeastU()) {
if (drawerOnBackAnimationCallback == null) {
drawerOnBackAnimationCallback = createOnBackAnimationCallback();
}
drawerLayout.post(
() ->
getOnBackInvokedDispatcher()
Expand All @@ -147,8 +111,11 @@ public void onDrawerClosed(@NonNull View drawerView) {
sideContainerBackHelper = null;

if (BuildCompat.isAtLeastU()) {
getOnBackInvokedDispatcher()
.unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);
if (drawerOnBackAnimationCallback != null) {
getOnBackInvokedDispatcher()
.unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);
drawerOnBackAnimationCallback = null;
}
} else {
drawerOnBackPressedCallback.remove();
}
Expand All @@ -166,4 +133,47 @@ public void onDrawerClosed(@NonNull View drawerView) {
protected boolean shouldShowDefaultDemoActionBar() {
return false;
}

@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
private OnBackAnimationCallback createOnBackAnimationCallback() {
return new OnBackAnimationCallback() {

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

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

@Override
public void onBackInvoked() {
BackEvent backEvent = sideContainerBackHelper.onHandleBackInvoked();
if (backEvent == null) {
drawerLayout.closeDrawers();
return;
}

DrawerLayout.LayoutParams drawerLayoutParams =
(LayoutParams) currentDrawerView.getLayoutParams();
int gravity = drawerLayoutParams.gravity;
AnimatorListener scrimCloseAnimatorListener =
DrawerLayoutUtils.getScrimCloseAnimatorListener(drawerLayout, currentDrawerView);
AnimatorUpdateListener scrimCloseAnimatorUpdateListener =
DrawerLayoutUtils.getScrimCloseAnimatorUpdateListener(drawerLayout);

sideContainerBackHelper.finishBackProgress(
backEvent, gravity, scrimCloseAnimatorListener, scrimCloseAnimatorUpdateListener);
}

@Override
public void onBackCancelled() {
sideContainerBackHelper.cancelBackProgress();
}
};
}
}

0 comments on commit d8c5c2c

Please sign in to comment.