Loading packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +16 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading @@ -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; } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java +19 −3 Original line number Diff line number Diff line Loading @@ -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"); } } Loading
packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +16 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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; Loading @@ -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; } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java +19 −3 Original line number Diff line number Diff line Loading @@ -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"); } }