OSDN Git Service

Calculate multipath quota excluding current day.
authorRemi NGUYEN VAN <reminv@google.com>
Mon, 2 Apr 2018 06:48:19 +0000 (15:48 +0900)
committerRemi NGUYEN VAN <reminv@google.com>
Mon, 2 Apr 2018 08:44:23 +0000 (17:44 +0900)
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

services/core/java/com/android/server/net/NetworkPolicyManagerService.java

index 98dee35..ebcc54b 100644 (file)
@@ -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<ZonedDateTime, ZonedDateTime> 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);
             }