NetworkPolicyManager.java revision 22c055e6424e0e9579711545d8f4800c0f796db8
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 android.text.format.Time.MONTH_DAY;
20
21import android.content.Context;
22import android.os.RemoteException;
23import android.text.format.Time;
24
25import java.io.PrintWriter;
26
27/**
28 * Manager for creating and modifying network policy rules.
29 *
30 * {@hide}
31 */
32public class NetworkPolicyManager {
33
34    /** No specific network policy, use system default. */
35    public static final int POLICY_NONE = 0x0;
36    /** Reject network usage on paid networks when application in background. */
37    public static final int POLICY_REJECT_PAID_BACKGROUND = 0x1;
38
39    /** All network traffic should be allowed. */
40    public static final int RULE_ALLOW_ALL = 0x0;
41    /** Reject traffic on paid networks. */
42    public static final int RULE_REJECT_PAID = 0x1;
43
44    private INetworkPolicyManager mService;
45
46    public NetworkPolicyManager(INetworkPolicyManager service) {
47        if (service == null) {
48            throw new IllegalArgumentException("missing INetworkPolicyManager");
49        }
50        mService = service;
51    }
52
53    public static NetworkPolicyManager getSystemService(Context context) {
54        return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE);
55    }
56
57    /** {@hide} */
58    public void setNetworkPolicies(NetworkPolicy[] policies) {
59        try {
60            mService.setNetworkPolicies(policies);
61        } catch (RemoteException e) {
62        }
63    }
64
65    /** {@hide} */
66    public NetworkPolicy[] getNetworkPolicies() {
67        try {
68            return mService.getNetworkPolicies();
69        } catch (RemoteException e) {
70            return null;
71        }
72    }
73
74    /**
75     * Set policy flags for specific UID.
76     *
77     * @param policy {@link #POLICY_NONE} or combination of flags like
78     *            {@link #POLICY_REJECT_PAID_BACKGROUND}.
79     */
80    public void setUidPolicy(int uid, int policy) {
81        try {
82            mService.setUidPolicy(uid, policy);
83        } catch (RemoteException e) {
84        }
85    }
86
87    public int getUidPolicy(int uid) {
88        try {
89            return mService.getUidPolicy(uid);
90        } catch (RemoteException e) {
91            return POLICY_NONE;
92        }
93    }
94
95    /**
96     * Compute the last cycle boundary for the given {@link NetworkPolicy}. For
97     * example, if cycle day is 20th, and today is June 15th, it will return May
98     * 20th. When cycle day doesn't exist in current month, it snaps to the 1st
99     * of following month.
100     *
101     * @hide
102     */
103    public static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) {
104        final Time now = new Time(Time.TIMEZONE_UTC);
105        now.set(currentTime);
106
107        // first, find cycle boundary for current month
108        final Time cycle = new Time(now);
109        cycle.hour = cycle.minute = cycle.second = 0;
110        snapToCycleDay(cycle, policy.cycleDay);
111
112        if (Time.compare(cycle, now) >= 0) {
113            // cycle boundary is beyond now, use last cycle boundary; start by
114            // pushing ourselves squarely into last month.
115            final Time lastMonth = new Time(now);
116            lastMonth.hour = lastMonth.minute = lastMonth.second = 0;
117            lastMonth.monthDay = 1;
118            lastMonth.month -= 1;
119            lastMonth.normalize(true);
120
121            cycle.set(lastMonth);
122            snapToCycleDay(cycle, policy.cycleDay);
123        }
124
125        return cycle.toMillis(true);
126    }
127
128    /** {@hide} */
129    public static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) {
130        final Time now = new Time(Time.TIMEZONE_UTC);
131        now.set(currentTime);
132
133        // first, find cycle boundary for current month
134        final Time cycle = new Time(now);
135        cycle.hour = cycle.minute = cycle.second = 0;
136        snapToCycleDay(cycle, policy.cycleDay);
137
138        if (Time.compare(cycle, now) <= 0) {
139            // cycle boundary is before now, use next cycle boundary; start by
140            // pushing ourselves squarely into next month.
141            final Time nextMonth = new Time(now);
142            nextMonth.hour = nextMonth.minute = nextMonth.second = 0;
143            nextMonth.monthDay = 1;
144            nextMonth.month += 1;
145            nextMonth.normalize(true);
146
147            cycle.set(nextMonth);
148            snapToCycleDay(cycle, policy.cycleDay);
149        }
150
151        return cycle.toMillis(true);
152    }
153
154    /**
155     * Snap to the cycle day for the current month given; when cycle day doesn't
156     * exist, it snaps to 1st of following month.
157     *
158     * @hide
159     */
160    public static void snapToCycleDay(Time time, int cycleDay) {
161        if (cycleDay > time.getActualMaximum(MONTH_DAY)) {
162            // cycle day isn't valid this month; snap to 1st of next month
163            time.month += 1;
164            time.monthDay = 1;
165        } else {
166            time.monthDay = cycleDay;
167        }
168        time.normalize(true);
169    }
170
171    /** {@hide} */
172    public static void dumpPolicy(PrintWriter fout, int policy) {
173        fout.write("[");
174        if ((policy & POLICY_REJECT_PAID_BACKGROUND) != 0) {
175            fout.write("REJECT_PAID_BACKGROUND");
176        }
177        fout.write("]");
178    }
179
180    /** {@hide} */
181    public static void dumpRules(PrintWriter fout, int rules) {
182        fout.write("[");
183        if ((rules & RULE_REJECT_PAID) != 0) {
184            fout.write("REJECT_PAID");
185        }
186        fout.write("]");
187    }
188
189}
190