Loading res/values/strings.xml +3 −0 Original line number Diff line number Diff line Loading @@ -4471,6 +4471,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 --> Loading src/com/android/settings/fuelgauge/PowerUsageSummary.java +17 −3 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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( Loading Loading @@ -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) { Loading @@ -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; Loading tests/robotests/src/com/android/settings/UtilsTest.java +26 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } } tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +24 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -75,6 +79,8 @@ public class PowerUsageSummaryTest { private BatterySipper mNormalBatterySipper; @Mock private BatterySipper mScreenBatterySipper; @Mock private PowerGaugePreference mPreference; private TestFragment mFragment; private FakeFeatureFactory mFeatureFactory; Loading @@ -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; Loading Loading @@ -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; Loading Loading
res/values/strings.xml +3 −0 Original line number Diff line number Diff line Loading @@ -4471,6 +4471,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 --> Loading
src/com/android/settings/fuelgauge/PowerUsageSummary.java +17 −3 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -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( Loading Loading @@ -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) { Loading @@ -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; Loading
tests/robotests/src/com/android/settings/UtilsTest.java +26 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } }
tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +24 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading Loading @@ -75,6 +79,8 @@ public class PowerUsageSummaryTest { private BatterySipper mNormalBatterySipper; @Mock private BatterySipper mScreenBatterySipper; @Mock private PowerGaugePreference mPreference; private TestFragment mFragment; private FakeFeatureFactory mFeatureFactory; Loading @@ -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; Loading Loading @@ -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; Loading