diff --git a/src/com/android/settings/fuelgauge/BatteryChartView.java b/src/com/android/settings/fuelgauge/BatteryChartView.java index f970072cb94..366946e12e2 100644 --- a/src/com/android/settings/fuelgauge/BatteryChartView.java +++ b/src/com/android/settings/fuelgauge/BatteryChartView.java @@ -103,8 +103,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick return; } // Sets the chart is clickable if there is at least one valid item in it. - for (int index = 0; index < mLevels.length; index++) { - if (mLevels[index] != 0) { + for (int index = 0; index < mLevels.length - 1; index++) { + if (mLevels[index] != 0 && mLevels[index + 1] != 0) { setClickable(true); break; } @@ -155,6 +155,11 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick return; } final int trapezoidIndex = getTrapezoidIndex(mTouchUpEvent.getX()); + // Ignores the click event if the level is zero. + if (trapezoidIndex == SELECTED_INDEX_INVALID + || (trapezoidIndex >= 0 && mLevels[trapezoidIndex] == 0)) { + return; + } // Selects all if users click the same trapezoid item two times. if (trapezoidIndex == mSelectedIndex) { setSelectedIndex(SELECTED_INDEX_ALL); diff --git a/src/com/android/settings/fuelgauge/ConvertUtils.java b/src/com/android/settings/fuelgauge/ConvertUtils.java index 2e8726e38ab..00dcb6be62b 100644 --- a/src/com/android/settings/fuelgauge/ConvertUtils.java +++ b/src/com/android/settings/fuelgauge/ConvertUtils.java @@ -70,8 +70,12 @@ public final class ConvertUtils { public static final int CONSUMER_TYPE_SYSTEM_BATTERY = 3; private static String sZoneId; - private static SimpleDateFormat sSimpleDateFormat; - private static SimpleDateFormat sSimpleDateFormatForHour; + private static String sZoneIdForHour; + + @VisibleForTesting + static SimpleDateFormat sSimpleDateFormat; + @VisibleForTesting + static SimpleDateFormat sSimpleDateFormatForHour; private ConvertUtils() {} @@ -140,25 +144,19 @@ public final class ConvertUtils { sZoneId = currentZoneId; sSimpleDateFormat = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH); - sSimpleDateFormatForHour = null; } return sSimpleDateFormat.format(new Date(timestamp)); } /** Converts UTC timestamp to local time hour data. */ - public static int utcToLocalTimeHour(long timestamp) { + public static String utcToLocalTimeHour(long timestamp) { final String currentZoneId = TimeZone.getDefault().getID(); - if (!currentZoneId.equals(sZoneId) || sSimpleDateFormatForHour == null) { - sZoneId = currentZoneId; - sSimpleDateFormat = null; - sSimpleDateFormatForHour = new SimpleDateFormat("HH", Locale.ENGLISH); - } - try { - return Integer.parseInt( - sSimpleDateFormatForHour.format(new Date(timestamp))); - } catch (NumberFormatException e) { - return Integer.MIN_VALUE; + if (!currentZoneId.equals(sZoneIdForHour) || sSimpleDateFormatForHour == null) { + sZoneIdForHour = currentZoneId; + sSimpleDateFormatForHour = new SimpleDateFormat("h aa", Locale.ENGLISH); } + return sSimpleDateFormatForHour.format(new Date(timestamp)) + .toLowerCase(Locale.getDefault()); } /** Gets indexed battery usage data for each corresponding time slot. */ diff --git a/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java index e95b158391b..12913a29dc8 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java @@ -39,10 +39,12 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; +import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.TimeZone; @@ -254,6 +256,32 @@ public final class ConvertUtilsTest { assertBatteryDiffEntry(entryList.get(0), 68, 40L, 50L); } + @Test + public void testUtcToLocalTime_returnExpectedResult() { + final long timestamp = 1619196786769L; + ConvertUtils.sSimpleDateFormat = null; + // Invokes the method first to create the SimpleDateFormat. + ConvertUtils.utcToLocalTime(/*timestamp=*/ 0); + ConvertUtils.sSimpleDateFormat + .setTimeZone(TimeZone.getTimeZone("GMT")); + + assertThat(ConvertUtils.utcToLocalTime(timestamp)) + .isEqualTo("Apr 23,2021 16:53:06"); + } + + @Test + public void testUtcToLocalTmeHour_returnExpectedResult() { + final long timestamp = 1619196786769L; + ConvertUtils.sSimpleDateFormatForHour = null; + // Invokes the method first to create the SimpleDateFormat. + ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0); + ConvertUtils.sSimpleDateFormatForHour + .setTimeZone(TimeZone.getTimeZone("GMT")); + + assertThat(ConvertUtils.utcToLocalTimeHour(timestamp)) + .isEqualTo("4 pm"); + } + private static BatteryHistEntry createBatteryHistEntry( String packageName, String appLabel, double consumePower, long uid, long foregroundUsageTimeInMs, long backgroundUsageTimeInMs) {