NetworkPolicy.java revision 41ff7ec82422a5b6d00892afdb3232bc0e53d851
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 lastSnooze; 42 43 public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes, 44 long lastSnooze) { 45 this.template = checkNotNull(template, "missing NetworkTemplate"); 46 this.cycleDay = cycleDay; 47 this.warningBytes = warningBytes; 48 this.limitBytes = limitBytes; 49 this.lastSnooze = lastSnooze; 50 } 51 52 public NetworkPolicy(Parcel in) { 53 template = in.readParcelable(null); 54 cycleDay = in.readInt(); 55 warningBytes = in.readLong(); 56 limitBytes = in.readLong(); 57 lastSnooze = in.readLong(); 58 } 59 60 /** {@inheritDoc} */ 61 public void writeToParcel(Parcel dest, int flags) { 62 dest.writeParcelable(template, flags); 63 dest.writeInt(cycleDay); 64 dest.writeLong(warningBytes); 65 dest.writeLong(limitBytes); 66 dest.writeLong(lastSnooze); 67 } 68 69 /** {@inheritDoc} */ 70 public int describeContents() { 71 return 0; 72 } 73 74 /** {@inheritDoc} */ 75 public int compareTo(NetworkPolicy another) { 76 if (another == null || another.limitBytes == LIMIT_DISABLED) { 77 // other value is missing or disabled; we win 78 return -1; 79 } 80 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) { 81 // we're disabled or other limit is smaller; they win 82 return 1; 83 } 84 return 0; 85 } 86 87 @Override 88 public int hashCode() { 89 return Objects.hashCode(template, cycleDay, warningBytes, limitBytes, lastSnooze); 90 } 91 92 @Override 93 public boolean equals(Object obj) { 94 if (obj instanceof NetworkPolicy) { 95 final NetworkPolicy other = (NetworkPolicy) obj; 96 return Objects.equal(template, other.template) && cycleDay == other.cycleDay 97 && warningBytes == other.warningBytes && limitBytes == other.limitBytes 98 && lastSnooze == other.lastSnooze; 99 } 100 return false; 101 } 102 103 @Override 104 public String toString() { 105 return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes=" 106 + warningBytes + ", limitBytes=" + limitBytes + ", lastSnooze=" + lastSnooze; 107 } 108 109 public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() { 110 public NetworkPolicy createFromParcel(Parcel in) { 111 return new NetworkPolicy(in); 112 } 113 114 public NetworkPolicy[] newArray(int size) { 115 return new NetworkPolicy[size]; 116 } 117 }; 118} 119