From 648a02048025641b7068ac301611fad52b806838 Mon Sep 17 00:00:00 2001 From: Matt Pietal Date: Wed, 20 Oct 2021 11:30:19 -0400 Subject: [PATCH] Don't register multiple predraw listeners Also make sure they are removed properly when the view becomes detached. Fixes: 203257107 Test: atest KeyguardClockSwitchControllerTest Change-Id: Ied4d4e7774d15af71648b188ac723c9c7635f506 --- .../android/keyguard/KeyguardClockSwitch.java | 25 ++++++++++++------- .../KeyguardClockSwitchController.java | 1 + .../KeyguardClockSwitchControllerTest.java | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitch.java b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitch.java index 6fd0c821bad1e..3d4e896178f6f 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitch.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitch.java @@ -89,6 +89,7 @@ public class KeyguardClockSwitch extends RelativeLayout { private int mClockSwitchYAmount; @VisibleForTesting boolean mChildrenAreLaidOut = false; + private OnPreDrawListener mPreDrawListener; public KeyguardClockSwitch(Context context, AttributeSet attrs) { super(context, attrs); @@ -284,15 +285,14 @@ public class KeyguardClockSwitch extends RelativeLayout { if (mChildrenAreLaidOut) { animateClockChange(clockSize == LARGE); mDisplayedClockSize = clockSize; - } else { - getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { - @Override - public boolean onPreDraw() { - switchToClock(clockSize); - getViewTreeObserver().removeOnPreDrawListener(this); - return true; - } - }); + } else if (mPreDrawListener == null) { + mPreDrawListener = () -> { + switchToClock(clockSize); + getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener); + mPreDrawListener = null; + return true; + }; + getViewTreeObserver().addOnPreDrawListener(mPreDrawListener); } return true; } @@ -303,6 +303,13 @@ public class KeyguardClockSwitch extends RelativeLayout { mChildrenAreLaidOut = true; } + void onViewDetached() { + if (mPreDrawListener != null) { + getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener); + mPreDrawListener = null; + } + } + public Paint getPaint() { return mClockView.getPaint(); } diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java index 01976b41a4b29..1931c0a1645ca 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java @@ -238,6 +238,7 @@ public class KeyguardClockSwitchController extends ViewController