From 0caec3716131e42b685c1be6072c7cc2780c6f33 Mon Sep 17 00:00:00 2001 From: ykhung Date: Fri, 28 May 2021 15:29:01 +0800 Subject: [PATCH] Resolve localization issue and refine UI for chart view Bug: 189293866 Bug: 189407613 Bug: 189413817 Test: make SettingsgRoboTests Change-Id: If38d133d88bb940c248ef1887548112ff576e0c4 --- res/layout/battery_chart_graph.xml | 4 ++-- .../settings/fuelgauge/BatteryChartView.java | 17 ++++++++++---- .../settings/fuelgauge/ConvertUtils.java | 23 ++++++++++++++----- .../settings/fuelgauge/ConvertUtilsTest.java | 22 +++++++++++++++--- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/res/layout/battery_chart_graph.xml b/res/layout/battery_chart_graph.xml index 65d41cfec93..db86693043e 100644 --- a/res/layout/battery_chart_graph.xml +++ b/res/layout/battery_chart_graph.xml @@ -34,8 +34,8 @@ ACCESSIBILITY_SERVICE_NAMES = Arrays.asList("SwitchAccessService", "TalkBackService", "JustSpeakService"); + // For drawing the percentage information. - private static final String[] PERCENTAGES = new String[] {"100%", "50%", "0%"}; + private static final String[] PERCENTAGES = new String[] { + formatPercentage(/*percentage=*/ 100, /*round=*/ true), + formatPercentage(/*percentage=*/ 50, /*round=*/ true), + formatPercentage(/*percentage=*/ 0, /*round=*/ true)}; + private static final int DEFAULT_TRAPEZOID_COUNT = 12; private static final int DEFAULT_TIMESTAMP_COUNT = 4; private static final int DIVIDER_COLOR = Color.parseColor("#CDCCC5"); @@ -220,13 +227,14 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick mIndent.right = mPercentageBounds[0].width() + mTextPadding; if (mTimestamps != null) { + int maxHeight = 0; for (int index = 0; index < DEFAULT_TIMESTAMP_COUNT; index++) { mTextPaint.getTextBounds( mTimestamps[index], 0, mTimestamps[index].length(), mTimestampsBounds[index]); + maxHeight = Math.max(maxHeight, mTimestampsBounds[index].height()); } - mIndent.bottom = mTimestampsBounds[0].height() - + round(mTextPadding * 1.5f); + mIndent.bottom = maxHeight + round(mTextPadding * 1.5f); } Log.d(TAG, "setIndent:" + mPercentageBounds[0]); } else { @@ -451,7 +459,8 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick private int getTimestampY(int index) { return getHeight() - mTimestampsBounds[index].height() - - mTimestampsBounds[index].top; + + (mTimestampsBounds[index].height() + mTimestampsBounds[index].top) + + round(mTextPadding * 1.5f); } private void drawTrapezoids(Canvas canvas) { diff --git a/src/com/android/settings/fuelgauge/ConvertUtils.java b/src/com/android/settings/fuelgauge/ConvertUtils.java index 82133ee89fe..19805648f3a 100644 --- a/src/com/android/settings/fuelgauge/ConvertUtils.java +++ b/src/com/android/settings/fuelgauge/ConvertUtils.java @@ -74,8 +74,12 @@ public final class ConvertUtils { public static final int CONSUMER_TYPE_USER_BATTERY = 2; public static final int CONSUMER_TYPE_SYSTEM_BATTERY = 3; - private static String sZoneId; - private static String sZoneIdForHour; + // For language is changed. + @VisibleForTesting static Locale sLocale; + @VisibleForTesting static Locale sLocaleForHour; + // For time zone is changed. + @VisibleForTesting static String sZoneId; + @VisibleForTesting static String sZoneIdForHour; private static boolean sIs24HourFormat; @VisibleForTesting @@ -130,28 +134,35 @@ public final class ConvertUtils { /** Converts UTC timestamp to human readable local time string. */ public static String utcToLocalTime(long timestamp) { + final Locale currentLocale = Locale.getDefault(); final String currentZoneId = TimeZone.getDefault().getID(); - if (!currentZoneId.equals(sZoneId) || sSimpleDateFormat == null) { + if (!currentZoneId.equals(sZoneId) + || !currentLocale.equals(sLocale) + || sSimpleDateFormat == null) { + sLocale = currentLocale; sZoneId = currentZoneId; sSimpleDateFormat = - new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", Locale.ENGLISH); + new SimpleDateFormat("MMM dd,yyyy HH:mm:ss", currentLocale); } return sSimpleDateFormat.format(new Date(timestamp)); } /** Converts UTC timestamp to local time hour data. */ public static String utcToLocalTimeHour(long timestamp, boolean is24HourFormat) { + final Locale currentLocale = Locale.getDefault(); final String currentZoneId = TimeZone.getDefault().getID(); if (!currentZoneId.equals(sZoneIdForHour) + || !currentLocale.equals(sLocaleForHour) || sIs24HourFormat != is24HourFormat || sSimpleDateFormatForHour == null) { + sLocaleForHour = currentLocale; sZoneIdForHour = currentZoneId; sIs24HourFormat = is24HourFormat; sSimpleDateFormatForHour = new SimpleDateFormat( - sIs24HourFormat ? "HH" : "h aa", Locale.ENGLISH); + sIs24HourFormat ? "HH" : "h aa", currentLocale); } return sSimpleDateFormatForHour.format(new Date(timestamp)) - .toLowerCase(Locale.getDefault()); + .toLowerCase(currentLocale); } /** 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 c9c5e465315..0c96b8eb970 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/ConvertUtilsTest.java @@ -40,6 +40,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.TimeZone; @@ -316,41 +317,56 @@ public final class ConvertUtilsTest { @Test public void testUtcToLocalTime_returnExpectedResult() { + ConvertUtils.sZoneId = null; + ConvertUtils.sLocale = null; final long timestamp = 1619196786769L; + final String expectedZoneId = "America/Los_Angeles"; ConvertUtils.sSimpleDateFormat = null; // Invokes the method first to create the SimpleDateFormat. ConvertUtils.utcToLocalTime(/*timestamp=*/ 0); ConvertUtils.sSimpleDateFormat - .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); + .setTimeZone(TimeZone.getTimeZone(expectedZoneId)); assertThat(ConvertUtils.utcToLocalTime(timestamp)) .isEqualTo("Apr 23,2021 09:53:06"); + assertThat(ConvertUtils.sZoneId).isNotEqualTo(expectedZoneId); + assertThat(ConvertUtils.sLocale).isEqualTo(Locale.getDefault()); } @Test public void testUtcToLocalTimeHour_12HourFormat_returnExpectedResult() { + ConvertUtils.sZoneIdForHour = null; + ConvertUtils.sLocaleForHour = null; final long timestamp = 1619196786769L; + final String expectedZoneId = "America/Los_Angeles"; ConvertUtils.sSimpleDateFormatForHour = null; // Invokes the method first to create the SimpleDateFormat. ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ false); ConvertUtils.sSimpleDateFormatForHour - .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); + .setTimeZone(TimeZone.getTimeZone(expectedZoneId)); assertThat(ConvertUtils.utcToLocalTimeHour( timestamp, /*is24HourFormat=*/ false)).isEqualTo("9 am"); + assertThat(ConvertUtils.sZoneIdForHour).isNotEqualTo(expectedZoneId); + assertThat(ConvertUtils.sLocaleForHour).isEqualTo(Locale.getDefault()); } @Test public void testUtcToLocalTimeHour_24HourFormat_returnExpectedResult() { + ConvertUtils.sZoneIdForHour = null; + ConvertUtils.sLocaleForHour = null; final long timestamp = 1619196786769L; + final String expectedZoneId = "America/Los_Angeles"; ConvertUtils.sSimpleDateFormatForHour = null; // Invokes the method first to create the SimpleDateFormat. ConvertUtils.utcToLocalTimeHour(/*timestamp=*/ 0, /*is24HourFormat=*/ true); ConvertUtils.sSimpleDateFormatForHour - .setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); + .setTimeZone(TimeZone.getTimeZone(expectedZoneId)); assertThat(ConvertUtils.utcToLocalTimeHour( timestamp, /*is24HourFormat=*/ true)).isEqualTo("09"); + assertThat(ConvertUtils.sZoneIdForHour).isNotEqualTo(expectedZoneId); + assertThat(ConvertUtils.sLocaleForHour).isEqualTo(Locale.getDefault()); } private static BatteryHistEntry createBatteryHistEntry(