Add quickUpdate method to BatteryHeaderPrefCtrl
This cl adds a method called quickUpdateHeaderPreference, which will
update the following items immediately without waiting for the
BatteryInfo:
1. Battery level
2. Battery charging status(whether to show lighting bolt)
3. Clear the charging summary
Note: this cl doesn't optimize the BatteryInfo loading time.
This cl also rename "TimeText" to "BatteryPercentText" because in
new UI that textview is used to display battery percentage.
Bug: 63029231
Test: RunSettingsRoboTests
Change-Id: I8cc886b35e937d73b46e47e303ff0994ccdcb61c
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
index bdd2413..079ab29 100644
--- a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
@@ -21,6 +21,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.os.BatteryManager;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.PreferenceScreen;
@@ -46,7 +47,7 @@
@VisibleForTesting
BatteryMeterView mBatteryMeterView;
@VisibleForTesting
- TextView mTimeText;
+ TextView mBatteryPercentText;
@VisibleForTesting
TextView mSummary1;
@VisibleForTesting
@@ -75,16 +76,11 @@
mBatteryLayoutPref = (LayoutPreference) screen.findPreference(KEY_BATTERY_HEADER);
mBatteryMeterView = (BatteryMeterView) mBatteryLayoutPref
.findViewById(R.id.battery_header_icon);
- mTimeText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
+ mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
mSummary2 = mBatteryLayoutPref.findViewById(R.id.summary2);
- Intent batteryBroadcast = mContext.registerReceiver(null,
- new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
- final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
-
- mBatteryMeterView.setBatteryLevel(batteryLevel);
- mTimeText.setText(Utils.formatPercentage(batteryLevel));
+ quickUpdateHeaderPreference();
}
@Override
@@ -106,7 +102,7 @@
}
public void updateHeaderPreference(BatteryInfo info) {
- mTimeText.setText(Utils.formatPercentage(info.batteryLevel));
+ mBatteryPercentText.setText(Utils.formatPercentage(info.batteryLevel));
if (info.remainingLabel == null) {
mSummary1.setText(info.statusLabel);
} else {
@@ -119,4 +115,21 @@
mBatteryMeterView.setBatteryLevel(info.batteryLevel);
mBatteryMeterView.setCharging(!info.discharging);
}
+
+ public void quickUpdateHeaderPreference() {
+ Intent batteryBroadcast = mContext.registerReceiver(null,
+ new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
+ final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
+ final boolean discharging =
+ batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
+
+ // Set battery level and charging status
+ mBatteryMeterView.setBatteryLevel(batteryLevel);
+ mBatteryMeterView.setCharging(!discharging);
+ mBatteryPercentText.setText(Utils.formatPercentage(batteryLevel));
+
+ // clear all the summaries
+ mSummary1.setText("");
+ mSummary2.setText("");
+ }
}