blob: 1b24f0c7b35f849bd3bc04b75eda7ce7c388986e [file] [log] [blame]
Jeff Sharkey21c9c452011-06-07 12:26:43 -07001/*
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
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070019import static com.android.internal.util.Preconditions.checkNotNull;
20
Jeff Sharkey21c9c452011-06-07 12:26:43 -070021import android.os.Parcel;
22import android.os.Parcelable;
23
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070024import com.android.internal.util.Objects;
25
Jeff Sharkey21c9c452011-06-07 12:26:43 -070026/**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070027 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle
28 * and limits to be enforced.
Jeff Sharkey21c9c452011-06-07 12:26:43 -070029 *
30 * @hide
31 */
32public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070033 public static final long WARNING_DISABLED = -1;
34 public static final long LIMIT_DISABLED = -1;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070035 public static final long SNOOZE_NEVER = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070036
37 public final NetworkTemplate template;
Jeff Sharkey22c055e2011-06-12 21:13:51 -070038 public int cycleDay;
39 public long warningBytes;
40 public long limitBytes;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070041 public long lastSnooze;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070042
Jeff Sharkey50e7e512011-10-10 16:50:35 -070043 private static final long DEFAULT_MTU = 1500;
44
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070045 public NetworkPolicy(NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes,
46 long lastSnooze) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070047 this.template = checkNotNull(template, "missing NetworkTemplate");
Jeff Sharkey21c9c452011-06-07 12:26:43 -070048 this.cycleDay = cycleDay;
49 this.warningBytes = warningBytes;
50 this.limitBytes = limitBytes;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070051 this.lastSnooze = lastSnooze;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070052 }
53
54 public NetworkPolicy(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070055 template = in.readParcelable(null);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070056 cycleDay = in.readInt();
57 warningBytes = in.readLong();
58 limitBytes = in.readLong();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070059 lastSnooze = in.readLong();
Jeff Sharkey21c9c452011-06-07 12:26:43 -070060 }
61
62 /** {@inheritDoc} */
63 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070064 dest.writeParcelable(template, flags);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070065 dest.writeInt(cycleDay);
66 dest.writeLong(warningBytes);
67 dest.writeLong(limitBytes);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070068 dest.writeLong(lastSnooze);
Jeff Sharkey21c9c452011-06-07 12:26:43 -070069 }
70
71 /** {@inheritDoc} */
72 public int describeContents() {
73 return 0;
74 }
75
Jeff Sharkey50e7e512011-10-10 16:50:35 -070076 /**
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 Sharkey21c9c452011-06-07 12:26:43 -070087 /** {@inheritDoc} */
88 public int compareTo(NetworkPolicy another) {
Jeff Sharkey22c055e2011-06-12 21:13:51 -070089 if (another == null || another.limitBytes == LIMIT_DISABLED) {
90 // other value is missing or disabled; we win
Jeff Sharkey21c9c452011-06-07 12:26:43 -070091 return -1;
Jeff Sharkey22c055e2011-06-12 21:13:51 -070092 }
93 if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) {
94 // we're disabled or other limit is smaller; they win
Jeff Sharkey21c9c452011-06-07 12:26:43 -070095 return 1;
96 }
Jeff Sharkey22c055e2011-06-12 21:13:51 -070097 return 0;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070098 }
99
100 @Override
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700101 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 Sharkey21c9c452011-06-07 12:26:43 -0700117 public String toString() {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700118 return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes="
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700119 + warningBytes + ", limitBytes=" + limitBytes + ", lastSnooze=" + lastSnooze;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700120 }
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}