Add statusLabel in BatteryInfo

The statusLabel shows the current status of the battery(i.e. not
charging, charging).

Bug: 35328749
Test: RunSettingsLibRoboTests
Change-Id: I5be13635be9b2749c0c62edeb1e0ac17ff423d83
This commit is contained in:
jackqdyulei
2017-02-14 11:12:40 -08:00
parent 0b6b10d1ee
commit 111418e7ad
2 changed files with 71 additions and 4 deletions

View File

@@ -38,6 +38,7 @@ public class BatteryInfo {
public long remainingTimeUs = 0;
public String batteryPercentString;
public String remainingLabel;
public String statusLabel;
private BatteryStats mStats;
private boolean mCharging;
private long timePeriod;
@@ -135,6 +136,7 @@ public class BatteryInfo {
info.batteryPercentString = Utils.formatPercentage(info.mBatteryLevel);
info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
final Resources resources = context.getResources();
info.statusLabel = Utils.getBatteryStatus(resources, batteryBroadcast, shortString);
if (!info.mCharging) {
final long drainTime = stats.computeBatteryTimeRemaining(elapsedRealtimeUs);
if (drainTime > 0) {
@@ -155,8 +157,6 @@ public class BatteryInfo {
}
} else {
final long chargeTime = stats.computeChargeTimeRemaining(elapsedRealtimeUs);
final String statusLabel = Utils.getBatteryStatus(
resources, batteryBroadcast, shortString);
final int status = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
@@ -184,9 +184,9 @@ public class BatteryInfo {
info.mChargeLabelString = resources.getString(
resId, info.batteryPercentString, timeString);
} else {
info.remainingLabel = statusLabel;
info.remainingLabel = null;
info.mChargeLabelString = resources.getString(
R.string.power_charging, info.batteryPercentString, statusLabel);
R.string.power_charging, info.batteryPercentString, info.statusLabel);
}
}
return info;

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.SystemClock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BatteryInfoTest {
private static final String STATUS_FULL = "Full";
private Intent mBatteryBroadcast;
@Mock
private BatteryStats mBatteryStats;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mBatteryBroadcast = new Intent();
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 0);
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_FULL);
when(mContext.getResources().getString(R.string.battery_info_status_full))
.thenReturn(STATUS_FULL);
}
@Test
public void testGetBatteryInfo_HasStatusLabel() {
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mBatteryBroadcast, mBatteryStats,
SystemClock.elapsedRealtime() * 1000, true);
assertThat(info.statusLabel).isEqualTo(STATUS_FULL);
}
}