Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0caec371 authored by ykhung's avatar ykhung
Browse files

Resolve localization issue and refine UI for chart view

Bug: 189293866
Bug: 189407613
Bug: 189413817
Test: make SettingsgRoboTests
Change-Id: If38d133d88bb940c248ef1887548112ff576e0c4
parent 2098665e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@
    <com.android.settings.fuelgauge.BatteryChartView
        android:id="@+id/battery_chart"
        android:layout_width="match_parent"
        android:layout_height="165dp"
        android:layout_marginBottom="16dp"
        android:layout_height="170dp"
        android:layout_marginBottom="6dp"
        android:visibility="invisible"
        android:contentDescription="@string/battery_usage_chart"
        android:textAppearance="?android:attr/textAppearanceSmall"
+13 −4
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@ package com.android.settings.fuelgauge;

import static java.lang.Math.round;

import static com.android.settings.Utils.formatPercentage;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.res.Resources;
@@ -53,8 +55,13 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
    private static final String TAG = "BatteryChartView";
    private static final List<String> 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) {
+17 −6
Original line number Diff line number Diff line
@@ -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. */
+19 −3
Original line number Diff line number Diff line
@@ -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(