Add summary for items in App Usage List in Battery page

This summary shows the active time of the app. For example
"Used for 27m".

Bug: 34467139
Test: RunSettingsRoboTests & Take screenshots
Change-Id: I0ef48427ad21be4dacd290fffb2c5d519ef58506
This commit is contained in:
jackqdyulei
2017-01-20 10:58:51 -08:00
parent bf441d39b9
commit 3ee22131a4
4 changed files with 70 additions and 4 deletions

View File

@@ -4465,6 +4465,9 @@
<!-- Representation of a mAh value. [CHAR LIMIT=NONE] -->
<string name="mah"><xliff:g id="number" example="30">%d</xliff:g> mAh</string>
<!-- Description for battery usage time for an app, i.e. Used for 30min. [CHAR LIMIT=60] -->
<string name="battery_used_for">Used for %1$s</string>
<!-- Menu label for viewing battery usage since unplugged -->
<string name="menu_stats_unplugged"><xliff:g id="unplugged">%1$s</xliff:g> since unplugged</string>
<!-- Menu label for viewing battery usage since unplugged -->

View File

@@ -31,6 +31,7 @@ import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
@@ -45,6 +46,7 @@ import com.android.internal.os.PowerProfile;
import com.android.settings.R;
import com.android.settings.Settings.HighPowerApplicationsActivity;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.applications.ManageApplications;
import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.SummaryLoader;
@@ -331,10 +333,12 @@ public class PowerUsageSummary extends PowerUsageBase {
final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
final BatteryStats stats = mStatsHelper.getStats();
final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
final Context context = getContext();
TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
int colorControl = getContext().getColor(value.resourceId);
final TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
final int colorControl = context.getColor(value.resourceId);
final String usedTime = context.getString(R.string.battery_used_for);
if (averagePower >= MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP || USE_FAKE_DATA) {
final List<BatterySipper> usageList = getCoalescedUsageList(
@@ -410,6 +414,7 @@ public class PowerUsageSummary extends PowerUsageBase {
pref.setTitle(entry.getLabel());
pref.setOrder(i + 1);
pref.setPercent(percentOfMax, percentOfTotal);
setUsageSummary(pref, usedTime, sipper.usageTimeMs);
if ((sipper.drainType != DrainType.APP
|| sipper.uidObj.getUid() == Process.ROOT_UID)
&& sipper.drainType != DrainType.USER) {
@@ -431,6 +436,15 @@ public class PowerUsageSummary extends PowerUsageBase {
BatteryEntry.startRequestQueue();
}
@VisibleForTesting
void setUsageSummary(Preference preference, String usedTimePrefix, long usageTimeMs) {
// Only show summary when usage time is longer than one minute
if (usageTimeMs >= DateUtils.MINUTE_IN_MILLIS) {
preference.setSummary(String.format(usedTimePrefix,
Utils.formatElapsedTime(getContext(), usageTimeMs, false)));
}
}
@VisibleForTesting
boolean shouldHideSipper(BatterySipper sipper) {
final DrainType drainType = sipper.drainType;

View File

@@ -6,6 +6,7 @@ import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.wifi.WifiManager;
import android.text.format.DateUtils;
import java.net.InetAddress;
import org.junit.Before;
import org.junit.Test;
@@ -71,4 +72,29 @@ public class UtilsTest {
// Should not crash here
assertThat(Utils.assignDefaultPhoto(null, 0)).isFalse();
}
@Test
public void testFormatElapsedTime_WithSeconds_ShowSeconds() {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "5m 0s";
assertThat(Utils.formatElapsedTime(mContext, testMillis, true)).isEqualTo(expectedTime);
}
@Test
public void testFormatElapsedTime_NoSeconds_DoNotShowSeconds() {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "5m";
assertThat(Utils.formatElapsedTime(mContext, testMillis, false)).isEqualTo(expectedTime);
}
@Test
public void testFormatElapsedTime_TimeMoreThanOneDay_ShowCorrectly() {
final double testMillis = 2 * DateUtils.DAY_IN_MILLIS
+ 4 * DateUtils.HOUR_IN_MILLIS + 15 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2d 4h 15m";
assertThat(Utils.formatElapsedTime(mContext, testMillis, false)).isEqualTo(expectedTime);
}
}

View File

@@ -19,6 +19,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -42,7 +43,10 @@ import java.util.List;
import static com.android.settings.fuelgauge.PowerUsageBase.MENU_STATS_REFRESH;
import static com.android.settings.fuelgauge.PowerUsageSummary.MENU_ADDITIONAL_BATTERY_INFO;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -75,6 +79,8 @@ public class PowerUsageSummaryTest {
private BatterySipper mNormalBatterySipper;
@Mock
private BatterySipper mScreenBatterySipper;
@Mock
private PowerGaugePreference mPreference;
private TestFragment mFragment;
private FakeFeatureFactory mFeatureFactory;
@@ -99,8 +105,9 @@ public class PowerUsageSummaryTest {
when(mFeatureFactory.powerUsageFeatureProvider.getAdditionalBatteryInfoIntent())
.thenReturn(ADDITIONAL_BATTERY_INFO_INTENT);
mPowerUsageSummary = new PowerUsageSummary();
mPowerUsageSummary = spy(new PowerUsageSummary());
when(mPowerUsageSummary.getContext()).thenReturn(mContext);
when(mNormalBatterySipper.getPackages()).thenReturn(PACKAGE_NAMES);
when(mNormalBatterySipper.getUid()).thenReturn(UID);
mNormalBatterySipper.totalPowerMah = POWER_MAH;
@@ -207,6 +214,22 @@ public class PowerUsageSummaryTest {
assertThat(mPowerUsageSummary.shouldHideSipper(mNormalBatterySipper)).isFalse();
}
@Test
public void testSetUsageSummary_TimeLessThanOneMinute_DoNotSetSummary() {
final long usageTimeMs = 59 * DateUtils.SECOND_IN_MILLIS;
mPowerUsageSummary.setUsageSummary(mPreference, "", usageTimeMs);
verify(mPreference, never()).setSummary(anyString());
}
@Test
public void testSetUsageSummary_TimeMoreThanOneMinute_SetSummary() {
final long usageTimeMs = 2 * DateUtils.MINUTE_IN_MILLIS;
mPowerUsageSummary.setUsageSummary(mPreference, "", usageTimeMs);
verify(mPreference).setSummary(anyString());
}
public static class TestFragment extends PowerUsageSummary {
private Context mContext;