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 | |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 22 | import android.util.BackupUtils; |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 23 | import android.util.Range; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 24 | import android.util.RecurrenceRule; |
| 25 | |
| 26 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 27 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 28 | import java.io.ByteArrayOutputStream; |
| 29 | import java.io.DataInputStream; |
| 30 | import java.io.DataOutputStream; |
| 31 | import java.io.IOException; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 32 | import java.time.ZoneId; |
| 33 | import java.time.ZonedDateTime; |
| 34 | import java.util.Iterator; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 35 | import java.util.Objects; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 37 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 38 | * Policy for networks matching a {@link NetworkTemplate}, including usage cycle |
| 39 | * and limits to be enforced. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 40 | * |
| 41 | * @hide |
| 42 | */ |
| 43 | public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 44 | private static final int VERSION_INIT = 1; |
| 45 | private static final int VERSION_RULE = 2; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 46 | private static final int VERSION_RAPID = 3; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 47 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 48 | public static final int CYCLE_NONE = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 49 | public static final long WARNING_DISABLED = -1; |
| 50 | public static final long LIMIT_DISABLED = -1; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 51 | public static final long SNOOZE_NEVER = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 52 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 53 | @UnsupportedAppUsage |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 54 | public NetworkTemplate template; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 55 | public RecurrenceRule cycleRule; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 56 | @UnsupportedAppUsage |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 57 | public long warningBytes = WARNING_DISABLED; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 58 | @UnsupportedAppUsage |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 59 | public long limitBytes = LIMIT_DISABLED; |
| 60 | public long lastWarningSnooze = SNOOZE_NEVER; |
| 61 | public long lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 62 | public long lastRapidSnooze = SNOOZE_NEVER; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 64 | @Deprecated public boolean metered = true; |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 65 | @UnsupportedAppUsage |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 66 | public boolean inferred = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 68 | private static final long DEFAULT_MTU = 1500; |
| 69 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 70 | 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 Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 78 | @Deprecated |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 79 | 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 Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 82 | SNOOZE_NEVER, metered, false); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 85 | @Deprecated |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 86 | @UnsupportedAppUsage |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 87 | public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone, |
| 88 | long warningBytes, long limitBytes, long lastWarningSnooze, long lastLimitSnooze, |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 89 | boolean metered, boolean inferred) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 90 | this(template, buildRule(cycleDay, ZoneId.of(cycleTimezone)), warningBytes, |
| 91 | limitBytes, lastWarningSnooze, lastLimitSnooze, metered, inferred); |
| 92 | } |
| 93 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 94 | @Deprecated |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 95 | public NetworkPolicy(NetworkTemplate template, RecurrenceRule cycleRule, long warningBytes, |
| 96 | long limitBytes, long lastWarningSnooze, long lastLimitSnooze, boolean metered, |
| 97 | boolean inferred) { |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 98 | 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 Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 105 | this.template = Preconditions.checkNotNull(template, "missing NetworkTemplate"); |
| 106 | this.cycleRule = Preconditions.checkNotNull(cycleRule, "missing RecurrenceRule"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 107 | this.warningBytes = warningBytes; |
| 108 | this.limitBytes = limitBytes; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 109 | this.lastWarningSnooze = lastWarningSnooze; |
| 110 | this.lastLimitSnooze = lastLimitSnooze; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 111 | this.lastRapidSnooze = lastRapidSnooze; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 112 | this.metered = metered; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 113 | this.inferred = inferred; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 116 | 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 Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 123 | lastRapidSnooze = source.readLong(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 124 | metered = source.readInt() != 0; |
| 125 | inferred = source.readInt() != 0; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 128 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 129 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 130 | dest.writeParcelable(template, flags); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 131 | dest.writeParcelable(cycleRule, flags); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 132 | dest.writeLong(warningBytes); |
| 133 | dest.writeLong(limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 134 | dest.writeLong(lastWarningSnooze); |
| 135 | dest.writeLong(lastLimitSnooze); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 136 | dest.writeLong(lastRapidSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 137 | dest.writeInt(metered ? 1 : 0); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 138 | dest.writeInt(inferred ? 1 : 0); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 141 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 142 | public int describeContents() { |
| 143 | return 0; |
| 144 | } |
| 145 | |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 146 | public Iterator<Range<ZonedDateTime>> cycleIterator() { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 147 | return cycleRule.cycleIterator(); |
| 148 | } |
| 149 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 150 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 151 | * Test if given measurement is over {@link #warningBytes}. |
| 152 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 153 | @UnsupportedAppUsage |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 154 | public boolean isOverWarning(long totalBytes) { |
| 155 | return warningBytes != WARNING_DISABLED && totalBytes >= warningBytes; |
| 156 | } |
| 157 | |
| 158 | /** |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 159 | * Test if given measurement is near enough to {@link #limitBytes} to be |
| 160 | * considered over-limit. |
| 161 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 162 | @UnsupportedAppUsage |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 163 | 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 Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Clear any existing snooze values, setting to {@link #SNOOZE_NEVER}. |
| 172 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 173 | @UnsupportedAppUsage |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 174 | public void clearSnooze() { |
| 175 | lastWarningSnooze = SNOOZE_NEVER; |
| 176 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 177 | lastRapidSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Test if this policy has a cycle defined, after which usage should reset. |
| 182 | */ |
| 183 | public boolean hasCycle() { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 184 | return cycleRule.cycleIterator().hasNext(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 187 | @Override |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 188 | @UnsupportedAppUsage |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 189 | public int compareTo(NetworkPolicy another) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 190 | if (another == null || another.limitBytes == LIMIT_DISABLED) { |
| 191 | // other value is missing or disabled; we win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 192 | return -1; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 193 | } |
| 194 | if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { |
| 195 | // we're disabled or other limit is smaller; they win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 196 | return 1; |
| 197 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 198 | return 0; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 202 | public int hashCode() { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 203 | return Objects.hash(template, cycleRule, warningBytes, limitBytes, |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 204 | lastWarningSnooze, lastLimitSnooze, lastRapidSnooze, metered, inferred); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public boolean equals(Object obj) { |
| 209 | if (obj instanceof NetworkPolicy) { |
| 210 | final NetworkPolicy other = (NetworkPolicy) obj; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 211 | return warningBytes == other.warningBytes |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 212 | && limitBytes == other.limitBytes |
| 213 | && lastWarningSnooze == other.lastWarningSnooze |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 214 | && lastLimitSnooze == other.lastLimitSnooze |
| 215 | && lastRapidSnooze == other.lastRapidSnooze |
| 216 | && metered == other.metered |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 217 | && inferred == other.inferred |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 218 | && Objects.equals(template, other.template) |
| 219 | && Objects.equals(cycleRule, other.cycleRule); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 225 | public String toString() { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 226 | 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 Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 233 | .append(" lastRapidSnooze=").append(lastRapidSnooze) |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 234 | .append(" metered=").append(metered) |
| 235 | .append(" inferred=").append(inferred) |
| 236 | .append("}").toString(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 239 | @UnsupportedAppUsage |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 240 | public static final @android.annotation.NonNull Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() { |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 241 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 242 | public NetworkPolicy createFromParcel(Parcel in) { |
| 243 | return new NetworkPolicy(in); |
| 244 | } |
| 245 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 246 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 247 | public NetworkPolicy[] newArray(int size) { |
| 248 | return new NetworkPolicy[size]; |
| 249 | } |
| 250 | }; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 251 | |
| 252 | public byte[] getBytesForBackup() throws IOException { |
| 253 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 254 | DataOutputStream out = new DataOutputStream(baos); |
| 255 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 256 | out.writeInt(VERSION_RAPID); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 257 | out.write(template.getBytesForBackup()); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 258 | cycleRule.writeToStream(out); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 259 | out.writeLong(warningBytes); |
| 260 | out.writeLong(limitBytes); |
| 261 | out.writeLong(lastWarningSnooze); |
| 262 | out.writeLong(lastLimitSnooze); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 263 | out.writeLong(lastRapidSnooze); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 264 | 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 Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 271 | final int version = in.readInt(); |
Annie Meng | 47f5c9c | 2018-02-27 14:48:21 +0000 | [diff] [blame] | 272 | if (version < VERSION_INIT || version > VERSION_RAPID) { |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 273 | throw new BackupUtils.BadVersionException("Unknown backup version: " + version); |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 274 | } |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 275 | |
| 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 Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 299 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 300 | } |