summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java24
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java22
2 files changed, 35 insertions, 11 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java
index 5ed59996bee3..2da622139b79 100644
--- a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java
+++ b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java
@@ -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;
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java
index cbc382b6b920..4f3b2005b197 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java
@@ -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");
+
}
}