Update app usage page active time format

- Make the text of active time support different states like foreground or background usage time less than one minutes

Bug: 178197718
Test: make SettingsRoboTests
Change-Id: I20be88e5b23a679c15f97bac4b9d400557685463
This commit is contained in:
Wesley.CW Wang
2021-04-21 20:13:55 +08:00
parent ee2086872e
commit 2cc7431fa3
3 changed files with 194 additions and 17 deletions

View File

@@ -6255,8 +6255,39 @@
<string name ="battery_detail_info_title">Since full charge</string> <string name ="battery_detail_info_title">Since full charge</string>
<!-- Title for the battery management group [CHAR LIMIT=40] --> <!-- Title for the battery management group [CHAR LIMIT=40] -->
<string name ="battery_detail_manage_title">Manage battery usage</string> <string name ="battery_detail_manage_title">Manage battery usage</string>
<!-- Description for battery total and background usage time for an app, i.e. 1 hr 15 min total • 39 min background for past 24 hr. Note: ^1 and ^2 should be used in all translations [CHAR LIMIT=120] --> <!-- Description for battery total and background usage time for an app, i.e. 1 hr 15 min total • 39 min background for past 24 hr. Note: ^1 and ^2 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_and_background_usage"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total • <xliff:g id="time" example="39 min">^2</xliff:g> background for past 24 hr</string> <string name="battery_total_and_background_usage"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total • <xliff:g id="time" example="39 min">^2</xliff:g> background for past 24 hr</string>
<!-- Description for battery total and background usage time in a time period for an app, i.e. 1 hr 15 min total • 39 min background for 12 am-2 am. Note: ^1, ^2 and ^3 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_and_background_usage_with_period"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total • <xliff:g id="time" example="39 min">^2</xliff:g> background for <xliff:g id="time_period" example="12 am-2 am">^3</xliff:g></string>
<!-- Description for battery total usage time is less than a minute for an app [CHAR LIMIT=120] -->
<string name="battery_total_usage_less_minute">Total less than a minute for past 24 hr</string>
<!-- Description for battery total usage time is less than a minute in a time period for an app, i.e. Total less than a minute for 12 am-2 am. Note: ^1 should be used in all translations[CHAR LIMIT=120] -->
<string name="battery_total_usage_less_minute_with_period">Total less than a minute for <xliff:g id="time_period" example="12 am-2 am">^1</xliff:g></string>
<!-- Description for battery background usage time is less than a minute for an app [CHAR LIMIT=120] -->
<string name="battery_background_usage_less_minute">Background less than a minute for past 24 hr</string>
<!-- Description for battery background usage time is less than a minute in a time period for an app, i.e. Background less than a minute for 12 am-2 am. Note: ^1 should be used in all translations[CHAR LIMIT=120] -->
<string name="battery_background_usage_less_minute_with_period">Background less than a minute for <xliff:g id="time_period" example="12 am-2 am">^1</xliff:g></string>
<!-- Description for battery total usage time for an app, i.e. 1 hr 15 min total for past 24 hr. Note: ^1 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_usage"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total for past 24 hr</string>
<!-- Description for battery total usage time in a time period for an app, i.e. 1 hr 15 min total for 12 am-2 am. Note: ^1 and ^2 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_usage_with_period"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total for <xliff:g id="time_period" example="12 am-2 am">^2</xliff:g></string>
<!-- Description for battery background usage time for an app, i.e. 1 hr 15 min background for past 24 hr. Note: ^1 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_background_usage"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> background for past 24 hr</string>
<!-- Description for battery background usage time in a time period for an app, i.e. 1 hr 15 min background for 12 am-2 am. Note: ^1 and ^2 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_background_usage_with_period"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> background for <xliff:g id="time_period" example="12 am-2 am">^2</xliff:g></string>
<!-- Description for battery total usage with background usage time less than a minute for an app, i.e. 1 hr 15 min total • background less than a minute for past 24 hr. Note: ^1 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_usage_and_background_less_minute_usage"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total • background less than a minute for past 24 hr</string>
<!-- Description for battery total usage with background usage time less than a minute in a time period for an app, i.e. 1 hr 15 min total • background less than a minute for 12 am-2 am. Note: ^1 and ^2 should be used in all translations [CHAR LIMIT=120] -->
<string name="battery_total_usage_and_background_less_minute_usage_with_period"><xliff:g id="time" example="1 hr 15 min">^1</xliff:g> total • background less than a minute for <xliff:g id="time_period" example="12 am-2 am">^2</xliff:g></string>
<!-- Description for no any battery usage for past 24 hr [CHAR LIMIT=120] -->
<string name="battery_not_usage">No usage for past 24 hr</string>
<!-- Graph subtext displayed to user when enhanced battery estimate is being used [CHAR LIMIT=120] --> <!-- Graph subtext displayed to user when enhanced battery estimate is being used [CHAR LIMIT=120] -->
<string name="advanced_battery_graph_subtext">Battery left estimate is based on your device usage</string> <string name="advanced_battery_graph_subtext">Battery left estimate is based on your device usage</string>

