From 2ee2b0a785adb98d481d2708fed8bb6325d46069 Mon Sep 17 00:00:00 2001 From: Salvador Martinez Date: Fri, 13 Apr 2018 13:34:46 -0700 Subject: [PATCH] Make util method public A utility method is private but we need it elsewhere. This CL just makes it public. Also it restricts the use of the method to just rounding positive values to positive multiples. It is only meant for rounding time. Test: robotests Bug: 77874313 Change-Id: Ieb222c7f752b6a907fca5f91ddd4f4a4b67a0d2a --- .../android/settingslib/utils/PowerUtil.java | 31 +++++++++++++------ .../settingslib/utils/PowerUtilTest.java | 14 +++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java index de29030f5a460..a53ff39043465 100644 --- a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +++ b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java @@ -24,11 +24,10 @@ import android.icu.util.Measure; import android.icu.util.MeasureUnit; import android.support.annotation.Nullable; import android.text.TextUtils; -import com.android.internal.annotations.VisibleForTesting; + import com.android.settingslib.R; -import java.time.Clock; + import java.time.Instant; -import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.concurrent.TimeUnit; @@ -102,7 +101,7 @@ public class PowerUtil { private static String getMoreThanOneDayString(Context context, long drainTimeMs, String percentageString, boolean basedOnUsage) { - final long roundedTimeMs = roundToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); + final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs, false /* withSeconds */); @@ -139,7 +138,7 @@ public class PowerUtil { String percentageString, boolean basedOnUsage) { // Get the time of day we think device will die rounded to the nearest 15 min. final long roundedTimeOfDayMs = - roundToNearestThreshold( + roundTimeToNearestThreshold( System.currentTimeMillis() + drainTimeMs, FIFTEEN_MINUTES_MILLIS); @@ -170,12 +169,24 @@ public class PowerUtil { return timeMs * 1000; } - private static long roundToNearestThreshold(long drainTime, long threshold) { - final long remainder = drainTime % threshold; - if (remainder < threshold / 2) { - return drainTime - remainder; + /** + * Rounds a time to the nearest multiple of the provided threshold. Note: This function takes + * the absolute value of the inputs since it is only meant to be used for times, not general + * purpose rounding. + * + * ex: roundTimeToNearestThreshold(41, 24) = 48 + * @param drainTime The amount to round + * @param threshold The value to round to a multiple of + * @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); + final long remainder = time % multiple; + if (remainder < multiple / 2) { + return time - remainder; } else { - return drainTime - remainder + threshold; + return time - remainder + 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 dfd48cc99894b..f2ef99c42df56 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 @@ -175,4 +175,18 @@ public class PowerUtilTest { // Add percentage to string when provided assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); } + + @Test + public void testRoundToNearestThreshold_roundsCorrectly() { + // test some pretty normal values + assertThat(PowerUtil.roundTimeToNearestThreshold(1200, 1000)).isEqualTo(1000); + assertThat(PowerUtil.roundTimeToNearestThreshold(800, 1000)).isEqualTo(1000); + assertThat(PowerUtil.roundTimeToNearestThreshold(1000, 1000)).isEqualTo(1000); + + // test the weird stuff + assertThat(PowerUtil.roundTimeToNearestThreshold(80, -200)).isEqualTo(0); + assertThat(PowerUtil.roundTimeToNearestThreshold(-150, 100)).isEqualTo(200); + assertThat(PowerUtil.roundTimeToNearestThreshold(-120, 100)).isEqualTo(100); + assertThat(PowerUtil.roundTimeToNearestThreshold(-200, -75)).isEqualTo(225); + } }