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; |
| 23 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 24 | import com.android.internal.util.Objects; |
| 25 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 26 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 27 | * Policy for networks matching a {@link NetworkTemplate}, including usage cycle |
| 28 | * and limits to be enforced. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 33 | public static final long WARNING_DISABLED = -1; |
| 34 | public static final long LIMIT_DISABLED = -1; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 35 | public static final long SNOOZE_NEVER = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 36 | |
| 37 | public final NetworkTemplate template; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 38 | public int cycleDay; |
| 39 | public long warningBytes; |
| 40 | public long limitBytes; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 41 | public long lastSnooze; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 42 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 43 | private static final long DEFAULT_MTU = 1500; |
| 44 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 45 | public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes, |
| 46 | long lastSnooze) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 47 | this.template = checkNotNull(template, "missing NetworkTemplate"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 48 | this.cycleDay = cycleDay; |
| 49 | this.warningBytes = warningBytes; |
| 50 | this.limitBytes = limitBytes; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 51 | this.lastSnooze = lastSnooze; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public NetworkPolicy(Parcel in) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 55 | template = in.readParcelable(null); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 56 | cycleDay = in.readInt(); |
| 57 | warningBytes = in.readLong(); |
| 58 | limitBytes = in.readLong(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 59 | lastSnooze = in.readLong(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** {@inheritDoc} */ |
| 63 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 64 | dest.writeParcelable(template, flags); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 65 | dest.writeInt(cycleDay); |
| 66 | dest.writeLong(warningBytes); |
| 67 | dest.writeLong(limitBytes); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 68 | dest.writeLong(lastSnooze); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** {@inheritDoc} */ |
| 72 | public int describeContents() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Test if given measurement is near enough to {@link #limitBytes} to be |
| 78 | * considered over-limit. |
| 79 | */ |
| 80 | public boolean isOverLimit(long totalBytes) { |
| 81 | // over-estimate, since kernel will trigger limit once first packet |
| 82 | // trips over limit. |
| 83 | totalBytes += 2 * DEFAULT_MTU; |
| 84 | return limitBytes != LIMIT_DISABLED && totalBytes >= limitBytes; |
| 85 | } |
| 86 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 87 | /** {@inheritDoc} */ |
| 88 | public int compareTo(NetworkPolicy another) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 89 | if (another == null || another.limitBytes == LIMIT_DISABLED) { |
| 90 | // other value is missing or disabled; we win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 91 | return -1; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 92 | } |
| 93 | if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { |
| 94 | // we're disabled or other limit is smaller; they win |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 95 | return 1; |
| 96 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 97 | return 0; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 101 | public int hashCode() { |
| 102 | return Objects.hashCode(template, cycleDay, warningBytes, limitBytes, lastSnooze); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object obj) { |
| 107 | if (obj instanceof NetworkPolicy) { |
| 108 | final NetworkPolicy other = (NetworkPolicy) obj; |
| 109 | return Objects.equal(template, other.template) && cycleDay == other.cycleDay |
| 110 | && warningBytes == other.warningBytes && limitBytes == other.limitBytes |
| 111 | && lastSnooze == other.lastSnooze; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | @Override |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 117 | public String toString() { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 118 | return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes=" |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 119 | + warningBytes + ", limitBytes=" + limitBytes + ", lastSnooze=" + lastSnooze; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() { |
| 123 | public NetworkPolicy createFromParcel(Parcel in) { |
| 124 | return new NetworkPolicy(in); |
| 125 | } |
| 126 | |
| 127 | public NetworkPolicy[] newArray(int size) { |
| 128 | return new NetworkPolicy[size]; |
| 129 | } |
| 130 | }; |
| 131 | } |