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

Commit 0aa853db authored by Pajace Chen's avatar Pajace Chen Committed by Automerger Merge Worker
Browse files

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

parents 19cfbb58 fa0068b4
Loading
Loading
Loading
Loading
+16 −8
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package com.android.settingslib.utils;
package com.android.settingslib.utils;


import static java.lang.Math.abs;

import android.content.Context;
import android.content.Context;
import android.icu.text.DateFormat;
import android.icu.text.DateFormat;
import android.icu.text.MeasureFormat;
import android.icu.text.MeasureFormat;
@@ -212,8 +214,8 @@ public class PowerUtil {
     * @return The rounded value as a long
     * @return The rounded value as a long
     */
     */
    public static long roundTimeToNearestThreshold(long drainTime, long threshold) {
    public static long roundTimeToNearestThreshold(long drainTime, long threshold) {
        long time = Math.abs(drainTime);
        long time = abs(drainTime);
        long multiple = Math.abs(threshold);
        long multiple = abs(threshold);
        final long remainder = time % multiple;
        final long remainder = time % multiple;
        if (remainder < multiple / 2) {
        if (remainder < multiple / 2) {
            return time - remainder;
            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(
    public static String getTargetTimeShortString(
            Context context, long targetTimeOffsetMs, long currentTimeMs) {
            Context context, long targetTimeOffsetMs, long currentTimeMs) {
        final long roundedTimeOfDayMs =
        long targetTimeMs = currentTimeMs + targetTimeOffsetMs;
                roundTimeToNearestThreshold(
        if (targetTimeOffsetMs >= FIFTEEN_MINUTES_MILLIS) {
                        currentTimeMs + targetTimeOffsetMs, FIFTEEN_MINUTES_MILLIS);
            targetTimeMs = roundUpTimeToNextThreshold(targetTimeMs, FIFTEEN_MINUTES_MILLIS);
        }


        // convert the time to a properly formatted string.
        // convert the time to a properly formatted string.
        String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
        String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
        DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
        DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
        Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs));
        Date date = Date.from(Instant.ofEpochMilli(targetTimeMs));
        return fmt.format(date);
        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 Original line Diff line number Diff line
@@ -87,15 +87,31 @@ public class PowerUtilTest {
    }
    }


    @Test
    @Test
    public void getTargetTimeShortString_returnsTimeShortString() {
    public void getTargetTimeShortString_lessThan15Minutes_returnsTimeShortStringWithoutRounded() {
        mContext.getSystemService(AlarmManager.class).setTimeZone("UTC");
        mContext.getSystemService(AlarmManager.class).setTimeZone("UTC");
        mContext.getResources().getConfiguration().setLocale(Locale.US);
        mContext.getResources().getConfiguration().setLocale(Locale.US);
        var currentTimeMs = Instant.parse("2024-06-06T15:00:00Z").toEpochMilli();
        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 =
        var actualTimeString =
                PowerUtil.getTargetTimeShortString(mContext, remainingTimeMs, currentTimeMs);
                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");

    }
    }
}
}