From f656f6d2c1f3ff50b2c4c71d04c1b0924d176c08 Mon Sep 17 00:00:00 2001 From: Wilson Wu Date: Thu, 26 Dec 2019 16:17:56 +0800 Subject: [PATCH] Use the main thread to update alignment indication The alignment status is reported from WLC-HAL level and runs on another thread. It may cause CalledFromWrongThreadException, if use that thread to update UI directly. Bug: 146770234 Test: atest SystemUITests:KeyguardIndicationControllerTest Change-Id: I89bf1c188d6ba094106e059f1590c9eaf3703bb8 (cherry picked from commit 0e72713fdf3e065ec4d1e8ca5c4f14dfcc74ebb6) Merged-In: I89bf1c188d6ba094106e059f1590c9eaf3703bb8 --- .../KeyguardIndicationController.java | 3 +- .../KeyguardIndicationControllerTest.java | 50 +++++++++++-------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java index 8ee7305d99c56..4256976249b15 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java @@ -166,7 +166,8 @@ public class KeyguardIndicationController implements StateListener, mStatusBarStateController = statusBarStateController; mKeyguardUpdateMonitor = keyguardUpdateMonitor; mDockManager = dockManager; - mDockManager.addAlignmentStateListener(this::handleAlignStateChanged); + mDockManager.addAlignmentStateListener( + alignState -> mHandler.post(() -> handleAlignStateChanged(alignState))); // lock icon is not used on all form factors. if (mLockIcon != null) { mLockIcon.setOnLongClickListener(this::handleLockLongClick); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java index 2fe51d35c490f..8592fd1e9be2b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/KeyguardIndicationControllerTest.java @@ -223,12 +223,14 @@ public class KeyguardIndicationControllerTest extends SysuiTestCase { @Test public void onAlignmentStateChanged_showsSlowChargingIndication() { - createController(); - verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); - mController.setVisible(true); + mInstrumentation.runOnMainSync(() -> { + createController(); + verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); + mController.setVisible(true); - mAlignmentListener.getValue().onAlignmentStateChanged( - DockManager.ALIGN_STATE_POOR); + mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR); + }); + mInstrumentation.waitForIdleSync(); assertThat(mTextView.getText()).isEqualTo( mContext.getResources().getString(R.string.dock_alignment_slow_charging)); @@ -238,11 +240,14 @@ public class KeyguardIndicationControllerTest extends SysuiTestCase { @Test public void onAlignmentStateChanged_showsNotChargingIndication() { - createController(); - verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); - mController.setVisible(true); + mInstrumentation.runOnMainSync(() -> { + createController(); + verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); + mController.setVisible(true); - mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); + mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); + }); + mInstrumentation.waitForIdleSync(); assertThat(mTextView.getText()).isEqualTo( mContext.getResources().getString(R.string.dock_alignment_not_charging)); @@ -252,13 +257,15 @@ public class KeyguardIndicationControllerTest extends SysuiTestCase { @Test public void onAlignmentStateChanged_whileDozing_showsSlowChargingIndication() { - createController(); - verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); - mController.setVisible(true); - mController.setDozing(true); + mInstrumentation.runOnMainSync(() -> { + createController(); + verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); + mController.setVisible(true); + mController.setDozing(true); - mAlignmentListener.getValue().onAlignmentStateChanged( - DockManager.ALIGN_STATE_POOR); + mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR); + }); + mInstrumentation.waitForIdleSync(); assertThat(mTextView.getText()).isEqualTo( mContext.getResources().getString(R.string.dock_alignment_slow_charging)); @@ -268,12 +275,15 @@ public class KeyguardIndicationControllerTest extends SysuiTestCase { @Test public void onAlignmentStateChanged_whileDozing_showsNotChargingIndication() { - createController(); - verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); - mController.setVisible(true); - mController.setDozing(true); + mInstrumentation.runOnMainSync(() -> { + createController(); + verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); + mController.setVisible(true); + mController.setDozing(true); - mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); + mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); + }); + mInstrumentation.waitForIdleSync(); assertThat(mTextView.getText()).isEqualTo( mContext.getResources().getString(R.string.dock_alignment_not_charging));