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