View File

@@ -27,6 +27,7 @@ import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@@ -282,20 +283,8 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
final long foregroundTimeMs = bundle.getLong(EXTRA_FOREGROUND_TIME); final long foregroundTimeMs = bundle.getLong(EXTRA_FOREGROUND_TIME);
final long backgroundTimeMs = bundle.getLong(EXTRA_BACKGROUND_TIME); final long backgroundTimeMs = bundle.getLong(EXTRA_BACKGROUND_TIME);
final long totalTimeMs = foregroundTimeMs + backgroundTimeMs; //TODO(b/178197718) Update layout to support multiple lines
//TODO(b/178197718) Refine the layout controller.setSummary(getAppActiveTime(foregroundTimeMs, backgroundTimeMs));
controller.setSummary(TextUtils.expandTemplate(
getText(R.string.battery_total_and_background_usage),
StringUtil.formatElapsedTime(
getContext(),
totalTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false),
StringUtil.formatElapsedTime(
getContext(),
backgroundTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false)));
controller.done(context, true /* rebindActions */); controller.done(context, true /* rebindActions */);
} }
@@ -388,4 +377,53 @@ public class AdvancedPowerUsageDetail extends DashboardFragment implements
private void updatePreferenceState(RadioButtonPreference preference, String selectedKey) { private void updatePreferenceState(RadioButtonPreference preference, String selectedKey) {
preference.setChecked(selectedKey.equals(preference.getKey())); preference.setChecked(selectedKey.equals(preference.getKey()));
} }
//TODO(b/178197718) Update method to support time period
private CharSequence getAppActiveTime(long foregroundTimeMs, long backgroundTimeMs) {
final long totalTimeMs = foregroundTimeMs + backgroundTimeMs;
final CharSequence usageTimeSummary;
if (totalTimeMs == 0) {
usageTimeSummary = getText(R.string.battery_not_usage);
// Shows background summary only if we don't have foreground usage time.
} else if (foregroundTimeMs == 0 && backgroundTimeMs != 0) {
usageTimeSummary = backgroundTimeMs < DateUtils.MINUTE_IN_MILLIS ?
getText(R.string.battery_background_usage_less_minute) :
TextUtils.expandTemplate(getText(R.string.battery_background_usage),
StringUtil.formatElapsedTime(
getContext(),
backgroundTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false));
// Shows total usage summary only if total usage time is small.
} else if (totalTimeMs < DateUtils.MINUTE_IN_MILLIS) {
usageTimeSummary = getText(R.string.battery_total_usage_less_minute);
// Shows different total usage summary when background usage time is small.
} else if (backgroundTimeMs < DateUtils.MINUTE_IN_MILLIS) {
usageTimeSummary = TextUtils.expandTemplate(
getText(backgroundTimeMs == 0 ?
R.string.battery_total_usage :
R.string.battery_total_usage_and_background_less_minute_usage),
StringUtil.formatElapsedTime(
getContext(),
totalTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false));
// Shows default summary.
} else {
usageTimeSummary = TextUtils.expandTemplate(
getText(R.string.battery_total_and_background_usage),
StringUtil.formatElapsedTime(
getContext(),
totalTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false),
StringUtil.formatElapsedTime(
getContext(),
backgroundTimeMs,
/* withSeconds */ false,
/* collapseTimeUnit */ false));
}
return usageTimeSummary;
}
} }

