blob: f8973ebaa0fc9fe3395baabe468abc3d5fc60f32 [file] [log] [blame]
Jeff Sharkey21c9c452011-06-07 12:26:43 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Mathew Inwood53f089f2018-08-08 14:44:44 +010019import android.annotation.UnsupportedAppUsage;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070020import android.os.Parcel;
21import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000022import android.util.BackupUtils;
Jeff Sharkey0fc6d032018-03-30 16:25:11 -060023import android.util.Range;
Jeff Sharkey17bebd22017-07-19 21:00:38 -060024import android.util.RecurrenceRule;
25
26import com.android.internal.util.Preconditions;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070027
Ritesh Reddyadca34a2016-02-04 18:33:30 +000028import java.io.ByteArrayOutputStream;
29import java.io.DataInputStream;
30import java.io.DataOutputStream;
31import java.io.IOException;
Jeff Sharkey17bebd22017-07-19 21:00:38 -060032import java.time.ZoneId;
33import java.time.ZonedDateTime;
34import java.util.Iterator;
Kenny Roote6585b32013-12-13 12:00:26 -080035import java.util.Objects;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070036
Jeff Sharkey21c9c452011-06-07 12:26:43 -070037/**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle
39 * and limits to be enforced.
Jeff Sharkey21c9c452011-06-07 12:26:43 -070040 *
41 * @hide
42 */
43public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
Jeff Sharkey17bebd22017-07-19 21:00:38 -060044 private static final int VERSION_INIT = 1;
45 private static final int VERSION_RULE = 2;
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -070046 private static final int VERSION_RAPID = 3;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000047
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070048 public static final int CYCLE_NONE = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070049 public static final long WARNING_DISABLED = -1;
50 public static final long LIMIT_DISABLED = -1;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070051 public static final long SNOOZE_NEVER = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070052
Mathew Inwood53f089f2018-08-08 14:44:44 +010053 @UnsupportedAppUsage
Jeff Sharkey32566012014-12-02 18:30:14 -080054 public NetworkTemplate template;
Jeff Sharkey17bebd22017-07-19 21:00:38 -060055 public RecurrenceRule cycleRule;
Mathew Inwood53f089f2018-08-08 14:44:44 +010056 @UnsupportedAppUsage
Jeff Sharkey53313d72017-07-13 16:47:32 -060057 public long warningBytes = WARNING_DISABLED;
Mathew Inwood53f089f2018-08-08 14:44:44 +010058 @UnsupportedAppUsage
Jeff Sharkey53313d72017-07-13 16:47:32 -060059 public long limitBytes = LIMIT_DISABLED;
60 public long lastWarningSnooze = SNOOZE_NEVER;
61 public long lastLimitSnooze = SNOOZE_NEVER;
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -070062 public long lastRapidSnooze = SNOOZE_NEVER;
Mathew Inwood53f089f2018-08-08 14:44:44 +010063 @UnsupportedAppUsage
Jeff Sharkey53313d72017-07-13 16:47:32 -060064 @Deprecated public boolean metered = true;
Mathew Inwood53f089f2018-08-08 14:44:44 +010065 @UnsupportedAppUsage
Jeff Sharkey53313d72017-07-13 16:47:32 -060066 public boolean inferred = false;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070067
Jeff Sharkey50e7e512011-10-10 16:50:35 -070068 private static final long DEFAULT_MTU = 1500;
69
Jeff Sharkey17bebd22017-07-19 21:00:38 -060070 public static RecurrenceRule buildRule(int cycleDay, ZoneId cycleTimezone) {
71 if (cycleDay != NetworkPolicy.CYCLE_NONE) {
72 return RecurrenceRule.buildRecurringMonthly(cycleDay, cycleTimezone);
73 } else {
74 return RecurrenceRule.buildNever();
75 }
Jeff Sharkey53313d72017-07-13 16:47:32 -060076 }
77
Jeff Sharkey837f9242012-03-20 16:52:20 -070078 @Deprecated
Jeff Sharkey9bf31502012-03-09 17:07:21 -080079 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone,
80 long warningBytes, long limitBytes, boolean metered) {
81 this(template, cycleDay, cycleTimezone, warningBytes, limitBytes, SNOOZE_NEVER,
Jeff Sharkey837f9242012-03-20 16:52:20 -070082 SNOOZE_NEVER, metered, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -080083 }
84
Jeff Sharkey17bebd22017-07-19 21:00:38 -060085 @Deprecated
Mathew Inwood53f089f2018-08-08 14:44:44 +010086 @UnsupportedAppUsage
Jeff Sharkey9bf31502012-03-09 17:07:21 -080087 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone,
88 long warningBytes, long limitBytes, long lastWarningSnooze, long lastLimitSnooze,
Jeff Sharkey837f9242012-03-20 16:52:20 -070089 boolean metered, boolean inferred) {
Jeff Sharkey17bebd22017-07-19 21:00:38 -060090 this(template, buildRule(cycleDay, ZoneId.of(cycleTimezone)), warningBytes,
91 limitBytes, lastWarningSnooze, lastLimitSnooze, metered, inferred);
92 }
93
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -070094 @Deprecated
Jeff Sharkey17bebd22017-07-19 21:00:38 -060095 public NetworkPolicy(NetworkTemplate template, RecurrenceRule cycleRule, long warningBytes,
96 long limitBytes, long lastWarningSnooze, long lastLimitSnooze, boolean metered,
97 boolean inferred) {
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -070098 this(template, cycleRule, warningBytes, limitBytes, lastWarningSnooze, lastLimitSnooze,
99 SNOOZE_NEVER, metered, inferred);
100 }
101
102 public NetworkPolicy(NetworkTemplate template, RecurrenceRule cycleRule, long warningBytes,
103 long limitBytes, long lastWarningSnooze, long lastLimitSnooze, long lastRapidSnooze,
104 boolean metered, boolean inferred) {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600105 this.template = Preconditions.checkNotNull(template, "missing NetworkTemplate");
106 this.cycleRule = Preconditions.checkNotNull(cycleRule, "missing RecurrenceRule");
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700107 this.warningBytes = warningBytes;
108 this.limitBytes = limitBytes;
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800109 this.lastWarningSnooze = lastWarningSnooze;
110 this.lastLimitSnooze = lastLimitSnooze;
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700111 this.lastRapidSnooze = lastRapidSnooze;
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800112 this.metered = metered;
Jeff Sharkey837f9242012-03-20 16:52:20 -0700113 this.inferred = inferred;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700114 }
115
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600116 private NetworkPolicy(Parcel source) {
117 template = source.readParcelable(null);
118 cycleRule = source.readParcelable(null);
119 warningBytes = source.readLong();
120 limitBytes = source.readLong();
121 lastWarningSnooze = source.readLong();
122 lastLimitSnooze = source.readLong();
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700123 lastRapidSnooze = source.readLong();
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600124 metered = source.readInt() != 0;
125 inferred = source.readInt() != 0;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700126 }
127
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800128 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700129 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700130 dest.writeParcelable(template, flags);
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600131 dest.writeParcelable(cycleRule, flags);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700132 dest.writeLong(warningBytes);
133 dest.writeLong(limitBytes);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800134 dest.writeLong(lastWarningSnooze);
135 dest.writeLong(lastLimitSnooze);
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700136 dest.writeLong(lastRapidSnooze);
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800137 dest.writeInt(metered ? 1 : 0);
Jeff Sharkey837f9242012-03-20 16:52:20 -0700138 dest.writeInt(inferred ? 1 : 0);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700139 }
140
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800141 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700142 public int describeContents() {
143 return 0;
144 }
145
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600146 public Iterator<Range<ZonedDateTime>> cycleIterator() {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600147 return cycleRule.cycleIterator();
148 }
149
Jeff Sharkey50e7e512011-10-10 16:50:35 -0700150 /**
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800151 * Test if given measurement is over {@link #warningBytes}.
152 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100153 @UnsupportedAppUsage
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800154 public boolean isOverWarning(long totalBytes) {
155 return warningBytes != WARNING_DISABLED && totalBytes >= warningBytes;
156 }
157
158 /**
Jeff Sharkey50e7e512011-10-10 16:50:35 -0700159 * Test if given measurement is near enough to {@link #limitBytes} to be
160 * considered over-limit.
161 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100162 @UnsupportedAppUsage
Jeff Sharkey50e7e512011-10-10 16:50:35 -0700163 public boolean isOverLimit(long totalBytes) {
164 // over-estimate, since kernel will trigger limit once first packet
165 // trips over limit.
166 totalBytes += 2 * DEFAULT_MTU;
167 return limitBytes != LIMIT_DISABLED && totalBytes >= limitBytes;
168 }
169
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800170 /**
171 * Clear any existing snooze values, setting to {@link #SNOOZE_NEVER}.
172 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100173 @UnsupportedAppUsage
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800174 public void clearSnooze() {
175 lastWarningSnooze = SNOOZE_NEVER;
176 lastLimitSnooze = SNOOZE_NEVER;
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700177 lastRapidSnooze = SNOOZE_NEVER;
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800178 }
179
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700180 /**
181 * Test if this policy has a cycle defined, after which usage should reset.
182 */
183 public boolean hasCycle() {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600184 return cycleRule.cycleIterator().hasNext();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700185 }
186
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800187 @Override
Mathew Inwood53f089f2018-08-08 14:44:44 +0100188 @UnsupportedAppUsage
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700189 public int compareTo(NetworkPolicy another) {
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700190 if (another == null || another.limitBytes == LIMIT_DISABLED) {
191 // other value is missing or disabled; we win
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700192 return -1;
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700193 }
194 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) {
195 // we're disabled or other limit is smaller; they win
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700196 return 1;
197 }
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700198 return 0;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700199 }
200
201 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700202 public int hashCode() {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600203 return Objects.hash(template, cycleRule, warningBytes, limitBytes,
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700204 lastWarningSnooze, lastLimitSnooze, lastRapidSnooze, metered, inferred);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700205 }
206
207 @Override
208 public boolean equals(Object obj) {
209 if (obj instanceof NetworkPolicy) {
210 final NetworkPolicy other = (NetworkPolicy) obj;
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600211 return warningBytes == other.warningBytes
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800212 && limitBytes == other.limitBytes
213 && lastWarningSnooze == other.lastWarningSnooze
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700214 && lastLimitSnooze == other.lastLimitSnooze
215 && lastRapidSnooze == other.lastRapidSnooze
216 && metered == other.metered
Jeff Sharkey837f9242012-03-20 16:52:20 -0700217 && inferred == other.inferred
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600218 && Objects.equals(template, other.template)
219 && Objects.equals(cycleRule, other.cycleRule);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700220 }
221 return false;
222 }
223
224 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700225 public String toString() {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600226 return new StringBuilder("NetworkPolicy{")
227 .append("template=").append(template)
228 .append(" cycleRule=").append(cycleRule)
229 .append(" warningBytes=").append(warningBytes)
230 .append(" limitBytes=").append(limitBytes)
231 .append(" lastWarningSnooze=").append(lastWarningSnooze)
232 .append(" lastLimitSnooze=").append(lastLimitSnooze)
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700233 .append(" lastRapidSnooze=").append(lastRapidSnooze)
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600234 .append(" metered=").append(metered)
235 .append(" inferred=").append(inferred)
236 .append("}").toString();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700237 }
238
Mathew Inwood53f089f2018-08-08 14:44:44 +0100239 @UnsupportedAppUsage
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700240 public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() {
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800241 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700242 public NetworkPolicy createFromParcel(Parcel in) {
243 return new NetworkPolicy(in);
244 }
245
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800246 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700247 public NetworkPolicy[] newArray(int size) {
248 return new NetworkPolicy[size];
249 }
250 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000251
252 public byte[] getBytesForBackup() throws IOException {
253 ByteArrayOutputStream baos = new ByteArrayOutputStream();
254 DataOutputStream out = new DataOutputStream(baos);
255
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700256 out.writeInt(VERSION_RAPID);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000257 out.write(template.getBytesForBackup());
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600258 cycleRule.writeToStream(out);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000259 out.writeLong(warningBytes);
260 out.writeLong(limitBytes);
261 out.writeLong(lastWarningSnooze);
262 out.writeLong(lastLimitSnooze);
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700263 out.writeLong(lastRapidSnooze);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000264 out.writeInt(metered ? 1 : 0);
265 out.writeInt(inferred ? 1 : 0);
266 return baos.toByteArray();
267 }
268
269 public static NetworkPolicy getNetworkPolicyFromBackup(DataInputStream in) throws IOException,
270 BackupUtils.BadVersionException {
Jeff Sharkey17bebd22017-07-19 21:00:38 -0600271 final int version = in.readInt();
Annie Meng47f5c9c2018-02-27 14:48:21 +0000272 if (version < VERSION_INIT || version > VERSION_RAPID) {
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700273 throw new BackupUtils.BadVersionException("Unknown backup version: " + version);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000274 }
Jeff Sharkeybfb43ea2018-02-03 12:08:16 -0700275
276 final NetworkTemplate template = NetworkTemplate.getNetworkTemplateFromBackup(in);
277 final RecurrenceRule cycleRule;
278 if (version >= VERSION_RULE) {
279 cycleRule = new RecurrenceRule(in);
280 } else {
281 final int cycleDay = in.readInt();
282 final String cycleTimezone = BackupUtils.readString(in);
283 cycleRule = buildRule(cycleDay, ZoneId.of(cycleTimezone));
284 }
285 final long warningBytes = in.readLong();
286 final long limitBytes = in.readLong();
287 final long lastWarningSnooze = in.readLong();
288 final long lastLimitSnooze = in.readLong();
289 final long lastRapidSnooze;
290 if (version >= VERSION_RAPID) {
291 lastRapidSnooze = in.readLong();
292 } else {
293 lastRapidSnooze = SNOOZE_NEVER;
294 }
295 final boolean metered = in.readInt() == 1;
296 final boolean inferred = in.readInt() == 1;
297 return new NetworkPolicy(template, cycleRule, warningBytes, limitBytes, lastWarningSnooze,
298 lastLimitSnooze, lastRapidSnooze, metered, inferred);
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000299 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700300}