blob: bc036376735d4c8efed8f7e45dbe4ced4a2cea57 [file] [log] [blame]
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -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 Sharkey4414cea2011-06-24 17:05:24 -070019import static android.content.pm.PackageManager.GET_SIGNATURES;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070020import static android.net.NetworkPolicy.CYCLE_NONE;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070021import static android.text.format.Time.MONTH_DAY;
22
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070023import android.content.Context;
Jeff Sharkey14711eb2011-06-15 10:29:17 -070024import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070025import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.content.pm.Signature;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070028import android.os.RemoteException;
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070029import android.os.UserHandle;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070030import android.text.format.Time;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070031
Jeff Sharkey4414cea2011-06-24 17:05:24 -070032import com.google.android.collect.Sets;
33
Jeff Sharkey1b861272011-05-22 00:34:52 -070034import java.io.PrintWriter;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070035import java.util.HashSet;
Jeff Sharkey1b861272011-05-22 00:34:52 -070036
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070037/**
38 * Manager for creating and modifying network policy rules.
39 *
40 * {@hide}
41 */
42public class NetworkPolicyManager {
43
44 /** No specific network policy, use system default. */
45 public static final int POLICY_NONE = 0x0;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070046 /** Reject network usage on metered networks when application in background. */
47 public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070048 /** Allow network use (metered or not) in the background in battery save mode. */
49 public static final int POLICY_ALLOW_BACKGROUND_BATTERY_SAVE = 0x2;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070050
51 /** All network traffic should be allowed. */
52 public static final int RULE_ALLOW_ALL = 0x0;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070053 /** Reject traffic on metered networks. */
54 public static final int RULE_REJECT_METERED = 0x1;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070055
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -070056 private static final boolean ALLOW_PLATFORM_APP_POLICY = true;
57
Jeff Sharkey14711eb2011-06-15 10:29:17 -070058 /**
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070059 * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
60 * applies to.
Jeff Sharkey14711eb2011-06-15 10:29:17 -070061 */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070062 public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";
Jeff Sharkey14711eb2011-06-15 10:29:17 -070063
Svet Ganov16a16892015-04-16 10:32:04 -070064 private final Context mContext;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070065 private INetworkPolicyManager mService;
66
Svet Ganov16a16892015-04-16 10:32:04 -070067 public NetworkPolicyManager(Context context, INetworkPolicyManager service) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070068 if (service == null) {
69 throw new IllegalArgumentException("missing INetworkPolicyManager");
70 }
Svet Ganov16a16892015-04-16 10:32:04 -070071 mContext = context;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070072 mService = service;
73 }
74
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070075 public static NetworkPolicyManager from(Context context) {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070076 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE);
77 }
78
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070079 /**
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070080 * Set policy flags for specific UID.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070081 *
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070082 * @param policy {@link #POLICY_NONE} or combination of flags like
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070083 * {@link #POLICY_REJECT_METERED_BACKGROUND}, {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070084 */
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070085 public void setUidPolicy(int uid, int policy) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070086 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070087 mService.setUidPolicy(uid, policy);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070088 } catch (RemoteException e) {
89 }
90 }
91
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070092 /**
93 * Add policy flags for specific UID. The given policy bits will be set for
94 * the uid. Policy flags may be either
95 * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
96 */
97 public void addUidPolicy(int uid, int policy) {
98 try {
99 mService.addUidPolicy(uid, policy);
100 } catch (RemoteException e) {
101 }
102 }
103
104 /**
105 * Clear/remove policy flags for specific UID. The given policy bits will be set for
106 * the uid. Policy flags may be either
107 * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}.
108 */
109 public void removeUidPolicy(int uid, int policy) {
110 try {
111 mService.removeUidPolicy(uid, policy);
112 } catch (RemoteException e) {
113 }
114 }
115
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700116 public int getUidPolicy(int uid) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700117 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700118 return mService.getUidPolicy(uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700119 } catch (RemoteException e) {
120 return POLICY_NONE;
121 }
122 }
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700123
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700124 public int[] getUidsWithPolicy(int policy) {
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700125 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700126 return mService.getUidsWithPolicy(policy);
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700127 } catch (RemoteException e) {
128 return new int[0];
129 }
130 }
131
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700132 public int[] getPowerSaveAppIdWhitelist() {
133 try {
134 return mService.getPowerSaveAppIdWhitelist();
135 } catch (RemoteException e) {
136 return new int[0];
137 }
138 }
139
Jeff Sharkey1a303952011-06-16 13:04:20 -0700140 public void registerListener(INetworkPolicyListener listener) {
141 try {
142 mService.registerListener(listener);
143 } catch (RemoteException e) {
144 }
145 }
146
147 public void unregisterListener(INetworkPolicyListener listener) {
148 try {
149 mService.unregisterListener(listener);
150 } catch (RemoteException e) {
151 }
152 }
153
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700154 public void setNetworkPolicies(NetworkPolicy[] policies) {
155 try {
156 mService.setNetworkPolicies(policies);
157 } catch (RemoteException e) {
158 }
159 }
160
161 public NetworkPolicy[] getNetworkPolicies() {
162 try {
Svet Ganov16a16892015-04-16 10:32:04 -0700163 return mService.getNetworkPolicies(mContext.getOpPackageName());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700164 } catch (RemoteException e) {
165 return null;
166 }
167 }
168
169 public void setRestrictBackground(boolean restrictBackground) {
170 try {
171 mService.setRestrictBackground(restrictBackground);
172 } catch (RemoteException e) {
173 }
174 }
175
176 public boolean getRestrictBackground() {
177 try {
178 return mService.getRestrictBackground();
179 } catch (RemoteException e) {
180 return false;
181 }
182 }
183
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700184 /**
Stuart Scott984dc852015-03-30 13:17:11 -0700185 * Resets network policy settings back to factory defaults.
186 *
187 * @hide
188 */
189 public void factoryReset(String subscriber) {
Stuart Scottf1fb3972015-04-02 18:00:02 -0700190 try {
191 mService.factoryReset(subscriber);
192 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -0700193 }
194 }
195
196 /**
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700197 * Compute the last cycle boundary for the given {@link NetworkPolicy}. For
198 * example, if cycle day is 20th, and today is June 15th, it will return May
199 * 20th. When cycle day doesn't exist in current month, it snaps to the 1st
200 * of following month.
201 *
202 * @hide
203 */
204 public static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700205 if (policy.cycleDay == CYCLE_NONE) {
206 throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
207 }
208
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800209 final Time now = new Time(policy.cycleTimezone);
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700210 now.set(currentTime);
211
212 // first, find cycle boundary for current month
213 final Time cycle = new Time(now);
214 cycle.hour = cycle.minute = cycle.second = 0;
215 snapToCycleDay(cycle, policy.cycleDay);
216
217 if (Time.compare(cycle, now) >= 0) {
218 // cycle boundary is beyond now, use last cycle boundary; start by
219 // pushing ourselves squarely into last month.
220 final Time lastMonth = new Time(now);
221 lastMonth.hour = lastMonth.minute = lastMonth.second = 0;
222 lastMonth.monthDay = 1;
223 lastMonth.month -= 1;
224 lastMonth.normalize(true);
225
226 cycle.set(lastMonth);
227 snapToCycleDay(cycle, policy.cycleDay);
228 }
229
230 return cycle.toMillis(true);
231 }
232
233 /** {@hide} */
234 public static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700235 if (policy.cycleDay == CYCLE_NONE) {
236 throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
237 }
238
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800239 final Time now = new Time(policy.cycleTimezone);
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700240 now.set(currentTime);
241
242 // first, find cycle boundary for current month
243 final Time cycle = new Time(now);
244 cycle.hour = cycle.minute = cycle.second = 0;
245 snapToCycleDay(cycle, policy.cycleDay);
246
247 if (Time.compare(cycle, now) <= 0) {
248 // cycle boundary is before now, use next cycle boundary; start by
249 // pushing ourselves squarely into next month.
250 final Time nextMonth = new Time(now);
251 nextMonth.hour = nextMonth.minute = nextMonth.second = 0;
252 nextMonth.monthDay = 1;
253 nextMonth.month += 1;
254 nextMonth.normalize(true);
255
256 cycle.set(nextMonth);
257 snapToCycleDay(cycle, policy.cycleDay);
258 }
259
260 return cycle.toMillis(true);
261 }
262
263 /**
264 * Snap to the cycle day for the current month given; when cycle day doesn't
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800265 * exist, it snaps to last second of current month.
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700266 *
267 * @hide
268 */
269 public static void snapToCycleDay(Time time, int cycleDay) {
270 if (cycleDay > time.getActualMaximum(MONTH_DAY)) {
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700271 // cycle day isn't valid this month; snap to last second of month
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700272 time.month += 1;
273 time.monthDay = 1;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700274 time.second = -1;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700275 } else {
276 time.monthDay = cycleDay;
277 }
278 time.normalize(true);
279 }
280
Jeff Sharkey497e4432011-06-14 17:27:29 -0700281 /**
282 * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
283 * usually to protect critical system services.
284 */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700285 @Deprecated
Jeff Sharkey497e4432011-06-14 17:27:29 -0700286 public static boolean isUidValidForPolicy(Context context, int uid) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700287 // first, quick-reject non-applications
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700288 if (!UserHandle.isApp(uid)) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700289 return false;
290 }
291
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700292 if (!ALLOW_PLATFORM_APP_POLICY) {
293 final PackageManager pm = context.getPackageManager();
294 final HashSet<Signature> systemSignature;
295 try {
296 systemSignature = Sets.newHashSet(
297 pm.getPackageInfo("android", GET_SIGNATURES).signatures);
298 } catch (NameNotFoundException e) {
299 throw new RuntimeException("problem finding system signature", e);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700300 }
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700301
302 try {
303 // reject apps signed with platform cert
304 for (String packageName : pm.getPackagesForUid(uid)) {
305 final HashSet<Signature> packageSignature = Sets.newHashSet(
306 pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
307 if (packageSignature.containsAll(systemSignature)) {
308 return false;
309 }
310 }
311 } catch (NameNotFoundException e) {
312 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700313 }
314
315 // nothing found above; we can apply policy to UID
316 return true;
Jeff Sharkey497e4432011-06-14 17:27:29 -0700317 }
318
Jeff Sharkey1b861272011-05-22 00:34:52 -0700319 /** {@hide} */
320 public static void dumpPolicy(PrintWriter fout, int policy) {
321 fout.write("[");
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700322 if ((policy & POLICY_REJECT_METERED_BACKGROUND) != 0) {
323 fout.write("REJECT_METERED_BACKGROUND");
Jeff Sharkey1b861272011-05-22 00:34:52 -0700324 }
325 fout.write("]");
326 }
327
328 /** {@hide} */
329 public static void dumpRules(PrintWriter fout, int rules) {
330 fout.write("[");
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700331 if ((rules & RULE_REJECT_METERED) != 0) {
332 fout.write("REJECT_METERED");
Jeff Sharkey1b861272011-05-22 00:34:52 -0700333 }
334 fout.write("]");
335 }
336
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700337}