Skip to content

Commit 4ed2634

Browse files
authored
Fix AlertView animation leak (#1667)
1 parent db28032 commit 4ed2634

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/alert/AlertView.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.mapbox.services.android.navigation.ui.v5.alert;
22

3-
import android.animation.Animator;
43
import android.animation.ObjectAnimator;
54
import android.content.Context;
65
import android.graphics.PorterDuff;
@@ -30,12 +29,12 @@
3029
*/
3130
public class AlertView extends CardView {
3231

32+
private static final String ALERT_VIEW_PROGRESS = "progress";
3333
private TextView alertText;
3434
private ProgressBar alertProgressBar;
3535

3636
private Animation fadeOut;
3737
private Animation slideDownTop;
38-
private ObjectAnimator countdownAnimation;
3938

4039
public AlertView(Context context) {
4140
this(context, null);
@@ -58,14 +57,6 @@ protected void onFinishInflate() {
5857
initBackground();
5958
}
6059

61-
@Override
62-
protected void onDetachedFromWindow() {
63-
super.onDetachedFromWindow();
64-
if (countdownAnimation != null) {
65-
countdownAnimation.cancel();
66-
}
67-
}
68-
6960
/**
7061
* Animates the View from top down to its set position.
7162
* <p>
@@ -157,31 +148,10 @@ private void initBackground() {
157148
}
158149

159150
private void startCountdown(long duration) {
160-
countdownAnimation = ObjectAnimator.ofInt(alertProgressBar,
161-
"progress", 0);
151+
ObjectAnimator countdownAnimation = ObjectAnimator.ofInt(alertProgressBar, ALERT_VIEW_PROGRESS, 0);
162152
countdownAnimation.setInterpolator(new LinearInterpolator());
163153
countdownAnimation.setDuration(duration);
164-
countdownAnimation.addListener(new Animator.AnimatorListener() {
165-
@Override
166-
public void onAnimationStart(Animator animation) {
167-
168-
}
169-
170-
@Override
171-
public void onAnimationEnd(Animator animation) {
172-
hide();
173-
}
174-
175-
@Override
176-
public void onAnimationCancel(Animator animation) {
177-
178-
}
179-
180-
@Override
181-
public void onAnimationRepeat(Animator animation) {
182-
183-
}
184-
});
154+
countdownAnimation.addListener(new AlertViewAnimatorListener(this));
185155
countdownAnimation.start();
186156
}
187157

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mapbox.services.android.navigation.ui.v5.alert;
2+
3+
import android.animation.Animator;
4+
5+
import java.lang.ref.WeakReference;
6+
7+
class AlertViewAnimatorListener implements Animator.AnimatorListener {
8+
9+
private final WeakReference<AlertView> alertViewWeakReference;
10+
11+
AlertViewAnimatorListener(AlertView alertView) {
12+
this.alertViewWeakReference = new WeakReference<>(alertView);
13+
}
14+
15+
@Override
16+
public void onAnimationStart(Animator animation) {
17+
}
18+
19+
@Override
20+
public void onAnimationEnd(Animator animation) {
21+
hideAlertView();
22+
}
23+
24+
@Override
25+
public void onAnimationCancel(Animator animation) {
26+
}
27+
28+
@Override
29+
public void onAnimationRepeat(Animator animation) {
30+
}
31+
32+
private void hideAlertView() {
33+
AlertView alertView = alertViewWeakReference.get();
34+
if (alertView != null) {
35+
alertView.hide();
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mapbox.services.android.navigation.ui.v5.alert;
2+
3+
import android.animation.Animator;
4+
5+
import org.junit.Test;
6+
7+
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.verify;
9+
10+
public class AlertViewAnimatorListenerTest {
11+
12+
@Test
13+
public void onAnimationEnd_alertViewIsHidden() {
14+
AlertView alertView = mock(AlertView.class);
15+
AlertViewAnimatorListener listener = new AlertViewAnimatorListener(alertView);
16+
17+
listener.onAnimationEnd(mock(Animator.class));
18+
19+
verify(alertView).hide();
20+
}
21+
}

0 commit comments

Comments
 (0)