NetworkPolicy.java revision 9bf3150cfae03421c9dd237b46657714859d871c
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 17package android.net; 18 19import static com.android.internal.util.Preconditions.checkNotNull; 20 21import android.os.Parcel; 22import android.os.Parcelable; 23 24import 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 */ 32public 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 String cycleTimezone; 40 public long warningBytes; 41 public long limitBytes; 42 public long lastWarningSnooze; 43 public long lastLimitSnooze; 44 public boolean metered; 45 46 private static final long DEFAULT_MTU = 1500; 47 48 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone, 49 long warningBytes, long limitBytes, boolean metered) { 50 this(template, cycleDay, cycleTimezone, warningBytes, limitBytes, SNOOZE_NEVER, 51 SNOOZE_NEVER, metered); 52 } 53 54 public NetworkPolicy(NetworkTemplate template, int cycleDay, String cycleTimezone, 55 long warningBytes, long limitBytes, long lastWarningSnooze, long lastLimitSnooze, 56 boolean metered) { 57 this.template = checkNotNull(template, "missing NetworkTemplate"); 58 this.cycleDay = cycleDay; 59 this.cycleTimezone = checkNotNull(cycleTimezone, "missing cycleTimezone"); 60 this.warningBytes = warningBytes; 61 this.limitBytes = limitBytes; 62 this.lastWarningSnooze = lastWarningSnooze; 63 this.lastLimitSnooze = lastLimitSnooze; 64 this.metered = metered; 65 } 66 67 public NetworkPolicy(Parcel in) { 68 template = in.readParcelable(null); 69 cycleDay = in.readInt(); 70 cycleTimezone = in.readString(); 71 warningBytes = in.readLong(); 72 limitBytes = in.readLong(); 73 lastWarningSnooze = in.readLong(); 74 lastLimitSnooze = in.readLong(); 75 metered = in.readInt() != 0; 76 } 77 78 @Override 79 public void writeToParcel(Parcel dest, int flags) { 80 dest.writeParcelable(template, flags); 81 dest.writeInt(cycleDay); 82 dest.writeString(cycleTimezone); 83 dest.writeLong(warningBytes); 84 dest.writeLong(limitBytes); 85 dest.writeLong(lastWarningSnooze); 86 dest.writeLong(lastLimitSnooze); 87 dest.writeInt(metered ? 1 : 0); 88 } 89 90 @Override 91 public int describeContents() { 92 return 0; 93 } 94 95 /** 96 * Test if given measurement is over {@link #warningBytes}. 97 */ 98 public boolean isOverWarning(long totalBytes) { 99 return warningBytes != WARNING_DISABLED && totalBytes >= warningBytes; 100 } 101 102 /** 103 * Test if given measurement is near enough to {@link #limitBytes} to be 104 * considered over-limit. 105 */ 106 public boolean isOverLimit(long totalBytes) { 107 // over-estimate, since kernel will trigger limit once first packet 108 // trips over limit. 109 totalBytes += 2 * DEFAULT_MTU; 110 return limitBytes != LIMIT_DISABLED && totalBytes >= limitBytes; 111 } 112 113 /** 114 * Clear any existing snooze values, setting to {@link #SNOOZE_NEVER}. 115 */ 116 public void clearSnooze() { 117 lastWarningSnooze = SNOOZE_NEVER; 118 lastLimitSnooze = SNOOZE_NEVER; 119 } 120 121 @Override 122 public int compareTo(NetworkPolicy another) { 123 if (another == null || another.limitBytes == LIMIT_DISABLED) { 124 // other value is missing or disabled; we win 125 return -1; 126 } 127 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { 128 // we're disabled or other limit is smaller; they win 129 return 1; 130 } 131 return 0; 132 } 133 134 @Override 135 public int hashCode() { 136 return Objects.hashCode(template, cycleDay, cycleTimezone, warningBytes, limitBytes, 137 lastWarningSnooze, lastLimitSnooze, metered); 138 } 139 140 @Override 141 public boolean equals(Object obj) { 142 if (obj instanceof NetworkPolicy) { 143 final NetworkPolicy other = (NetworkPolicy) obj; 144 return cycleDay == other.cycleDay && warningBytes == other.warningBytes 145 && limitBytes == other.limitBytes 146 && lastWarningSnooze == other.lastWarningSnooze 147 && lastLimitSnooze == other.lastLimitSnooze && metered == other.metered 148 && Objects.equal(cycleTimezone, other.cycleTimezone) 149 && Objects.equal(template, other.template); 150 } 151 return false; 152 } 153 154 @Override 155 public String toString() { 156 return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", cycleTimezone=" 157 + cycleTimezone + ", warningBytes=" + warningBytes + ", limitBytes=" + limitBytes 158 + ", lastWarningSnooze=" + lastWarningSnooze + ", lastLimitSnooze=" 159 + lastLimitSnooze + ", metered=" + metered; 160 } 161 162 public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() { 163 @Override 164 public NetworkPolicy createFromParcel(Parcel in) { 165 return new NetworkPolicy(in); 166 } 167 168 @Override 169 public NetworkPolicy[] newArray(int size) { 170 return new NetworkPolicy[size]; 171 } 172 }; 173} 174