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 19 import static com.android.internal.util.Preconditions.checkNotNull; 20 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.android.internal.util.Objects; 25 26 /** 27 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle 28 * and limits to be enforced. 29 * 30 * @hide 31 */ 32 public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> { 33 public static final long WARNING_DISABLED = -1; 34 public static final long LIMIT_DISABLED = -1; 35 public static final long SNOOZE_NEVER = -1; 36 37 public final NetworkTemplate template; 38 public int cycleDay; 39 public long warningBytes; 40 public long limitBytes; 41 public long lastSnooze; 42 43 private static final long DEFAULT_MTU = 1500; 44 45 public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes, 46 long lastSnooze) { 47 this.template = checkNotNull(template, "missing NetworkTemplate"); 48 this.cycleDay = cycleDay; 49 this.warningBytes = warningBytes; 50 this.limitBytes = limitBytes; 51 this.lastSnooze = lastSnooze; 52 } 53 54 public NetworkPolicy(Parcel in) { 55 template = in.readParcelable(null); 56 cycleDay = in.readInt(); 57 warningBytes = in.readLong(); 58 limitBytes = in.readLong(); 59 lastSnooze = in.readLong(); 60 } 61 62 /** {@inheritDoc} */ 63 public void writeToParcel(Parcel dest, int flags) { 64 dest.writeParcelable(template, flags); 65 dest.writeInt(cycleDay); 66 dest.writeLong(warningBytes); 67 dest.writeLong(limitBytes); 68 dest.writeLong(lastSnooze); 69 } 70 71 /** {@inheritDoc} */ 72 public int describeContents() { 73 return 0; 74 } 75 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 87 /** {@inheritDoc} */ 88 public int compareTo(NetworkPolicy another) { 89 if (another == null || another.limitBytes == LIMIT_DISABLED) { 90 // other value is missing or disabled; we win 91 return -1; 92 } 93 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { 94 // we're disabled or other limit is smaller; they win 95 return 1; 96 } 97 return 0; 98 } 99 100 @Override 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 117 public String toString() { 118 return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes=" 119 + warningBytes + ", limitBytes=" + limitBytes + ", lastSnooze=" + lastSnooze; 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 } 132