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