blob: 14442a2088cdfdaf7f3863da2fb2282226a0d5a1 [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;
Aaron Huang2d471ca2019-12-30 20:11:36 +080024import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.SystemService;
Sudheer Shankae359c3d2017-02-22 18:41:29 -080026import android.app.ActivityManager;
Artur Satayev26958002019-12-10 17:47:52 +000027import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070028import android.content.Context;
Jeff Sharkey14711eb2011-06-15 10:29:17 -070029import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070030import android.content.pm.PackageManager;
31import android.content.pm.PackageManager.NameNotFoundException;
32import android.content.pm.Signature;
Jeff Sharkey43d2a172017-07-12 10:50:42 -060033import android.net.wifi.WifiConfiguration;
34import android.net.wifi.WifiInfo;
Mathew Inwood31755f92018-12-20 13:53:36 +000035import android.os.Build;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070036import android.os.RemoteException;
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -070037import android.os.UserHandle;
Sarah Chin7af1fd02019-09-26 11:37:13 -070038import android.telephony.SubscriptionPlan;
Felipe Leme46c4fc32016-05-04 09:21:43 -070039import android.util.DebugUtils;
Jeff Sharkey53313d72017-07-13 16:47:32 -060040import android.util.Pair;
Jeff Sharkey0fc6d032018-03-30 16:25:11 -060041import android.util.Range;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070042
Jeff Sharkey4414cea2011-06-24 17:05:24 -070043import com.google.android.collect.Sets;
44
Aaron Huang2d471ca2019-12-30 20:11:36 +080045import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
Jeff Sharkey53313d72017-07-13 16:47:32 -060047import java.time.ZonedDateTime;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070048import java.util.HashSet;
Jeff Sharkey53313d72017-07-13 16:47:32 -060049import java.util.Iterator;
Aaron Huang9204f462019-12-25 00:26:47 +080050import java.util.Map;
51import java.util.concurrent.ConcurrentHashMap;
Jeff Sharkey1b861272011-05-22 00:34:52 -070052
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070053/**
54 * Manager for creating and modifying network policy rules.
55 *
Aaron Huang9204f462019-12-25 00:26:47 +080056 * @hide
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070057 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060058@SystemService(Context.NETWORK_POLICY_SERVICE)
Aaron Huang2d471ca2019-12-30 20:11:36 +080059@SystemApi
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070060public class NetworkPolicyManager {
61
Felipe Leme46b451f2016-08-19 08:46:17 -070062 /* POLICY_* are masks and can be ORed, although currently they are not.*/
Aaron Huang2d471ca2019-12-30 20:11:36 +080063 /**
64 * No specific network policy, use system default.
65 * @hide
66 */
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070067 public static final int POLICY_NONE = 0x0;
Aaron Huang2d471ca2019-12-30 20:11:36 +080068 /**
69 * Reject network usage on metered networks when application in background.
70 * @hide
71 */
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070072 public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
Aaron Huang2d471ca2019-12-30 20:11:36 +080073 /**
74 * Allow metered network use in the background even when in data usage save mode.
75 * @hide
76 */
Felipe Leme46b451f2016-08-19 08:46:17 -070077 public static final int POLICY_ALLOW_METERED_BACKGROUND = 0x4;
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070078
Felipe Leme46c4fc32016-05-04 09:21:43 -070079 /*
80 * Rules defining whether an uid has access to a network given its type (metered / non-metered).
81 *
82 * These rules are bits and can be used in bitmask operations; in particular:
83 * - rule & RULE_MASK_METERED: returns the metered-networks status.
84 * - rule & RULE_MASK_ALL: returns the all-networks status.
85 *
86 * The RULE_xxx_ALL rules applies to all networks (metered or non-metered), but on
87 * metered networks, the RULE_xxx_METERED rules should be checked first. For example,
88 * if the device is on Battery Saver Mode and Data Saver Mode simulatenously, and a uid
89 * is whitelisted for the former but not the latter, its status would be
90 * RULE_REJECT_METERED | RULE_ALLOW_ALL, meaning it could have access to non-metered
91 * networks but not to metered networks.
92 *
93 * See network-policy-restrictions.md for more info.
94 */
Aaron Huang9204f462019-12-25 00:26:47 +080095
Aaron Huang2d471ca2019-12-30 20:11:36 +080096 /**
97 * No specific rule was set
98 * @hide
99 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700100 public static final int RULE_NONE = 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800101 /**
102 * Allow traffic on metered networks.
103 * @hide
104 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700105 public static final int RULE_ALLOW_METERED = 1 << 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800106 /**
107 * Temporarily allow traffic on metered networks because app is on foreground.
108 * @hide
109 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700110 public static final int RULE_TEMPORARY_ALLOW_METERED = 1 << 1;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800111 /**
112 * Reject traffic on metered networks.
113 * @hide
114 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700115 public static final int RULE_REJECT_METERED = 1 << 2;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800116 /**
117 * Network traffic should be allowed on all networks (metered or non-metered), although
118 * metered-network restrictions could still apply.
119 * @hide
120 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700121 public static final int RULE_ALLOW_ALL = 1 << 5;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800122 /**
123 * Reject traffic on all networks.
124 * @hide
125 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700126 public static final int RULE_REJECT_ALL = 1 << 6;
Aaron Huang9204f462019-12-25 00:26:47 +0800127
Aaron Huang2d471ca2019-12-30 20:11:36 +0800128 /**
129 * Mask used to get the {@code RULE_xxx_METERED} rules
130 * @hide
131 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700132 public static final int MASK_METERED_NETWORKS = 0b00001111;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800133 /**
134 * Mask used to get the {@code RULE_xxx_ALL} rules
135 * @hide
136 */
Felipe Leme46c4fc32016-05-04 09:21:43 -0700137 public static final int MASK_ALL_NETWORKS = 0b11110000;
Amith Yamasani15e472352015-04-24 19:06:07 -0700138
Aaron Huang2d471ca2019-12-30 20:11:36 +0800139 /** @hide */
Amith Yamasani15e472352015-04-24 19:06:07 -0700140 public static final int FIREWALL_RULE_DEFAULT = 0;
Aaron Huang2d471ca2019-12-30 20:11:36 +0800141 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700142 public static final String FIREWALL_CHAIN_NAME_NONE = "none";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800143 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700144 public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800145 /** @hide */
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700146 public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby";
Aaron Huang2d471ca2019-12-30 20:11:36 +0800147 /** @hide */
Felipe Leme011b98f2016-02-10 17:28:31 -0800148 public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave";
Xiaohui Chenb41c9f72015-06-17 15:55:37 -0700149
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700150 private static final boolean ALLOW_PLATFORM_APP_POLICY = true;
151
Aaron Huang2d471ca2019-12-30 20:11:36 +0800152 /** @hide */
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800153 public static final int FOREGROUND_THRESHOLD_STATE =
154 ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
155
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700156 /**
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700157 * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it
158 * applies to.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800159 * @hide
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700160 */
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700161 public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE";
Jeff Sharkey14711eb2011-06-15 10:29:17 -0700162
Aaron Huang2d471ca2019-12-30 20:11:36 +0800163 /**
164 * Mask used to check if an override value is marked as unmetered.
165 */
166 public static final int SUBSCRIPTION_OVERRIDE_UNMETERED = 1 << 0;
167
168 /**
169 * Mask used to check if an override value is marked as congested.
170 */
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)
297 @SystemApi
298 public void registerSubscriptionCallback(@NonNull SubscriptionCallback callback) {
299 if (callback == null) {
300 throw new NullPointerException("Callback cannot be null.");
301 }
302
303 final SubscriptionCallbackProxy callbackProxy = new SubscriptionCallbackProxy(callback);
304 if (null != mCallbackMap.putIfAbsent(callback, callbackProxy)) {
305 throw new IllegalArgumentException("Callback is already registered.");
306 }
307 registerListener(callbackProxy);
308 }
309
310 /** @hide */
311 @RequiresPermission(android.Manifest.permission.OBSERVE_NETWORK_POLICY)
312 @SystemApi
313 public void unregisterSubscriptionCallback(@NonNull SubscriptionCallback callback) {
314 if (callback == null) {
315 throw new NullPointerException("Callback cannot be null.");
316 }
317
318 final SubscriptionCallbackProxy callbackProxy = mCallbackMap.remove(callback);
319 if (callbackProxy == null) return;
320
321 unregisterListener(callbackProxy);
322 }
323
324 /** @hide */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700325 public void setNetworkPolicies(NetworkPolicy[] policies) {
326 try {
327 mService.setNetworkPolicies(policies);
328 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700329 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700330 }
331 }
332
Aaron Huang2d471ca2019-12-30 20:11:36 +0800333 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100334 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700335 public NetworkPolicy[] getNetworkPolicies() {
336 try {
Svet Ganov16a16892015-04-16 10:32:04 -0700337 return mService.getNetworkPolicies(mContext.getOpPackageName());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700338 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700339 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700340 }
341 }
342
Aaron Huang2d471ca2019-12-30 20:11:36 +0800343 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100344 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700345 public void setRestrictBackground(boolean restrictBackground) {
346 try {
347 mService.setRestrictBackground(restrictBackground);
348 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700349 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700350 }
351 }
352
Aaron Huang2d471ca2019-12-30 20:11:36 +0800353 /** @hide */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100354 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700355 public boolean getRestrictBackground() {
356 try {
357 return mService.getRestrictBackground();
358 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700359 throw e.rethrowFromSystemServer();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700360 }
361 }
362
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700363 /**
Aaron Huang2d471ca2019-12-30 20:11:36 +0800364 * Override connections to be temporarily marked as either unmetered or congested,
365 * along with automatic timeouts if desired.
366 *
367 * @param subId the subscriber ID this override applies to.
368 * @param overrideMask the bitmask that specifies which of the overrides is being
369 * set or cleared.
370 * @param overrideValue the override values to set or clear.
371 * @param timeoutMillis the timeout after which the requested override will
372 * be automatically cleared, or {@code 0} to leave in the
373 * requested state until explicitly cleared, or the next reboot,
374 * whichever happens first
375 * @param callingPackage the name of the package making the call.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800376 */
377 public void setSubscriptionOverride(int subId, @SubscriptionOverrideMask int overrideMask,
378 @SubscriptionOverrideMask int overrideValue, long timeoutMillis,
379 @NonNull String callingPackage) {
380 try {
381 mService.setSubscriptionOverride(subId, overrideMask, overrideValue, timeoutMillis,
382 callingPackage);
383 } catch (RemoteException e) {
384 throw e.rethrowFromSystemServer();
385 }
386 }
387
388 /**
Aaron Huang58d95592020-01-16 21:53:55 +0800389 * Set the subscription plans for a specific subscriber.
390 *
391 * @param subId the subscriber this relationship applies to.
392 * @param plans the list of plans.
393 * @param callingPackage the name of the package making the call
394 */
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.
409 */
410 @NonNull
411 public SubscriptionPlan[] getSubscriptionPlans(int subId, @NonNull String callingPackage) {
412 try {
413 return mService.getSubscriptionPlans(subId, callingPackage);
414 } catch (RemoteException e) {
415 throw e.rethrowFromSystemServer();
416 }
417 }
418
419 /**
Stuart Scott984dc852015-03-30 13:17:11 -0700420 * Resets network policy settings back to factory defaults.
421 *
422 * @hide
423 */
424 public void factoryReset(String subscriber) {
Stuart Scottf1fb3972015-04-02 18:00:02 -0700425 try {
426 mService.factoryReset(subscriber);
427 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700428 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -0700429 }
430 }
431
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700432 /** {@hide} */
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600433 @Deprecated
Jeff Sharkey53313d72017-07-13 16:47:32 -0600434 public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) {
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600435 final Iterator<Range<ZonedDateTime>> it = policy.cycleIterator();
436 return new Iterator<Pair<ZonedDateTime, ZonedDateTime>>() {
437 @Override
438 public boolean hasNext() {
439 return it.hasNext();
440 }
441
442 @Override
443 public Pair<ZonedDateTime, ZonedDateTime> next() {
Jeff Sharkeye67463d2018-04-13 14:38:01 -0600444 if (hasNext()) {
445 final Range<ZonedDateTime> r = it.next();
446 return Pair.create(r.getLower(), r.getUpper());
447 } else {
448 return Pair.create(null, null);
449 }
Jeff Sharkey0fc6d032018-03-30 16:25:11 -0600450 }
451 };
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700452 }
453
Jeff Sharkey497e4432011-06-14 17:27:29 -0700454 /**
455 * Check if given UID can have a {@link #setUidPolicy(int, int)} defined,
456 * usually to protect critical system services.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800457 * @hide
Jeff Sharkey497e4432011-06-14 17:27:29 -0700458 */
Jeff Sharkey8a8b5812012-03-21 18:13:36 -0700459 @Deprecated
Jeff Sharkey497e4432011-06-14 17:27:29 -0700460 public static boolean isUidValidForPolicy(Context context, int uid) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700461 // first, quick-reject non-applications
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700462 if (!UserHandle.isApp(uid)) {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700463 return false;
464 }
465
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700466 if (!ALLOW_PLATFORM_APP_POLICY) {
467 final PackageManager pm = context.getPackageManager();
468 final HashSet<Signature> systemSignature;
469 try {
470 systemSignature = Sets.newHashSet(
471 pm.getPackageInfo("android", GET_SIGNATURES).signatures);
472 } catch (NameNotFoundException e) {
473 throw new RuntimeException("problem finding system signature", e);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700474 }
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700475
476 try {
477 // reject apps signed with platform cert
478 for (String packageName : pm.getPackagesForUid(uid)) {
479 final HashSet<Signature> packageSignature = Sets.newHashSet(
480 pm.getPackageInfo(packageName, GET_SIGNATURES).signatures);
481 if (packageSignature.containsAll(systemSignature)) {
482 return false;
483 }
484 }
485 } catch (NameNotFoundException e) {
486 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700487 }
488
489 // nothing found above; we can apply policy to UID
490 return true;
Jeff Sharkey497e4432011-06-14 17:27:29 -0700491 }
Felipe Leme46c4fc32016-05-04 09:21:43 -0700492
Felipe Lemeb146f762016-08-19 09:52:16 -0700493 /**
Felipe Leme46c4fc32016-05-04 09:21:43 -0700494 * @hide
495 */
496 public static String uidRulesToString(int uidRules) {
497 final StringBuilder string = new StringBuilder().append(uidRules).append(" (");
498 if (uidRules == RULE_NONE) {
499 string.append("NONE");
500 } else {
501 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, "RULE_", uidRules));
502 }
503 string.append(")");
504 return string.toString();
505 }
Felipe Lemeb146f762016-08-19 09:52:16 -0700506
507 /**
508 * @hide
509 */
510 public static String uidPoliciesToString(int uidPolicies) {
511 final StringBuilder string = new StringBuilder().append(uidPolicies).append(" (");
512 if (uidPolicies == POLICY_NONE) {
513 string.append("NONE");
514 } else {
515 string.append(DebugUtils.flagsToString(NetworkPolicyManager.class,
516 "POLICY_", uidPolicies));
517 }
518 string.append(")");
519 return string.toString();
520 }
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800521
522 /**
523 * Returns true if {@param procState} is considered foreground and as such will be allowed
524 * to access network when the device is idle or in battery saver mode. Otherwise, false.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800525 * @hide
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800526 */
527 public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) {
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800528 return procState <= FOREGROUND_THRESHOLD_STATE;
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800529 }
530
531 /**
532 * Returns true if {@param procState} is considered foreground and as such will be allowed
533 * to access network when the device is in data saver mode. Otherwise, false.
Aaron Huang2d471ca2019-12-30 20:11:36 +0800534 * @hide
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800535 */
536 public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) {
Sudheer Shankad993dcf2018-02-11 12:22:16 -0800537 return procState <= FOREGROUND_THRESHOLD_STATE;
Sudheer Shankae359c3d2017-02-22 18:41:29 -0800538 }
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600539
Aaron Huang2d471ca2019-12-30 20:11:36 +0800540 /** @hide */
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600541 public static String resolveNetworkId(WifiConfiguration config) {
David Sub4d222a2020-01-27 13:27:50 -0800542 return WifiInfo.sanitizeSsid(config.isPasspoint()
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600543 ? config.providerFriendlyName : config.SSID);
544 }
545
Aaron Huang2d471ca2019-12-30 20:11:36 +0800546 /** @hide */
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600547 public static String resolveNetworkId(String ssid) {
David Sub4d222a2020-01-27 13:27:50 -0800548 return WifiInfo.sanitizeSsid(ssid);
Jeff Sharkey43d2a172017-07-12 10:50:42 -0600549 }
Jeff Sharkey75d31892018-01-18 22:01:59 +0900550
Aaron Huang9204f462019-12-25 00:26:47 +0800551 /** @hide */
552 @SystemApi
553 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}