View File

@@ -249,14 +249,122 @@ public class AdvancedPowerUsageDetailTest {
} }
@Test @Test
public void testInitHeader_hasCorrectSummary() { public void testInitHeader_noUsageTime_hasCorrectSummary() {
mFragment.mAppEntry = null; Bundle bundle = new Bundle(2);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, /* value */ 0);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, /* value */ 0);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader(); mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class); ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture()); verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString()) assertThat(captor.getValue().toString())
.isEqualTo("0 min total • 0 min background for past 24 hr"); .isEqualTo("No usage for past 24 hr");
}
@Test
public void testInitHeader_backgroundTwoMinutesForegroundZero_hasCorrectSummary() {
final long backgroundTimeTwoMinutes = 120000;
final long foregroundTimeZero = 0;
Bundle bundle = new Bundle(2);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeTwoMinutes);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeZero);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("2 min background for past 24 hr");
}
@Test
public void testInitHeader_backgroundLessThanAMinutesForegroundZero_hasCorrectSummary() {
final long backgroundTimeLessThanAMinute = 59999;
final long foregroundTimeZero = 0;
Bundle bundle = new Bundle(2);
bundle.putLong(
AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeLessThanAMinute);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeZero);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("Background less than a minute for past 24 hr");
}
@Test
public void testInitHeader_totalUsageLessThanAMinutes_hasCorrectSummary() {
final long backgroundTimeLessThanHalfMinute = 20000;
final long foregroundTimeLessThanHalfMinute = 20000;
Bundle bundle = new Bundle(2);
bundle.putLong(
AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeLessThanHalfMinute);
bundle.putLong(
AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeLessThanHalfMinute);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("Total less than a minute for past 24 hr");
}
@Test
public void testInitHeader_TotalAMinutesBackgroundLessThanAMinutes_hasCorrectSummary() {
final long backgroundTimeZero = 59999;
final long foregroundTimeTwoMinutes = 1;
Bundle bundle = new Bundle(2);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeZero);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeTwoMinutes);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("1 min total • background less than a minute for past 24 hr");
}
@Test
public void testInitHeader_TotalAMinutesBackgroundZero_hasCorrectSummary() {
final long backgroundTimeZero = 0;
final long foregroundTimeAMinutes = 60000;
Bundle bundle = new Bundle(2);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeZero);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeAMinutes);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("1 min total for past 24 hr");
}
@Test
public void testInitHeader_foregroundTwoMinutesBackgroundFourMinutes_hasCorrectSummary() {
final long backgroundTimeFourMinute = 240000;
final long foregroundTimeTwoMinutes = 120000;
Bundle bundle = new Bundle(2);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_BACKGROUND_TIME, backgroundTimeFourMinute);
bundle.putLong(AdvancedPowerUsageDetail.EXTRA_FOREGROUND_TIME, foregroundTimeTwoMinutes);
when(mFragment.getArguments()).thenReturn(bundle);
mFragment.initHeader();
ArgumentCaptor<CharSequence> captor = ArgumentCaptor.forClass(CharSequence.class);
verify(mEntityHeaderController).setSummary(captor.capture());
assertThat(captor.getValue().toString())
.isEqualTo("6 min total • 4 min background for past 24 hr");
} }
@Test @Test