[Memory Leak] Avoid adding duplicated OnScrollChangedListener to RecentsView

Use ArraySet to replace List to avoid duplication

Fix: 413449336
Test: presubmit
Flag: NONE - release code
Change-Id: I3bc21fe09ae8e6d5de4ad0389fbd1d723a620c2e
This commit is contained in:
Fengjiang Li
2025-04-25 15:41:27 -07:00
parent 8a422c3ea6
commit c4ae445ede
@@ -109,6 +109,7 @@ import android.os.VibrationEffect;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.ArraySet;
import android.util.AttributeSet;
import android.util.FloatProperty;
import android.util.Log;
@@ -142,6 +143,7 @@ import androidx.dynamicanimation.animation.SpringAnimation;
import com.android.internal.jank.Cuj;
import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.BaseActivity.MultiWindowModeChangedListener;
import com.android.launcher3.BuildConfig;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Insettable;
import com.android.launcher3.PagedView;
@@ -546,7 +548,7 @@ public abstract class RecentsView<
private final PointF mTempPointF = new PointF();
private final Matrix mTempMatrix = new Matrix();
private final float[] mTempFloat = new float[1];
private final List<OnScrollChangedListener> mScrollListeners = new ArrayList<>();
private final ArraySet<OnScrollChangedListener> mScrollListeners = new ArraySet<>();
// The threshold at which we update the SystemUI flags when animating from the task into the app
public static final float UPDATE_SYSUI_FLAGS_THRESHOLD = 0.85f;
@@ -6733,6 +6735,14 @@ public abstract class RecentsView<
* Adds a listener for scroll changes
*/
public void addOnScrollChangedListener(OnScrollChangedListener listener) {
if (mScrollListeners.contains(listener)) {
if (BuildConfig.IS_STUDIO_BUILD) {
throw new IllegalStateException(
"Should not add duplicated OnScrollChangedListener");
} else {
mScrollListeners.remove(listener);
}
}
mScrollListeners.add(listener);
}
@@ -6867,7 +6877,7 @@ public abstract class RecentsView<
runActionOnRemoteHandles(remoteTargetHandle ->
remoteTargetHandle.getTaskViewSimulator().setScroll(getScrollOffset()));
for (int i = mScrollListeners.size() - 1; i >= 0; i--) {
mScrollListeners.get(i).onScrollChanged();
mScrollListeners.valueAt(i).onScrollChanged();
}
}