From 1c2b8088db28d2f5a94043a63a9ede64d5f8d463 Mon Sep 17 00:00:00 2001 From: Evan Laird Date: Wed, 6 Apr 2022 17:19:53 -0400 Subject: [PATCH] Use background call for BatteryController#init() BatteryController was doing an initial battery estimate fetch on startup, but using the wrong method which didn't use a worker thread, and this could cause ANRs Fixes: 222669305 Test: atest BatteryControllerTest Change-Id: I2116af2f5989970d53805b28fd179240099b556d --- .../statusbar/policy/BatteryControllerImpl.java | 6 +++++- .../statusbar/policy/BatteryControllerTest.java | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java index 1e71dea29eba8..f4e83dd71cb70 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java @@ -18,6 +18,7 @@ package com.android.systemui.statusbar.policy; import static android.os.BatteryManager.EXTRA_PRESENT; +import android.annotation.WorkerThread; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -43,6 +44,7 @@ import com.android.systemui.dagger.qualifiers.Main; import com.android.systemui.demomode.DemoMode; import com.android.systemui.demomode.DemoModeController; import com.android.systemui.power.EnhancedEstimates; +import com.android.systemui.util.Assert; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -134,7 +136,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC } mDemoModeController.addCallback(this); updatePowerSave(); - updateEstimate(); + updateEstimateInBackground(); } @Override @@ -339,7 +341,9 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC } } + @WorkerThread private void updateEstimate() { + Assert.isNotMainThread(); // if the estimate has been cached we can just use that, otherwise get a new one and // throw it in the cache. mEstimate = Estimate.getCachedEstimateIfAvailable(mContext); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java index b714df50106e5..fec2123b304ad 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/BatteryControllerTest.java @@ -67,7 +67,7 @@ public class BatteryControllerTest extends SysuiTestCase { private MockitoSession mMockitoSession; @Before - public void setUp() { + public void setUp() throws IllegalStateException { MockitoAnnotations.initMocks(this); mMockitoSession = mockitoSession() .initMocks(this) @@ -81,6 +81,7 @@ public class BatteryControllerTest extends SysuiTestCase { mDemoModeController, new Handler(), new Handler()); + // Can throw if updateEstimate is called on the main thread mBatteryController.init(); } @@ -185,4 +186,14 @@ public class BatteryControllerTest extends SysuiTestCase { Assert.assertNull(mBatteryController.getLastPowerSaverStartView()); } + + @Test + public void testBatteryEstimateFetch_doesNotThrow() throws IllegalStateException { + mBatteryController.getEstimatedTimeRemainingString( + (String estimate) -> { + // don't care about the result + }); + TestableLooper.get(this).processAllMessages(); + // Should not throw an exception + } }