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