From bed7b970e731ce7c5e91f1ec8e11a042ece72233 Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Mon, 2 Apr 2018 15:48:19 +0900 Subject: [PATCH] Calculate multipath quota excluding current day. The current calculation includes data usage for the current day, which causes the returned daily quota to change during the day. This makes it difficult to use for callers that need a fixed data limit for the day. Current usage in MultipathPolicyTracker (the only caller) is incorrect because of this, as it assumes that quota is not constantly changing. Test: Tests in go/ag/3803612 pass Bug: b/72631572 Bug: b/72877610 Change-Id: I9b3edde2dee7c7479d428e2bf2b73297afed3e6b --- .../server/net/NetworkPolicyManagerService.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java index 98dee35168fe2..ebcc54bfc6176 100644 --- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java +++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java @@ -230,9 +230,11 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.nio.charset.StandardCharsets; import java.time.Clock; +import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -1749,11 +1751,17 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { final Pair cycle = plan.cycleIterator().next(); final long start = cycle.first.toInstant().toEpochMilli(); final long end = cycle.second.toInstant().toEpochMilli(); + final Instant now = mClock.instant(); + final long startOfDay = ZonedDateTime.ofInstant(now, cycle.first.getZone()) + .truncatedTo(ChronoUnit.DAYS) + .toInstant().toEpochMilli(); final long totalBytes = getTotalBytes( - NetworkTemplate.buildTemplateMobileAll(state.subscriberId), start, end); + NetworkTemplate.buildTemplateMobileAll(state.subscriberId), + start, startOfDay); final long remainingBytes = limitBytes - totalBytes; - final long remainingDays = Math.max(1, (end - mClock.millis()) - / TimeUnit.DAYS.toMillis(1)); + // Number of remaining days including current day + final long remainingDays = + 1 + ((end - now.toEpochMilli() - 1) / TimeUnit.DAYS.toMillis(1)); quotaBytes = Math.max(0, (remainingBytes / remainingDays) / 10); }