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