blob: 9870e7b6fa0c04936ff6172264c240200092fae7 [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
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070019import static com.android.internal.util.Preconditions.checkNotNull;
20
Jeff Sharkey21c9c452011-06-07 12:26:43 -070021import android.os.Parcel;
22import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000023import android.util.BackupUtils;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070024
Ritesh Reddyadca34a2016-02-04 18:33:30 +000025import java.io.ByteArrayOutputStream;
26import java.io.DataInputStream;
27import java.io.DataOutputStream;
28import java.io.IOException;
Kenny Roote6585b32013-12-13 12:00:26 -080029import java.util.Objects;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070030
Jeff Sharkey21c9c452011-06-07 12:26:43 -070031/**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070032 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle
33 * and limits to be enforced.
Jeff Sharkey21c9c452011-06-07 12:26:43 -070034 *
35 * @hide
36 */
37public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
Ritesh Reddyadca34a2016-02-04 18:33:30 +000038 /**
39 * Current Version of the Backup Serializer.
40 */
41 private static final int BACKUP_VERSION = 1;
42
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070043 public static final int CYCLE_NONE = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070044 public static final long WARNING_DISABLED = -1;
45 public static final long LIMIT_DISABLED = -1;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070046 public static final long SNOOZE_NEVER = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070047
Jeff Sharkey32566012014-12-02 18:30:14 -080048 public NetworkTemplate template;
Jeff Sharkey22c055e2011-06-12 21:13:51 -070049 public int cycleDay;
Jeff Sharkey9bf31502012-03-09 17:07:21 -080050 public String cycleTimezone;
Jeff Sharkey22c055e2011-06-12 21:13:51 -070051 public long warningBytes;
52 public long limitBytes;
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -080053 public long lastWarningSnooze;
54 public long lastLimitSnooze;
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -080055 public boolean metered;
Jeff Sharkey837f9242012-03-20 16:52:20 -070056 public boolean inferred;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070057
Jeff Sharkey50e7e512011-10-10 16:50:35 -070058 private static final long DEFAULT_MTU = 1500;
59
Jeff Sharkey837f9242012-03-20 16:52:20 -070060 @Deprecated
Jeff Sharkey9bf31502012-03-09 17:07:21 -080061 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone,
62 long warningBytes, long limitBytes, boolean metered) {
63 this(template, cycleDay, cycleTimezone, warningBytes, limitBytes, SNOOZE_NEVER,
Jeff Sharkey837f9242012-03-20 16:52:20 -070064 SNOOZE_NEVER, metered, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -080065 }
66
Jeff Sharkey9bf31502012-03-09 17:07:21 -080067 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone,
68 long warningBytes, long limitBytes, long lastWarningSnooze, long lastLimitSnooze,
Jeff Sharkey837f9242012-03-20 16:52:20 -070069 boolean metered, boolean inferred) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070070 this.template = checkNotNull(template, "missing NetworkTemplate");
Jeff Sharkey21c9c452011-06-07 12:26:43 -070071 this.cycleDay = cycleDay;
Jeff Sharkey9bf31502012-03-09 17:07:21 -080072 this.cycleTimezone = checkNotNull(cycleTimezone, "missing cycleTimezone");
Jeff Sharkey21c9c452011-06-07 12:26:43 -070073 this.warningBytes = warningBytes;
74 this.limitBytes = limitBytes;
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -080075 this.lastWarningSnooze = lastWarningSnooze;
76 this.lastLimitSnooze = lastLimitSnooze;
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -080077 this.metered = metered;
Jeff Sharkey837f9242012-03-20 16:52:20 -070078 this.inferred = inferred;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070079 }
80
81 public NetworkPolicy(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070082 template = in.readParcelable(null);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070083 cycleDay = in.readInt();
Jeff Sharkey9bf31502012-03-09 17:07:21 -080084 cycleTimezone = in.readString();
Jeff Sharkey21c9c452011-06-07 12:26:43 -070085 warningBytes = in.readLong();
86 limitBytes = in.readLong();
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -080087 lastWarningSnooze = in.readLong();
88 lastLimitSnooze = in.readLong();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -080089 metered = in.readInt() != 0;
Jeff Sharkey837f9242012-03-20 16:52:20 -070090 inferred = in.readInt() != 0;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070091 }
92
Jeff Sharkey9bf31502012-03-09 17:07:21 -080093 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -070094 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070095 dest.writeParcelable(template, flags);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070096 dest.writeInt(cycleDay);
Jeff Sharkey9bf31502012-03-09 17:07:21 -080097 dest.writeString(cycleTimezone);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070098 dest.writeLong(warningBytes);
99 dest.writeLong(limitBytes);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800100 dest.writeLong(lastWarningSnooze);
101 dest.writeLong(lastLimitSnooze);
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800102 dest.writeInt(metered ? 1 : 0);
Jeff Sharkey837f9242012-03-20 16:52:20 -0700103 dest.writeInt(inferred ? 1 : 0);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700104 }
105
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800106 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700107 public int describeContents() {
108 return 0;
109 }
110
Jeff Sharkey50e7e512011-10-10 16:50:35 -0700111 /**
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800112 * Test if given measurement is over {@link #warningBytes}.
113 */
114 public boolean isOverWarning(long totalBytes) {
115 return warningBytes != WARNING_DISABLED && totalBytes >= warningBytes;
116 }
117
118 /**
Jeff Sharkey50e7e512011-10-10 16:50:35 -0700119 * Test if given measurement is near enough to {@link #limitBytes} to be
120 * considered over-limit.
121 */
122 public boolean isOverLimit(long totalBytes) {
123 // over-estimate, since kernel will trigger limit once first packet
124 // trips over limit.
125 totalBytes += 2 * DEFAULT_MTU;
126 return limitBytes != LIMIT_DISABLED && totalBytes >= limitBytes;
127 }
128
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800129 /**
130 * Clear any existing snooze values, setting to {@link #SNOOZE_NEVER}.
131 */
132 public void clearSnooze() {
133 lastWarningSnooze = SNOOZE_NEVER;
134 lastLimitSnooze = SNOOZE_NEVER;
135 }
136
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700137 /**
138 * Test if this policy has a cycle defined, after which usage should reset.
139 */
140 public boolean hasCycle() {
141 return cycleDay != CYCLE_NONE;
142 }
143
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800144 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700145 public int compareTo(NetworkPolicy another) {
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700146 if (another == null || another.limitBytes == LIMIT_DISABLED) {
147 // other value is missing or disabled; we win
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700148 return -1;
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700149 }
150 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) {
151 // we're disabled or other limit is smaller; they win
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700152 return 1;
153 }
Jeff Sharkey22c055e2011-06-12 21:13:51 -0700154 return 0;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700155 }
156
157 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700158 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -0800159 return Objects.hash(template, cycleDay, cycleTimezone, warningBytes, limitBytes,
Jeff Sharkey837f9242012-03-20 16:52:20 -0700160 lastWarningSnooze, lastLimitSnooze, metered, inferred);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700161 }
162
163 @Override
164 public boolean equals(Object obj) {
165 if (obj instanceof NetworkPolicy) {
166 final NetworkPolicy other = (NetworkPolicy) obj;
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800167 return cycleDay == other.cycleDay && warningBytes == other.warningBytes
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800168 && limitBytes == other.limitBytes
169 && lastWarningSnooze == other.lastWarningSnooze
170 && lastLimitSnooze == other.lastLimitSnooze && metered == other.metered
Jeff Sharkey837f9242012-03-20 16:52:20 -0700171 && inferred == other.inferred
Kenny Roote6585b32013-12-13 12:00:26 -0800172 && Objects.equals(cycleTimezone, other.cycleTimezone)
173 && Objects.equals(template, other.template);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700174 }
175 return false;
176 }
177
178 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700179 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700180 final StringBuilder builder = new StringBuilder("NetworkPolicy");
181 builder.append("[").append(template).append("]:");
182 builder.append(" cycleDay=").append(cycleDay);
183 builder.append(", cycleTimezone=").append(cycleTimezone);
184 builder.append(", warningBytes=").append(warningBytes);
185 builder.append(", limitBytes=").append(limitBytes);
186 builder.append(", lastWarningSnooze=").append(lastWarningSnooze);
187 builder.append(", lastLimitSnooze=").append(lastLimitSnooze);
188 builder.append(", metered=").append(metered);
189 builder.append(", inferred=").append(inferred);
190 return builder.toString();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700191 }
192
193 public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() {
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800194 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700195 public NetworkPolicy createFromParcel(Parcel in) {
196 return new NetworkPolicy(in);
197 }
198
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800199 @Override
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700200 public NetworkPolicy[] newArray(int size) {
201 return new NetworkPolicy[size];
202 }
203 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000204
205 public byte[] getBytesForBackup() throws IOException {
206 ByteArrayOutputStream baos = new ByteArrayOutputStream();
207 DataOutputStream out = new DataOutputStream(baos);
208
209 out.writeInt(BACKUP_VERSION);
210 out.write(template.getBytesForBackup());
211 out.writeInt(cycleDay);
212 BackupUtils.writeString(out, cycleTimezone);
213 out.writeLong(warningBytes);
214 out.writeLong(limitBytes);
215 out.writeLong(lastWarningSnooze);
216 out.writeLong(lastLimitSnooze);
217 out.writeInt(metered ? 1 : 0);
218 out.writeInt(inferred ? 1 : 0);
219 return baos.toByteArray();
220 }
221
222 public static NetworkPolicy getNetworkPolicyFromBackup(DataInputStream in) throws IOException,
223 BackupUtils.BadVersionException {
224 int version = in.readInt();
225 if (version < 1 || version > BACKUP_VERSION) {
226 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
227 }
228
229 NetworkTemplate template = NetworkTemplate.getNetworkTemplateFromBackup(in);
230 int cycleDay = in.readInt();
231 String cycleTimeZone = BackupUtils.readString(in);
232 long warningBytes = in.readLong();
233 long limitBytes = in.readLong();
234 long lastWarningSnooze = in.readLong();
235 long lastLimitSnooze = in.readLong();
236 boolean metered = in.readInt() == 1;
237 boolean inferred = in.readInt() == 1;
238 return new NetworkPolicy(template, cycleDay, cycleTimeZone, warningBytes, limitBytes,
239 lastWarningSnooze, lastLimitSnooze, metered, inferred);
240 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700241}