Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 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 | |
| 17 | package android.net; |
| 18 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.GET_SIGNATURES; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 20 | |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 21 | import android.annotation.SystemService; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.PackageManager.NameNotFoundException; |
| 28 | import android.content.pm.Signature; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 29 | import android.net.wifi.WifiConfiguration; |
| 30 | import android.net.wifi.WifiInfo; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 31 | import android.os.Build; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 34 | import android.util.DebugUtils; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 35 | import android.util.Pair; |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 36 | import android.util.Range; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 38 | import com.google.android.collect.Sets; |
| 39 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 40 | import java.time.ZonedDateTime; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 41 | import java.util.HashSet; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 42 | import java.util.Iterator; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 43 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Manager for creating and modifying network policy rules. |
| 46 | * |
| 47 | * {@hide} |
| 48 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 49 | @SystemService(Context.NETWORK_POLICY_SERVICE) |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 50 | public class NetworkPolicyManager { |
| 51 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 52 | /* POLICY_* are masks and can be ORed, although currently they are not.*/ |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 53 | /** No specific network policy, use system default. */ |
| 54 | public static final int POLICY_NONE = 0x0; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 55 | /** Reject network usage on metered networks when application in background. */ |
| 56 | public static final int POLICY_REJECT_METERED_BACKGROUND = 0x1; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 57 | /** Allow metered network use in the background even when in data usage save mode. */ |
| 58 | public static final int POLICY_ALLOW_METERED_BACKGROUND = 0x4; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 59 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Rules defining whether an uid has access to a network given its type (metered / non-metered). |
| 62 | * |
| 63 | * These rules are bits and can be used in bitmask operations; in particular: |
| 64 | * - rule & RULE_MASK_METERED: returns the metered-networks status. |
| 65 | * - rule & RULE_MASK_ALL: returns the all-networks status. |
| 66 | * |
| 67 | * The RULE_xxx_ALL rules applies to all networks (metered or non-metered), but on |
| 68 | * metered networks, the RULE_xxx_METERED rules should be checked first. For example, |
| 69 | * if the device is on Battery Saver Mode and Data Saver Mode simulatenously, and a uid |
| 70 | * is whitelisted for the former but not the latter, its status would be |
| 71 | * RULE_REJECT_METERED | RULE_ALLOW_ALL, meaning it could have access to non-metered |
| 72 | * networks but not to metered networks. |
| 73 | * |
| 74 | * See network-policy-restrictions.md for more info. |
| 75 | */ |
| 76 | /** No specific rule was set */ |
| 77 | public static final int RULE_NONE = 0; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 78 | /** Allow traffic on metered networks. */ |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 79 | public static final int RULE_ALLOW_METERED = 1 << 0; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 80 | /** Temporarily allow traffic on metered networks because app is on foreground. */ |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 81 | public static final int RULE_TEMPORARY_ALLOW_METERED = 1 << 1; |
| 82 | /** Reject traffic on metered networks. */ |
| 83 | public static final int RULE_REJECT_METERED = 1 << 2; |
| 84 | /** Network traffic should be allowed on all networks (metered or non-metered), although |
| 85 | * metered-network restrictions could still apply. */ |
| 86 | public static final int RULE_ALLOW_ALL = 1 << 5; |
| 87 | /** Reject traffic on all networks. */ |
| 88 | public static final int RULE_REJECT_ALL = 1 << 6; |
| 89 | /** Mask used to get the {@code RULE_xxx_METERED} rules */ |
| 90 | public static final int MASK_METERED_NETWORKS = 0b00001111; |
| 91 | /** Mask used to get the {@code RULE_xxx_ALL} rules */ |
| 92 | public static final int MASK_ALL_NETWORKS = 0b11110000; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 93 | |
| 94 | public static final int FIREWALL_RULE_DEFAULT = 0; |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 95 | public static final int FIREWALL_RULE_ALLOW = INetd.FIREWALL_RULE_ALLOW; |
| 96 | public static final int FIREWALL_RULE_DENY = INetd.FIREWALL_RULE_DENY; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 97 | |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 98 | public static final int FIREWALL_TYPE_WHITELIST = INetd.FIREWALL_WHITELIST; |
| 99 | public static final int FIREWALL_TYPE_BLACKLIST = INetd.FIREWALL_BLACKLIST; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 100 | |
Luke Huang | a241db9 | 2018-07-31 20:15:24 +0800 | [diff] [blame] | 101 | public static final int FIREWALL_CHAIN_NONE = INetd.FIREWALL_CHAIN_NONE; |
| 102 | public static final int FIREWALL_CHAIN_DOZABLE = INetd.FIREWALL_CHAIN_DOZABLE; |
| 103 | public static final int FIREWALL_CHAIN_STANDBY = INetd.FIREWALL_CHAIN_STANDBY; |
| 104 | public static final int FIREWALL_CHAIN_POWERSAVE = INetd.FIREWALL_CHAIN_POWERSAVE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 105 | |
| 106 | public static final String FIREWALL_CHAIN_NAME_NONE = "none"; |
| 107 | public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable"; |
| 108 | public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby"; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 109 | public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave"; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 110 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 111 | private static final boolean ALLOW_PLATFORM_APP_POLICY = true; |
| 112 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 113 | public static final int FOREGROUND_THRESHOLD_STATE = |
| 114 | ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; |
| 115 | |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 116 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 117 | * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it |
| 118 | * applies to. |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 119 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 120 | public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE"; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 121 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 122 | public static final int OVERRIDE_UNMETERED = 1 << 0; |
| 123 | public static final int OVERRIDE_CONGESTED = 1 << 1; |
| 124 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 125 | private final Context mContext; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 126 | @UnsupportedAppUsage |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 127 | private INetworkPolicyManager mService; |
| 128 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 129 | public NetworkPolicyManager(Context context, INetworkPolicyManager service) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 130 | if (service == null) { |
| 131 | throw new IllegalArgumentException("missing INetworkPolicyManager"); |
| 132 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 133 | mContext = context; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 134 | mService = service; |
| 135 | } |
| 136 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 137 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 138 | public static NetworkPolicyManager from(Context context) { |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 139 | return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); |
| 140 | } |
| 141 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 142 | /** |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 143 | * Set policy flags for specific UID. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 144 | * |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 145 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 146 | * although it is not validated. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 147 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 148 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 149 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 150 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 151 | mService.setUidPolicy(uid, policy); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 152 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 153 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 157 | /** |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 158 | * Add policy flags for specific UID. |
| 159 | * |
| 160 | * <p>The given policy bits will be set for the uid. |
| 161 | * |
| 162 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 163 | * although it is not validated. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 164 | */ |
| 165 | public void addUidPolicy(int uid, int policy) { |
| 166 | try { |
| 167 | mService.addUidPolicy(uid, policy); |
| 168 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 169 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 174 | * Clear/remove policy flags for specific UID. |
| 175 | * |
| 176 | * <p>The given policy bits will be set for the uid. |
| 177 | * |
| 178 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 179 | * although it is not validated. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 180 | */ |
| 181 | public void removeUidPolicy(int uid, int policy) { |
| 182 | try { |
| 183 | mService.removeUidPolicy(uid, policy); |
| 184 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 185 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 189 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 190 | public int getUidPolicy(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 191 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 192 | return mService.getUidPolicy(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 193 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 194 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 197 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 199 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 200 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 201 | return mService.getUidsWithPolicy(policy); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 202 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 203 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 207 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 208 | public void registerListener(INetworkPolicyListener listener) { |
| 209 | try { |
| 210 | mService.registerListener(listener); |
| 211 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 212 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 216 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 217 | public void unregisterListener(INetworkPolicyListener listener) { |
| 218 | try { |
| 219 | mService.unregisterListener(listener); |
| 220 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 221 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 225 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
| 226 | try { |
| 227 | mService.setNetworkPolicies(policies); |
| 228 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 229 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 233 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 234 | public NetworkPolicy[] getNetworkPolicies() { |
| 235 | try { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 236 | return mService.getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 237 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 238 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 242 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 243 | public void setRestrictBackground(boolean restrictBackground) { |
| 244 | try { |
| 245 | mService.setRestrictBackground(restrictBackground); |
| 246 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 247 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 251 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 252 | public boolean getRestrictBackground() { |
| 253 | try { |
| 254 | return mService.getRestrictBackground(); |
| 255 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 256 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 260 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 261 | * Resets network policy settings back to factory defaults. |
| 262 | * |
| 263 | * @hide |
| 264 | */ |
| 265 | public void factoryReset(String subscriber) { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 266 | try { |
| 267 | mService.factoryReset(subscriber); |
| 268 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 269 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 273 | /** {@hide} */ |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 274 | @Deprecated |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 275 | public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) { |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 276 | final Iterator<Range<ZonedDateTime>> it = policy.cycleIterator(); |
| 277 | return new Iterator<Pair<ZonedDateTime, ZonedDateTime>>() { |
| 278 | @Override |
| 279 | public boolean hasNext() { |
| 280 | return it.hasNext(); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public Pair<ZonedDateTime, ZonedDateTime> next() { |
Jeff Sharkey | e67463d | 2018-04-13 14:38:01 -0600 | [diff] [blame] | 285 | if (hasNext()) { |
| 286 | final Range<ZonedDateTime> r = it.next(); |
| 287 | return Pair.create(r.getLower(), r.getUpper()); |
| 288 | } else { |
| 289 | return Pair.create(null, null); |
| 290 | } |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 291 | } |
| 292 | }; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 295 | /** |
| 296 | * Check if given UID can have a {@link #setUidPolicy(int, int)} defined, |
| 297 | * usually to protect critical system services. |
| 298 | */ |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 299 | @Deprecated |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 300 | public static boolean isUidValidForPolicy(Context context, int uid) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 301 | // first, quick-reject non-applications |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 302 | if (!UserHandle.isApp(uid)) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 306 | if (!ALLOW_PLATFORM_APP_POLICY) { |
| 307 | final PackageManager pm = context.getPackageManager(); |
| 308 | final HashSet<Signature> systemSignature; |
| 309 | try { |
| 310 | systemSignature = Sets.newHashSet( |
| 311 | pm.getPackageInfo("android", GET_SIGNATURES).signatures); |
| 312 | } catch (NameNotFoundException e) { |
| 313 | throw new RuntimeException("problem finding system signature", e); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 314 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 315 | |
| 316 | try { |
| 317 | // reject apps signed with platform cert |
| 318 | for (String packageName : pm.getPackagesForUid(uid)) { |
| 319 | final HashSet<Signature> packageSignature = Sets.newHashSet( |
| 320 | pm.getPackageInfo(packageName, GET_SIGNATURES).signatures); |
| 321 | if (packageSignature.containsAll(systemSignature)) { |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | } catch (NameNotFoundException e) { |
| 326 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // nothing found above; we can apply policy to UID |
| 330 | return true; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 331 | } |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 332 | |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 333 | /** |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 334 | * @hide |
| 335 | */ |
| 336 | public static String uidRulesToString(int uidRules) { |
| 337 | final StringBuilder string = new StringBuilder().append(uidRules).append(" ("); |
| 338 | if (uidRules == RULE_NONE) { |
| 339 | string.append("NONE"); |
| 340 | } else { |
| 341 | string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, "RULE_", uidRules)); |
| 342 | } |
| 343 | string.append(")"); |
| 344 | return string.toString(); |
| 345 | } |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 346 | |
| 347 | /** |
| 348 | * @hide |
| 349 | */ |
| 350 | public static String uidPoliciesToString(int uidPolicies) { |
| 351 | final StringBuilder string = new StringBuilder().append(uidPolicies).append(" ("); |
| 352 | if (uidPolicies == POLICY_NONE) { |
| 353 | string.append("NONE"); |
| 354 | } else { |
| 355 | string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, |
| 356 | "POLICY_", uidPolicies)); |
| 357 | } |
| 358 | string.append(")"); |
| 359 | return string.toString(); |
| 360 | } |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 361 | |
| 362 | /** |
| 363 | * Returns true if {@param procState} is considered foreground and as such will be allowed |
| 364 | * to access network when the device is idle or in battery saver mode. Otherwise, false. |
| 365 | */ |
| 366 | public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 367 | return procState <= FOREGROUND_THRESHOLD_STATE; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Returns true if {@param procState} is considered foreground and as such will be allowed |
| 372 | * to access network when the device is in data saver mode. Otherwise, false. |
| 373 | */ |
| 374 | public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 375 | return procState <= FOREGROUND_THRESHOLD_STATE; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 376 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 377 | |
| 378 | public static String resolveNetworkId(WifiConfiguration config) { |
| 379 | return WifiInfo.removeDoubleQuotes(config.isPasspoint() |
| 380 | ? config.providerFriendlyName : config.SSID); |
| 381 | } |
| 382 | |
| 383 | public static String resolveNetworkId(String ssid) { |
| 384 | return WifiInfo.removeDoubleQuotes(ssid); |
| 385 | } |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 386 | |
| 387 | /** {@hide} */ |
| 388 | public static class Listener extends INetworkPolicyListener.Stub { |
| 389 | @Override public void onUidRulesChanged(int uid, int uidRules) { } |
| 390 | @Override public void onMeteredIfacesChanged(String[] meteredIfaces) { } |
| 391 | @Override public void onRestrictBackgroundChanged(boolean restrictBackground) { } |
| 392 | @Override public void onUidPoliciesChanged(int uid, int uidPolicies) { } |
| 393 | @Override public void onSubscriptionOverride(int subId, int overrideMask, int overrideValue) { } |
| 394 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 395 | } |