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

Commit fa0068b4 authored by Pajace Chen's avatar Pajace Chen Committed by Android (Google) Code Review
Browse files

Merge "Fix rounded time to full issues" into 24D1-dev

parents bf70c203 2982b3b4
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.settingslib.utils;

import static java.lang.Math.abs;

import android.content.Context;
import android.icu.text.DateFormat;
import android.icu.text.MeasureFormat;
@@ -212,8 +214,8 @@ public class PowerUtil {
     * @return The rounded value as a long
     */
    public static long roundTimeToNearestThreshold(long drainTime, long threshold) {
        long time = Math.abs(drainTime);
        long multiple = Math.abs(threshold);
        long time = abs(drainTime);
        long multiple = abs(threshold);
        final long remainder = time % multiple;
        if (remainder < multiple / 2) {
            return time - remainder;
@@ -222,18 +224,24 @@ public class PowerUtil {
        }
    }

    /** Gets the rounded target time string in a short format. */
    /** Gets the target time string in a short format. */
    public static String getTargetTimeShortString(
            Context context, long targetTimeOffsetMs, long currentTimeMs) {
        final long roundedTimeOfDayMs =
                roundTimeToNearestThreshold(
                        currentTimeMs + targetTimeOffsetMs, FIFTEEN_MINUTES_MILLIS);
        long targetTimeMs = currentTimeMs + targetTimeOffsetMs;
        if (targetTimeOffsetMs >= FIFTEEN_MINUTES_MILLIS) {
            targetTimeMs = roundUpTimeToNextThreshold(targetTimeMs, FIFTEEN_MINUTES_MILLIS);
        }

        // convert the time to a properly formatted string.
        String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
        DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
        Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs));
        Date date = Date.from(Instant.ofEpochMilli(targetTimeMs));
        return fmt.format(date);
    }
}

    private static long roundUpTimeToNextThreshold(long timeMs, long threshold) {
        var time = abs(timeMs);
        var multiple = abs(threshold);
        return ((time + multiple - 1) / multiple) * multiple;
    }
}
+19 −3
Original line number Diff line number Diff line
@@ -87,15 +87,31 @@ public class PowerUtilTest {
    }

    @Test
    public void getTargetTimeShortString_returnsTimeShortString() {
    public void getTargetTimeShortString_lessThan15Minutes_returnsTimeShortStringWithoutRounded() {
        mContext.getSystemService(AlarmManager.class).setTimeZone("UTC");
        mContext.getResources().getConfiguration().setLocale(Locale.US);
        var currentTimeMs = Instant.parse("2024-06-06T15:00:00Z").toEpochMilli();
        var remainingTimeMs = Duration.ofMinutes(30).toMillis();
        var remainingTimeMs = Duration.ofMinutes(15).toMillis() - 1;

        var actualTimeString =
                PowerUtil.getTargetTimeShortString(mContext, remainingTimeMs, currentTimeMs);

        assertThat(actualTimeString).isEqualTo("3:30 PM");
        // due to timezone issue in test case, focus on rounded minutes, remove hours part.
        assertThat(actualTimeString).endsWith("14 PM");
    }

    @Test
    public void getTargetTimeShortString_moreThan15Minutes_returnsTimeShortStringWithRounded() {
        mContext.getSystemService(AlarmManager.class).setTimeZone("UTC");
        mContext.getResources().getConfiguration().setLocale(Locale.US);
        var currentTimeMs = Instant.parse("2024-06-06T15:00:00Z").toEpochMilli();
        var remainingTimeMs = Duration.ofMinutes(15).toMillis() + 1;

        var actualTimeString =
                PowerUtil.getTargetTimeShortString(mContext, remainingTimeMs, currentTimeMs);

        // due to timezone issue in test case, focus on rounded minutes, remove hours part.
        assertThat(actualTimeString).endsWith("30 PM");

    }
}