Merge "Don't register multiple predraw listeners" into sc-v2-dev am: e07b60ab9e

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/16094262

Change-Id: Ief2855876fcf2aacb9484bfce58c3fe86147a8d5
This commit is contained in:
TreeHugger Robot
2021-10-20 19:24:01 +00:00
committed by Automerger Merge Worker
3 changed files with 18 additions and 10 deletions

View File

@@ -89,6 +89,7 @@ public class KeyguardClockSwitch extends RelativeLayout {
private int mClockSwitchYAmount; private int mClockSwitchYAmount;
@VisibleForTesting boolean mChildrenAreLaidOut = false; @VisibleForTesting boolean mChildrenAreLaidOut = false;
private OnPreDrawListener mPreDrawListener;
public KeyguardClockSwitch(Context context, AttributeSet attrs) { public KeyguardClockSwitch(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@@ -284,15 +285,14 @@ public class KeyguardClockSwitch extends RelativeLayout {
if (mChildrenAreLaidOut) { if (mChildrenAreLaidOut) {
animateClockChange(clockSize == LARGE); animateClockChange(clockSize == LARGE);
mDisplayedClockSize = clockSize; mDisplayedClockSize = clockSize;
} else { } else if (mPreDrawListener == null) {
getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { mPreDrawListener = () -> {
@Override
public boolean onPreDraw() {
switchToClock(clockSize); switchToClock(clockSize);
getViewTreeObserver().removeOnPreDrawListener(this); getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener);
mPreDrawListener = null;
return true; return true;
} };
}); getViewTreeObserver().addOnPreDrawListener(mPreDrawListener);
} }
return true; return true;
} }
@@ -303,6 +303,13 @@ public class KeyguardClockSwitch extends RelativeLayout {
mChildrenAreLaidOut = true; mChildrenAreLaidOut = true;
} }
void onViewDetached() {
if (mPreDrawListener != null) {
getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener);
mPreDrawListener = null;
}
}
public Paint getPaint() { public Paint getPaint() {
return mClockView.getPaint(); return mClockView.getPaint();
} }

View File

@@ -238,6 +238,7 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
} }
mColorExtractor.removeOnColorsChangedListener(mColorsListener); mColorExtractor.removeOnColorsChangedListener(mColorsListener);
mView.setClockPlugin(null, mStatusBarStateController.getState()); mView.setClockPlugin(null, mStatusBarStateController.getState());
mView.onViewDetached();
} }
/** /**

View File

@@ -194,7 +194,7 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase {
verifyAttachment(times(1)); verifyAttachment(times(1));
listenerArgumentCaptor.getValue().onViewDetachedFromWindow(mView); listenerArgumentCaptor.getValue().onViewDetachedFromWindow(mView);
verify(mView).onViewDetached();
verify(mColorExtractor).removeOnColorsChangedListener( verify(mColorExtractor).removeOnColorsChangedListener(
any(ColorExtractor.OnColorsChangedListener.class)); any(ColorExtractor.OnColorsChangedListener.class));
} }