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
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;
@VisibleForTesting
BatteryMeterView mBatteryMeterView;
@VisibleForTesting
- TextView mTimeText;
+ TextView mBatteryPercentText;
@VisibleForTesting
TextView mSummary1;
@VisibleForTesting
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
}
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 {
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("");
+ }
}
public void setCharging(boolean charging) {
mDrawable.setCharging(charging);
+ postInvalidate();
+ }
+
+ public boolean getCharging() {
+ return mDrawable.getCharging();
}
public static class BatteryMeterDrawable extends BatteryMeterDrawableBase {
performBatteryHeaderClick();
}
+ @Override
+ protected void restartBatteryStatsLoader() {
+ super.restartBatteryStatsLoader();
+ mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
+ }
+
private static class SummaryProvider implements SummaryLoader.SummaryProvider {
private final Context mContext;
private final SummaryLoader mLoader;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
+
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
private BatteryHeaderPreferenceController mController;
private Context mContext;
private BatteryMeterView mBatteryMeterView;
- private TextView mTimeText;
+ private TextView mBatteryPercentText;
private TextView mSummary;
private TextView mSummary2;
private LayoutPreference mBatteryLayoutPref;
mLifecycle = new Lifecycle();
mContext = spy(RuntimeEnvironment.application);
mBatteryMeterView = new BatteryMeterView(mContext);
- mTimeText = new TextView(mContext);
+ mBatteryPercentText = new TextView(mContext);
mSummary = new TextView(mContext);
ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
mSummary2 = new TextView(mContext);
mBatteryIntent = new Intent();
mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL);
mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100);
+ mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
mBatteryLayoutPref = new LayoutPreference(mContext, R.layout.battery_header);
mController = new BatteryHeaderPreferenceController(
mContext, mActivity, mPreferenceFragment, mLifecycle);
mController.mBatteryMeterView = mBatteryMeterView;
- mController.mTimeText = mTimeText;
+ mController.mBatteryPercentText = mBatteryPercentText;
mController.mSummary1 = mSummary;
mController.mSummary2 = mSummary2;
}
verify(mEntityHeaderController).styleActionBar(mActivity);
}
+
+ @Test
+ public void testQuickUpdateHeaderPreference_showBatteryLevelAndChargingState() {
+ mController.quickUpdateHeaderPreference();
+
+ assertThat(mBatteryMeterView.getBatteryLevel()).isEqualTo(BATTERY_LEVEL);
+ assertThat(mBatteryMeterView.getCharging()).isTrue();
+ assertThat(mBatteryPercentText.getText()).isEqualTo("60%");
+ }
}