Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6f927be4 authored by Junyu Lai's avatar Junyu Lai Committed by Android (Google) Code Review
Browse files

Merge "[SP29.1] Simplify logic of calculating and applying data limit" into sc-dev

parents 647da08b 5acc19c9
Loading
Loading
Loading
Loading
+19 −24
Original line number Diff line number Diff line
@@ -2034,8 +2034,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {

            final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED;
            final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED;
            if (hasLimit || policy.metered) {
                final long quotaBytes;
            long limitBytes = Long.MAX_VALUE;
            if (hasLimit && policy.hasCycle()) {
                final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager
                        .cycleIterator(policy).next();
@@ -2043,30 +2042,26 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                final long end = cycle.second.toInstant().toEpochMilli();
                final long totalBytes = getTotalBytes(policy.template, start, end);

                    if (policy.lastLimitSnooze >= start) {
                        // snoozing past quota, but we still need to restrict apps,
                        // so push really high quota.
                        quotaBytes = Long.MAX_VALUE;
                    } else {
                if (policy.lastLimitSnooze < start) {
                    // remaining "quota" bytes are based on total usage in
                    // current cycle. kernel doesn't like 0-byte rules, so we
                    // set 1-byte quota and disable the radio later.
                        quotaBytes = Math.max(1, policy.limitBytes - totalBytes);
                    limitBytes = Math.max(1, policy.limitBytes - totalBytes);
                }
                } else {
                    // metered network, but no policy limit; we still need to
                    // restrict apps, so push really high quota.
                    quotaBytes = Long.MAX_VALUE;
            }

            if (hasLimit || policy.metered) {
                if (matchingIfaces.size() > 1) {
                    // TODO: switch to shared quota once NMS supports
                    Slog.w(TAG, "shared quota unsupported; generating rule for each iface");
                }

                // Set the interface limit. For interfaces which has no cycle, or metered with
                // no policy limit, or snoozed limit notification; we still need to put iptables
                // rule hooks to restrict apps for data saver, so push really high quota.
                for (int j = matchingIfaces.size() - 1; j >= 0; j--) {
                    final String iface = matchingIfaces.valueAt(j);
                    setInterfaceQuotaAsync(iface, quotaBytes);
                    setInterfaceQuotaAsync(iface, limitBytes);
                    newMeteredIfaces.add(iface);
                }
            }