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 | import static android.text.format.Time.MONTH_DAY; |
| 22 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.PackageManager.NameNotFoundException; |
| 27 | import android.content.pm.Signature; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 30 | import android.text.format.Time; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 32 | import com.google.android.collect.Sets; |
| 33 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 34 | import java.io.PrintWriter; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 35 | import java.util.HashSet; |
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 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 52 | /* RULE_* are not masks and they must be exclusive */ |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 53 | public static final int RULE_UNKNOWN = -1; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 54 | /** All network traffic should be allowed. */ |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 55 | public static final int RULE_ALLOW_ALL = 0; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 56 | /** Reject traffic on metered networks. */ |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 57 | public static final int RULE_REJECT_METERED = 1; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 58 | /** Reject traffic on all networks. */ |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 59 | public static final int RULE_REJECT_ALL = 2; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 60 | /** Allow traffic on metered networks. */ |
| 61 | public static final int RULE_ALLOW_METERED = 3; |
| 62 | /** Temporarily allow traffic on metered networks because app is on foreground. */ |
| 63 | public static final int RULE_TEMPORARY_ALLOW_METERED = 4; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 64 | |
| 65 | public static final int FIREWALL_RULE_DEFAULT = 0; |
| 66 | public static final int FIREWALL_RULE_ALLOW = 1; |
| 67 | public static final int FIREWALL_RULE_DENY = 2; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 68 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 69 | public static final int FIREWALL_TYPE_WHITELIST = 0; |
| 70 | public static final int FIREWALL_TYPE_BLACKLIST = 1; |
| 71 | |
| 72 | public static final int FIREWALL_CHAIN_NONE = 0; |
| 73 | public static final int FIREWALL_CHAIN_DOZABLE = 1; |
| 74 | public static final int FIREWALL_CHAIN_STANDBY = 2; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 75 | public static final int FIREWALL_CHAIN_POWERSAVE = 3; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 76 | |
| 77 | public static final String FIREWALL_CHAIN_NAME_NONE = "none"; |
| 78 | public static final String FIREWALL_CHAIN_NAME_DOZABLE = "dozable"; |
| 79 | public static final String FIREWALL_CHAIN_NAME_STANDBY = "standby"; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 80 | public static final String FIREWALL_CHAIN_NAME_POWERSAVE = "powersave"; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 81 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 82 | private static final boolean ALLOW_PLATFORM_APP_POLICY = true; |
| 83 | |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 84 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 85 | * {@link Intent} extra that indicates which {@link NetworkTemplate} rule it |
| 86 | * applies to. |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 87 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 88 | public static final String EXTRA_NETWORK_TEMPLATE = "android.net.NETWORK_TEMPLATE"; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 89 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 90 | private final Context mContext; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 91 | private INetworkPolicyManager mService; |
| 92 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 93 | public NetworkPolicyManager(Context context, INetworkPolicyManager service) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 94 | if (service == null) { |
| 95 | throw new IllegalArgumentException("missing INetworkPolicyManager"); |
| 96 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 97 | mContext = context; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 98 | mService = service; |
| 99 | } |
| 100 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 101 | public static NetworkPolicyManager from(Context context) { |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 102 | return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); |
| 103 | } |
| 104 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 105 | /** |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 106 | * Set policy flags for specific UID. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 107 | * |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 108 | * @param policy {@link #POLICY_NONE} or combination of flags like |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 109 | * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 110 | */ |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 111 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 112 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 113 | mService.setUidPolicy(uid, policy); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 114 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 115 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Add policy flags for specific UID. The given policy bits will be set for |
| 121 | * the uid. Policy flags may be either |
| 122 | * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}. |
| 123 | */ |
| 124 | public void addUidPolicy(int uid, int policy) { |
| 125 | try { |
| 126 | mService.addUidPolicy(uid, policy); |
| 127 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 128 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Clear/remove policy flags for specific UID. The given policy bits will be set for |
| 134 | * the uid. Policy flags may be either |
| 135 | * {@link #POLICY_REJECT_METERED_BACKGROUND} or {@link #POLICY_ALLOW_BACKGROUND_BATTERY_SAVE}. |
| 136 | */ |
| 137 | public void removeUidPolicy(int uid, int policy) { |
| 138 | try { |
| 139 | mService.removeUidPolicy(uid, policy); |
| 140 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 141 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 145 | public int getUidPolicy(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 146 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 147 | return mService.getUidPolicy(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 148 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 149 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 153 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 154 | try { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 155 | return mService.getUidsWithPolicy(policy); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 156 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 157 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 161 | public void registerListener(INetworkPolicyListener listener) { |
| 162 | try { |
| 163 | mService.registerListener(listener); |
| 164 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 165 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | public void unregisterListener(INetworkPolicyListener listener) { |
| 170 | try { |
| 171 | mService.unregisterListener(listener); |
| 172 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 173 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 177 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
| 178 | try { |
| 179 | mService.setNetworkPolicies(policies); |
| 180 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 181 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | public NetworkPolicy[] getNetworkPolicies() { |
| 186 | try { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 187 | return mService.getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -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 | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | public void setRestrictBackground(boolean restrictBackground) { |
| 194 | try { |
| 195 | mService.setRestrictBackground(restrictBackground); |
| 196 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 197 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | public boolean getRestrictBackground() { |
| 202 | try { |
| 203 | return mService.getRestrictBackground(); |
| 204 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 205 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 209 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 210 | * Resets network policy settings back to factory defaults. |
| 211 | * |
| 212 | * @hide |
| 213 | */ |
| 214 | public void factoryReset(String subscriber) { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 215 | try { |
| 216 | mService.factoryReset(subscriber); |
| 217 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 218 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 223 | * Compute the last cycle boundary for the given {@link NetworkPolicy}. For |
| 224 | * example, if cycle day is 20th, and today is June 15th, it will return May |
| 225 | * 20th. When cycle day doesn't exist in current month, it snaps to the 1st |
| 226 | * of following month. |
| 227 | * |
| 228 | * @hide |
| 229 | */ |
| 230 | public static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 231 | if (policy.cycleDay == CYCLE_NONE) { |
| 232 | throw new IllegalArgumentException("Unable to compute boundary without cycleDay"); |
| 233 | } |
| 234 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 235 | final Time now = new Time(policy.cycleTimezone); |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 236 | now.set(currentTime); |
| 237 | |
| 238 | // first, find cycle boundary for current month |
| 239 | final Time cycle = new Time(now); |
| 240 | cycle.hour = cycle.minute = cycle.second = 0; |
| 241 | snapToCycleDay(cycle, policy.cycleDay); |
| 242 | |
| 243 | if (Time.compare(cycle, now) >= 0) { |
| 244 | // cycle boundary is beyond now, use last cycle boundary; start by |
| 245 | // pushing ourselves squarely into last month. |
| 246 | final Time lastMonth = new Time(now); |
| 247 | lastMonth.hour = lastMonth.minute = lastMonth.second = 0; |
| 248 | lastMonth.monthDay = 1; |
| 249 | lastMonth.month -= 1; |
| 250 | lastMonth.normalize(true); |
| 251 | |
| 252 | cycle.set(lastMonth); |
| 253 | snapToCycleDay(cycle, policy.cycleDay); |
| 254 | } |
| 255 | |
| 256 | return cycle.toMillis(true); |
| 257 | } |
| 258 | |
| 259 | /** {@hide} */ |
| 260 | public static long computeNextCycleBoundary(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 | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 265 | final Time now = new Time(policy.cycleTimezone); |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 266 | now.set(currentTime); |
| 267 | |
| 268 | // first, find cycle boundary for current month |
| 269 | final Time cycle = new Time(now); |
| 270 | cycle.hour = cycle.minute = cycle.second = 0; |
| 271 | snapToCycleDay(cycle, policy.cycleDay); |
| 272 | |
| 273 | if (Time.compare(cycle, now) <= 0) { |
| 274 | // cycle boundary is before now, use next cycle boundary; start by |
| 275 | // pushing ourselves squarely into next month. |
| 276 | final Time nextMonth = new Time(now); |
| 277 | nextMonth.hour = nextMonth.minute = nextMonth.second = 0; |
| 278 | nextMonth.monthDay = 1; |
| 279 | nextMonth.month += 1; |
| 280 | nextMonth.normalize(true); |
| 281 | |
| 282 | cycle.set(nextMonth); |
| 283 | snapToCycleDay(cycle, policy.cycleDay); |
| 284 | } |
| 285 | |
| 286 | return cycle.toMillis(true); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * 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] | 291 | * exist, it snaps to last second of current month. |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 292 | * |
| 293 | * @hide |
| 294 | */ |
| 295 | public static void snapToCycleDay(Time time, int cycleDay) { |
| 296 | if (cycleDay > time.getActualMaximum(MONTH_DAY)) { |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 297 | // cycle day isn't valid this month; snap to last second of month |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 298 | time.month += 1; |
| 299 | time.monthDay = 1; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 300 | time.second = -1; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 301 | } else { |
| 302 | time.monthDay = cycleDay; |
| 303 | } |
| 304 | time.normalize(true); |
| 305 | } |
| 306 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Check if given UID can have a {@link #setUidPolicy(int, int)} defined, |
| 309 | * usually to protect critical system services. |
| 310 | */ |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 311 | @Deprecated |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 312 | public static boolean isUidValidForPolicy(Context context, int uid) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 313 | // first, quick-reject non-applications |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 314 | if (!UserHandle.isApp(uid)) { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 318 | if (!ALLOW_PLATFORM_APP_POLICY) { |
| 319 | final PackageManager pm = context.getPackageManager(); |
| 320 | final HashSet<Signature> systemSignature; |
| 321 | try { |
| 322 | systemSignature = Sets.newHashSet( |
| 323 | pm.getPackageInfo("android", GET_SIGNATURES).signatures); |
| 324 | } catch (NameNotFoundException e) { |
| 325 | throw new RuntimeException("problem finding system signature", e); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 326 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 327 | |
| 328 | try { |
| 329 | // reject apps signed with platform cert |
| 330 | for (String packageName : pm.getPackagesForUid(uid)) { |
| 331 | final HashSet<Signature> packageSignature = Sets.newHashSet( |
| 332 | pm.getPackageInfo(packageName, GET_SIGNATURES).signatures); |
| 333 | if (packageSignature.containsAll(systemSignature)) { |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | } catch (NameNotFoundException e) { |
| 338 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // nothing found above; we can apply policy to UID |
| 342 | return true; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 343 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 344 | } |