Remove BatteryHeaderPreferenceController usage in PowerUsageSummary.
This is a no-op refactor. This change makes the BatteryHeaderPreference Catalyst migration doesn't depend on PowerUsageSummary.
Bug: 372774754
Test: manual
Flag: EXEMPT bug fix
Change-Id: I3d8d2856036f6840a0a163c35668f55baee487ab
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
index ca49d5b..18c0f01 100644
--- a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
@@ -16,10 +16,17 @@
package com.android.settings.fuelgauge;
+import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT;
+
import android.content.Context;
import android.content.Intent;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleEventObserver;
+import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
@@ -29,27 +36,56 @@
/** Controller that update the battery header view */
public class BatteryHeaderPreferenceController extends BasePreferenceController
- implements PreferenceControllerMixin {
+ implements PreferenceControllerMixin, LifecycleEventObserver {
private static final String TAG = "BatteryHeaderPreferenceController";
private static final int BATTERY_MAX_LEVEL = 100;
- @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPref;
+ @Nullable @VisibleForTesting BatteryBroadcastReceiver mBatteryBroadcastReceiver;
+ @Nullable @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPreference;
public BatteryHeaderPreferenceController(Context context, String key) {
super(context, key);
}
@Override
+ public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
+ @NonNull Lifecycle.Event event) {
+ switch (event) {
+ case ON_CREATE:
+ mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
+ mBatteryBroadcastReceiver.setBatteryChangedListener(
+ type -> {
+ if (type != BATTERY_NOT_PRESENT) {
+ quickUpdateHeaderPreference();
+ }
+ });
+ break;
+ case ON_START:
+ if (mBatteryBroadcastReceiver != null) {
+ mBatteryBroadcastReceiver.register();
+ }
+ break;
+ case ON_STOP:
+ if (mBatteryBroadcastReceiver != null) {
+ mBatteryBroadcastReceiver.unRegister();
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ @Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
- mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
+ mBatteryUsageProgressBarPreference = screen.findPreference(getPreferenceKey());
// Hide the bottom summary from the progress bar.
- mBatteryUsageProgressBarPref.setBottomSummary("");
+ mBatteryUsageProgressBarPreference.setBottomSummary("");
if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference();
} else {
- mBatteryUsageProgressBarPref.setVisible(false);
+ mBatteryUsageProgressBarPreference.setVisible(false);
}
}
@@ -60,12 +96,17 @@
/** Updates {@link UsageProgressBarPreference} information. */
public void quickUpdateHeaderPreference() {
+ if (mBatteryUsageProgressBarPreference == null) {
+ return;
+ }
+
Intent batteryBroadcast =
com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
- mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
- mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
+ mBatteryUsageProgressBarPreference.setUsageSummary(
+ formatBatteryPercentageText(batteryLevel));
+ mBatteryUsageProgressBarPreference.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
}
private CharSequence formatBatteryPercentageText(int batteryLevel) {
diff --git a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
index 9ba346c..66c2ee9 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummary.java
@@ -36,7 +36,6 @@
import com.android.settings.R;
import com.android.settings.Utils;
-import com.android.settings.fuelgauge.BatteryHeaderPreferenceController;
import com.android.settings.fuelgauge.BatteryHeaderTextPreferenceController;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryInfoLoader;
@@ -68,7 +67,6 @@
@VisibleForTesting BatteryUtils mBatteryUtils;
@VisibleForTesting BatteryInfo mBatteryInfo;
- @VisibleForTesting BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
@VisibleForTesting BatteryHeaderTextPreferenceController mBatteryHeaderTextPreferenceController;
@VisibleForTesting BatteryTipPreferenceController mBatteryTipPreferenceController;
@VisibleForTesting boolean mNeedUpdateBatteryTip;
@@ -95,7 +93,6 @@
@Override
public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
- mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
mBatteryHeaderTextPreferenceController.updateHeaderPreference(batteryInfo);
mBatteryHeaderTextPreferenceController.updateHeaderByBatteryTips(
mBatteryTipPreferenceController.getCurrentBatteryTip(), batteryInfo);
@@ -132,7 +129,6 @@
super.onAttach(context);
final Activity activity = getActivity();
- mBatteryHeaderPreferenceController = use(BatteryHeaderPreferenceController.class);
mBatteryHeaderTextPreferenceController = use(BatteryHeaderTextPreferenceController.class);
mBatteryTipPreferenceController = use(BatteryTipPreferenceController.class);
@@ -255,15 +251,6 @@
}
@Override
- protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
- super.restartBatteryStatsLoader(refreshType);
- // Update battery header if battery is present.
- if (mIsBatteryPresent) {
- mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
- }
- }
-
- @Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBatteryTipPreferenceController.saveInstanceState(outState);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
index aa13abc..a4b8b29 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
@@ -27,6 +27,8 @@
import android.hardware.usb.UsbManager;
import android.os.BatteryManager;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
@@ -55,10 +57,11 @@
private static final int BATTERY_MAX_LEVEL = 100;
@Mock private PreferenceScreen mPreferenceScreen;
- @Mock private BatteryInfo mBatteryInfo;
+ @Mock private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
@Mock private EntityHeaderController mEntityHeaderController;
- @Mock private UsageProgressBarPreference mBatteryUsageProgressBarPref;
+ @Mock private UsageProgressBarPreference mBatteryUsageProgressBarPreference;
@Mock private UsbManager mUsbManager;
+ @Mock private LifecycleOwner mLifecycleOwner;
private BatteryHeaderPreferenceController mController;
private Context mContext;
@@ -78,14 +81,12 @@
mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
- doReturn(mBatteryUsageProgressBarPref)
+ doReturn(mBatteryUsageProgressBarPreference)
.when(mPreferenceScreen)
.findPreference(PREF_KEY);
- mBatteryInfo.batteryLevel = BATTERY_LEVEL;
-
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
- mController.mBatteryUsageProgressBarPref = mBatteryUsageProgressBarPref;
+ mController.mBatteryUsageProgressBarPreference = mBatteryUsageProgressBarPreference;
BatteryUtils.setChargingStringV2Enabled(null);
}
@@ -97,20 +98,44 @@
}
@Test
+ public void onStateChanged_onCreate_receiverCreated() {
+ mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_CREATE);
+
+ assertThat(mController.mBatteryBroadcastReceiver).isNotNull();
+ }
+
+ @Test
+ public void onStateChanged_onStart_receiverRegistered() {
+ mController.mBatteryBroadcastReceiver = mBatteryBroadcastReceiver;
+
+ mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_START);
+
+ verify(mBatteryBroadcastReceiver).register();
+ }
+
+ @Test
+ public void onStateChanged_onStop_receiverUnregistered() {
+ mController.mBatteryBroadcastReceiver = mBatteryBroadcastReceiver;
+
+ mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_STOP);
+
+ verify(mBatteryBroadcastReceiver).unRegister();
+ }
+
+ @Test
public void displayPreference_displayBatteryLevel() {
mController.displayPreference(mPreferenceScreen);
- verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
- verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
+ verify(mBatteryUsageProgressBarPreference).setUsageSummary(formatBatteryPercentageText());
+ verify(mBatteryUsageProgressBarPreference).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
-
@Test
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
mController.quickUpdateHeaderPreference();
- verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
- verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
+ verify(mBatteryUsageProgressBarPreference).setUsageSummary(formatBatteryPercentageText());
+ verify(mBatteryUsageProgressBarPreference).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
@@ -125,14 +150,14 @@
mController.displayPreference(mPreferenceScreen);
- assertThat(mBatteryUsageProgressBarPref.isVisible()).isFalse();
+ assertThat(mBatteryUsageProgressBarPreference.isVisible()).isFalse();
}
@Test
public void displayPreference_init_setEmptyBottomSummary() {
mController.displayPreference(mPreferenceScreen);
- verify(mBatteryUsageProgressBarPref).setBottomSummary("");
+ verify(mBatteryUsageProgressBarPreference).setBottomSummary("");
}
private CharSequence formatBatteryPercentageText() {