Resolve locale not update issues in the chart view

- read locale from configuration rather than Locale.getDefault
- refine 12-24 format to align the current status bar short style
- resolve locale change not update chart percentage label
- extend timestamp label in the chart graph from 4 to 5 labels

Bug: 190150515
Bug: 190422902
Bug: 190226837
Test: make SettingsRoboTests
Change-Id: I5347964900123a6d112dbc37c2af87eb7d73f1d2
This commit is contained in:
ykhung
2021-06-09 11:41:33 +08:00
parent ae1797ec46
commit 2a75186e4b
7 changed files with 104 additions and 50 deletions

View File

@@ -17,6 +17,7 @@ import android.annotation.IntDef;
import android.content.ContentValues;
import android.content.Context;
import android.os.BatteryUsageStats;
import android.os.LocaleList;
import android.os.UserHandle;
import android.text.format.DateUtils;
import android.util.Log;
@@ -133,8 +134,8 @@ public final class ConvertUtils {
}
/** Converts UTC timestamp to human readable local time string. */
public static String utcToLocalTime(long timestamp) {
final Locale currentLocale = Locale.getDefault();
public static String utcToLocalTime(Context context, long timestamp) {
final Locale currentLocale = getLocale(context);
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneId)
|| !currentLocale.equals(sLocale)
@@ -148,8 +149,9 @@ public final class ConvertUtils {
}
/** Converts UTC timestamp to local time hour data. */
public static String utcToLocalTimeHour(long timestamp, boolean is24HourFormat) {
final Locale currentLocale = Locale.getDefault();
public static String utcToLocalTimeHour(
Context context, long timestamp, boolean is24HourFormat) {
final Locale currentLocale = getLocale(context);
final String currentZoneId = TimeZone.getDefault().getID();
if (!currentZoneId.equals(sZoneIdForHour)
|| !currentLocale.equals(sLocaleForHour)
@@ -159,7 +161,7 @@ public final class ConvertUtils {
sZoneIdForHour = currentZoneId;
sIs24HourFormat = is24HourFormat;
sSimpleDateFormatForHour = new SimpleDateFormat(
sIs24HourFormat ? "HH" : "h aa", currentLocale);
sIs24HourFormat ? "HH" : "h", currentLocale);
}
return sSimpleDateFormatForHour.format(new Date(timestamp))
.toLowerCase(currentLocale);
@@ -356,4 +358,15 @@ public final class ConvertUtils {
? entry3 : null;
}
}
@VisibleForTesting
static Locale getLocale(Context context) {
if (context == null) {
return Locale.getDefault();
}
final LocaleList locales =
context.getResources().getConfiguration().getLocales();
return locales != null && !locales.isEmpty() ? locales.get(0)
: Locale.getDefault();
}
}