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
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 1e71dea..f4e83dd 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 @@
 
 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.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 @@
         }
         mDemoModeController.addCallback(this);
         updatePowerSave();
-        updateEstimate();
+        updateEstimateInBackground();
     }
 
     @Override
@@ -339,7 +341,9 @@
         }
     }
 
+    @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 b714df5..fec2123 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 @@
     private MockitoSession mMockitoSession;
 
     @Before
-    public void setUp() {
+    public void setUp() throws IllegalStateException {
         MockitoAnnotations.initMocks(this);
         mMockitoSession = mockitoSession()
                 .initMocks(this)
@@ -81,6 +81,7 @@
                 mDemoModeController,
                 new Handler(),
                 new Handler());
+        // Can throw if updateEstimate is called on the main thread
         mBatteryController.init();
     }
 
@@ -185,4 +186,14 @@
 
         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
+    }
 }