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; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Artur Satayev | 33f9217 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
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 | 55418ea | 2018-12-20 15:30:45 +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; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 95 | |
| 96 | public static final String FIREWALL_CHAIN_NAME_NONE = "none"; |
| 97 | public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable"; |
| 98 | public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby"; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 99 | public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave"; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 100 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 101 | private static final boolean ALLOW_PLATFORM_APP_POLICY = true; |
| 102 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 103 | public static final int FOREGROUND_THRESHOLD_STATE = |
| 104 | ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; |
| 105 | |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 106 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 107 | * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it |
| 108 | * applies to. |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 109 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 110 | public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE"; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 111 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 112 | public static final int OVERRIDE_UNMETERED = 1 << 0; |
| 113 | public static final int OVERRIDE_CONGESTED = 1 << 1; |
| 114 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 115 | private final Context mContext; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 116 | @UnsupportedAppUsage |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 117 | private INetworkPolicyManager mService; |
| 118 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 119 | public NetworkPolicyManager(Context context, INetworkPolicyManager service) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 120 | if (service == null) { |
| 121 | throw new IllegalArgumentException("missing INetworkPolicyManager"); |
| 122 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 123 | mContext = context; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 124 | mService = service; |
| 125 | } |
| 126 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 128 | public static NetworkPolicyManager from(Context context) { |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 129 | return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); |
| 130 | } |
| 131 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 132 | /** |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 133 | * Set policy flags for specific UID. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 134 | * |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 135 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 136 | * although it is not validated. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 137 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 138 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 139 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 140 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 141 | mService.setUidPolicy(uid, policy); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 142 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 143 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 147 | /** |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 148 | * Add policy flags for specific UID. |
| 149 | * |
| 150 | * <p>The given policy bits will be set for the uid. |
| 151 | * |
| 152 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 153 | * although it is not validated. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 154 | */ |
| 155 | public void addUidPolicy(int uid, int policy) { |
| 156 | try { |
| 157 | mService.addUidPolicy(uid, policy); |
| 158 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 159 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 164 | * Clear/remove policy flags for specific UID. |
| 165 | * |
| 166 | * <p>The given policy bits will be set for the uid. |
| 167 | * |
| 168 | * @param policy should be {@link #POLICY_NONE} or any combination of {@code POLICY_} flags, |
| 169 | * although it is not validated. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 170 | */ |
| 171 | public void removeUidPolicy(int uid, int policy) { |
| 172 | try { |
| 173 | mService.removeUidPolicy(uid, policy); |
| 174 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 175 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 179 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 180 | public int getUidPolicy(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 181 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 182 | return mService.getUidPolicy(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 183 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 184 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 187 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 188 | @UnsupportedAppUsage |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 189 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 190 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 191 | return mService.getUidsWithPolicy(policy); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 192 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 193 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 197 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 198 | public void registerListener(INetworkPolicyListener listener) { |
| 199 | try { |
| 200 | mService.registerListener(listener); |
| 201 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 202 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 206 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 207 | public void unregisterListener(INetworkPolicyListener listener) { |
| 208 | try { |
| 209 | mService.unregisterListener(listener); |
| 210 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 211 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 215 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
| 216 | try { |
| 217 | mService.setNetworkPolicies(policies); |
| 218 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 219 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 223 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 224 | public NetworkPolicy[] getNetworkPolicies() { |
| 225 | try { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 226 | return mService.getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 227 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 228 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 232 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 233 | public void setRestrictBackground(boolean restrictBackground) { |
| 234 | try { |
| 235 | mService.setRestrictBackground(restrictBackground); |
| 236 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 237 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 241 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 242 | public boolean getRestrictBackground() { |
| 243 | try { |
| 244 | return mService.getRestrictBackground(); |
| 245 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 246 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 250 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 251 | * Resets network policy settings back to factory defaults. |
| 252 | * |
| 253 | * @hide |
| 254 | */ |
| 255 | public void factoryReset(String subscriber) { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 256 | try { |
| 257 | mService.factoryReset(subscriber); |
| 258 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 259 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 263 | /** {@hide} */ |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 264 | @Deprecated |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 265 | public static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(NetworkPolicy policy) { |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 266 | final Iterator<Range<ZonedDateTime>> it = policy.cycleIterator(); |
| 267 | return new Iterator<Pair<ZonedDateTime, ZonedDateTime>>() { |
| 268 | @Override |
| 269 | public boolean hasNext() { |
| 270 | return it.hasNext(); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public Pair<ZonedDateTime, ZonedDateTime> next() { |
Jeff Sharkey | e67463d | 2018-04-13 14:38:01 -0600 | [diff] [blame] | 275 | if (hasNext()) { |
| 276 | final Range<ZonedDateTime> r = it.next(); |
| 277 | return Pair.create(r.getLower(), r.getUpper()); |
| 278 | } else { |
| 279 | return Pair.create(null, null); |
| 280 | } |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 281 | } |
| 282 | }; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Check if given UID can have a {@link #setUidPolicy(int, int)} defined, |
| 287 | * usually to protect critical system services. |
| 288 | */ |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 289 | @Deprecated |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 290 | public static boolean isUidValidForPolicy(Context context, int uid) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 291 | // first, quick-reject non-applications |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 292 | if (!UserHandle.isApp(uid)) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 296 | if (!ALLOW_PLATFORM_APP_POLICY) { |
| 297 | final PackageManager pm = context.getPackageManager(); |
| 298 | final HashSet<Signature> systemSignature; |
| 299 | try { |
| 300 | systemSignature = Sets.newHashSet( |
| 301 | pm.getPackageInfo("android", GET_SIGNATURES).signatures); |
| 302 | } catch (NameNotFoundException e) { |
| 303 | throw new RuntimeException("problem finding system signature", e); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 304 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 305 | |
| 306 | try { |
| 307 | // reject apps signed with platform cert |
| 308 | for (String packageName : pm.getPackagesForUid(uid)) { |
| 309 | final HashSet<Signature> packageSignature = Sets.newHashSet( |
| 310 | pm.getPackageInfo(packageName, GET_SIGNATURES).signatures); |
| 311 | if (packageSignature.containsAll(systemSignature)) { |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | } catch (NameNotFoundException e) { |
| 316 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // nothing found above; we can apply policy to UID |
| 320 | return true; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 321 | } |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 322 | |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 323 | /** |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 324 | * @hide |
| 325 | */ |
| 326 | public static String uidRulesToString(int uidRules) { |
| 327 | final StringBuilder string = new StringBuilder().append(uidRules).append(" ("); |
| 328 | if (uidRules == RULE_NONE) { |
| 329 | string.append("NONE"); |
| 330 | } else { |
| 331 | string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, "RULE_", uidRules)); |
| 332 | } |
| 333 | string.append(")"); |
| 334 | return string.toString(); |
| 335 | } |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * @hide |
| 339 | */ |
| 340 | public static String uidPoliciesToString(int uidPolicies) { |
| 341 | final StringBuilder string = new StringBuilder().append(uidPolicies).append(" ("); |
| 342 | if (uidPolicies == POLICY_NONE) { |
| 343 | string.append("NONE"); |
| 344 | } else { |
| 345 | string.append(DebugUtils.flagsToString(NetworkPolicyManager.class, |
| 346 | "POLICY_", uidPolicies)); |
| 347 | } |
| 348 | string.append(")"); |
| 349 | return string.toString(); |
| 350 | } |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * Returns true if {@param procState} is considered foreground and as such will be allowed |
| 354 | * to access network when the device is idle or in battery saver mode. Otherwise, false. |
| 355 | */ |
| 356 | public static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 357 | return procState <= FOREGROUND_THRESHOLD_STATE; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Returns true if {@param procState} is considered foreground and as such will be allowed |
| 362 | * to access network when the device is in data saver mode. Otherwise, false. |
| 363 | */ |
| 364 | public static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 365 | return procState <= FOREGROUND_THRESHOLD_STATE; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 366 | } |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 367 | |
| 368 | public static String resolveNetworkId(WifiConfiguration config) { |
| 369 | return WifiInfo.removeDoubleQuotes(config.isPasspoint() |
| 370 | ? config.providerFriendlyName : config.SSID); |
| 371 | } |
| 372 | |
| 373 | public static String resolveNetworkId(String ssid) { |
| 374 | return WifiInfo.removeDoubleQuotes(ssid); |
| 375 | } |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 376 | |
| 377 | /** {@hide} */ |
| 378 | public static class Listener extends INetworkPolicyListener.Stub { |
| 379 | @Override public void onUidRulesChanged(int uid, int uidRules) { } |
| 380 | @Override public void onMeteredIfacesChanged(String[] meteredIfaces) { } |
| 381 | @Override public void onRestrictBackgroundChanged(boolean restrictBackground) { } |
| 382 | @Override public void onUidPoliciesChanged(int uid, int uidPolicies) { } |
Sarah Chin | 1431ab2 | 2019-08-19 18:08:21 -0700 | [diff] [blame] | 383 | @Override public void onSubscriptionOverride(int subId, int overrideMask, int overrideValue, |
| 384 | long networkTypeMask) { } |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 385 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 386 | } |