NetworkPolicy.java revision 1b5a2a96f793211bfbd39aa29cc41031dfa23950
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
24/**
25 * Policy for networks matching a {@link NetworkTemplate}, including usage cycle
26 * and limits to be enforced.
27 *
28 * @hide
29 */
30public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
31    public static final long WARNING_DISABLED = -1;
32    public static final long LIMIT_DISABLED = -1;
33
34    public final NetworkTemplate template;
35    public int cycleDay;
36    public long warningBytes;
37    public long limitBytes;
38
39    // TODO: teach how to snooze limit for current cycle
40
41    public NetworkPolicy(
42            NetworkTemplate template, int cycleDay, long warningBytes, long limitBytes) {
43        this.template = checkNotNull(template, "missing NetworkTemplate");
44        this.cycleDay = cycleDay;
45        this.warningBytes = warningBytes;
46        this.limitBytes = limitBytes;
47    }
48
49    public NetworkPolicy(Parcel in) {
50        template = in.readParcelable(null);
51        cycleDay = in.readInt();
52        warningBytes = in.readLong();
53        limitBytes = in.readLong();
54    }
55
56    /** {@inheritDoc} */
57    public void writeToParcel(Parcel dest, int flags) {
58        dest.writeParcelable(template, flags);
59        dest.writeInt(cycleDay);
60        dest.writeLong(warningBytes);
61        dest.writeLong(limitBytes);
62    }
63
64    /** {@inheritDoc} */
65    public int describeContents() {
66        return 0;
67    }
68
69    /** {@inheritDoc} */
70    public int compareTo(NetworkPolicy another) {
71        if (another == null || another.limitBytes == LIMIT_DISABLED) {
72            // other value is missing or disabled; we win
73            return -1;
74        }
75        if (limitBytes == LIMIT_DISABLED || another.limitBytes < limitBytes) {
76            // we're disabled or other limit is smaller; they win
77            return 1;
78        }
79        return 0;
80    }
81
82    @Override
83    public String toString() {
84        return "NetworkPolicy[" + template + "]: cycleDay=" + cycleDay + ", warningBytes="
85                + warningBytes + ", limitBytes=" + limitBytes;
86    }
87
88    public static final Creator<NetworkPolicy> CREATOR = new Creator<NetworkPolicy>() {
89        public NetworkPolicy createFromParcel(Parcel in) {
90            return new NetworkPolicy(in);
91        }
92
93        public NetworkPolicy[] newArray(int size) {
94            return new NetworkPolicy[size];
95        }
96    };
97}
98