Skip to content

Commit

Permalink
[ProgressIndicator] Unregistered animator complete callback on detach…
Browse files Browse the repository at this point in the history
… from window to prevent memory leak.

Resolves #1440

PiperOrigin-RevId: 320435677
  • Loading branch information
pekingme authored and hunterstich committed Jul 10, 2020
1 parent 44ff45c commit fabd36f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,15 @@ void resetPropertiesForNextCycle() {
}

@Override
void registerAnimatorsCompleteCallback(AnimationCallback callback) {
public void registerAnimatorsCompleteCallback(@NonNull AnimationCallback callback) {
animatorCompleteCallback = callback;
}

@Override
public void unregisterAnimatorsCompleteCallback() {
animatorCompleteCallback = null;
}

// ******************* Helper methods *******************

/** Returns the index of the next available color for indicator. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,11 @@ protected void registerDrawable(@NonNull IndeterminateDrawable drawable) {
* only {@link AnimationCallback#onAnimationEnd(Drawable)} should be overridden. Overriding
* other events may cause undesired result.
*/
abstract void registerAnimatorsCompleteCallback(AnimationCallback callback);
public abstract void registerAnimatorsCompleteCallback(@NonNull AnimationCallback callback);

/**
* Unregisters the {@link AnimationCallback} for the process to be done after the current
* animation cycle.
*/
public abstract void unregisterAnimatorsCompleteCallback();
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,15 @@ public void requestCancelAnimatorAfterCurrentCycle() {
}

@Override
public void registerAnimatorsCompleteCallback(AnimationCallback callback) {
public void registerAnimatorsCompleteCallback(@NonNull AnimationCallback callback) {
animatorCompleteCallback = callback;
}

@Override
public void unregisterAnimatorsCompleteCallback() {
animatorCompleteCallback = null;
}

// ******************* Helper methods *******************

/** Rotates the color used in segment colors. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public void requestCancelAnimatorAfterCurrentCycle() {

@Override
public void registerAnimatorsCompleteCallback(AnimationCallback callback) {
animatorCompleteCallback = callback;
// In seamless mode, indeterminate mode cannot be switched. This is left as blank in purpose.
}

@Override
public void unregisterAnimatorsCompleteCallback() {
// In seamless mode, indeterminate mode cannot be switched. This is left as blank in purpose.
}

// ******************* Helper methods *******************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,6 @@ private void initializeDrawables() {
setProgressDrawable(new DeterminateDrawable(spec, drawingDelegate));
}

registerAnimationCallbacks();
updateProgressDrawableAnimationScale();
applyNewVisibility();
}

Expand Down Expand Up @@ -316,8 +314,6 @@ public void initializeDrawables(
setIndeterminate(
indeterminateDrawable != null && (determinateDrawable == null || isIndeterminate()));

registerAnimationCallbacks();
updateProgressDrawableAnimationScale();
applyNewVisibility();
}

Expand All @@ -340,6 +336,16 @@ private void registerAnimationCallbacks() {
}
}

private void unregisterAnimationCallbacks() {
if (getIndeterminateDrawable() != null) {
getIndeterminateDrawable().unregisterAnimationCallback(hideAnimationCallback);
getIndeterminateDrawable().getAnimatorDelegate().unregisterAnimatorsCompleteCallback();
}
if (getProgressDrawable() != null) {
getProgressDrawable().unregisterAnimationCallback(hideAnimationCallback);
}
}

private void updateProgressDrawableAnimationScale() {
systemAnimationScale = getSystemAnimatorDurationScale();
if (systemAnimationScale > 0) {
Expand Down Expand Up @@ -441,6 +447,7 @@ private void applyNewVisibility() {
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
registerAnimationCallbacks();
// Shows with animation.
if (visibleToUser()) {
show();
Expand All @@ -452,6 +459,7 @@ protected void onDetachedFromWindow() {
// Removes the delayedHide runnable from the queue if it has been scheduled.
removeCallbacks(delayedHide);
getCurrentDrawable().setVisible(false, false);
unregisterAnimationCallbacks();
super.onDetachedFromWindow();
}

Expand Down

0 comments on commit fabd36f

Please sign in to comment.