Skip to content

Commit

Permalink
[Predictive Back] Fixed IllegalStateException crashes caused by Mater…
Browse files Browse the repository at this point in the history
…ialBackAnimationHelper.

PiperOrigin-RevId: 555180647
  • Loading branch information
afohrman authored and dsn5ft committed Aug 9, 2023
1 parent af7d09a commit 02dc779
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import android.animation.TimeInterpolator;
import android.content.Context;
import android.util.Log;
import android.view.View;
import androidx.activity.BackEventCompat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.core.view.animation.PathInterpolatorCompat;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* Base helper class for views that support back handling, which assists with common animation
Expand All @@ -37,6 +37,7 @@
@RestrictTo(Scope.LIBRARY_GROUP)
public abstract class MaterialBackAnimationHelper<V extends View> {

private static final String TAG = "MaterialBackHelper";
private static final int HIDE_DURATION_MAX_DEFAULT = 300;
private static final int HIDE_DURATION_MIN_DEFAULT = 150;
private static final int CANCEL_DURATION_DEFAULT = 100;
Expand Down Expand Up @@ -78,11 +79,14 @@ protected void onStartBackProgress(@NonNull BackEventCompat backEvent) {
this.backEvent = backEvent;
}

protected void onUpdateBackProgress(@NonNull BackEventCompat backEvent) {
@Nullable
protected BackEventCompat onUpdateBackProgress(@NonNull BackEventCompat backEvent) {
if (this.backEvent == null) {
throw new IllegalStateException("Must call startBackProgress() before updateBackProgress()");
Log.w(TAG, "Must call startBackProgress() before updateBackProgress()");
}
BackEventCompat finalBackEvent = this.backEvent;
this.backEvent = backEvent;
return finalBackEvent;
}

@Nullable
Expand All @@ -92,11 +96,11 @@ public BackEventCompat onHandleBackInvoked() {
return finalBackEvent;
}

@CanIgnoreReturnValue
@NonNull
@Nullable
protected BackEventCompat onCancelBackProgress() {
if (this.backEvent == null) {
throw new IllegalStateException(
Log.w(
TAG,
"Must call startBackProgress() and updateBackProgress() before cancelBackProgress()");
}
BackEventCompat finalBackEvent = this.backEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public void startBackProgress(@NonNull BackEventCompat backEvent) {
}

public void updateBackProgress(@NonNull BackEventCompat backEvent) {
super.onUpdateBackProgress(backEvent);
if (super.onUpdateBackProgress(backEvent) == null) {
return;
}

updateBackProgress(backEvent.getProgress());
}
Expand Down Expand Up @@ -131,7 +133,9 @@ public void onAnimationEnd(Animator animation) {
}

public void cancelBackProgress() {
super.onCancelBackProgress();
if (super.onCancelBackProgress() == null) {
return;
}

Animator animator = createResetScaleAnimator();
animator.setDuration(cancelDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public void startBackProgress(float touchY, @Nullable View collapsedView) {

public void updateBackProgress(
@NonNull BackEventCompat backEvent, @Nullable View collapsedView, float collapsedCornerSize) {
super.onUpdateBackProgress(backEvent);
if (super.onUpdateBackProgress(backEvent) == null) {
return;
}

if (collapsedView != null && collapsedView.getVisibility() != View.INVISIBLE) {
collapsedView.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -150,7 +152,9 @@ public void finishBackProgress(long duration, @Nullable View collapsedView) {
}

public void cancelBackProgress(@Nullable View collapsedView) {
super.onCancelBackProgress();
if (super.onCancelBackProgress() == null) {
return;
}

AnimatorSet cancelAnimatorSet = createResetScaleAndTranslationAnimator(collapsedView);
if (view instanceof ClippableRoundedCornerLayout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public void startBackProgress(@NonNull BackEventCompat backEvent) {
}

public void updateBackProgress(@NonNull BackEventCompat backEvent, @GravityInt int gravity) {
super.onUpdateBackProgress(backEvent);
if (super.onUpdateBackProgress(backEvent) == null) {
return;
}

boolean leftSwipeEdge = backEvent.getSwipeEdge() == BackEventCompat.EDGE_LEFT;
updateBackProgress(backEvent.getProgress(), leftSwipeEdge, gravity);
Expand Down Expand Up @@ -155,7 +157,9 @@ public void onAnimationEnd(Animator animation) {
}

public void cancelBackProgress() {
super.onCancelBackProgress();
if (super.onCancelBackProgress() == null) {
return;
}

AnimatorSet cancelAnimatorSet = new AnimatorSet();
cancelAnimatorSet.playTogether(
Expand Down

0 comments on commit 02dc779

Please sign in to comment.