Add NPE check for battery header update.
Battery header uses AsyncTask to update the label. So if user press back button before the update completes, the "getContext()" in AsyncTask callback will return null, which causes the crash. It seems there is no good way to fix it expect adding a NPE check. Bug: 35650224 Test: RunSettingsRoboTests Change-Id: I7bd9fd87caa13614fe1896cf72557a09744691c1
This commit is contained in:
@@ -532,6 +532,10 @@ public class PowerUsageSummary extends PowerUsageBase {
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void updateHeaderPreference(BatteryInfo info) {
|
void updateHeaderPreference(BatteryInfo info) {
|
||||||
|
final Context context = getContext();
|
||||||
|
if (context == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
final BatteryMeterView batteryView = (BatteryMeterView) mBatteryLayoutPref
|
final BatteryMeterView batteryView = (BatteryMeterView) mBatteryLayoutPref
|
||||||
.findViewById(R.id.battery_header_icon);
|
.findViewById(R.id.battery_header_icon);
|
||||||
final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.time);
|
final TextView timeText = (TextView) mBatteryLayoutPref.findViewById(R.id.time);
|
||||||
@@ -542,8 +546,7 @@ public class PowerUsageSummary extends PowerUsageBase {
|
|||||||
R.string.estimated_time_left : R.string.estimated_charging_time_left;
|
R.string.estimated_time_left : R.string.estimated_charging_time_left;
|
||||||
|
|
||||||
if (info.remainingTimeUs != 0) {
|
if (info.remainingTimeUs != 0) {
|
||||||
timeText.setText(Utils.formatElapsedTime(getContext(),
|
timeText.setText(Utils.formatElapsedTime(context, info.remainingTimeUs / 1000, false));
|
||||||
info.remainingTimeUs / 1000, false));
|
|
||||||
} else {
|
} else {
|
||||||
timeText.setText(info.statusLabel);
|
timeText.setText(info.statusLabel);
|
||||||
}
|
}
|
||||||
|
@@ -358,6 +358,14 @@ public class PowerUsageSummaryTest {
|
|||||||
verify(mSummary1).setText(R.string.estimated_time_left);
|
verify(mSummary1).setText(R.string.estimated_time_left);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateHeaderPreference_AsyncUpdate_ShouldNotCrash() {
|
||||||
|
when(mPowerUsageSummary.getContext()).thenReturn(null);
|
||||||
|
mBatteryInfo.remainingTimeUs = REMAINING_TIME_US;
|
||||||
|
|
||||||
|
//Should not crash
|
||||||
|
mPowerUsageSummary.updateHeaderPreference(mBatteryInfo);
|
||||||
|
}
|
||||||
|
|
||||||
private void testToggleAllApps(final boolean isShowApps) {
|
private void testToggleAllApps(final boolean isShowApps) {
|
||||||
mFragment.mShowAllApps = isShowApps;
|
mFragment.mShowAllApps = isShowApps;
|
||||||
|
Reference in New Issue
Block a user