Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 23 | import android.util.BackupUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 24 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 25 | import java.io.ByteArrayOutputStream; |
| 26 | import java.io.DataInputStream; |
| 27 | import java.io.DataOutputStream; |
| 28 | import java.io.IOException; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 29 | import java.util.Objects; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 30 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 31 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 32 | * Policy for networks matching a {@link NetworkTemplate}, including usage cycle |
| 33 | * and limits to be enforced. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> { |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Current Version of the Backup Serializer. |
| 40 | */ |
| 41 | private static final int BACKUP_VERSION = 1; |
| 42 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 43 | public static final int CYCLE_NONE = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 44 | public static final long WARNING_DISABLED = -1; |
| 45 | public static final long LIMIT_DISABLED = -1; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 46 | public static final long SNOOZE_NEVER = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 47 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 48 | public NetworkTemplate template; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 49 | public int cycleDay; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 50 | public String cycleTimezone; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 51 | public long warningBytes; |
| 52 | public long limitBytes; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 53 | public long lastWarningSnooze; |
| 54 | public long lastLimitSnooze; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 55 | public boolean metered; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 56 | public boolean inferred; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 58 | private static final long DEFAULT_MTU = 1500; |
| 59 | |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 60 | @Deprecated |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 61 | 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 Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 64 | SNOOZE_NEVER, metered, false); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 67 | public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone, |
| 68 | long warningBytes, long limitBytes, long lastWarningSnooze, long lastLimitSnooze, |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 69 | boolean metered, boolean inferred) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 70 | this.template = checkNotNull(template, "missing NetworkTemplate"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 71 | this.cycleDay = cycleDay; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 72 | this.cycleTimezone = checkNotNull(cycleTimezone, "missing cycleTimezone"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 73 | this.warningBytes = warningBytes; |
| 74 | this.limitBytes = limitBytes; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 75 | this.lastWarningSnooze = lastWarningSnooze; |
| 76 | this.lastLimitSnooze = lastLimitSnooze; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 77 | this.metered = metered; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 78 | this.inferred = inferred; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public NetworkPolicy(Parcel in) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 82 | template = in.readParcelable(null); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 83 | cycleDay = in.readInt(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 84 | cycleTimezone = in.readString(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 85 | warningBytes = in.readLong(); |
| 86 | limitBytes = in.readLong(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 87 | lastWarningSnooze = in.readLong(); |
| 88 | lastLimitSnooze = in.readLong(); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 89 | metered = in.readInt() != 0; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 90 | inferred = in.readInt() != 0; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 93 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 94 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 95 | dest.writeParcelable(template, flags); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 96 | dest.writeInt(cycleDay); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 97 | dest.writeString(cycleTimezone); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 98 | dest.writeLong(warningBytes); |
| 99 | dest.writeLong(limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 100 | dest.writeLong(lastWarningSnooze); |
| 101 | dest.writeLong(lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 102 | dest.writeInt(metered ? 1 : 0); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 103 | dest.writeInt(inferred ? 1 : 0); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 106 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 107 | public int describeContents() { |
| 108 | return 0; |
| 109 | } |
| 110 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 111 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 112 | * 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 Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 119 | * 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 Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 129 | /** |
| 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 Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 137 | /** |
| 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 Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 144 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 145 | public int compareTo(NetworkPolicy another) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 146 | if (another == null || another.limitBytes == LIMIT_DISABLED) { |
| 147 | // other value is missing or disabled; we win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 148 | return -1; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 149 | } |
| 150 | if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { |
| 151 | // we're disabled or other limit is smaller; they win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 152 | return 1; |
| 153 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 154 | return 0; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 158 | public int hashCode() { |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 159 | return Objects.hash(template, cycleDay, cycleTimezone, warningBytes, limitBytes, |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 160 | lastWarningSnooze, lastLimitSnooze, metered, inferred); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public boolean equals(Object obj) { |
| 165 | if (obj instanceof NetworkPolicy) { |
| 166 | final NetworkPolicy other = (NetworkPolicy) obj; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 167 | return cycleDay == other.cycleDay && warningBytes == other.warningBytes |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 168 | && limitBytes == other.limitBytes |
| 169 | && lastWarningSnooze == other.lastWarningSnooze |
| 170 | && lastLimitSnooze == other.lastLimitSnooze && metered == other.metered |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 171 | && inferred == other.inferred |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 172 | && Objects.equals(cycleTimezone, other.cycleTimezone) |
| 173 | && Objects.equals(template, other.template); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 179 | public String toString() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 180 | 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 Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() { |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 194 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 195 | public NetworkPolicy createFromParcel(Parcel in) { |
| 196 | return new NetworkPolicy(in); |
| 197 | } |
| 198 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 199 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 200 | public NetworkPolicy[] newArray(int size) { |
| 201 | return new NetworkPolicy[size]; |
| 202 | } |
| 203 | }; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 204 | |
| 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 Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 241 | } |