blob: 1922b6df2e7f38ff5ef868be26cb19e1b06acf9b [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 Sharkeycd2ca402011-06-10 15:14:07 -070020
Aaron Huang2d471ca2019-12-30 20:11:36 +080021import android.annotation.IntDef;
22import android.annotation.NonNull;
Aaron Huang9204f462019-12-25 00:26:47 +080023import android.annotation.RequiresPermission;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Sudheer Shankae359c3d2017-02-22 18:41:29 -080025import android.app.ActivityManager;
Artur Satayev26958002019-12-10 17:47:52 +000026import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070027import android.content.Context;
Jeff Sharkey14711eb2011-06-15 10:29:17 -070028import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070029import android.content.pm.PackageManager;
30import android.content.pm.PackageManager.NameNotFoundException;
31import android.content.pm.Signature;
Jeff Sharkey43d2a172017-07-12 10:50:42 -060032import android.net.wifi.WifiConfiguration;
33import android.net.wifi.WifiInfo;
Mathew Inwood31755f92018-12-20 13:53:36 +000034import android.os.Build;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070035import android.os.RemoteException;
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070036import android.os.UserHandle;
Sarah Chin7af1fd02019-09-26 11:37:13 -070037import android.telephony.SubscriptionPlan;
Felipe Leme46c4fc32016-05-04 09:21:43 -070038import android.util.DebugUtils;
Jeff Sharkey53313d72017-07-13 16:47:32 -060039import android.util.Pair;
Jeff Sharkey0fc6d032018-03-30 16:25:11 -060040import android.util.Range;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070041
Jeff Sharkey4414cea2011-06-24 17:05:24 -070042import com.google.android.collect.Sets;
43
Aaron Huang2d471ca2019-12-30 20:11:36 +080044import java.lang.annotation.Retention;
45import java.lang.annotation.RetentionPolicy;
Jeff Sharkey53313d72017-07-13 16:47:32 -060046import java.time.ZonedDateTime;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070047import java.util.HashSet;
Jeff Sharkey53313d72017-07-13 16:47:32 -060048import java.util.Iterator;
Aaron Huang9204f462019-12-25 00:26:47 +080049import java.util.Map;
50import java.util.concurrent.ConcurrentHashMap;
Jeff Sharkey1b861272011-05-22 00:34:52 -070051
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070052/**
53 * Manager for creating and modifying network policy rules.
54 *
Aaron Huang9204f462019-12-25 00:26:47 +080055 * @hide
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070056 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060057@SystemService(Context.NETWORK_POLICY_SERVICE)
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070058public class NetworkPolicyManager {
59
Felipe Leme46b451f2016-08-19 08:46:17 -070060 /* POLICY_* are masks and can be ORed, although currently they are not.*/
Aaron Huang2d471ca2019-12-30 20:11:36 +080061 /**
62 * No specific network policy, use system default.
63 * @hide
64 */
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070065 public static final int POLICY_NONE = 0x0;
Aaron Huang2d471ca2019-12-30 20:11:36 +080066 /**
67 * Reject network usage on metered networks when application in background.
68 * @hide
69 */
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070070 public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
Aaron Huang2d471ca2019-12-30 20:11:36 +080071 /**
72 * Allow metered network use in the background even when in data usage save mode.
73 * @hide
74 */
Felipe Leme46b451f2016-08-19 08:46:17 -070075 public static final int POLICY_ALLOW_METERED_BACKGROUND = 0x4;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070076
Felipe Leme46c4fc32016-05-04 09:21:43 -070077 /*
78 * Rules defining whether an uid has access to a network given its type (metered / non-metered).
79 *
80 * These rules are bits and can be used in bitmask operations; in particular:
81 * - rule & RULE_MASK_METERED: returns the metered-networks status.
82 * - rule & RULE_MASK_ALL: returns the all-networks status.
83 *
84 * The RULE_xxx_ALL rules applies to all networks (metered or non-metered), but on
85 * metered networks, the RULE_xxx_METERED rules should be checked first. For example,
86 * if the device is on Battery Saver Mode and Data Saver Mode simulatenously, and a uid
87 * is whitelisted for the former but not the latter, its status would be
88 * RULE_REJECT_METERED | RULE_ALLOW_ALL, meaning it could have access to non-metered
89 * networks but not to metered networks.
90 *
91 * See network-policy-restrictions.md for more info.
92 */
Aaron Huang9204f462019-12-25 00:26:47 +080093
Aaron Huang2d471ca2019-12-30 20:11:36 +080094 /**
95 * No specific rule was set
96 * @hide
97 */
Felipe Leme46c4fc32016-05-04 09:21:43 -070098 public static final int RULE_NONE = 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +080099 /**
100 * Allow traffic on metered networks.
101 * @hide
102 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700103 public static final int RULE_ALLOW_METERED = 1 << 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800104 /**
105 * Temporarily allow traffic on metered networks because app is on foreground.
106 * @hide
107 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700108 public static final int RULE_TEMPORARY_ALLOW_METERED = 1 << 1;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800109 /**
110 * Reject traffic on metered networks.
111 * @hide
112 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700113 public static final int RULE_REJECT_METERED = 1 << 2;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800114 /**
115 * Network traffic should be allowed on all networks (metered or non-metered), although
116 * metered-network restrictions could still apply.
117 * @hide
118 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700119 public static final int RULE_ALLOW_ALL = 1 << 5;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800120 /**
121 * Reject traffic on all networks.
122 * @hide
123 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700124 public static final int RULE_REJECT_ALL = 1 << 6;
Aaron Huang9204f462019-12-25 00:26:47 +0800125
Aaron Huang2d471ca2019-12-30 20:11:36 +0800126 /**
127 * Mask used to get the {@code RULE_xxx_METERED} rules
128 * @hide
129 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700130 public static final int MASK_METERED_NETWORKS = 0b00001111;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800131 /**
132 * Mask used to get the {@code RULE_xxx_ALL} rules
133 * @hide
134 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700135 public static final int MASK_ALL_NETWORKS = 0b11110000;
Amith Yamasani15e472352015-04-24 19:06:07 -0700136
Aaron Huang2d471ca2019-12-30 20:11:36 +0800137 /** @hide */
Amith Yamasani15e472352015-04-24 19:06:07 -0700138 public static final int FIREWALL_RULE_DEFAULT = 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800139 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700140 public static final String FIREWALL_CHAIN_NAME_NONE = "none";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800141 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700142 public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800143 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700144 public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800145 /** @hide */
Felipe Leme011b98f2016-02-10 17:28:31 -0800146 public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave";
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700147
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700148 private static final boolean ALLOW_PLATFORM_APP_POLICY = true;
149
Aaron Huang2d471ca2019-12-30 20:11:36 +0800150 /** @hide */
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800151 public static final int FOREGROUND_THRESHOLD_STATE =
152 ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
153
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700154 /**
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700155 * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
156 * applies to.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800157 * @hide
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700158 */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700159 public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700160
Aaron Huang2d471ca2019-12-30 20:11:36 +0800161 /**
162 * Mask used to check if an override value is marked as unmetered.
Aaron Huangf68546e2020-03-12 17:52:33 +0800163 * @hide
Aaron Huang2d471ca2019-12-30 20:11:36 +0800164 */
165 public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1 << 0;
166
167 /**
168 * Mask used to check if an override value is marked as congested.
Aaron Huangf68546e2020-03-12 17:52:33 +0800169 * @hide
Aaron Huang2d471ca2019-12-30 20:11:36 +0800170 */
171 public static final int SUBSCRIPTION_OVERRIDE_CONGESTED = 1 << 1;
172
173 /**
174 * @hide
175 */
176 @Retention(RetentionPolicy.SOURCE)
177 @IntDef(flag = true, prefix = { "SUBSCRIPTION_OVERRIDE_" }, value = {
178 SUBSCRIPTION_OVERRIDE_UNMETERED,
179 SUBSCRIPTION_OVERRIDE_CONGESTED
180 })
181 public @interface SubscriptionOverrideMask {}
Jeff Sharkey75d31892018-01-18 22:01:59 +0900182
Svet Ganov16a16892015-04-16 10:32:04 -0700183 private final Context mContext;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100184 @UnsupportedAppUsage
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700185 private INetworkPolicyManager mService;
186
Aaron Huang9204f462019-12-25 00:26:47 +0800187 private final Map<SubscriptionCallback, SubscriptionCallbackProxy>
188 mCallbackMap = new ConcurrentHashMap<>();
189
Aaron Huang2d471ca2019-12-30 20:11:36 +0800190 /** @hide */
Svet Ganov16a16892015-04-16 10:32:04 -0700191 public NetworkPolicyManager(Context context, INetworkPolicyManager service) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700192 if (service == null) {
193 throw new IllegalArgumentException("missing INetworkPolicyManager");
194 }
Svet Ganov16a16892015-04-16 10:32:04 -0700195 mContext = context;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700196 mService = service;
197 }
198
Aaron Huang2d471ca2019-12-30 20:11:36 +0800199 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100200 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700201 public static NetworkPolicyManager from(Context context) {
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700202 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE);
203 }
204
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700205 /**
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700206 * Set policy flags for specific UID.
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700207 *
Felipe Leme8546a442016-08-23 09:38:20 -0700208 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
209 * although it is not validated.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800210 * @hide
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700211 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100212 @UnsupportedAppUsage
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700213 public void setUidPolicy(int uid, int policy) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700214 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700215 mService.setUidPolicy(uid, policy);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700216 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700217 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700218 }
219 }
220
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700221 /**
Felipe Leme8546a442016-08-23 09:38:20 -0700222 * Add policy flags for specific UID.
223 *
224 * <p>The given policy bits will be set for the uid.
225 *
226 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
227 * although it is not validated.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800228 * @hide
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700229 */
230 public void addUidPolicy(int uid, int policy) {
231 try {
232 mService.addUidPolicy(uid, policy);
233 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700234 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700235 }
236 }
237
238 /**
Felipe Leme8546a442016-08-23 09:38:20 -0700239 * Clear/remove policy flags for specific UID.
240 *
241 * <p>The given policy bits will be set for the uid.
242 *
243 * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags,
244 * although it is not validated.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800245 * @hide
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700246 */
247 public void removeUidPolicy(int uid, int policy) {
248 try {
249 mService.removeUidPolicy(uid, policy);
250 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700251 throw e.rethrowFromSystemServer();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700252 }
253 }
254
Aaron Huang2d471ca2019-12-30 20:11:36 +0800255 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100256 @UnsupportedAppUsage
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700257 public int getUidPolicy(int uid) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700258 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700259 return mService.getUidPolicy(uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700260 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700261 throw e.rethrowFromSystemServer();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700262 }
263 }
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700264
Aaron Huang2d471ca2019-12-30 20:11:36 +0800265 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100266 @UnsupportedAppUsage
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700267 public int[] getUidsWithPolicy(int policy) {
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700268 try {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700269 return mService.getUidsWithPolicy(policy);
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700270 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700271 throw e.rethrowFromSystemServer();
Jeff Sharkey854b2b12012-04-13 16:03:40 -0700272 }
273 }
274
Aaron Huang2d471ca2019-12-30 20:11:36 +0800275 /** @hide */
Mathew Inwood31755f92018-12-20 13:53:36 +0000276 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey1a303952011-06-16 13:04:20 -0700277 public void registerListener(INetworkPolicyListener listener) {
278 try {
279 mService.registerListener(listener);
280 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700281 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700282 }
283 }
284
Aaron Huang2d471ca2019-12-30 20:11:36 +0800285 /** @hide */
Mathew Inwood31755f92018-12-20 13:53:36 +0000286 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey1a303952011-06-16 13:04:20 -0700287 public void unregisterListener(INetworkPolicyListener listener) {
288 try {
289 mService.unregisterListener(listener);
290 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700291 throw e.rethrowFromSystemServer();
Jeff Sharkey1a303952011-06-16 13:04:20 -0700292 }
293 }
294
Aaron Huang2d471ca2019-12-30 20:11:36 +0800295 /** @hide */
Aaron Huang9204f462019-12-25 00:26:47 +0800296 @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY)
Aaron Huang9204f462019-12-25 00:26:47 +0800297 public void registerSubscriptionCallback(@NonNull SubscriptionCallback callback) {
298 if (callback == null) {
299 throw new NullPointerException("Callback cannot be null.");
300 }
301
302 final SubscriptionCallbackProxy callbackProxy = new SubscriptionCallbackProxy(callback);
303 if (null != mCallbackMap.putIfAbsent(callback, callbackProxy)) {
304 throw new IllegalArgumentException("Callback is already registered.");
305 }
306 registerListener(callbackProxy);
307 }
308
309 /** @hide */
310 @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY)
Aaron Huang9204f462019-12-25 00:26:47 +0800311 public void unregisterSubscriptionCallback(@NonNull SubscriptionCallback callback) {
312 if (callback == null) {
313 throw new NullPointerException("Callback cannot be null.");
314 }
315
316 final SubscriptionCallbackProxy callbackProxy = mCallbackMap.remove(callback);
317 if (callbackProxy == null) return;
318
319 unregisterListener(callbackProxy);
320 }
321
322 /** @hide */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700323 public void setNetworkPolicies(NetworkPolicy[] policies) {
324 try {
325 mService.setNetworkPolicies(policies);
326 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700327 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700328 }
329 }
330
Aaron Huang2d471ca2019-12-30 20:11:36 +0800331 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100332 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700333 public NetworkPolicy[] getNetworkPolicies() {
334 try {
Svet Ganov16a16892015-04-16 10:32:04 -0700335 return mService.getNetworkPolicies(mContext.getOpPackageName());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700336 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700337 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700338 }
339 }
340
Aaron Huang2d471ca2019-12-30 20:11:36 +0800341 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100342 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700343 public void setRestrictBackground(boolean restrictBackground) {
344 try {
345 mService.setRestrictBackground(restrictBackground);
346 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700347 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700348 }
349 }
350
Aaron Huang2d471ca2019-12-30 20:11:36 +0800351 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100352 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700353 public boolean getRestrictBackground() {
354 try {
355 return mService.getRestrictBackground();
356 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700357 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700358 }
359 }
360
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700361 /**
Aaron Huang2d471ca2019-12-30 20:11:36 +0800362 * Override connections to be temporarily marked as either unmetered or congested,
363 * along with automatic timeouts if desired.
364 *
365 * @param subId the subscriber ID this override applies to.
366 * @param overrideMask the bitmask that specifies which of the overrides is being
367 * set or cleared.
368 * @param overrideValue the override values to set or clear.
369 * @param timeoutMillis the timeout after which the requested override will
370 * be automatically cleared, or {@code 0} to leave in the
371 * requested state until explicitly cleared, or the next reboot,
372 * whichever happens first
373 * @param callingPackage the name of the package making the call.
Aaron Huangf68546e2020-03-12 17:52:33 +0800374 * @hide
Aaron Huang2d471ca2019-12-30 20:11:36 +0800375 */
376 public void setSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask,
377 @SubscriptionOverrideMask int overrideValue, long timeoutMillis,
378 @NonNull String callingPackage) {
379 try {
380 mService.setSubscriptionOverride(subId, overrideMask, overrideValue, timeoutMillis,
381 callingPackage);
382 } catch (RemoteException e) {
383 throw e.rethrowFromSystemServer();
384 }
385 }
386
387 /**
Aaron Huang58d95592020-01-16 21:53:55 +0800388 * Set the subscription plans for a specific subscriber.
389 *
390 * @param subId the subscriber this relationship applies to.
391 * @param plans the list of plans.
392 * @param callingPackage the name of the package making the call
Aaron Huangf68546e2020-03-12 17:52:33 +0800393 * @hide
Aaron Huang58d95592020-01-16 21:53:55 +0800394 */
395 public void setSubscriptionPlans(int subId, @NonNull SubscriptionPlan[] plans,
396 @NonNull String callingPackage) {
397 try {
398 mService.setSubscriptionPlans(subId, plans, callingPackage);
399 } catch (RemoteException e) {
400 throw e.rethrowFromSystemServer();
401 }
402 }
403
404 /**
405 * Get subscription plans for the given subscription id.
406 *
407 * @param subId the subscriber to get the subscription plans for.
408 * @param callingPackage the name of the package making the call.
Aaron Huangf68546e2020-03-12 17:52:33 +0800409 * @hide
Aaron Huang58d95592020-01-16 21:53:55 +0800410 */
411 @NonNull
412 public SubscriptionPlan[] getSubscriptionPlans(int subId, @NonNull String callingPackage) {
413 try {
414 return mService.getSubscriptionPlans(subId, callingPackage);
415 } catch (RemoteException e) {
416 throw e.rethrowFromSystemServer();
417 }
418 }
419
420 /**
Stuart Scott984dc852015-03-30 13:17:11 -0700421 * Resets network policy settings back to factory defaults.
422 *
423 * @hide
424 */
425 public void factoryReset(String subscriber) {
Stuart Scottf1fb3972015-04-02 18:00:02 -0700426 try {
427 mService.factoryReset(subscriber);
428 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700429 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -0700430 }
431 }
432
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700433 /** {@hide} */
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600434 @Deprecated
Jeff Sharkey53313d72017-07-13 16:47:32 -0600435 public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) {
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600436 final Iterator<Range<ZonedDateTime>> it = policy.cycleIterator();
437 return new Iterator<Pair<ZonedDateTime, ZonedDateTime>>() {
438 @Override
439 public boolean hasNext() {
440 return it.hasNext();
441 }
442
443 @Override
444 public Pair<ZonedDateTime, ZonedDateTime> next() {
Jeff Sharkeye67463d2018-04-13 14:38:01 -0600445 if (hasNext()) {
446 final Range<ZonedDateTime> r = it.next();
447 return Pair.create(r.getLower(), r.getUpper());
448 } else {
449 return Pair.create(null, null);
450 }
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600451 }
452 };
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700453 }
454
Jeff Sharkey497e4432011-06-14 17:27:29 -0700455 /**
456 * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
457 * usually to protect critical system services.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800458 * @hide
Jeff Sharkey497e4432011-06-14 17:27:29 -0700459 */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700460 @Deprecated
Jeff Sharkey497e4432011-06-14 17:27:29 -0700461 public static boolean isUidValidForPolicy(Context context, int uid) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700462 // first, quick-reject non-applications
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700463 if (!UserHandle.isApp(uid)) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700464 return false;
465 }
466
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700467 if (!ALLOW_PLATFORM_APP_POLICY) {
468 final PackageManager pm = context.getPackageManager();
469 final HashSet<Signature> systemSignature;
470 try {
471 systemSignature = Sets.newHashSet(
472 pm.getPackageInfo("android", GET_SIGNATURES).signatures);
473 } catch (NameNotFoundException e) {
474 throw new RuntimeException("problem finding system signature", e);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700475 }
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700476
477 try {
478 // reject apps signed with platform cert
479 for (String packageName : pm.getPackagesForUid(uid)) {
480 final HashSet<Signature> packageSignature = Sets.newHashSet(
481 pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
482 if (packageSignature.containsAll(systemSignature)) {
483 return false;
484 }
485 }
486 } catch (NameNotFoundException e) {
487 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700488 }
489
490 // nothing found above; we can apply policy to UID
491 return true;
Jeff Sharkey497e4432011-06-14 17:27:29 -0700492 }
Felipe Leme46c4fc32016-05-04 09:21:43 -0700493
Felipe Lemeb146f762016-08-19 09:52:16 -0700494 /**
Felipe Leme46c4fc32016-05-04 09:21:43 -0700495 * @hide
496 */
497 public static String uidRulesToString(int uidRules) {
498 final StringBuilder string = new StringBuilder().append(uidRules).append(" (");
499 if (uidRules == RULE_NONE) {
500 string.append("NONE");
501 } else {
502 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, "RULE_", uidRules));
503 }
504 string.append(")");
505 return string.toString();
506 }
Felipe Lemeb146f762016-08-19 09:52:16 -0700507
508 /**
509 * @hide
510 */
511 public static String uidPoliciesToString(int uidPolicies) {
512 final StringBuilder string = new StringBuilder().append(uidPolicies).append(" (");
513 if (uidPolicies == POLICY_NONE) {
514 string.append("NONE");
515 } else {
516 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class,
517 "POLICY_", uidPolicies));
518 }
519 string.append(")");
520 return string.toString();
521 }
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800522
523 /**
524 * Returns true if {@param procState} is considered foreground and as such will be allowed
525 * to access network when the device is idle or in battery saver mode. Otherwise, false.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800526 * @hide
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800527 */
528 public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) {
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800529 return procState <= FOREGROUND_THRESHOLD_STATE;
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800530 }
531
532 /**
533 * Returns true if {@param procState} is considered foreground and as such will be allowed
534 * to access network when the device is in data saver mode. Otherwise, false.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800535 * @hide
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800536 */
537 public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) {
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800538 return procState <= FOREGROUND_THRESHOLD_STATE;
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800539 }
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600540
Aaron Huang2d471ca2019-12-30 20:11:36 +0800541 /** @hide */
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600542 public static String resolveNetworkId(WifiConfiguration config) {
David Sub4d222a2020-01-27 13:27:50 -0800543 return WifiInfo.sanitizeSsid(config.isPasspoint()
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600544 ? config.providerFriendlyName : config.SSID);
545 }
546
Aaron Huang2d471ca2019-12-30 20:11:36 +0800547 /** @hide */
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600548 public static String resolveNetworkId(String ssid) {
David Sub4d222a2020-01-27 13:27:50 -0800549 return WifiInfo.sanitizeSsid(ssid);
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600550 }
Jeff Sharkey75d31892018-01-18 22:01:59 +0900551
Aaron Huang9204f462019-12-25 00:26:47 +0800552 /** @hide */
Aaron Huang9204f462019-12-25 00:26:47 +0800553 public static class SubscriptionCallback {
554 /**
555 * Notify clients of a new override about a given subscription.
556 *
557 * @param subId the subscriber this override applies to.
558 * @param overrideMask a bitmask that specifies which of the overrides is set.
559 * @param overrideValue a bitmask that specifies the override values.
560 */
561 public void onSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask,
562 @SubscriptionOverrideMask int overrideValue) {}
563
564 /**
565 * Notify of subscription plans change about a given subscription.
566 *
567 * @param subId the subscriber id that got subscription plans change.
568 * @param plans the list of subscription plans.
569 */
570 public void onSubscriptionPlansChanged(int subId, @NonNull SubscriptionPlan[] plans) {}
571 }
572
573 /**
574 * SubscriptionCallback proxy for SubscriptionCallback object.
575 * @hide
576 */
577 public class SubscriptionCallbackProxy extends Listener {
578 private final SubscriptionCallback mCallback;
579
580 SubscriptionCallbackProxy(SubscriptionCallback callback) {
581 mCallback = callback;
582 }
583
584 @Override
585 public void onSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask,
586 @SubscriptionOverrideMask int overrideValue) {
587 mCallback.onSubscriptionOverride(subId, overrideMask, overrideValue);
588 }
589
590 @Override
591 public void onSubscriptionPlansChanged(int subId, SubscriptionPlan[] plans) {
592 mCallback.onSubscriptionPlansChanged(subId, plans);
593 }
594 }
595
Jeff Sharkey75d31892018-01-18 22:01:59 +0900596 /** {@hide} */
597 public static class Listener extends INetworkPolicyListener.Stub {
598 @Override public void onUidRulesChanged(int uid, int uidRules) { }
599 @Override public void onMeteredIfacesChanged(String[] meteredIfaces) { }
600 @Override public void onRestrictBackgroundChanged(boolean restrictBackground) { }
601 @Override public void onUidPoliciesChanged(int uid, int uidPolicies) { }
Sarah Chin7af1fd02019-09-26 11:37:13 -0700602 @Override public void onSubscriptionOverride(int subId, int overrideMask,
603 int overrideValue) { }
604 @Override public void onSubscriptionPlansChanged(int subId, SubscriptionPlan[] plans) { }
Jeff Sharkey75d31892018-01-18 22:01:59 +0900605 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -0700606}