Skip to content

Commit

Permalink
Fix AlertView animation leak
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jan 15, 2019
1 parent 55afb23 commit ce7d83d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.services.android.navigation.ui.v5.alert;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PorterDuff;
Expand Down Expand Up @@ -30,12 +29,12 @@
*/
public class AlertView extends CardView {

private static final String ALERT_VIEW_PROGRESS = "progress";
private TextView alertText;
private ProgressBar alertProgressBar;

private Animation fadeOut;
private Animation slideDownTop;
private ObjectAnimator countdownAnimation;

public AlertView(Context context) {
this(context, null);
Expand All @@ -58,14 +57,6 @@ protected void onFinishInflate() {
initBackground();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (countdownAnimation != null) {
countdownAnimation.cancel();
}
}

/**
* Animates the View from top down to its set position.
* <p>
Expand Down Expand Up @@ -157,31 +148,10 @@ private void initBackground() {
}

private void startCountdown(long duration) {
countdownAnimation = ObjectAnimator.ofInt(alertProgressBar,
"progress", 0);
ObjectAnimator countdownAnimation = ObjectAnimator.ofInt(alertProgressBar, ALERT_VIEW_PROGRESS, 0);
countdownAnimation.setInterpolator(new LinearInterpolator());
countdownAnimation.setDuration(duration);
countdownAnimation.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
hide();
}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});
countdownAnimation.addListener(new AlertViewAnimatorListener(this));
countdownAnimation.start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mapbox.services.android.navigation.ui.v5.alert;

import android.animation.Animator;

import java.lang.ref.WeakReference;

class AlertViewAnimatorListener implements Animator.AnimatorListener {

private final WeakReference<AlertView> alertViewWeakReference;

AlertViewAnimatorListener(AlertView alertView) {
this.alertViewWeakReference = new WeakReference<>(alertView);
}

@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
hideAlertView();
}

@Override
public void onAnimationCancel(Animator animation) {
}

@Override
public void onAnimationRepeat(Animator animation) {
}

private void hideAlertView() {
AlertView alertView = alertViewWeakReference.get();
if (alertView != null) {
alertView.hide();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mapbox.services.android.navigation.ui.v5.alert;

import android.animation.Animator;

import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class AlertViewAnimatorListenerTest {

@Test
public void onAnimationEnd_alertViewIsHidden() {
AlertView alertView = mock(AlertView.class);
AlertViewAnimatorListener listener = new AlertViewAnimatorListener(alertView);

listener.onAnimationEnd(mock(Animator.class));

verify(alertView).hide();
}
}

0 comments on commit ce7d83d

Please sign in to comment.