Merge "Use new time format in BatteryInfo"
This commit is contained in:
@@ -510,7 +510,7 @@ public class BatteryHistoryChart extends View {
|
||||
mInfo = info;
|
||||
mDrainString = "";
|
||||
mChargeDurationString = "";
|
||||
setContentDescription(mInfo.chargeLabelString);
|
||||
setContentDescription(mInfo.chargeLabel);
|
||||
|
||||
int pos = 0;
|
||||
int lastInteresting = 0;
|
||||
@@ -589,7 +589,8 @@ public class BatteryHistoryChart extends View {
|
||||
mMaxPercentLabelStringWidth = (int)mTextPaint.measureText(mMaxPercentLabelString);
|
||||
mMinPercentLabelStringWidth = (int)mTextPaint.measureText(mMinPercentLabelString);
|
||||
mDrainStringWidth = (int)mHeaderTextPaint.measureText(mDrainString);
|
||||
mChargeLabelStringWidth = (int)mHeaderTextPaint.measureText(mInfo.chargeLabelString);
|
||||
mChargeLabelStringWidth = (int) mHeaderTextPaint.measureText(
|
||||
mInfo.chargeLabel.toString());
|
||||
mChargeDurationStringWidth = (int)mHeaderTextPaint.measureText(mChargeDurationString);
|
||||
mTextAscent = (int)mTextPaint.ascent();
|
||||
mTextDescent = (int)mTextPaint.descent();
|
||||
@@ -1211,8 +1212,9 @@ public class BatteryHistoryChart extends View {
|
||||
|
||||
int headerTop = -mHeaderTextAscent + (mHeaderTextDescent-mHeaderTextAscent)/3;
|
||||
mHeaderTextPaint.setTextAlign(textAlignLeft);
|
||||
if (DEBUG) Log.d(TAG, "Drawing charge label string: " + mInfo.chargeLabelString);
|
||||
canvas.drawText(mInfo.chargeLabelString, textStartX, headerTop, mHeaderTextPaint);
|
||||
if (DEBUG) Log.d(TAG, "Drawing charge label string: " + mInfo.chargeLabel);
|
||||
canvas.drawText(mInfo.chargeLabel.toString(), textStartX, headerTop,
|
||||
mHeaderTextPaint);
|
||||
int stringHalfWidth = mChargeDurationStringWidth / 2;
|
||||
if (layoutRtl) stringHalfWidth = -stringHalfWidth;
|
||||
int headerCenter = ((width-mChargeDurationStringWidth-mDrainStringWidth)/2)
|
||||
|
@@ -25,23 +25,25 @@ import android.os.BatteryStats.HistoryItem;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.WorkerThread;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.R;
|
||||
import com.android.settingslib.Utils;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settingslib.graph.UsageView;
|
||||
|
||||
public class BatteryInfo {
|
||||
|
||||
public String chargeLabelString;
|
||||
public CharSequence chargeLabel;
|
||||
public CharSequence remainingLabel;
|
||||
public int batteryLevel;
|
||||
public boolean discharging = true;
|
||||
public long remainingTimeUs = 0;
|
||||
public String batteryPercentString;
|
||||
public String remainingLabel;
|
||||
public String statusLabel;
|
||||
private boolean mCharging;
|
||||
private BatteryStats mStats;
|
||||
@@ -172,27 +174,25 @@ public class BatteryInfo {
|
||||
if (!info.mCharging) {
|
||||
if (drainTimeUs > 0) {
|
||||
info.remainingTimeUs = drainTimeUs;
|
||||
String timeString = Formatter.formatShortElapsedTime(context,
|
||||
batteryUtils.convertUsToMs(drainTimeUs));
|
||||
info.remainingLabel = resources.getString(
|
||||
shortString ?
|
||||
(basedOnUsage ?
|
||||
R.string.power_remaining_duration_only_short_enhanced :
|
||||
R.string.power_remaining_duration_only_short) :
|
||||
(basedOnUsage ?
|
||||
R.string.power_remaining_duration_only_enhanced :
|
||||
R.string.power_remaining_duration_only),
|
||||
timeString);
|
||||
info.chargeLabelString = resources.getString(
|
||||
CharSequence timeString = Utils.formatElapsedTime(context,
|
||||
batteryUtils.convertUsToMs(drainTimeUs), false /* withSeconds */);
|
||||
info.remainingLabel = TextUtils.expandTemplate(context.getText(shortString ?
|
||||
(basedOnUsage ?
|
||||
R.string.power_remaining_duration_only_short_enhanced :
|
||||
R.string.power_remaining_duration_only_short) :
|
||||
(basedOnUsage ?
|
||||
R.string.power_remaining_duration_only_enhanced :
|
||||
R.string.power_remaining_duration_only)), timeString);
|
||||
info.chargeLabel = TextUtils.expandTemplate(context.getText(
|
||||
shortString ?
|
||||
R.string.power_discharging_duration_short :
|
||||
basedOnUsage ?
|
||||
R.string.power_discharging_duration_enhanced :
|
||||
R.string.power_discharging_duration,
|
||||
R.string.power_discharging_duration),
|
||||
info.batteryPercentString, timeString);
|
||||
} else {
|
||||
info.remainingLabel = null;
|
||||
info.chargeLabelString = info.batteryPercentString;
|
||||
info.chargeLabel = info.batteryPercentString;
|
||||
}
|
||||
} else {
|
||||
final long chargeTime = stats.computeChargeTimeRemaining(elapsedRealtimeUs);
|
||||
@@ -201,19 +201,19 @@ public class BatteryInfo {
|
||||
info.discharging = false;
|
||||
if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
|
||||
info.remainingTimeUs = chargeTime;
|
||||
String timeString = Formatter.formatShortElapsedTime(context,
|
||||
batteryUtils.convertUsToMs(chargeTime));
|
||||
CharSequence timeString = Utils.formatElapsedTime(context,
|
||||
batteryUtils.convertUsToMs(drainTimeUs), false /* withSeconds */);
|
||||
int resId = shortString ? R.string.power_charging_duration_short
|
||||
: R.string.power_charging_duration;
|
||||
info.remainingLabel = resources.getString(
|
||||
R.string.power_remaining_charging_duration_only, timeString);
|
||||
info.chargeLabelString = resources.getString(
|
||||
resId, info.batteryPercentString, timeString);
|
||||
info.remainingLabel = TextUtils.expandTemplate(context.getText(
|
||||
R.string.power_remaining_charging_duration_only), timeString);
|
||||
info.chargeLabel = TextUtils.expandTemplate(context.getText(resId),
|
||||
info.batteryPercentString, timeString);
|
||||
} else {
|
||||
final String chargeStatusLabel = resources.getString(
|
||||
R.string.battery_info_status_charging_lower);
|
||||
info.remainingLabel = null;
|
||||
info.chargeLabelString = resources.getString(
|
||||
info.chargeLabel = resources.getString(
|
||||
R.string.power_charging, info.batteryPercentString, chargeStatusLabel);
|
||||
}
|
||||
}
|
||||
|
@@ -769,7 +769,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
|
||||
BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() {
|
||||
@Override
|
||||
public void onBatteryInfoLoaded(BatteryInfo info) {
|
||||
mLoader.setSummary(SummaryProvider.this, info.chargeLabelString);
|
||||
mLoader.setSummary(SummaryProvider.this, info.chargeLabel);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@@ -23,53 +23,51 @@ import android.os.BatteryManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.R;
|
||||
|
||||
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.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.AdditionalMatchers.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class BatteryInfoTest {
|
||||
private static final String STATUS_FULL = "Full";
|
||||
private static final String STATUS_CHARGING_NO_TIME = "Charging";
|
||||
private static final String STATUS_CHARGING_TIME = "Charging - 2h left";
|
||||
private static final String STATUS_CHARGING_NO_TIME = "50% - charging";
|
||||
private static final String STATUS_CHARGING_TIME = "50% - 0m until fully charged";
|
||||
private static final int PLUGGED_IN = 1;
|
||||
private static final long REMAINING_TIME_NULL = -1;
|
||||
private static final long REMAINING_TIME = 2;
|
||||
public static final String ENHANCED_STRING_SUFFIX = "left based on your usage";
|
||||
private Intent mDisChargingBatteryBroadcast;
|
||||
private Intent mChargingBatteryBroadcast;
|
||||
private Context mRealContext;
|
||||
private Context mContext;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private BatteryStats mBatteryStats;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Resources mResources;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
FakeFeatureFactory.setupForTest(mContext);
|
||||
|
||||
mDisChargingBatteryBroadcast = new Intent();
|
||||
@@ -86,14 +84,6 @@ public class BatteryInfoTest {
|
||||
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
|
||||
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
|
||||
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||
|
||||
when(mContext.getResources().getString(R.string.battery_info_status_full))
|
||||
.thenReturn(STATUS_FULL);
|
||||
when(mContext.getResources().getString(eq(R.string.power_charging), any(),
|
||||
any())).thenReturn(STATUS_CHARGING_NO_TIME);
|
||||
when(mContext.getResources().getString(eq(R.string.power_charging_duration), any(),
|
||||
any())).thenReturn(STATUS_CHARGING_TIME);
|
||||
mRealContext = RuntimeEnvironment.application;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,7 +102,7 @@ public class BatteryInfoTest {
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
|
||||
assertThat(info.chargeLabelString).isEqualTo(STATUS_CHARGING_TIME);
|
||||
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,7 +111,7 @@ public class BatteryInfoTest {
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
|
||||
assertThat(info.chargeLabelString).isEqualTo(STATUS_CHARGING_NO_TIME);
|
||||
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_NO_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,12 +124,6 @@ public class BatteryInfoTest {
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_basedOnUsageTrue_usesUsageString() {
|
||||
doReturn(mResources).when(mContext).getResources();
|
||||
when(mResources.getString(eq(R.string.battery_info_status_full))).thenReturn("");
|
||||
when(mResources.getString(eq(R.string.power_remaining_duration_only_enhanced), any()))
|
||||
.thenReturn(ENHANCED_STRING_SUFFIX);
|
||||
when(mResources.getString(eq(R.string.power_remaining_duration_only_short_enhanced), any()))
|
||||
.thenReturn(ENHANCED_STRING_SUFFIX);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */,
|
||||
1000, true /* basedOnUsage */);
|
||||
@@ -147,18 +131,12 @@ public class BatteryInfoTest {
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */,
|
||||
1000, true /* basedOnUsage */);
|
||||
|
||||
assertThat(info.remainingLabel).contains(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info2.remainingLabel).contains(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info.remainingLabel.toString()).contains(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info2.remainingLabel.toString()).contains(ENHANCED_STRING_SUFFIX);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_basedOnUsageFalse_usesDefaultString() {
|
||||
doReturn(mResources).when(mContext).getResources();
|
||||
when(mResources.getString(eq(R.string.battery_info_status_full))).thenReturn("");
|
||||
when(mResources.getString(not(eq(R.string.power_remaining_duration_only_enhanced)), any()))
|
||||
.thenReturn(ENHANCED_STRING_SUFFIX);
|
||||
when(mResources.getString(not(eq(R.string.power_remaining_duration_only_short_enhanced)),
|
||||
any())).thenReturn("");
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */,
|
||||
1000, false /* basedOnUsage */);
|
||||
@@ -166,7 +144,7 @@ public class BatteryInfoTest {
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */,
|
||||
1000, false /* basedOnUsage */);
|
||||
|
||||
assertThat(info.remainingLabel).doesNotContain(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info2.remainingLabel).doesNotContain(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info.remainingLabel.toString()).doesNotContain(ENHANCED_STRING_SUFFIX);
|
||||
assertThat(info2.remainingLabel.toString()).doesNotContain(ENHANCED_STRING_SUFFIX);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user