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
This commit is contained in:
Salvador Martinez
2018-04-13 13:34:46 -07:00
parent 5e09e0a34b
commit 2ee2b0a785
2 changed files with 35 additions and 10 deletions

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}