Merge "Use background call for BatteryController#init()" into tm-dev

This commit is contained in:
Evan Laird
2022-04-07 14:30:54 +00:00
committed by Android (Google) Code Review
2 changed files with 17 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ package com.android.systemui.statusbar.policy;
import static android.os.BatteryManager.EXTRA_PRESENT; import static android.os.BatteryManager.EXTRA_PRESENT;
import android.annotation.WorkerThread;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; 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.DemoMode;
import com.android.systemui.demomode.DemoModeController; import com.android.systemui.demomode.DemoModeController;
import com.android.systemui.power.EnhancedEstimates; import com.android.systemui.power.EnhancedEstimates;
import com.android.systemui.util.Assert;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
@@ -134,7 +136,7 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
} }
mDemoModeController.addCallback(this); mDemoModeController.addCallback(this);
updatePowerSave(); updatePowerSave();
updateEstimate(); updateEstimateInBackground();
} }
@Override @Override
@@ -339,7 +341,9 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
} }
} }
@WorkerThread
private void updateEstimate() { private void updateEstimate() {
Assert.isNotMainThread();
// if the estimate has been cached we can just use that, otherwise get a new one and // if the estimate has been cached we can just use that, otherwise get a new one and
// throw it in the cache. // throw it in the cache.
mEstimate = Estimate.getCachedEstimateIfAvailable(mContext); mEstimate = Estimate.getCachedEstimateIfAvailable(mContext);

View File

@@ -67,7 +67,7 @@ public class BatteryControllerTest extends SysuiTestCase {
private MockitoSession mMockitoSession; private MockitoSession mMockitoSession;
@Before @Before
public void setUp() { public void setUp() throws IllegalStateException {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mMockitoSession = mockitoSession() mMockitoSession = mockitoSession()
.initMocks(this) .initMocks(this)
@@ -81,6 +81,7 @@ public class BatteryControllerTest extends SysuiTestCase {
mDemoModeController, mDemoModeController,
new Handler(), new Handler(),
new Handler()); new Handler());
// Can throw if updateEstimate is called on the main thread
mBatteryController.init(); mBatteryController.init();
} }
@@ -185,4 +186,14 @@ public class BatteryControllerTest extends SysuiTestCase {
Assert.assertNull(mBatteryController.getLastPowerSaverStartView()); 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
}
} }