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

Commit fc5e6ede authored by Junyu Lai's avatar Junyu Lai Committed by Automerger Merge Worker
Browse files

Merge "[SP29.1] Simplify logic of calculating and applying data limit" am:...

Merge "[SP29.1] Simplify logic of calculating and applying data limit" am: 6b9470ff am: 37dcb826

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1652211

Change-Id: I73805c127c9a20de1e502ccedfd65195c76a6b6a
parents 06165260 37dcb826
Loading
Loading
Loading
Loading
+19 −24
Original line number Original line Diff line number Diff line
@@ -2035,8 +2035,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {


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


                    if (policy.lastLimitSnooze >= start) {
                if (policy.lastLimitSnooze < start) {
                        // snoozing past quota, but we still need to restrict apps,
                        // so push really high quota.
                        quotaBytes = Long.MAX_VALUE;
                    } else {
                    // remaining "quota" bytes are based on total usage in
                    // remaining "quota" bytes are based on total usage in
                    // current cycle. kernel doesn't like 0-byte rules, so we
                    // current cycle. kernel doesn't like 0-byte rules, so we
                    // set 1-byte quota and disable the radio later.
                    // 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) {
                if (matchingIfaces.size() > 1) {
                    // TODO: switch to shared quota once NMS supports
                    // TODO: switch to shared quota once NMS supports
                    Slog.w(TAG, "shared quota unsupported; generating rule for each iface");
                    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--) {
                for (int j = matchingIfaces.size() - 1; j >= 0; j--) {
                    final String iface = matchingIfaces.valueAt(j);
                    final String iface = matchingIfaces.valueAt(j);
                    setInterfaceQuotaAsync(iface, quotaBytes);
                    setInterfaceQuotaAsync(iface, limitBytes);
                    newMeteredIfaces.add(iface);
                    newMeteredIfaces.add(iface);
                }
                }
            }
            }