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