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 com.android.server.net; |
| 18 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_NETWORK_STATE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 22 | import static android.Manifest.permission.MANAGE_SUBSCRIPTION_PLANS; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 23 | import static android.Manifest.permission.NETWORK_SETTINGS; |
| 24 | import static android.Manifest.permission.NETWORK_STACK; |
| 25 | import static android.Manifest.permission.OBSERVE_NETWORK_POLICY; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 26 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 27 | import static android.Manifest.permission.READ_PHONE_STATE; |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 28 | import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 29 | import static android.content.Intent.ACTION_PACKAGE_ADDED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 30 | import static android.content.Intent.ACTION_UID_REMOVED; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 31 | import static android.content.Intent.ACTION_USER_ADDED; |
| 32 | import static android.content.Intent.ACTION_USER_REMOVED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 33 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 34 | import static android.content.pm.PackageManager.MATCH_ANY_USER; |
| 35 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 36 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| 37 | import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; |
| 38 | import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 39 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 40 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 41 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 42 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 43 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 44 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 45 | import static android.net.INetd.FIREWALL_CHAIN_DOZABLE; |
| 46 | import static android.net.INetd.FIREWALL_CHAIN_POWERSAVE; |
| 47 | import static android.net.INetd.FIREWALL_CHAIN_STANDBY; |
| 48 | import static android.net.INetd.FIREWALL_RULE_ALLOW; |
| 49 | import static android.net.INetd.FIREWALL_RULE_DENY; |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 50 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 51 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 52 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 53 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 54 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 55 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 56 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 57 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 58 | import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS; |
| 59 | import static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 60 | import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 61 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 62 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 63 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 64 | import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 65 | import static android.net.NetworkPolicyManager.RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 66 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 67 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 68 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 69 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileIdleOrPowerSaveMode; |
| 70 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileOnRestrictBackground; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 71 | import static android.net.NetworkPolicyManager.resolveNetworkId; |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 72 | import static android.net.NetworkPolicyManager.uidPoliciesToString; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 73 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 74 | import static android.net.NetworkTemplate.MATCH_MOBILE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 75 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 76 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 77 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 78 | import static android.os.Trace.TRACE_TAG_NETWORK; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 79 | import static android.provider.Settings.Global.NETPOLICY_OVERRIDE_ENABLED; |
| 80 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_ENABLED; |
| 81 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_FRAC_JOBS; |
| 82 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_FRAC_MULTIPATH; |
| 83 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_LIMITED; |
| 84 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_UNLIMITED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 85 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 86 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_THRESHOLD_DISABLED; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 87 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_USE_PLATFORM_DEFAULT; |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 88 | import static android.telephony.CarrierConfigManager.KEY_DATA_LIMIT_NOTIFICATION_BOOL; |
| 89 | import static android.telephony.CarrierConfigManager.KEY_DATA_RAPID_NOTIFICATION_BOOL; |
| 90 | import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_NOTIFICATION_BOOL; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 91 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 92 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 93 | import static com.android.internal.util.ArrayUtils.appendInt; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 94 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 95 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 96 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 97 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 98 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 99 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 100 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 101 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 102 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 103 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 104 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_DEFAULT; |
| 105 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_NON_METERED; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 106 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_SYSTEM; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 107 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_TMP_WHITELIST; |
| 108 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_WHITELIST; |
| 109 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_BG_RESTRICT; |
| 110 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_BLACKLIST; |
| 111 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_POWER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 112 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 113 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 114 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 115 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 116 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 117 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 118 | import android.Manifest; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 119 | import android.annotation.IntDef; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 120 | import android.annotation.NonNull; |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 121 | import android.annotation.Nullable; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 122 | import android.app.ActivityManager; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 123 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 124 | import android.app.AppGlobals; |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 125 | import android.app.AppOpsManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 126 | import android.app.IActivityManager; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 127 | import android.app.IUidObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 128 | import android.app.Notification; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 129 | import android.app.NotificationManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 130 | import android.app.PendingIntent; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 131 | import android.app.usage.UsageStatsManagerInternal; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 132 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 133 | import android.content.ComponentName; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 134 | import android.content.ContentResolver; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 135 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 136 | import android.content.Intent; |
| 137 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 138 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 139 | import android.content.pm.IPackageManager; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 140 | import android.content.pm.PackageManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 141 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 142 | import android.content.pm.UserInfo; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 143 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 144 | import android.net.ConnectivityManager; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 145 | import android.net.ConnectivityManager.NetworkCallback; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 146 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 147 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 148 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 149 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 150 | import android.net.INetworkStatsService; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 151 | import android.net.LinkProperties; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 152 | import android.net.Network; |
| 153 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 154 | import android.net.NetworkIdentity; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 155 | import android.net.NetworkPolicy; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 156 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 157 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 158 | import android.net.NetworkRequest; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 159 | import android.net.NetworkSpecifier; |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 160 | import android.net.NetworkStack; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 161 | import android.net.NetworkState; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 162 | import android.net.NetworkStats; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 163 | import android.net.NetworkTemplate; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 164 | import android.net.StringNetworkSpecifier; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 165 | import android.net.TrafficStats; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 166 | import android.net.wifi.WifiConfiguration; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 167 | import android.net.wifi.WifiManager; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 168 | import android.os.BestClock; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 169 | import android.os.Binder; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 170 | import android.os.Environment; |
| 171 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 172 | import android.os.HandlerThread; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 173 | import android.os.IDeviceIdleController; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 174 | import android.os.INetworkManagementService; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 175 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 176 | import android.os.MessageQueue.IdleHandler; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 177 | import android.os.PersistableBundle; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 178 | import android.os.PowerManager; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 179 | import android.os.PowerManager.ServiceType; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 180 | import android.os.PowerManagerInternal; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 181 | import android.os.PowerSaveState; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 182 | import android.os.Process; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 183 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 184 | import android.os.RemoteException; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 185 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 186 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 187 | import android.os.ShellCallback; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 188 | import android.os.SystemClock; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 189 | import android.os.SystemProperties; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 190 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 191 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 192 | import android.os.UserManager; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 193 | import android.provider.Settings; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 194 | import android.provider.Settings.Global; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 195 | import android.telephony.CarrierConfigManager; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 196 | import android.telephony.SubscriptionInfo; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 197 | import android.telephony.SubscriptionManager; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 198 | import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 199 | import android.telephony.SubscriptionPlan; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 200 | import android.telephony.TelephonyManager; |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 201 | import android.text.TextUtils; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 202 | import android.text.format.DateUtils; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 203 | import android.text.format.Formatter; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 204 | import android.util.ArrayMap; |
| 205 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 206 | import android.util.AtomicFile; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 207 | import android.util.DataUnit; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 208 | import android.util.IntArray; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 209 | import android.util.Log; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 210 | import android.util.Pair; |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 211 | import android.util.Range; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 212 | import android.util.RecurrenceRule; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 213 | import android.util.Slog; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 214 | import android.util.SparseArray; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 215 | import android.util.SparseBooleanArray; |
| 216 | import android.util.SparseIntArray; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 217 | import android.util.SparseLongArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 218 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 219 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 220 | import com.android.internal.R; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 221 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 222 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 223 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 224 | import com.android.internal.notification.SystemNotificationChannels; |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 225 | import com.android.internal.os.RoSystemProperties; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 226 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 227 | import com.android.internal.util.ArrayUtils; |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 228 | import com.android.internal.util.ConcurrentUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 229 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 230 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 231 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 232 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 233 | import com.android.internal.util.StatLogger; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 234 | import com.android.server.EventLogTags; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 235 | import com.android.server.LocalServices; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 236 | import com.android.server.ServiceThread; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 237 | import com.android.server.SystemConfig; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 238 | |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 239 | import libcore.io.IoUtils; |
| 240 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 241 | import org.xmlpull.v1.XmlPullParser; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 242 | import org.xmlpull.v1.XmlSerializer; |
| 243 | |
| 244 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 245 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 246 | import java.io.FileInputStream; |
| 247 | import java.io.FileNotFoundException; |
| 248 | import java.io.FileOutputStream; |
| 249 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 250 | import java.io.PrintWriter; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 251 | import java.lang.annotation.Retention; |
| 252 | import java.lang.annotation.RetentionPolicy; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 253 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 254 | import java.time.Clock; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 255 | import java.time.Instant; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 256 | import java.time.ZoneId; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 257 | import java.time.ZoneOffset; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 258 | import java.time.ZonedDateTime; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 259 | import java.time.temporal.ChronoUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 260 | import java.util.ArrayList; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 261 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 262 | import java.util.Calendar; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 263 | import java.util.List; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 264 | import java.util.Objects; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 265 | import java.util.Set; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 266 | import java.util.concurrent.CountDownLatch; |
| 267 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 268 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 269 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 270 | * Service that maintains low-level network policy rules, using |
| 271 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 272 | * <p> |
| 273 | * Derives active rules by combining a given policy with other system status, |
| 274 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 275 | * enforcement. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 276 | * |
| 277 | * <p> |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 278 | * This class uses 2 locks to synchronize state: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 279 | * <ul> |
| 280 | * <li>{@code mUidRulesFirstLock}: used to guard state related to individual UIDs (such as firewall |
| 281 | * rules). |
| 282 | * <li>{@code mNetworkPoliciesSecondLock}: used to guard state related to network interfaces (such |
| 283 | * as network policies). |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 284 | * </ul> |
| 285 | * |
| 286 | * <p> |
| 287 | * As such, methods that require synchronization have the following prefixes: |
| 288 | * <ul> |
| 289 | * <li>{@code UL()}: require the "UID" lock ({@code mUidRulesFirstLock}). |
| 290 | * <li>{@code NL()}: require the "Network" lock ({@code mNetworkPoliciesSecondLock}). |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 291 | * <li>{@code AL()}: require all locks, which must be obtained in order ({@code mUidRulesFirstLock} |
| 292 | * first, then {@code mNetworkPoliciesSecondLock}, then {@code mYetAnotherGuardThirdLock}, etc.. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 293 | * </ul> |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 294 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 295 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 296 | static final String TAG = NetworkPolicyLogger.TAG; |
| 297 | private static final boolean LOGD = NetworkPolicyLogger.LOGD; |
| 298 | private static final boolean LOGV = NetworkPolicyLogger.LOGV; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 299 | |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 300 | /** |
| 301 | * No opportunistic quota could be calculated from user data plan or data settings. |
| 302 | */ |
| 303 | public static final int OPPORTUNISTIC_QUOTA_UNKNOWN = -1; |
| 304 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 305 | private static final int VERSION_INIT = 1; |
| 306 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 307 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 308 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 309 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 310 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 311 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 312 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 313 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 314 | private static final int VERSION_SWITCH_UID = 10; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 315 | private static final int VERSION_ADDED_CYCLE = 11; |
| 316 | private static final int VERSION_LATEST = VERSION_ADDED_CYCLE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 317 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 318 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 319 | public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 320 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 321 | public static final int TYPE_LIMIT = SystemMessage.NOTE_NET_LIMIT; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 322 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 323 | public static final int TYPE_LIMIT_SNOOZED = SystemMessage.NOTE_NET_LIMIT_SNOOZED; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 324 | @VisibleForTesting |
| 325 | public static final int TYPE_RAPID = SystemMessage.NOTE_NET_RAPID; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 326 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 327 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 328 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 329 | private static final String TAG_SUBSCRIPTION_PLAN = "subscription-plan"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 330 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 331 | private static final String TAG_APP_POLICY = "app-policy"; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 332 | private static final String TAG_WHITELIST = "whitelist"; |
| 333 | private static final String TAG_RESTRICT_BACKGROUND = "restrict-background"; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 334 | private static final String TAG_REVOKED_RESTRICT_BACKGROUND = "revoked-restrict-background"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 335 | |
| 336 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 337 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 338 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 339 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 340 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 341 | @Deprecated private static final String ATTR_CYCLE_DAY = "cycleDay"; |
| 342 | @Deprecated private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
| 343 | private static final String ATTR_CYCLE_START = "cycleStart"; |
| 344 | private static final String ATTR_CYCLE_END = "cycleEnd"; |
| 345 | private static final String ATTR_CYCLE_PERIOD = "cyclePeriod"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 346 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 347 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 348 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 349 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 350 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 351 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 352 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 353 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 354 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 355 | private static final String ATTR_POLICY = "policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 356 | private static final String ATTR_SUB_ID = "subId"; |
| 357 | private static final String ATTR_TITLE = "title"; |
| 358 | private static final String ATTR_SUMMARY = "summary"; |
| 359 | private static final String ATTR_LIMIT_BEHAVIOR = "limitBehavior"; |
| 360 | private static final String ATTR_USAGE_BYTES = "usageBytes"; |
| 361 | private static final String ATTR_USAGE_TIME = "usageTime"; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 362 | private static final String ATTR_OWNER_PACKAGE = "ownerPackage"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 363 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 364 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 365 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 366 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 367 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 368 | private static final String ACTION_SNOOZE_RAPID = |
| 369 | "com.android.server.net.action.SNOOZE_RAPID"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 370 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Indicates the maximum wait time for admin data to be available; |
| 373 | */ |
| 374 | private static final long WAIT_FOR_ADMIN_DATA_TIMEOUT_MS = 10_000; |
| 375 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 376 | private static final long QUOTA_UNLIMITED_DEFAULT = DataUnit.MEBIBYTES.toBytes(20); |
| 377 | private static final float QUOTA_LIMITED_DEFAULT = 0.1f; |
| 378 | private static final float QUOTA_FRAC_JOBS_DEFAULT = 0.5f; |
| 379 | private static final float QUOTA_FRAC_MULTIPATH_DEFAULT = 0.5f; |
| 380 | |
Svetoslav Ganov | 24c2775 | 2016-08-31 18:09:00 -0700 | [diff] [blame] | 381 | private static final int MSG_RULES_CHANGED = 1; |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 382 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 383 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 384 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 385 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 386 | private static final int MSG_UPDATE_INTERFACE_QUOTA = 10; |
| 387 | private static final int MSG_REMOVE_INTERFACE_QUOTA = 11; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 388 | private static final int MSG_POLICIES_CHANGED = 13; |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 389 | private static final int MSG_RESET_FIREWALL_RULES_BY_UID = 15; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 390 | private static final int MSG_SUBSCRIPTION_OVERRIDE = 16; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 391 | private static final int MSG_METERED_RESTRICTED_PACKAGES_CHANGED = 17; |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 392 | private static final int MSG_SET_NETWORK_TEMPLATE_ENABLED = 18; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 393 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 394 | private static final int UID_MSG_STATE_CHANGED = 100; |
| 395 | private static final int UID_MSG_GONE = 101; |
| 396 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 397 | private static final String PROP_SUB_PLAN_OWNER = "persist.sys.sub_plan_owner"; |
| 398 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 399 | private final Context mContext; |
| 400 | private final IActivityManager mActivityManager; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 401 | private NetworkStatsManagerInternal mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 402 | private final INetworkManagementService mNetworkManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 403 | private UsageStatsManagerInternal mUsageStats; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 404 | private final Clock mClock; |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 405 | private final UserManager mUserManager; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 406 | private final CarrierConfigManager mCarrierConfigManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 407 | |
| 408 | private IConnectivityManager mConnManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 409 | private PowerManagerInternal mPowerManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 410 | private IDeviceIdleController mDeviceIdleController; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 411 | @GuardedBy("mUidRulesFirstLock") |
| 412 | private PowerSaveState mRestrictBackgroundPowerState; |
| 413 | |
| 414 | // Store the status of restrict background before turning on battery saver. |
| 415 | // Used to restore mRestrictBackground when battery saver is turned off. |
| 416 | private boolean mRestrictBackgroundBeforeBsm; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 417 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 418 | // Denotes the status of restrict background read from disk. |
| 419 | private boolean mLoadedRestrictBackground; |
| 420 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 421 | // See main javadoc for instructions on how to use these locks. |
| 422 | final Object mUidRulesFirstLock = new Object(); |
| 423 | final Object mNetworkPoliciesSecondLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 424 | |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 425 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
| 426 | volatile boolean mSystemReady; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 427 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 428 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackground; |
| 429 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictPower; |
| 430 | @GuardedBy("mUidRulesFirstLock") volatile boolean mDeviceIdleMode; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 431 | // Store whether user flipped restrict background in battery saver mode |
| 432 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackgroundChangedInBsm; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 433 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 434 | private final boolean mSuppressDefaultPolicy; |
| 435 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 436 | private final CountDownLatch mAdminDataAvailableLatch = new CountDownLatch(1); |
| 437 | |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 438 | private volatile boolean mNetworkManagerReady; |
| 439 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 440 | /** Defined network policies. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 441 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 442 | final ArrayMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = new ArrayMap<>(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 443 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 444 | /** Map from subId to subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 445 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 446 | final SparseArray<SubscriptionPlan[]> mSubscriptionPlans = new SparseArray<>(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 447 | /** Map from subId to package name that owns subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 448 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 449 | final SparseArray<String> mSubscriptionPlansOwner = new SparseArray<>(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 450 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 451 | /** Map from subId to daily opportunistic quota. */ |
| 452 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 453 | final SparseLongArray mSubscriptionOpportunisticQuota = new SparseLongArray(); |
| 454 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 455 | /** Defined UID policies. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 456 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 457 | /** Currently derived rules for each UID. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 458 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidRules = new SparseIntArray(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 459 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 460 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 461 | final SparseIntArray mUidFirewallStandbyRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 462 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 463 | final SparseIntArray mUidFirewallDozableRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 464 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 465 | final SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 466 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 467 | /** Set of states for the child firewall chains. True if the chain is active. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 468 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 469 | final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray(); |
| 470 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 471 | // "Power save mode" is the concept used in the DeviceIdleController that includes various |
| 472 | // features including Doze and Battery Saver. It include Battery Saver, but "power save mode" |
| 473 | // and "battery saver" are not equivalent. |
| 474 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 475 | /** |
| 476 | * UIDs that have been white-listed to always be able to have network access |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 477 | * in power save mode, except device idle (doze) still applies. |
| 478 | * TODO: An int array might be sufficient |
| 479 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 480 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 481 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 482 | |
| 483 | /** |
| 484 | * UIDs that have been white-listed to always be able to have network access |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 485 | * in power save mode. |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 486 | * TODO: An int array might be sufficient |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 487 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 488 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 489 | private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 490 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 491 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 492 | private final SparseBooleanArray mPowerSaveTempWhitelistAppIds = new SparseBooleanArray(); |
| 493 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 494 | /** |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 495 | * UIDs that have been white-listed temporarily to be able to have network access despite being |
| 496 | * idle. Other power saving restrictions still apply. |
| 497 | */ |
| 498 | @GuardedBy("mUidRulesFirstLock") |
| 499 | private final SparseBooleanArray mAppIdleTempWhitelistAppIds = new SparseBooleanArray(); |
| 500 | |
| 501 | /** |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 502 | * UIDs that have been initially white-listed by system to avoid restricted background. |
| 503 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 504 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 505 | private final SparseBooleanArray mDefaultRestrictBackgroundWhitelistUids = |
| 506 | new SparseBooleanArray(); |
| 507 | |
| 508 | /** |
| 509 | * UIDs that have been initially white-listed by system to avoid restricted background, |
| 510 | * but later revoked by user. |
| 511 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 512 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 513 | private final SparseBooleanArray mRestrictBackgroundWhitelistRevokedUids = |
| 514 | new SparseBooleanArray(); |
| 515 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 516 | /** Set of ifaces that are metered. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 517 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 518 | private ArraySet<String> mMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 519 | /** Set of over-limit templates that have been notified. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 520 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 521 | private final ArraySet<NetworkTemplate> mOverLimitNotified = new ArraySet<>(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 522 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 523 | /** Set of currently active {@link Notification} tags. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 524 | @GuardedBy("mNetworkPoliciesSecondLock") |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 525 | private final ArraySet<NotificationId> mActiveNotifs = new ArraySet<>(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 526 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 527 | /** Foreground at UID granularity. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 528 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 529 | final SparseIntArray mUidState = new SparseIntArray(); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 530 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 531 | /** Map from network ID to last observed meteredness state */ |
| 532 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 533 | private final SparseBooleanArray mNetworkMetered = new SparseBooleanArray(); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 534 | /** Map from network ID to last observed roaming state */ |
| 535 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 536 | private final SparseBooleanArray mNetworkRoaming = new SparseBooleanArray(); |
| 537 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 538 | /** Map from netId to subId as of last update */ |
| 539 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 540 | private final SparseIntArray mNetIdToSubId = new SparseIntArray(); |
| 541 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 542 | /** Map from subId to subscriberId as of last update */ |
| 543 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 544 | private final SparseArray<String> mSubIdToSubscriberId = new SparseArray<>(); |
| 545 | /** Set of all merged subscriberId as of last update */ |
| 546 | @GuardedBy("mNetworkPoliciesSecondLock") |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 547 | private List<String[]> mMergedSubscriberIds = new ArrayList<>(); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 548 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 549 | /** |
| 550 | * Indicates the uids restricted by admin from accessing metered data. It's a mapping from |
| 551 | * userId to restricted uids which belong to that user. |
| 552 | */ |
| 553 | @GuardedBy("mUidRulesFirstLock") |
| 554 | private final SparseArray<Set<Integer>> mMeteredRestrictedUids = new SparseArray<>(); |
| 555 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 556 | private final RemoteCallbackList<INetworkPolicyListener> |
| 557 | mListeners = new RemoteCallbackList<>(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 558 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 559 | final Handler mHandler; |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 560 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 561 | final Handler mUidEventHandler; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 562 | |
| 563 | private final ServiceThread mUidEventThread; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 564 | |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 565 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 566 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 567 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 568 | private final AppOpsManager mAppOps; |
| 569 | |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 570 | private final IPackageManager mIPm; |
| 571 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 572 | private ActivityManagerInternal mActivityManagerInternal; |
| 573 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 574 | private final NetworkPolicyLogger mLogger = new NetworkPolicyLogger(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 575 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 576 | // TODO: keep whitelist of system-critical services that should never have |
| 577 | // rules enforced, such as system, phone, and radio UIDs. |
| 578 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 579 | // TODO: migrate notifications to SystemUI |
| 580 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 581 | |
| 582 | interface Stats { |
| 583 | int UPDATE_NETWORK_ENABLED = 0; |
| 584 | int IS_UID_NETWORKING_BLOCKED = 1; |
| 585 | |
| 586 | int COUNT = IS_UID_NETWORKING_BLOCKED + 1; |
| 587 | } |
| 588 | |
| 589 | public final StatLogger mStatLogger = new StatLogger(new String[] { |
| 590 | "updateNetworkEnabledNL()", |
| 591 | "isUidNetworkingBlocked()", |
| 592 | }); |
| 593 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 594 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 595 | INetworkManagementService networkManagement) { |
| 596 | this(context, activityManager, networkManagement, AppGlobals.getPackageManager(), |
| 597 | getDefaultClock(), getDefaultSystemDir(), false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 600 | private static @NonNull File getDefaultSystemDir() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 601 | return new File(Environment.getDataDirectory(), "system"); |
| 602 | } |
| 603 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 604 | private static @NonNull Clock getDefaultClock() { |
| 605 | return new BestClock(ZoneOffset.UTC, SystemClock.currentNetworkTimeClock(), |
| 606 | Clock.systemUTC()); |
| 607 | } |
| 608 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 609 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 610 | INetworkManagementService networkManagement, IPackageManager pm, Clock clock, |
| 611 | File systemDir, boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 612 | mContext = checkNotNull(context, "missing context"); |
| 613 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 614 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 615 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService( |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 616 | Context.DEVICE_IDLE_CONTROLLER)); |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 617 | mClock = checkNotNull(clock, "missing Clock"); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 618 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 619 | mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 620 | mIPm = pm; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 621 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 622 | HandlerThread thread = new HandlerThread(TAG); |
| 623 | thread.start(); |
| 624 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 625 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 626 | // We create another thread for the UID events, which are more time-critical. |
| 627 | mUidEventThread = new ServiceThread(TAG + ".uid", Process.THREAD_PRIORITY_FOREGROUND, |
| 628 | /*allowIo=*/ false); |
| 629 | mUidEventThread.start(); |
| 630 | mUidEventHandler = new Handler(mUidEventThread.getLooper(), mUidEventHandlerCallback); |
| 631 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 632 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 633 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 634 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml"), "net-policy"); |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 635 | |
| 636 | mAppOps = context.getSystemService(AppOpsManager.class); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 637 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 638 | // Expose private service for system components to use. |
| 639 | LocalServices.addService(NetworkPolicyManagerInternal.class, |
| 640 | new NetworkPolicyManagerInternalImpl()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 644 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 645 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 646 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 647 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 648 | void updatePowerSaveWhitelistUL() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 649 | try { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 650 | int[] whitelist = mDeviceIdleController.getAppIdWhitelistExceptIdle(); |
| 651 | mPowerSaveWhitelistExceptIdleAppIds.clear(); |
| 652 | if (whitelist != null) { |
| 653 | for (int uid : whitelist) { |
| 654 | mPowerSaveWhitelistExceptIdleAppIds.put(uid, true); |
| 655 | } |
| 656 | } |
| 657 | whitelist = mDeviceIdleController.getAppIdWhitelist(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 658 | mPowerSaveWhitelistAppIds.clear(); |
| 659 | if (whitelist != null) { |
| 660 | for (int uid : whitelist) { |
| 661 | mPowerSaveWhitelistAppIds.put(uid, true); |
| 662 | } |
| 663 | } |
| 664 | } catch (RemoteException e) { |
| 665 | } |
| 666 | } |
| 667 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 668 | /** |
| 669 | * Whitelists pre-defined apps for restrict background, but only if the user didn't already |
| 670 | * revoke the whitelist. |
| 671 | * |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 672 | * @return whether any uid has been whitelisted. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 673 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 674 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 675 | boolean addDefaultRestrictBackgroundWhitelistUidsUL() { |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 676 | final List<UserInfo> users = mUserManager.getUsers(); |
| 677 | final int numberUsers = users.size(); |
| 678 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 679 | boolean changed = false; |
| 680 | for (int i = 0; i < numberUsers; i++) { |
| 681 | final UserInfo user = users.get(i); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 682 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(user.id) || changed; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 683 | } |
| 684 | return changed; |
| 685 | } |
| 686 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 687 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 688 | private boolean addDefaultRestrictBackgroundWhitelistUidsUL(int userId) { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 689 | final SystemConfig sysConfig = SystemConfig.getInstance(); |
| 690 | final PackageManager pm = mContext.getPackageManager(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 691 | final ArraySet<String> allowDataUsage = sysConfig.getAllowInDataUsageSave(); |
| 692 | boolean changed = false; |
| 693 | for (int i = 0; i < allowDataUsage.size(); i++) { |
| 694 | final String pkg = allowDataUsage.valueAt(i); |
| 695 | if (LOGD) |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 696 | Slog.d(TAG, "checking restricted background whitelisting for package " + pkg |
| 697 | + " and user " + userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 698 | final ApplicationInfo app; |
| 699 | try { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 700 | app = pm.getApplicationInfoAsUser(pkg, PackageManager.MATCH_SYSTEM_ONLY, userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 701 | } catch (PackageManager.NameNotFoundException e) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 702 | if (LOGD) Slog.d(TAG, "No ApplicationInfo for package " + pkg); |
| 703 | // Ignore it - some apps on allow-in-data-usage-save are optional. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 704 | continue; |
| 705 | } |
| 706 | if (!app.isPrivilegedApp()) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 707 | Slog.e(TAG, "addDefaultRestrictBackgroundWhitelistUidsUL(): " |
| 708 | + "skipping non-privileged app " + pkg); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 709 | continue; |
| 710 | } |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 711 | final int uid = UserHandle.getUid(userId, app.uid); |
| 712 | mDefaultRestrictBackgroundWhitelistUids.append(uid, true); |
| 713 | if (LOGD) |
| 714 | Slog.d(TAG, "Adding uid " + uid + " (user " + userId + ") to default restricted " |
| 715 | + "background whitelist. Revoked status: " |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 716 | + mRestrictBackgroundWhitelistRevokedUids.get(uid)); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 717 | if (!mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 718 | if (LOGD) |
| 719 | Slog.d(TAG, "adding default package " + pkg + " (uid " + uid + " for user " |
| 720 | + userId + ") to restrict background whitelist"); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 721 | setUidPolicyUncheckedUL(uid, POLICY_ALLOW_METERED_BACKGROUND, false); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 722 | changed = true; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | return changed; |
| 726 | } |
| 727 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 728 | private void initService(CountDownLatch initCompleteSignal) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 729 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "systemReady"); |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 730 | final int oldPriority = Process.getThreadPriority(Process.myTid()); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 731 | try { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 732 | // Boost thread's priority during system server init |
| 733 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 734 | if (!isBandwidthControlEnabled()) { |
| 735 | Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy"); |
| 736 | return; |
| 737 | } |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 738 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 739 | mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 740 | mNetworkStats = LocalServices.getService(NetworkStatsManagerInternal.class); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 741 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 742 | synchronized (mUidRulesFirstLock) { |
| 743 | synchronized (mNetworkPoliciesSecondLock) { |
| 744 | updatePowerSaveWhitelistUL(); |
| 745 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 746 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 747 | new PowerManagerInternal.LowPowerModeListener() { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 748 | @Override |
| 749 | public int getServiceType() { |
| 750 | return ServiceType.NETWORK_FIREWALL; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 751 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 752 | |
| 753 | @Override |
| 754 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 755 | final boolean enabled = result.batterySaverEnabled; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 756 | if (LOGD) { |
| 757 | Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")"); |
| 758 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 759 | synchronized (mUidRulesFirstLock) { |
| 760 | if (mRestrictPower != enabled) { |
| 761 | mRestrictPower = enabled; |
| 762 | updateRulesForRestrictPowerUL(); |
| 763 | } |
| 764 | } |
| 765 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 766 | }); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 767 | mRestrictPower = mPowerManagerInternal.getLowPowerState( |
| 768 | ServiceType.NETWORK_FIREWALL).batterySaverEnabled; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 769 | |
| 770 | mSystemReady = true; |
| 771 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 772 | waitForAdminData(); |
| 773 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 774 | // read policy from disk |
| 775 | readPolicyAL(); |
| 776 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 777 | // Update the restrictBackground if battery saver is turned on |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 778 | mRestrictBackgroundBeforeBsm = mLoadedRestrictBackground; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 779 | mRestrictBackgroundPowerState = mPowerManagerInternal |
| 780 | .getLowPowerState(ServiceType.DATA_SAVER); |
| 781 | final boolean localRestrictBackground = |
| 782 | mRestrictBackgroundPowerState.batterySaverEnabled; |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 783 | if (localRestrictBackground && !mLoadedRestrictBackground) { |
| 784 | mLoadedRestrictBackground = true; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 785 | } |
| 786 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 787 | new PowerManagerInternal.LowPowerModeListener() { |
| 788 | @Override |
| 789 | public int getServiceType() { |
| 790 | return ServiceType.DATA_SAVER; |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 795 | synchronized (mUidRulesFirstLock) { |
| 796 | updateRestrictBackgroundByLowPowerModeUL(result); |
| 797 | } |
| 798 | } |
| 799 | }); |
| 800 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 801 | if (addDefaultRestrictBackgroundWhitelistUidsUL()) { |
| 802 | writePolicyAL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 803 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 804 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 805 | setRestrictBackgroundUL(mLoadedRestrictBackground); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 806 | updateRulesForGlobalChangeAL(false); |
| 807 | updateNotificationsNL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 808 | } |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 809 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 810 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 811 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 812 | try { |
| 813 | mActivityManager.registerUidObserver(mUidObserver, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 814 | ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE, |
Sudheer Shanka | 6a3c07e | 2018-09-11 15:21:22 -0700 | [diff] [blame] | 815 | NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE, "android"); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 816 | mNetworkManager.registerObserver(mAlertObserver); |
| 817 | } catch (RemoteException e) { |
| 818 | // ignored; both services live in system_server |
| 819 | } |
| 820 | |
| 821 | // listen for changes to power save whitelist |
| 822 | final IntentFilter whitelistFilter = new IntentFilter( |
| 823 | PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 824 | mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler); |
| 825 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 826 | // watch for network interfaces to be claimed |
| 827 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 828 | mContext.registerReceiver(mConnReceiver, connFilter, NETWORK_STACK, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 829 | |
| 830 | // listen for package changes to update policy |
| 831 | final IntentFilter packageFilter = new IntentFilter(); |
| 832 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
| 833 | packageFilter.addDataScheme("package"); |
| 834 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
| 835 | |
| 836 | // listen for UID changes to update policy |
| 837 | mContext.registerReceiver( |
| 838 | mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler); |
| 839 | |
| 840 | // listen for user changes to update policy |
| 841 | final IntentFilter userFilter = new IntentFilter(); |
| 842 | userFilter.addAction(ACTION_USER_ADDED); |
| 843 | userFilter.addAction(ACTION_USER_REMOVED); |
| 844 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 845 | |
| 846 | // listen for stats update events |
| 847 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 848 | mContext.registerReceiver( |
| 849 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 850 | |
| 851 | // listen for restrict background changes from notifications |
| 852 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 853 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 854 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 855 | // Listen for snooze from notifications |
| 856 | mContext.registerReceiver(mSnoozeReceiver, |
| 857 | new IntentFilter(ACTION_SNOOZE_WARNING), MANAGE_NETWORK_POLICY, mHandler); |
| 858 | mContext.registerReceiver(mSnoozeReceiver, |
| 859 | new IntentFilter(ACTION_SNOOZE_RAPID), MANAGE_NETWORK_POLICY, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 860 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 861 | // listen for configured wifi networks to be loaded |
| 862 | final IntentFilter wifiFilter = |
| 863 | new IntentFilter(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 864 | mContext.registerReceiver(mWifiReceiver, wifiFilter, null, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 865 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 866 | // listen for carrier config changes to update data cycle information |
| 867 | final IntentFilter carrierConfigFilter = new IntentFilter( |
| 868 | ACTION_CARRIER_CONFIG_CHANGED); |
| 869 | mContext.registerReceiver(mCarrierConfigReceiver, carrierConfigFilter, null, mHandler); |
| 870 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 871 | // listen for meteredness changes |
| 872 | mContext.getSystemService(ConnectivityManager.class).registerNetworkCallback( |
| 873 | new NetworkRequest.Builder().build(), mNetworkCallback); |
| 874 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 875 | mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener()); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 876 | |
| 877 | // Listen for subscriber changes |
| 878 | mContext.getSystemService(SubscriptionManager.class).addOnSubscriptionsChangedListener( |
| 879 | new OnSubscriptionsChangedListener(mHandler.getLooper()) { |
| 880 | @Override |
| 881 | public void onSubscriptionsChanged() { |
| 882 | updateNetworksInternal(); |
| 883 | } |
| 884 | }); |
| 885 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 886 | // tell systemReady() that the service has been initialized |
| 887 | initCompleteSignal.countDown(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 888 | } finally { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 889 | // Restore the default priority after init is done |
| 890 | Process.setThreadPriority(oldPriority); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 891 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 892 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 895 | public CountDownLatch networkScoreAndNetworkManagementServiceReady() { |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 896 | mNetworkManagerReady = true; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 897 | final CountDownLatch initCompleteSignal = new CountDownLatch(1); |
| 898 | mHandler.post(() -> initService(initCompleteSignal)); |
| 899 | return initCompleteSignal; |
| 900 | } |
| 901 | |
| 902 | public void systemReady(CountDownLatch initCompleteSignal) { |
| 903 | // wait for initService to complete |
| 904 | try { |
| 905 | if (!initCompleteSignal.await(30, TimeUnit.SECONDS)) { |
| 906 | throw new IllegalStateException("Service " + TAG +" init timeout"); |
| 907 | } |
| 908 | } catch (InterruptedException e) { |
| 909 | Thread.currentThread().interrupt(); |
| 910 | throw new IllegalStateException("Service " + TAG + " init interrupted", e); |
| 911 | } |
| 912 | } |
| 913 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 914 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 915 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 916 | mUidEventHandler.obtainMessage(UID_MSG_STATE_CHANGED, |
| 917 | uid, procState, procStateSeq).sendToTarget(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 920 | @Override public void onUidGone(int uid, boolean disabled) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 921 | mUidEventHandler.obtainMessage(UID_MSG_GONE, uid, 0).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 922 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 923 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 924 | @Override public void onUidActive(int uid) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 927 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 928 | } |
| 929 | |
| 930 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 931 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 932 | }; |
| 933 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 934 | final private BroadcastReceiver mPowerSaveWhitelistReceiver = new BroadcastReceiver() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 935 | @Override |
| 936 | public void onReceive(Context context, Intent intent) { |
| 937 | // on background handler thread, and POWER_SAVE_WHITELIST_CHANGED is protected |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 938 | synchronized (mUidRulesFirstLock) { |
| 939 | updatePowerSaveWhitelistUL(); |
| 940 | updateRulesForRestrictPowerUL(); |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 941 | updateRulesForAppIdleUL(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | }; |
| 945 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 946 | final private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 947 | @Override |
| 948 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 949 | // on background handler thread, and PACKAGE_ADDED is protected |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 950 | |
| 951 | final String action = intent.getAction(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 952 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 953 | if (uid == -1) return; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 954 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 955 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 956 | // update rules for UID, since it might be subject to |
| 957 | // global background data policy |
| 958 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 959 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 960 | updateRestrictionRulesForUidUL(uid); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 961 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | }; |
| 965 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 966 | final private BroadcastReceiver mUidRemovedReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 967 | @Override |
| 968 | public void onReceive(Context context, Intent intent) { |
| 969 | // on background handler thread, and UID_REMOVED is protected |
| 970 | |
| 971 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 972 | if (uid == -1) return; |
| 973 | |
| 974 | // remove any policy and update rules to clean up |
| 975 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 976 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 977 | onUidDeletedUL(uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 978 | synchronized (mNetworkPoliciesSecondLock) { |
| 979 | writePolicyAL(); |
| 980 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | }; |
| 984 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 985 | final private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 986 | @Override |
| 987 | public void onReceive(Context context, Intent intent) { |
| 988 | // on background handler thread, and USER_ADDED and USER_REMOVED |
| 989 | // broadcasts are protected |
| 990 | |
| 991 | final String action = intent.getAction(); |
| 992 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 993 | if (userId == -1) return; |
| 994 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 995 | switch (action) { |
| 996 | case ACTION_USER_REMOVED: |
| 997 | case ACTION_USER_ADDED: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 998 | synchronized (mUidRulesFirstLock) { |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 999 | // Remove any persistable state for the given user; both cleaning up after a |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1000 | // USER_REMOVED, and one last sanity check during USER_ADDED |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1001 | removeUserStateUL(userId, true); |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 1002 | // Removing outside removeUserStateUL since that can also be called when |
| 1003 | // user resets app preferences. |
| 1004 | mMeteredRestrictedUids.remove(userId); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1005 | if (action == ACTION_USER_ADDED) { |
| 1006 | // Add apps that are whitelisted by default. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1007 | addDefaultRestrictBackgroundWhitelistUidsUL(userId); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1008 | } |
| 1009 | // Update global restrict for that user |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1010 | synchronized (mNetworkPoliciesSecondLock) { |
| 1011 | updateRulesForGlobalChangeAL(true); |
| 1012 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1013 | } |
| 1014 | break; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | }; |
| 1018 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1019 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1020 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 1021 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 1022 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1023 | final private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1024 | @Override |
| 1025 | public void onReceive(Context context, Intent intent) { |
| 1026 | // on background handler thread, and verified |
| 1027 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 1028 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1029 | synchronized (mNetworkPoliciesSecondLock) { |
| 1030 | updateNetworkEnabledNL(); |
| 1031 | updateNotificationsNL(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | }; |
| 1035 | |
| 1036 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1037 | * Receiver that watches for {@link Notification} control of |
| 1038 | * {@link #mRestrictBackground}. |
| 1039 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1040 | final private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1041 | @Override |
| 1042 | public void onReceive(Context context, Intent intent) { |
| 1043 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 1044 | // permission above. |
| 1045 | |
| 1046 | setRestrictBackground(false); |
| 1047 | } |
| 1048 | }; |
| 1049 | |
| 1050 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1051 | * Receiver that watches for {@link Notification} control of |
| 1052 | * {@link NetworkPolicy#lastWarningSnooze}. |
| 1053 | */ |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1054 | final private BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1055 | @Override |
| 1056 | public void onReceive(Context context, Intent intent) { |
| 1057 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 1058 | // permission above. |
| 1059 | |
| 1060 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1061 | if (ACTION_SNOOZE_WARNING.equals(intent.getAction())) { |
| 1062 | performSnooze(template, TYPE_WARNING); |
| 1063 | } else if (ACTION_SNOOZE_RAPID.equals(intent.getAction())) { |
| 1064 | performSnooze(template, TYPE_RAPID); |
| 1065 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1066 | } |
| 1067 | }; |
| 1068 | |
| 1069 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1070 | * Receiver that watches for {@link WifiConfiguration} to be loaded so that |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1071 | * we can perform upgrade logic. After initial upgrade logic, it updates |
| 1072 | * {@link #mMeteredIfaces} based on configuration changes. |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1073 | */ |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1074 | final private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1075 | @Override |
| 1076 | public void onReceive(Context context, Intent intent) { |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1077 | synchronized (mUidRulesFirstLock) { |
| 1078 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1079 | upgradeWifiMeteredOverrideAL(); |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | // Only need to perform upgrade logic once |
| 1083 | mContext.unregisterReceiver(this); |
| 1084 | } |
| 1085 | }; |
| 1086 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1087 | private static boolean updateCapabilityChange(SparseBooleanArray lastValues, boolean newValue, |
| 1088 | Network network) { |
| 1089 | final boolean lastValue = lastValues.get(network.netId, false); |
| 1090 | final boolean changed = (lastValue != newValue) || lastValues.indexOfKey(network.netId) < 0; |
| 1091 | if (changed) { |
| 1092 | lastValues.put(network.netId, newValue); |
| 1093 | } |
| 1094 | return changed; |
| 1095 | } |
| 1096 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1097 | private final NetworkCallback mNetworkCallback = new NetworkCallback() { |
| 1098 | @Override |
| 1099 | public void onCapabilitiesChanged(Network network, |
| 1100 | NetworkCapabilities networkCapabilities) { |
| 1101 | if (network == null || networkCapabilities == null) return; |
| 1102 | |
| 1103 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1104 | final boolean newMetered = !networkCapabilities |
| 1105 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1106 | final boolean meteredChanged = updateCapabilityChange( |
| 1107 | mNetworkMetered, newMetered, network); |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1108 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1109 | final boolean newRoaming = !networkCapabilities |
| 1110 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); |
| 1111 | final boolean roamingChanged = updateCapabilityChange( |
| 1112 | mNetworkRoaming, newRoaming, network); |
| 1113 | |
| 1114 | if (meteredChanged || roamingChanged) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 1115 | mLogger.meterednessChanged(network.netId, newMetered); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1116 | updateNetworkRulesNL(); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | }; |
| 1121 | |
| 1122 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1123 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 1124 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1125 | final private INetworkManagementEventObserver mAlertObserver |
| 1126 | = new BaseNetworkObserver() { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1127 | @Override |
| 1128 | public void limitReached(String limitName, String iface) { |
| 1129 | // only someone like NMS should be calling us |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1130 | NetworkStack.checkNetworkStackPermission(mContext); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1131 | |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 1132 | if (!LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 1133 | mHandler.obtainMessage(MSG_LIMIT_REACHED, iface).sendToTarget(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | }; |
| 1137 | |
| 1138 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1139 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 1140 | * to show visible notifications as needed. |
| 1141 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1142 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1143 | void updateNotificationsNL() { |
| 1144 | if (LOGV) Slog.v(TAG, "updateNotificationsNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1145 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNotificationsNL"); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1146 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1147 | // keep track of previously active notifications |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1148 | final ArraySet<NotificationId> beforeNotifs = new ArraySet<NotificationId>(mActiveNotifs); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1149 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1150 | |
| 1151 | // TODO: when switching to kernel notifications, compute next future |
| 1152 | // cycle boundary to recompute notifications. |
| 1153 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1154 | // examine stats for each active policy |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1155 | final long now = mClock.millis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1156 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1157 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1158 | final int subId = findRelevantSubIdNL(policy.template); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1159 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1160 | // ignore policies that aren't relevant to user |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1161 | if (subId == INVALID_SUBSCRIPTION_ID) continue; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1162 | if (!policy.hasCycle()) continue; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1163 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1164 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1165 | .cycleIterator(policy).next(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1166 | final long cycleStart = cycle.first.toInstant().toEpochMilli(); |
| 1167 | final long cycleEnd = cycle.second.toInstant().toEpochMilli(); |
| 1168 | final long totalBytes = getTotalBytes(policy.template, cycleStart, cycleEnd); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1169 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1170 | // Carrier might want to manage notifications themselves |
| 1171 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 1172 | if (!CarrierConfigManager.isConfigForIdentifiedCarrier(config)) { |
| 1173 | if (LOGV) Slog.v(TAG, "isConfigForIdentifiedCarrier returned false"); |
| 1174 | // Don't show notifications until we confirm that the loaded config is from an |
| 1175 | // identified carrier, which may want to manage their own notifications. This method |
| 1176 | // should be called every time the carrier config changes anyways, and there's no |
| 1177 | // reason to alert if there isn't a carrier. |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1181 | final boolean notifyWarning = getBooleanDefeatingNullable(config, |
| 1182 | KEY_DATA_WARNING_NOTIFICATION_BOOL, true); |
| 1183 | final boolean notifyLimit = getBooleanDefeatingNullable(config, |
| 1184 | KEY_DATA_LIMIT_NOTIFICATION_BOOL, true); |
| 1185 | final boolean notifyRapid = getBooleanDefeatingNullable(config, |
| 1186 | KEY_DATA_RAPID_NOTIFICATION_BOOL, true); |
| 1187 | |
| 1188 | // Notify when data usage is over warning |
| 1189 | if (notifyWarning) { |
| 1190 | if (policy.isOverWarning(totalBytes) && !policy.isOverLimit(totalBytes)) { |
| 1191 | final boolean snoozedThisCycle = policy.lastWarningSnooze >= cycleStart; |
| 1192 | if (!snoozedThisCycle) { |
| 1193 | enqueueNotification(policy, TYPE_WARNING, totalBytes, null); |
| 1194 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1195 | } |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1196 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1197 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1198 | // Notify when data usage is over limit |
| 1199 | if (notifyLimit) { |
| 1200 | if (policy.isOverLimit(totalBytes)) { |
| 1201 | final boolean snoozedThisCycle = policy.lastLimitSnooze >= cycleStart; |
| 1202 | if (snoozedThisCycle) { |
| 1203 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes, null); |
| 1204 | } else { |
| 1205 | enqueueNotification(policy, TYPE_LIMIT, totalBytes, null); |
| 1206 | notifyOverLimitNL(policy.template); |
| 1207 | } |
| 1208 | } else { |
| 1209 | notifyUnderLimitNL(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1210 | } |
| 1211 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1212 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1213 | // Warn if average usage over last 4 days is on track to blow pretty |
| 1214 | // far past the plan limits. |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1215 | if (notifyRapid && policy.limitBytes != LIMIT_DISABLED) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1216 | final long recentDuration = TimeUnit.DAYS.toMillis(4); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1217 | final long recentStart = now - recentDuration; |
| 1218 | final long recentEnd = now; |
| 1219 | final long recentBytes = getTotalBytes(policy.template, recentStart, recentEnd); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1220 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1221 | final long cycleDuration = cycleEnd - cycleStart; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1222 | final long projectedBytes = (recentBytes * cycleDuration) / recentDuration; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1223 | final long alertBytes = (policy.limitBytes * 3) / 2; |
| 1224 | |
| 1225 | if (LOGD) { |
| 1226 | Slog.d(TAG, "Rapid usage considering recent " + recentBytes + " projected " |
| 1227 | + projectedBytes + " alert " + alertBytes); |
| 1228 | } |
| 1229 | |
| 1230 | final boolean snoozedRecently = policy.lastRapidSnooze >= now |
| 1231 | - DateUtils.DAY_IN_MILLIS; |
| 1232 | if (projectedBytes > alertBytes && !snoozedRecently) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1233 | enqueueNotification(policy, TYPE_RAPID, 0, |
| 1234 | findRapidBlame(policy.template, recentStart, recentEnd)); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1239 | // cancel stale notifications that we didn't renew above |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1240 | for (int i = beforeNotifs.size()-1; i >= 0; i--) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1241 | final NotificationId notificationId = beforeNotifs.valueAt(i); |
| 1242 | if (!mActiveNotifs.contains(notificationId)) { |
| 1243 | cancelNotification(notificationId); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1244 | } |
| 1245 | } |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1246 | |
| 1247 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /** |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1251 | * Attempt to find a specific app to blame for rapid data usage during the |
| 1252 | * given time period. |
| 1253 | */ |
| 1254 | private @Nullable ApplicationInfo findRapidBlame(NetworkTemplate template, |
| 1255 | long start, long end) { |
| 1256 | long totalBytes = 0; |
| 1257 | long maxBytes = 0; |
| 1258 | int maxUid = 0; |
| 1259 | |
| 1260 | final NetworkStats stats = getNetworkUidBytes(template, start, end); |
| 1261 | NetworkStats.Entry entry = null; |
| 1262 | for (int i = 0; i < stats.size(); i++) { |
| 1263 | entry = stats.getValues(i, entry); |
| 1264 | final long bytes = entry.rxBytes + entry.txBytes; |
| 1265 | totalBytes += bytes; |
| 1266 | if (bytes > maxBytes) { |
| 1267 | maxBytes = bytes; |
| 1268 | maxUid = entry.uid; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // Only point blame if the majority of usage was done by a single app. |
| 1273 | // TODO: support shared UIDs |
| 1274 | if (maxBytes > 0 && maxBytes > totalBytes / 2) { |
| 1275 | final String[] packageNames = mContext.getPackageManager().getPackagesForUid(maxUid); |
Jeff Sharkey | d37154e | 2018-03-26 16:50:59 -0600 | [diff] [blame] | 1276 | if (packageNames != null && packageNames.length == 1) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1277 | try { |
| 1278 | return mContext.getPackageManager().getApplicationInfo(packageNames[0], |
| 1279 | MATCH_ANY_USER | MATCH_DISABLED_COMPONENTS | MATCH_DIRECT_BOOT_AWARE |
| 1280 | | MATCH_DIRECT_BOOT_UNAWARE | MATCH_UNINSTALLED_PACKAGES); |
| 1281 | } catch (NameNotFoundException ignored) { |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return null; |
| 1287 | } |
| 1288 | |
| 1289 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1290 | * Test if given {@link NetworkTemplate} is relevant to user based on |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1291 | * current device state, such as when |
| 1292 | * {@link TelephonyManager#getSubscriberId()} matches. This is regardless of |
| 1293 | * data connection status. |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1294 | * |
| 1295 | * @return relevant subId, or {@link #INVALID_SUBSCRIPTION_ID} when no |
| 1296 | * matching subId found. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1297 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1298 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1299 | private int findRelevantSubIdNL(NetworkTemplate template) { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1300 | // Mobile template is relevant when any active subscriber matches |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1301 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 1302 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 1303 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1304 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1305 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, |
| 1306 | true); |
| 1307 | if (template.matches(probeIdent)) { |
| 1308 | return subId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1309 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1310 | } |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1311 | return INVALID_SUBSCRIPTION_ID; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1315 | * Notify that given {@link NetworkTemplate} is over |
| 1316 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 1317 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1318 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1319 | private void notifyOverLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1320 | if (!mOverLimitNotified.contains(template)) { |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1321 | mContext.startActivity(buildNetworkOverLimitIntent(mContext.getResources(), template)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1322 | mOverLimitNotified.add(template); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1326 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1327 | private void notifyUnderLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1328 | mOverLimitNotified.remove(template); |
| 1329 | } |
| 1330 | |
| 1331 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1332 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 1333 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 1334 | */ |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1335 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes, |
| 1336 | ApplicationInfo rapidBlame) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1337 | final NotificationId notificationId = new NotificationId(policy, type); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1338 | final Notification.Builder builder = |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1339 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_ALERTS); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1340 | builder.setOnlyAlertOnce(true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1341 | builder.setWhen(0L); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 1342 | builder.setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 1343 | com.android.internal.R.color.system_notification_accent_color)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1344 | |
| 1345 | final Resources res = mContext.getResources(); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1346 | final CharSequence title; |
| 1347 | final CharSequence body; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1348 | switch (type) { |
| 1349 | case TYPE_WARNING: { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1350 | title = res.getText(R.string.data_usage_warning_title); |
| 1351 | body = res.getString(R.string.data_usage_warning_body, |
Anton Hansson | a6101e8 | 2019-03-29 15:50:09 +0000 | [diff] [blame] | 1352 | Formatter.formatFileSize(mContext, totalBytes, Formatter.FLAG_IEC_UNITS)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1353 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1354 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1355 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1356 | final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template); |
| 1357 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1358 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1359 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1360 | final Intent viewIntent = buildViewDataUsageIntent(res, policy.template); |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 1361 | // TODO: Resolve to single code path. |
| 1362 | if (isHeadlessSystemUserBuild()) { |
| 1363 | builder.setContentIntent(PendingIntent.getActivityAsUser( |
| 1364 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT, |
| 1365 | /* options= */ null, UserHandle.CURRENT)); |
| 1366 | } else { |
| 1367 | builder.setContentIntent(PendingIntent.getActivity( |
| 1368 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1369 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | } |
| 1372 | case TYPE_LIMIT: { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1373 | switch (policy.template.getMatchRule()) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1374 | case MATCH_MOBILE: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1375 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1376 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1377 | case MATCH_WIFI: |
| 1378 | title = res.getText(R.string.data_usage_wifi_limit_title); |
| 1379 | break; |
| 1380 | default: |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1381 | return; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1382 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1383 | body = res.getText(R.string.data_usage_limit_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1384 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1385 | builder.setOngoing(true); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1386 | builder.setSmallIcon(R.drawable.stat_notify_disabled_data); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1387 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1388 | final Intent intent = buildNetworkOverLimitIntent(res, policy.template); |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 1389 | // TODO: Resolve to single code path. |
| 1390 | if (isHeadlessSystemUserBuild()) { |
| 1391 | builder.setContentIntent(PendingIntent.getActivityAsUser( |
| 1392 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, |
| 1393 | /* options= */ null, UserHandle.CURRENT)); |
| 1394 | } else { |
| 1395 | builder.setContentIntent(PendingIntent.getActivity( |
| 1396 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1397 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | case TYPE_LIMIT_SNOOZED: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1401 | switch (policy.template.getMatchRule()) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1402 | case MATCH_MOBILE: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1403 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 1404 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1405 | case MATCH_WIFI: |
| 1406 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 1407 | break; |
| 1408 | default: |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1409 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1410 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1411 | final long overBytes = totalBytes - policy.limitBytes; |
| 1412 | body = res.getString(R.string.data_usage_limit_snoozed_body, |
Anton Hansson | a6101e8 | 2019-03-29 15:50:09 +0000 | [diff] [blame] | 1413 | Formatter.formatFileSize(mContext, overBytes, Formatter.FLAG_IEC_UNITS)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1414 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1415 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1416 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1417 | builder.setChannelId(SystemNotificationChannels.NETWORK_STATUS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1418 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1419 | final Intent intent = buildViewDataUsageIntent(res, policy.template); |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 1420 | // TODO: Resolve to single code path. |
| 1421 | if (isHeadlessSystemUserBuild()) { |
| 1422 | builder.setContentIntent(PendingIntent.getActivityAsUser( |
| 1423 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, |
| 1424 | /* options= */ null, UserHandle.CURRENT)); |
| 1425 | } else { |
| 1426 | builder.setContentIntent(PendingIntent.getActivity( |
| 1427 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1428 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1431 | case TYPE_RAPID: { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1432 | title = res.getText(R.string.data_usage_rapid_title); |
| 1433 | if (rapidBlame != null) { |
| 1434 | body = res.getString(R.string.data_usage_rapid_app_body, |
| 1435 | rapidBlame.loadLabel(mContext.getPackageManager())); |
| 1436 | } else { |
| 1437 | body = res.getString(R.string.data_usage_rapid_body); |
| 1438 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1439 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1440 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1441 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1442 | final Intent snoozeIntent = buildSnoozeRapidIntent(policy.template); |
| 1443 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1444 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1445 | |
| 1446 | final Intent viewIntent = buildViewDataUsageIntent(res, policy.template); |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 1447 | // TODO: Resolve to single code path. |
| 1448 | if (isHeadlessSystemUserBuild()) { |
| 1449 | builder.setContentIntent(PendingIntent.getActivityAsUser( |
| 1450 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT, |
| 1451 | /* options= */ null, UserHandle.CURRENT)); |
| 1452 | } else { |
| 1453 | builder.setContentIntent(PendingIntent.getActivity( |
| 1454 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1455 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1456 | break; |
| 1457 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1458 | default: { |
| 1459 | return; |
| 1460 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1463 | builder.setTicker(title); |
| 1464 | builder.setContentTitle(title); |
| 1465 | builder.setContentText(body); |
| 1466 | builder.setStyle(new Notification.BigTextStyle().bigText(body)); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1467 | |
| 1468 | mContext.getSystemService(NotificationManager.class).notifyAsUser(notificationId.getTag(), |
| 1469 | notificationId.getId(), builder.build(), UserHandle.ALL); |
| 1470 | mActiveNotifs.add(notificationId); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1473 | private void cancelNotification(NotificationId notificationId) { |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1474 | mContext.getSystemService(NotificationManager.class).cancel(notificationId.getTag(), |
| 1475 | notificationId.getId()); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1479 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1480 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1481 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1482 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1483 | @Override |
| 1484 | public void onReceive(Context context, Intent intent) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1485 | // on background handler thread, and verified NETWORK_STACK |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1486 | // permission above. |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1487 | updateNetworksInternal(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1488 | } |
| 1489 | }; |
| 1490 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1491 | private void updateNetworksInternal() { |
| 1492 | // Get all of our cross-process communication with telephony out of |
| 1493 | // the way before we acquire internal locks. |
| 1494 | updateSubscriptions(); |
| 1495 | |
| 1496 | synchronized (mUidRulesFirstLock) { |
| 1497 | synchronized (mNetworkPoliciesSecondLock) { |
| 1498 | ensureActiveMobilePolicyAL(); |
| 1499 | normalizePoliciesNL(); |
| 1500 | updateNetworkEnabledNL(); |
| 1501 | updateNetworkRulesNL(); |
| 1502 | updateNotificationsNL(); |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1507 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1508 | void updateNetworks() throws InterruptedException { |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1509 | updateNetworksInternal(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1510 | final CountDownLatch latch = new CountDownLatch(1); |
| 1511 | mHandler.post(() -> { |
| 1512 | latch.countDown(); |
| 1513 | }); |
| 1514 | latch.await(5, TimeUnit.SECONDS); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1515 | } |
| 1516 | |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1517 | @VisibleForTesting |
| 1518 | Handler getHandlerForTesting() { |
| 1519 | return mHandler; |
| 1520 | } |
| 1521 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1522 | /** |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1523 | * Update mobile policies with data cycle information from {@link CarrierConfigManager} |
| 1524 | * if necessary. |
| 1525 | * |
| 1526 | * @param subId that has its associated NetworkPolicy updated if necessary |
| 1527 | * @return if any policies were updated |
| 1528 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1529 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1530 | private boolean maybeUpdateMobilePolicyCycleAL(int subId, String subscriberId) { |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1531 | if (LOGV) Slog.v(TAG, "maybeUpdateMobilePolicyCycleAL()"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1532 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1533 | // find and update the mobile NetworkPolicy for this subscriber id |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1534 | boolean policyUpdated = false; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1535 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1536 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1537 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1538 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1539 | if (template.matches(probeIdent)) { |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1540 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1541 | policyUpdated |= updateDefaultMobilePolicyAL(subId, policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1542 | } |
| 1543 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1544 | return policyUpdated; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * Returns the cycle day that should be used for a mobile NetworkPolicy. |
| 1549 | * |
| 1550 | * It attempts to get an appropriate cycle day from the passed in CarrierConfig. If it's unable |
| 1551 | * to do so, it returns the fallback value. |
| 1552 | * |
| 1553 | * @param config The CarrierConfig to read the value from. |
| 1554 | * @param fallbackCycleDay to return if the CarrierConfig can't be read. |
| 1555 | * @return cycleDay to use in the mobile NetworkPolicy. |
| 1556 | */ |
| 1557 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1558 | int getCycleDayFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1559 | int fallbackCycleDay) { |
| 1560 | if (config == null) { |
| 1561 | return fallbackCycleDay; |
| 1562 | } |
| 1563 | int cycleDay = |
| 1564 | config.getInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT); |
| 1565 | if (cycleDay == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1566 | return fallbackCycleDay; |
| 1567 | } |
| 1568 | // validate cycleDay value |
| 1569 | final Calendar cal = Calendar.getInstance(); |
| 1570 | if (cycleDay < cal.getMinimum(Calendar.DAY_OF_MONTH) || |
| 1571 | cycleDay > cal.getMaximum(Calendar.DAY_OF_MONTH)) { |
| 1572 | Slog.e(TAG, "Invalid date in " |
| 1573 | + "CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT: " + cycleDay); |
| 1574 | return fallbackCycleDay; |
| 1575 | } |
| 1576 | return cycleDay; |
| 1577 | } |
| 1578 | |
| 1579 | /** |
| 1580 | * Returns the warning bytes that should be used for a mobile NetworkPolicy. |
| 1581 | * |
| 1582 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1583 | * to do so, it returns the fallback value. |
| 1584 | * |
| 1585 | * @param config The CarrierConfig to read the value from. |
| 1586 | * @param fallbackWarningBytes to return if the CarrierConfig can't be read. |
| 1587 | * @return warningBytes to use in the mobile NetworkPolicy. |
| 1588 | */ |
| 1589 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1590 | long getWarningBytesFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1591 | long fallbackWarningBytes) { |
| 1592 | if (config == null) { |
| 1593 | return fallbackWarningBytes; |
| 1594 | } |
| 1595 | long warningBytes = |
| 1596 | config.getLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG); |
| 1597 | |
| 1598 | if (warningBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1599 | return WARNING_DISABLED; |
| 1600 | } else if (warningBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1601 | return getPlatformDefaultWarningBytes(); |
| 1602 | } else if (warningBytes < 0) { |
| 1603 | Slog.e(TAG, "Invalid value in " |
| 1604 | + "CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; expected a " |
| 1605 | + "non-negative value but got: " + warningBytes); |
| 1606 | return fallbackWarningBytes; |
| 1607 | } |
| 1608 | |
| 1609 | return warningBytes; |
| 1610 | } |
| 1611 | |
| 1612 | /** |
| 1613 | * Returns the limit bytes that should be used for a mobile NetworkPolicy. |
| 1614 | * |
| 1615 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1616 | * to do so, it returns the fallback value. |
| 1617 | * |
| 1618 | * @param config The CarrierConfig to read the value from. |
| 1619 | * @param fallbackLimitBytes to return if the CarrierConfig can't be read. |
| 1620 | * @return limitBytes to use in the mobile NetworkPolicy. |
| 1621 | */ |
| 1622 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1623 | long getLimitBytesFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1624 | long fallbackLimitBytes) { |
| 1625 | if (config == null) { |
| 1626 | return fallbackLimitBytes; |
| 1627 | } |
| 1628 | long limitBytes = |
| 1629 | config.getLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG); |
| 1630 | |
| 1631 | if (limitBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1632 | return LIMIT_DISABLED; |
| 1633 | } else if (limitBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1634 | return getPlatformDefaultLimitBytes(); |
| 1635 | } else if (limitBytes < 0) { |
| 1636 | Slog.e(TAG, "Invalid value in " |
| 1637 | + "CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; expected a " |
| 1638 | + "non-negative value but got: " + limitBytes); |
| 1639 | return fallbackLimitBytes; |
| 1640 | } |
| 1641 | return limitBytes; |
| 1642 | } |
| 1643 | |
| 1644 | /** |
| 1645 | * Receiver that watches for {@link CarrierConfigManager} to be changed. |
| 1646 | */ |
| 1647 | private BroadcastReceiver mCarrierConfigReceiver = new BroadcastReceiver() { |
| 1648 | @Override |
| 1649 | public void onReceive(Context context, Intent intent) { |
| 1650 | // No need to do a permission check, because the ACTION_CARRIER_CONFIG_CHANGED |
| 1651 | // broadcast is protected and can't be spoofed. Runs on a background handler thread. |
| 1652 | |
| 1653 | if (!intent.hasExtra(PhoneConstants.SUBSCRIPTION_KEY)) { |
| 1654 | return; |
| 1655 | } |
| 1656 | final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, -1); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1657 | |
| 1658 | // Get all of our cross-process communication with telephony out of |
| 1659 | // the way before we acquire internal locks. |
| 1660 | updateSubscriptions(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1661 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1662 | synchronized (mUidRulesFirstLock) { |
| 1663 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1664 | final String subscriberId = mSubIdToSubscriberId.get(subId, null); |
| 1665 | if (subscriberId != null) { |
| 1666 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 1667 | maybeUpdateMobilePolicyCycleAL(subId, subscriberId); |
| 1668 | } else { |
| 1669 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 1670 | } |
| 1671 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1672 | // update network and notification rules, as the data cycle changed and it's |
| 1673 | // possible that we should be triggering warnings/limits now |
| 1674 | handleNetworkPoliciesUpdateAL(true); |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | }; |
| 1679 | |
| 1680 | /** |
| 1681 | * Handles all tasks that need to be run after a new network policy has been set, or an existing |
| 1682 | * one has been updated. |
| 1683 | * |
| 1684 | * @param shouldNormalizePolicies true iff network policies need to be normalized after the |
| 1685 | * update. |
| 1686 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1687 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1688 | void handleNetworkPoliciesUpdateAL(boolean shouldNormalizePolicies) { |
| 1689 | if (shouldNormalizePolicies) { |
| 1690 | normalizePoliciesNL(); |
| 1691 | } |
| 1692 | updateNetworkEnabledNL(); |
| 1693 | updateNetworkRulesNL(); |
| 1694 | updateNotificationsNL(); |
| 1695 | writePolicyAL(); |
| 1696 | } |
| 1697 | |
| 1698 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1699 | * Proactively control network data connections when they exceed |
| 1700 | * {@link NetworkPolicy#limitBytes}. |
| 1701 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1702 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1703 | void updateNetworkEnabledNL() { |
| 1704 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1705 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNetworkEnabledNL"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1706 | |
| 1707 | // TODO: reset any policy-disabled networks when any policy is removed |
| 1708 | // completely, which is currently rare case. |
| 1709 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 1710 | final long startTime = mStatLogger.getTime(); |
| 1711 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1712 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1713 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1714 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1715 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1716 | setNetworkTemplateEnabled(policy.template, true); |
| 1717 | continue; |
| 1718 | } |
| 1719 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1720 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1721 | .cycleIterator(policy).next(); |
| 1722 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1723 | final long end = cycle.second.toInstant().toEpochMilli(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1724 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1725 | |
| 1726 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1727 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 1728 | && policy.lastLimitSnooze < start; |
| 1729 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1730 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1731 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1732 | } |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 1733 | |
| 1734 | mStatLogger.logDurationStat(Stats.UPDATE_NETWORK_ENABLED, startTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1735 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1739 | * Proactively disable networks that match the given |
| 1740 | * {@link NetworkTemplate}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1741 | */ |
| 1742 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 1743 | // Don't call setNetworkTemplateEnabledInner() directly because we may have a lock |
| 1744 | // held. Call it via the handler. |
| 1745 | mHandler.obtainMessage(MSG_SET_NETWORK_TEMPLATE_ENABLED, enabled ? 1 : 0, 0, template) |
| 1746 | .sendToTarget(); |
| 1747 | } |
| 1748 | |
| 1749 | private void setNetworkTemplateEnabledInner(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1750 | // TODO: reach into ConnectivityManager to proactively disable bringing |
| 1751 | // up this network, since we know that traffic will be blocked. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1752 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1753 | if (template.getMatchRule() == MATCH_MOBILE) { |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1754 | // If mobile data usage hits the limit or if the user resumes the data, we need to |
| 1755 | // notify telephony. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1756 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1757 | final IntArray matchingSubIds = new IntArray(); |
| 1758 | synchronized (mNetworkPoliciesSecondLock) { |
| 1759 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 1760 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 1761 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
| 1762 | |
| 1763 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1764 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, |
| 1765 | true); |
| 1766 | // Template is matched when subscriber id matches. |
| 1767 | if (template.matches(probeIdent)) { |
| 1768 | matchingSubIds.add(subId); |
| 1769 | } |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1770 | } |
| 1771 | } |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1772 | |
| 1773 | // Only talk with telephony outside of locks |
| 1774 | final TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 1775 | for (int i = 0; i < matchingSubIds.size(); i++) { |
| 1776 | final int subId = matchingSubIds.get(i); |
| 1777 | tm.setPolicyDataEnabled(enabled, subId); |
| 1778 | } |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1779 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | /** |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1783 | * Collect all ifaces from a {@link NetworkState} into the given set. |
| 1784 | */ |
| 1785 | private static void collectIfaces(ArraySet<String> ifaces, NetworkState state) { |
| 1786 | final String baseIface = state.linkProperties.getInterfaceName(); |
| 1787 | if (baseIface != null) { |
| 1788 | ifaces.add(baseIface); |
| 1789 | } |
| 1790 | for (LinkProperties stackedLink : state.linkProperties.getStackedLinks()) { |
| 1791 | final String stackedIface = stackedLink.getInterfaceName(); |
| 1792 | if (stackedIface != null) { |
| 1793 | ifaces.add(stackedIface); |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /** |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1799 | * Examine all currently active subscriptions from |
| 1800 | * {@link SubscriptionManager#getActiveSubscriptionIdList()} and update |
| 1801 | * internal data structures. |
| 1802 | * <p> |
| 1803 | * Callers <em>must not</em> hold any locks when this method called. |
| 1804 | */ |
| 1805 | void updateSubscriptions() { |
| 1806 | if (LOGV) Slog.v(TAG, "updateSubscriptions()"); |
| 1807 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateSubscriptions"); |
| 1808 | |
| 1809 | final TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 1810 | final SubscriptionManager sm = mContext.getSystemService(SubscriptionManager.class); |
| 1811 | |
| 1812 | final int[] subIds = ArrayUtils.defeatNullable(sm.getActiveSubscriptionIdList()); |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 1813 | final List<String[]> mergedSubscriberIdsList = new ArrayList(); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1814 | |
| 1815 | final SparseArray<String> subIdToSubscriberId = new SparseArray<>(subIds.length); |
| 1816 | for (int subId : subIds) { |
| 1817 | final String subscriberId = tm.getSubscriberId(subId); |
| 1818 | if (!TextUtils.isEmpty(subscriberId)) { |
| 1819 | subIdToSubscriberId.put(subId, subscriberId); |
| 1820 | } else { |
| 1821 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 1822 | } |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 1823 | |
| 1824 | String[] mergedSubscriberId = ArrayUtils.defeatNullable( |
| 1825 | tm.createForSubscriptionId(subId).getMergedSubscriberIdsFromGroup()); |
| 1826 | mergedSubscriberIdsList.add(mergedSubscriberId); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | synchronized (mNetworkPoliciesSecondLock) { |
| 1830 | mSubIdToSubscriberId.clear(); |
| 1831 | for (int i = 0; i < subIdToSubscriberId.size(); i++) { |
| 1832 | mSubIdToSubscriberId.put(subIdToSubscriberId.keyAt(i), |
| 1833 | subIdToSubscriberId.valueAt(i)); |
| 1834 | } |
| 1835 | |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 1836 | mMergedSubscriberIds = mergedSubscriberIdsList; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1840 | } |
| 1841 | |
| 1842 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1843 | * Examine all connected {@link NetworkState}, looking for |
| 1844 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1845 | * remaining quota based on usage cycle and historical stats. |
| 1846 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1847 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1848 | void updateNetworkRulesNL() { |
| 1849 | if (LOGV) Slog.v(TAG, "updateNetworkRulesNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1850 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNetworkRulesNL"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1851 | |
| 1852 | final NetworkState[] states; |
| 1853 | try { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1854 | states = defeatNullable(mConnManager.getAllNetworkState()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1855 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1856 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1860 | // First, generate identities of all connected networks so we can |
| 1861 | // quickly compare them against all defined policies below. |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1862 | mNetIdToSubId.clear(); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1863 | final ArrayMap<NetworkState, NetworkIdentity> identified = new ArrayMap<>(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1864 | for (NetworkState state : states) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1865 | if (state.network != null) { |
| 1866 | mNetIdToSubId.put(state.network.netId, parseSubId(state)); |
| 1867 | } |
Wei Liu | b8eaf45 | 2016-01-25 10:32:27 -0800 | [diff] [blame] | 1868 | if (state.networkInfo != null && state.networkInfo.isConnected()) { |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1869 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, |
| 1870 | true); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1871 | identified.put(state, ident); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1875 | final ArraySet<String> newMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1876 | long lowestRule = Long.MAX_VALUE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1877 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1878 | // For every well-defined policy, compute remaining data based on |
| 1879 | // current cycle and historical stats, and push to kernel. |
| 1880 | final ArraySet<String> matchingIfaces = new ArraySet<>(); |
| 1881 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1882 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1883 | |
| 1884 | // Collect all ifaces that match this policy |
| 1885 | matchingIfaces.clear(); |
| 1886 | for (int j = identified.size() - 1; j >= 0; j--) { |
| 1887 | if (policy.template.matches(identified.valueAt(j))) { |
| 1888 | collectIfaces(matchingIfaces, identified.keyAt(j)); |
| 1889 | } |
| 1890 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1891 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1892 | if (LOGD) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1893 | Slog.d(TAG, "Applying " + policy + " to ifaces " + matchingIfaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1896 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1897 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1898 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1899 | final long quotaBytes; |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1900 | if (hasLimit && policy.hasCycle()) { |
| 1901 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1902 | .cycleIterator(policy).next(); |
| 1903 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1904 | final long end = cycle.second.toInstant().toEpochMilli(); |
| 1905 | final long totalBytes = getTotalBytes(policy.template, start, end); |
| 1906 | |
| 1907 | if (policy.lastLimitSnooze >= start) { |
| 1908 | // snoozing past quota, but we still need to restrict apps, |
| 1909 | // so push really high quota. |
| 1910 | quotaBytes = Long.MAX_VALUE; |
| 1911 | } else { |
| 1912 | // remaining "quota" bytes are based on total usage in |
| 1913 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1914 | // set 1-byte quota and disable the radio later. |
| 1915 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1916 | } |
| 1917 | } else { |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1918 | // metered network, but no policy limit; we still need to |
| 1919 | // restrict apps, so push really high quota. |
| 1920 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1921 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1922 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1923 | if (matchingIfaces.size() > 1) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1924 | // TODO: switch to shared quota once NMS supports |
| 1925 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1928 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1929 | final String iface = matchingIfaces.valueAt(j); |
| 1930 | setInterfaceQuotaAsync(iface, quotaBytes); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1931 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1932 | } |
| 1933 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1934 | |
| 1935 | // keep track of lowest warning or limit of active policies |
| 1936 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1937 | lowestRule = policy.warningBytes; |
| 1938 | } |
| 1939 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1940 | lowestRule = policy.limitBytes; |
| 1941 | } |
| 1942 | } |
| 1943 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1944 | // One final pass to catch any metered ifaces that don't have explicitly |
| 1945 | // defined policies; typically Wi-Fi networks. |
| 1946 | for (NetworkState state : states) { |
| 1947 | if (state.networkInfo != null && state.networkInfo.isConnected() |
| 1948 | && !state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 1949 | matchingIfaces.clear(); |
| 1950 | collectIfaces(matchingIfaces, state); |
| 1951 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1952 | final String iface = matchingIfaces.valueAt(j); |
| 1953 | if (!newMeteredIfaces.contains(iface)) { |
| 1954 | setInterfaceQuotaAsync(iface, Long.MAX_VALUE); |
| 1955 | newMeteredIfaces.add(iface); |
| 1956 | } |
| 1957 | } |
| 1958 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1961 | // Remove quota from any interfaces that are no longer metered. |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1962 | for (int i = mMeteredIfaces.size() - 1; i >= 0; i--) { |
| 1963 | final String iface = mMeteredIfaces.valueAt(i); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1964 | if (!newMeteredIfaces.contains(iface)) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1965 | removeInterfaceQuotaAsync(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | mMeteredIfaces = newMeteredIfaces; |
| 1969 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1970 | final ContentResolver cr = mContext.getContentResolver(); |
| 1971 | final boolean quotaEnabled = Settings.Global.getInt(cr, |
| 1972 | NETPOLICY_QUOTA_ENABLED, 1) != 0; |
| 1973 | final long quotaUnlimited = Settings.Global.getLong(cr, |
| 1974 | NETPOLICY_QUOTA_UNLIMITED, QUOTA_UNLIMITED_DEFAULT); |
| 1975 | final float quotaLimited = Settings.Global.getFloat(cr, |
| 1976 | NETPOLICY_QUOTA_LIMITED, QUOTA_LIMITED_DEFAULT); |
| 1977 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1978 | // Finally, calculate our opportunistic quotas |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1979 | mSubscriptionOpportunisticQuota.clear(); |
| 1980 | for (NetworkState state : states) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1981 | if (!quotaEnabled) continue; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1982 | if (state.network == null) continue; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1983 | final int subId = getSubIdLocked(state.network); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1984 | final SubscriptionPlan plan = getPrimarySubscriptionPlanLocked(subId); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1985 | if (plan == null) continue; |
| 1986 | |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 1987 | final long quotaBytes; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1988 | final long limitBytes = plan.getDataLimitBytes(); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1989 | if (!state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_ROAMING)) { |
| 1990 | // Clamp to 0 when roaming |
| 1991 | quotaBytes = 0; |
| 1992 | } else if (limitBytes == SubscriptionPlan.BYTES_UNKNOWN) { |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 1993 | quotaBytes = OPPORTUNISTIC_QUOTA_UNKNOWN; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1994 | } else if (limitBytes == SubscriptionPlan.BYTES_UNLIMITED) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1995 | // Unlimited data; let's use 20MiB/day (600MiB/month) |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1996 | quotaBytes = quotaUnlimited; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1997 | } else { |
| 1998 | // Limited data; let's only use 10% of remaining budget |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 1999 | final Range<ZonedDateTime> cycle = plan.cycleIterator().next(); |
| 2000 | final long start = cycle.getLower().toInstant().toEpochMilli(); |
| 2001 | final long end = cycle.getUpper().toInstant().toEpochMilli(); |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 2002 | final Instant now = mClock.instant(); |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 2003 | final long startOfDay = ZonedDateTime.ofInstant(now, cycle.getLower().getZone()) |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 2004 | .truncatedTo(ChronoUnit.DAYS) |
| 2005 | .toInstant().toEpochMilli(); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 2006 | final long totalBytes = getTotalBytes( |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 2007 | NetworkTemplate.buildTemplateMobileAll(state.subscriberId), |
| 2008 | start, startOfDay); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 2009 | final long remainingBytes = limitBytes - totalBytes; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 2010 | // Number of remaining days including current day |
| 2011 | final long remainingDays = |
| 2012 | 1 + ((end - now.toEpochMilli() - 1) / TimeUnit.DAYS.toMillis(1)); |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 2013 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 2014 | quotaBytes = Math.max(0, (long) ((remainingBytes / remainingDays) * quotaLimited)); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | mSubscriptionOpportunisticQuota.put(subId, quotaBytes); |
| 2018 | } |
| 2019 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 2020 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 2021 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 2022 | |
| 2023 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 2024 | |
| 2025 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /** |
| 2029 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 2030 | * have at least a default mobile policy defined. |
| 2031 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2032 | @GuardedBy("mNetworkPoliciesSecondLock") |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2033 | private void ensureActiveMobilePolicyAL() { |
| 2034 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyAL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2035 | if (mSuppressDefaultPolicy) return; |
| 2036 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 2037 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 2038 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 2039 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2040 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2041 | ensureActiveMobilePolicyAL(subId, subscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2042 | } |
| 2043 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2044 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2045 | /** |
| 2046 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 2047 | * have at least a default mobile policy defined. |
| 2048 | * |
| 2049 | * @param subId to build a default policy for |
| 2050 | * @param subscriberId that we check for an existing policy |
| 2051 | * @return true if a mobile network policy was added, or false one already existed. |
| 2052 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2053 | @GuardedBy("mNetworkPoliciesSecondLock") |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2054 | private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2055 | // Poke around to see if we already have a policy |
| 2056 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 2057 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2058 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 2059 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 2060 | if (template.matches(probeIdent)) { |
| 2061 | if (LOGD) { |
| 2062 | Slog.d(TAG, "Found template " + template + " which matches subscriber " |
| 2063 | + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 2064 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2065 | return false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2069 | Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 2070 | + "; generating default policy"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2071 | final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2072 | addNetworkPolicyAL(policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2073 | return true; |
| 2074 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2075 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2076 | private long getPlatformDefaultWarningBytes() { |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2077 | final int dataWarningConfig = mContext.getResources().getInteger( |
| 2078 | com.android.internal.R.integer.config_networkPolicyDefaultWarning); |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2079 | if (dataWarningConfig == WARNING_DISABLED) { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2080 | return WARNING_DISABLED; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2081 | } else { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2082 | return dataWarningConfig * MB_IN_BYTES; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2083 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2084 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2085 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2086 | private long getPlatformDefaultLimitBytes() { |
| 2087 | return LIMIT_DISABLED; |
| 2088 | } |
| 2089 | |
| 2090 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 2091 | NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2092 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2093 | final RecurrenceRule cycleRule = NetworkPolicy |
| 2094 | .buildRule(ZonedDateTime.now().getDayOfMonth(), ZoneId.systemDefault()); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2095 | final NetworkPolicy policy = new NetworkPolicy(template, cycleRule, |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2096 | getPlatformDefaultWarningBytes(), getPlatformDefaultLimitBytes(), |
| 2097 | SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 2098 | synchronized (mUidRulesFirstLock) { |
| 2099 | synchronized (mNetworkPoliciesSecondLock) { |
| 2100 | updateDefaultMobilePolicyAL(subId, policy); |
| 2101 | } |
| 2102 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2103 | return policy; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2106 | /** |
| 2107 | * Update the given {@link NetworkPolicy} based on any carrier-provided |
| 2108 | * defaults via {@link SubscriptionPlan} or {@link CarrierConfigManager}. |
| 2109 | * Leaves policy untouched if the user has modified it. |
| 2110 | * |
| 2111 | * @return if the policy was modified |
| 2112 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2113 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2114 | private boolean updateDefaultMobilePolicyAL(int subId, NetworkPolicy policy) { |
| 2115 | if (!policy.inferred) { |
| 2116 | if (LOGD) Slog.d(TAG, "Ignoring user-defined policy " + policy); |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | final NetworkPolicy original = new NetworkPolicy(policy.template, policy.cycleRule, |
| 2121 | policy.warningBytes, policy.limitBytes, policy.lastWarningSnooze, |
| 2122 | policy.lastLimitSnooze, policy.metered, policy.inferred); |
| 2123 | |
| 2124 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
| 2125 | if (!ArrayUtils.isEmpty(plans)) { |
| 2126 | final SubscriptionPlan plan = plans[0]; |
| 2127 | policy.cycleRule = plan.getCycleRule(); |
| 2128 | final long planLimitBytes = plan.getDataLimitBytes(); |
| 2129 | if (planLimitBytes == SubscriptionPlan.BYTES_UNKNOWN) { |
| 2130 | policy.warningBytes = getPlatformDefaultWarningBytes(); |
| 2131 | policy.limitBytes = getPlatformDefaultLimitBytes(); |
| 2132 | } else if (planLimitBytes == SubscriptionPlan.BYTES_UNLIMITED) { |
| 2133 | policy.warningBytes = NetworkPolicy.WARNING_DISABLED; |
| 2134 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 2135 | } else { |
| 2136 | policy.warningBytes = (planLimitBytes * 9) / 10; |
| 2137 | switch (plan.getDataLimitBehavior()) { |
| 2138 | case SubscriptionPlan.LIMIT_BEHAVIOR_BILLED: |
| 2139 | case SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED: |
| 2140 | policy.limitBytes = planLimitBytes; |
| 2141 | break; |
| 2142 | default: |
| 2143 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 2144 | break; |
| 2145 | } |
| 2146 | } |
| 2147 | } else { |
| 2148 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
| 2149 | final int currentCycleDay; |
| 2150 | if (policy.cycleRule.isMonthly()) { |
| 2151 | currentCycleDay = policy.cycleRule.start.getDayOfMonth(); |
| 2152 | } else { |
| 2153 | currentCycleDay = NetworkPolicy.CYCLE_NONE; |
| 2154 | } |
| 2155 | final int cycleDay = getCycleDayFromCarrierConfig(config, currentCycleDay); |
| 2156 | policy.cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.systemDefault()); |
| 2157 | policy.warningBytes = getWarningBytesFromCarrierConfig(config, policy.warningBytes); |
| 2158 | policy.limitBytes = getLimitBytesFromCarrierConfig(config, policy.limitBytes); |
| 2159 | } |
| 2160 | |
| 2161 | if (policy.equals(original)) { |
| 2162 | return false; |
| 2163 | } else { |
| 2164 | Slog.d(TAG, "Updated " + original + " to " + policy); |
| 2165 | return true; |
| 2166 | } |
| 2167 | } |
| 2168 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2169 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2170 | private void readPolicyAL() { |
| 2171 | if (LOGV) Slog.v(TAG, "readPolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2172 | |
| 2173 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2174 | mNetworkPolicy.clear(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2175 | mSubscriptionPlans.clear(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2176 | mSubscriptionPlansOwner.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2177 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2178 | |
| 2179 | FileInputStream fis = null; |
| 2180 | try { |
| 2181 | fis = mPolicyFile.openRead(); |
| 2182 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2183 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2184 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2185 | // Must save the <restrict-background> tags and convert them to <uid-policy> later, |
| 2186 | // to skip UIDs that were explicitly blacklisted. |
| 2187 | final SparseBooleanArray whitelistedRestrictBackground = new SparseBooleanArray(); |
| 2188 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2189 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2190 | int version = VERSION_INIT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2191 | boolean insideWhitelist = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2192 | while ((type = in.next()) != END_DOCUMENT) { |
| 2193 | final String tag = in.getName(); |
| 2194 | if (type == START_TAG) { |
| 2195 | if (TAG_POLICY_LIST.equals(tag)) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 2196 | final boolean oldValue = mRestrictBackground; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2197 | version = readIntAttribute(in, ATTR_VERSION); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2198 | mLoadedRestrictBackground = (version >= VERSION_ADDED_RESTRICT_BACKGROUND) |
| 2199 | && readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2200 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 2201 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 2202 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2203 | final String networkId; |
| 2204 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 2205 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 2206 | } else { |
| 2207 | networkId = null; |
| 2208 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2209 | final RecurrenceRule cycleRule; |
| 2210 | if (version >= VERSION_ADDED_CYCLE) { |
| 2211 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 2212 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 2213 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 2214 | cycleRule = new RecurrenceRule( |
| 2215 | RecurrenceRule.convertZonedDateTime(start), |
| 2216 | RecurrenceRule.convertZonedDateTime(end), |
| 2217 | RecurrenceRule.convertPeriod(period)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 2218 | } else { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2219 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
| 2220 | final String cycleTimezone; |
| 2221 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 2222 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 2223 | } else { |
| 2224 | cycleTimezone = "UTC"; |
| 2225 | } |
| 2226 | cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.of(cycleTimezone)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 2227 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2228 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 2229 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2230 | final long lastLimitSnooze; |
| 2231 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 2232 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 2233 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 2234 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2235 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2236 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2237 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2238 | final boolean metered; |
| 2239 | if (version >= VERSION_ADDED_METERED) { |
| 2240 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 2241 | } else { |
| 2242 | switch (networkTemplate) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 2243 | case MATCH_MOBILE: |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2244 | metered = true; |
| 2245 | break; |
| 2246 | default: |
| 2247 | metered = false; |
| 2248 | } |
| 2249 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2250 | final long lastWarningSnooze; |
| 2251 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 2252 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 2253 | } else { |
| 2254 | lastWarningSnooze = SNOOZE_NEVER; |
| 2255 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2256 | final boolean inferred; |
| 2257 | if (version >= VERSION_ADDED_INFERRED) { |
| 2258 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 2259 | } else { |
| 2260 | inferred = false; |
| 2261 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2262 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2263 | final NetworkTemplate template = new NetworkTemplate(networkTemplate, |
| 2264 | subscriberId, networkId); |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2265 | if (template.isPersistable()) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2266 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleRule, |
| 2267 | warningBytes, limitBytes, lastWarningSnooze, |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2268 | lastLimitSnooze, metered, inferred)); |
| 2269 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2270 | |
| 2271 | } else if (TAG_SUBSCRIPTION_PLAN.equals(tag)) { |
| 2272 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 2273 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 2274 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 2275 | final SubscriptionPlan.Builder builder = new SubscriptionPlan.Builder( |
| 2276 | RecurrenceRule.convertZonedDateTime(start), |
| 2277 | RecurrenceRule.convertZonedDateTime(end), |
| 2278 | RecurrenceRule.convertPeriod(period)); |
| 2279 | builder.setTitle(readStringAttribute(in, ATTR_TITLE)); |
| 2280 | builder.setSummary(readStringAttribute(in, ATTR_SUMMARY)); |
| 2281 | |
| 2282 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES, |
| 2283 | SubscriptionPlan.BYTES_UNKNOWN); |
| 2284 | final int limitBehavior = readIntAttribute(in, ATTR_LIMIT_BEHAVIOR, |
| 2285 | SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN); |
| 2286 | if (limitBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 2287 | && limitBehavior != SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
| 2288 | builder.setDataLimit(limitBytes, limitBehavior); |
| 2289 | } |
| 2290 | |
| 2291 | final long usageBytes = readLongAttribute(in, ATTR_USAGE_BYTES, |
| 2292 | SubscriptionPlan.BYTES_UNKNOWN); |
| 2293 | final long usageTime = readLongAttribute(in, ATTR_USAGE_TIME, |
| 2294 | SubscriptionPlan.TIME_UNKNOWN); |
| 2295 | if (usageBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 2296 | && usageTime != SubscriptionPlan.TIME_UNKNOWN) { |
| 2297 | builder.setDataUsage(usageBytes, usageTime); |
| 2298 | } |
| 2299 | |
| 2300 | final int subId = readIntAttribute(in, ATTR_SUB_ID); |
| 2301 | final SubscriptionPlan plan = builder.build(); |
| 2302 | mSubscriptionPlans.put(subId, ArrayUtils.appendElement( |
| 2303 | SubscriptionPlan.class, mSubscriptionPlans.get(subId), plan)); |
| 2304 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2305 | final String ownerPackage = readStringAttribute(in, ATTR_OWNER_PACKAGE); |
| 2306 | mSubscriptionPlansOwner.put(subId, ownerPackage); |
| 2307 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2308 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2309 | final int uid = readIntAttribute(in, ATTR_UID); |
| 2310 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 2311 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2312 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2313 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2314 | } else { |
| 2315 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 2316 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2317 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2318 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 2319 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 2320 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2321 | // TODO: set for other users during upgrade |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 2322 | // app policy is deprecated so this is only used in pre system user split. |
| 2323 | final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2324 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2325 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2326 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2327 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2328 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2329 | } else if (TAG_WHITELIST.equals(tag)) { |
| 2330 | insideWhitelist = true; |
| 2331 | } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 2332 | final int uid = readIntAttribute(in, ATTR_UID); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2333 | whitelistedRestrictBackground.append(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2334 | } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 2335 | final int uid = readIntAttribute(in, ATTR_UID); |
| 2336 | mRestrictBackgroundWhitelistRevokedUids.put(uid, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2337 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2338 | } else if (type == END_TAG) { |
| 2339 | if (TAG_WHITELIST.equals(tag)) { |
| 2340 | insideWhitelist = false; |
| 2341 | } |
| 2342 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2346 | final int size = whitelistedRestrictBackground.size(); |
| 2347 | for (int i = 0; i < size; i++) { |
| 2348 | final int uid = whitelistedRestrictBackground.keyAt(i); |
| 2349 | final int policy = mUidPolicy.get(uid, POLICY_NONE); |
| 2350 | if ((policy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
| 2351 | Slog.w(TAG, "ignoring restrict-background-whitelist for " + uid |
| 2352 | + " because its policy is " + uidPoliciesToString(policy)); |
| 2353 | continue; |
| 2354 | } |
| 2355 | if (UserHandle.isApp(uid)) { |
| 2356 | final int newPolicy = policy | POLICY_ALLOW_METERED_BACKGROUND; |
| 2357 | if (LOGV) |
| 2358 | Log.v(TAG, "new policy for " + uid + ": " + uidPoliciesToString(newPolicy)); |
| 2359 | setUidPolicyUncheckedUL(uid, newPolicy, false); |
| 2360 | } else { |
| 2361 | Slog.w(TAG, "unable to update policy on UID " + uid); |
| 2362 | } |
| 2363 | } |
| 2364 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2365 | } catch (FileNotFoundException e) { |
| 2366 | // missing policy is okay, probably first boot |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2367 | upgradeDefaultBackgroundDataUL(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2368 | } catch (Exception e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2369 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2370 | } finally { |
| 2371 | IoUtils.closeQuietly(fis); |
| 2372 | } |
| 2373 | } |
| 2374 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2375 | /** |
| 2376 | * Upgrade legacy background data flags, notifying listeners of one last |
| 2377 | * change to always-true. |
| 2378 | */ |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2379 | private void upgradeDefaultBackgroundDataUL() { |
| 2380 | // This method is only called when we're unable to find the network policy flag, which |
| 2381 | // usually happens on first boot of a new device and not one that has received an OTA. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2382 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2383 | // Seed from the default value configured for this device. |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2384 | mLoadedRestrictBackground = Settings.Global.getInt( |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2385 | mContext.getContentResolver(), Global.DEFAULT_RESTRICT_BACKGROUND_DATA, 0) == 1; |
| 2386 | |
| 2387 | // NOTE: We used to read the legacy setting here : |
| 2388 | // |
| 2389 | // final int legacyFlagValue = Settings.Secure.getInt( |
| 2390 | // mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, ..); |
| 2391 | // |
| 2392 | // This is no longer necessary because we will never upgrade directly from Gingerbread |
| 2393 | // to O+. Devices upgrading from ICS onwards to O will have a netpolicy.xml file that |
| 2394 | // contains the correct value that we will continue to use. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2397 | /** |
| 2398 | * Perform upgrade step of moving any user-defined meterness overrides over |
| 2399 | * into {@link WifiConfiguration}. |
| 2400 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2401 | @GuardedBy({"mNetworkPoliciesSecondLock", "mUidRulesFirstLock"}) |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2402 | private void upgradeWifiMeteredOverrideAL() { |
| 2403 | boolean modified = false; |
| 2404 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2405 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2406 | for (int i = 0; i < mNetworkPolicy.size(); ) { |
| 2407 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 2408 | if (policy.template.getMatchRule() == NetworkTemplate.MATCH_WIFI |
| 2409 | && !policy.inferred) { |
| 2410 | mNetworkPolicy.removeAt(i); |
| 2411 | modified = true; |
| 2412 | |
| 2413 | final String networkId = resolveNetworkId(policy.template.getNetworkId()); |
| 2414 | for (WifiConfiguration config : configs) { |
| 2415 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2416 | Slog.d(TAG, "Found network " + networkId + "; upgrading metered hint"); |
| 2417 | config.meteredOverride = policy.metered |
| 2418 | ? WifiConfiguration.METERED_OVERRIDE_METERED |
| 2419 | : WifiConfiguration.METERED_OVERRIDE_NOT_METERED; |
| 2420 | wm.updateNetwork(config); |
| 2421 | } |
| 2422 | } |
| 2423 | } else { |
| 2424 | i++; |
| 2425 | } |
| 2426 | } |
| 2427 | if (modified) { |
| 2428 | writePolicyAL(); |
| 2429 | } |
| 2430 | } |
| 2431 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2432 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2433 | void writePolicyAL() { |
| 2434 | if (LOGV) Slog.v(TAG, "writePolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2435 | |
| 2436 | FileOutputStream fos = null; |
| 2437 | try { |
| 2438 | fos = mPolicyFile.startWrite(); |
| 2439 | |
| 2440 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2441 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2442 | out.startDocument(null, true); |
| 2443 | |
| 2444 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2445 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2446 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2447 | |
| 2448 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2449 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2450 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2451 | final NetworkTemplate template = policy.template; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2452 | if (!template.isPersistable()) continue; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2453 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2454 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2455 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 2456 | final String subscriberId = template.getSubscriberId(); |
| 2457 | if (subscriberId != null) { |
| 2458 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2459 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2460 | final String networkId = template.getNetworkId(); |
| 2461 | if (networkId != null) { |
| 2462 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 2463 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2464 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2465 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.start)); |
| 2466 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2467 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.end)); |
| 2468 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2469 | RecurrenceRule.convertPeriod(policy.cycleRule.period)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2470 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 2471 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2472 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 2473 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2474 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2475 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2476 | out.endTag(null, TAG_NETWORK_POLICY); |
| 2477 | } |
| 2478 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2479 | // write all known subscription plans |
| 2480 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 2481 | final int subId = mSubscriptionPlans.keyAt(i); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2482 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2483 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 2484 | if (ArrayUtils.isEmpty(plans)) continue; |
| 2485 | |
| 2486 | for (SubscriptionPlan plan : plans) { |
| 2487 | out.startTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2488 | writeIntAttribute(out, ATTR_SUB_ID, subId); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2489 | writeStringAttribute(out, ATTR_OWNER_PACKAGE, ownerPackage); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2490 | final RecurrenceRule cycleRule = plan.getCycleRule(); |
| 2491 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2492 | RecurrenceRule.convertZonedDateTime(cycleRule.start)); |
| 2493 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2494 | RecurrenceRule.convertZonedDateTime(cycleRule.end)); |
| 2495 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2496 | RecurrenceRule.convertPeriod(cycleRule.period)); |
| 2497 | writeStringAttribute(out, ATTR_TITLE, plan.getTitle()); |
| 2498 | writeStringAttribute(out, ATTR_SUMMARY, plan.getSummary()); |
| 2499 | writeLongAttribute(out, ATTR_LIMIT_BYTES, plan.getDataLimitBytes()); |
| 2500 | writeIntAttribute(out, ATTR_LIMIT_BEHAVIOR, plan.getDataLimitBehavior()); |
| 2501 | writeLongAttribute(out, ATTR_USAGE_BYTES, plan.getDataUsageBytes()); |
| 2502 | writeLongAttribute(out, ATTR_USAGE_TIME, plan.getDataUsageTime()); |
| 2503 | out.endTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2504 | } |
| 2505 | } |
| 2506 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2507 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2508 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2509 | final int uid = mUidPolicy.keyAt(i); |
| 2510 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2511 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2512 | // skip writing empty policies |
| 2513 | if (policy == POLICY_NONE) continue; |
| 2514 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2515 | out.startTag(null, TAG_UID_POLICY); |
| 2516 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2517 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2518 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | out.endTag(null, TAG_POLICY_LIST); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2522 | |
| 2523 | // write all whitelists |
| 2524 | out.startTag(null, TAG_WHITELIST); |
| 2525 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2526 | // revoked restrict background whitelist |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2527 | int size = mRestrictBackgroundWhitelistRevokedUids.size(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2528 | for (int i = 0; i < size; i++) { |
| 2529 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2530 | out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2531 | writeIntAttribute(out, ATTR_UID, uid); |
| 2532 | out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2533 | } |
| 2534 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2535 | out.endTag(null, TAG_WHITELIST); |
| 2536 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2537 | out.endDocument(); |
| 2538 | |
| 2539 | mPolicyFile.finishWrite(fos); |
| 2540 | } catch (IOException e) { |
| 2541 | if (fos != null) { |
| 2542 | mPolicyFile.failWrite(fos); |
| 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2547 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2548 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2549 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2550 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2551 | if (!UserHandle.isApp(uid)) { |
| 2552 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2553 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2554 | synchronized (mUidRulesFirstLock) { |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2555 | final long token = Binder.clearCallingIdentity(); |
| 2556 | try { |
| 2557 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2558 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2559 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2560 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2561 | } |
| 2562 | } finally { |
| 2563 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2564 | } |
| 2565 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2568 | @Override |
| 2569 | public void addUidPolicy(int uid, int policy) { |
| 2570 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2571 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2572 | if (!UserHandle.isApp(uid)) { |
| 2573 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2574 | } |
| 2575 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2576 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2577 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2578 | policy |= oldPolicy; |
| 2579 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2580 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2581 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2582 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2583 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2587 | public void removeUidPolicy(int uid, int policy) { |
| 2588 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2589 | |
| 2590 | if (!UserHandle.isApp(uid)) { |
| 2591 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2592 | } |
| 2593 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2594 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2595 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2596 | policy = oldPolicy & ~policy; |
| 2597 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2598 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2599 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2604 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2605 | private void setUidPolicyUncheckedUL(int uid, int oldPolicy, int policy, boolean persist) { |
Sudheer Shanka | 5723ccb | 2018-02-13 11:08:19 -0800 | [diff] [blame] | 2606 | setUidPolicyUncheckedUL(uid, policy, false); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2607 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2608 | final boolean notifyApp; |
| 2609 | if (!isUidValidForWhitelistRules(uid)) { |
| 2610 | notifyApp = false; |
| 2611 | } else { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2612 | final boolean wasBlacklisted = oldPolicy == POLICY_REJECT_METERED_BACKGROUND; |
| 2613 | final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND; |
| 2614 | final boolean wasWhitelisted = oldPolicy == POLICY_ALLOW_METERED_BACKGROUND; |
| 2615 | final boolean isWhitelisted = policy == POLICY_ALLOW_METERED_BACKGROUND; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2616 | final boolean wasBlocked = wasBlacklisted || (mRestrictBackground && !wasWhitelisted); |
| 2617 | final boolean isBlocked = isBlacklisted || (mRestrictBackground && !isWhitelisted); |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2618 | if ((wasWhitelisted && (!isWhitelisted || isBlacklisted)) |
| 2619 | && mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2620 | && !mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2621 | if (LOGD) |
| 2622 | Slog.d(TAG, "Adding uid " + uid + " to revoked restrict background whitelist"); |
| 2623 | mRestrictBackgroundWhitelistRevokedUids.append(uid, true); |
| 2624 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2625 | notifyApp = wasBlocked != isBlocked; |
| 2626 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2627 | mHandler.obtainMessage(MSG_POLICIES_CHANGED, uid, policy, Boolean.valueOf(notifyApp)) |
| 2628 | .sendToTarget(); |
Sudheer Shanka | 5723ccb | 2018-02-13 11:08:19 -0800 | [diff] [blame] | 2629 | if (persist) { |
| 2630 | synchronized (mNetworkPoliciesSecondLock) { |
| 2631 | writePolicyAL(); |
| 2632 | } |
| 2633 | } |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2636 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2637 | private void setUidPolicyUncheckedUL(int uid, int policy, boolean persist) { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2638 | if (policy == POLICY_NONE) { |
| 2639 | mUidPolicy.delete(uid); |
| 2640 | } else { |
| 2641 | mUidPolicy.put(uid, policy); |
| 2642 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2643 | |
| 2644 | // uid policy changed, recompute rules and persist policy. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2645 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2646 | if (persist) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2647 | synchronized (mNetworkPoliciesSecondLock) { |
| 2648 | writePolicyAL(); |
| 2649 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2654 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2655 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2656 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2657 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2658 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2659 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2660 | } |
| 2661 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2662 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2663 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2664 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2665 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2666 | int[] uids = new int[0]; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2667 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2668 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2669 | final int uid = mUidPolicy.keyAt(i); |
| 2670 | final int uidPolicy = mUidPolicy.valueAt(i); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 2671 | if ((policy == POLICY_NONE && uidPolicy == POLICY_NONE) || |
| 2672 | (uidPolicy & policy) != 0) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2673 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2677 | return uids; |
| 2678 | } |
| 2679 | |
| 2680 | /** |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2681 | * Removes any persistable state associated with given {@link UserHandle}, persisting |
| 2682 | * if any changes that are made. |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2683 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2684 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2685 | boolean removeUserStateUL(int userId, boolean writePolicy) { |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2686 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2687 | mLogger.removingUserState(userId); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2688 | boolean changed = false; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2689 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2690 | // Remove entries from revoked default restricted background UID whitelist |
| 2691 | for (int i = mRestrictBackgroundWhitelistRevokedUids.size() - 1; i >= 0; i--) { |
| 2692 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2693 | if (UserHandle.getUserId(uid) == userId) { |
| 2694 | mRestrictBackgroundWhitelistRevokedUids.removeAt(i); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2695 | changed = true; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2699 | // Remove associated UID policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2700 | int[] uids = new int[0]; |
| 2701 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2702 | final int uid = mUidPolicy.keyAt(i); |
| 2703 | if (UserHandle.getUserId(uid) == userId) { |
| 2704 | uids = appendInt(uids, uid); |
| 2705 | } |
| 2706 | } |
| 2707 | |
| 2708 | if (uids.length > 0) { |
| 2709 | for (int uid : uids) { |
| 2710 | mUidPolicy.delete(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2711 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2712 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2713 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2714 | synchronized (mNetworkPoliciesSecondLock) { |
| 2715 | updateRulesForGlobalChangeAL(true); |
| 2716 | if (writePolicy && changed) { |
| 2717 | writePolicyAL(); |
| 2718 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2719 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2720 | return changed; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2723 | private boolean checkAnyPermissionOf(String... permissions) { |
| 2724 | for (String permission : permissions) { |
| 2725 | if (mContext.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) { |
| 2726 | return true; |
| 2727 | } |
| 2728 | } |
| 2729 | return false; |
| 2730 | } |
| 2731 | |
| 2732 | private void enforceAnyPermissionOf(String... permissions) { |
| 2733 | if (!checkAnyPermissionOf(permissions)) { |
| 2734 | throw new SecurityException("Requires one of the following permissions: " |
| 2735 | + String.join(", ", permissions) + "."); |
| 2736 | } |
| 2737 | } |
| 2738 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2739 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2740 | public void registerListener(INetworkPolicyListener listener) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2741 | // TODO: Remove CONNECTIVITY_INTERNAL and the *AnyPermissionOf methods above after all apps |
| 2742 | // have declared OBSERVE_NETWORK_POLICY. |
| 2743 | enforceAnyPermissionOf(CONNECTIVITY_INTERNAL, OBSERVE_NETWORK_POLICY); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2744 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | @Override |
| 2748 | public void unregisterListener(INetworkPolicyListener listener) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2749 | // TODO: Remove CONNECTIVITY_INTERNAL and the *AnyPermissionOf methods above after all apps |
| 2750 | // have declared OBSERVE_NETWORK_POLICY. |
| 2751 | enforceAnyPermissionOf(CONNECTIVITY_INTERNAL, OBSERVE_NETWORK_POLICY); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2752 | mListeners.unregister(listener); |
| 2753 | } |
| 2754 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2755 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2756 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2757 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2758 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2759 | final long token = Binder.clearCallingIdentity(); |
| 2760 | try { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2761 | synchronized (mUidRulesFirstLock) { |
| 2762 | synchronized (mNetworkPoliciesSecondLock) { |
| 2763 | normalizePoliciesNL(policies); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2764 | handleNetworkPoliciesUpdateAL(false); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2765 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2766 | } |
| 2767 | } finally { |
| 2768 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2772 | void addNetworkPolicyAL(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2773 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2774 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 2775 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2778 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2779 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2780 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2781 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 2782 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 2783 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 2784 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2785 | } catch (SecurityException e) { |
| 2786 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2787 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2788 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 2789 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 2790 | return new NetworkPolicy[0]; |
| 2791 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2794 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2795 | final int size = mNetworkPolicy.size(); |
| 2796 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 2797 | for (int i = 0; i < size; i++) { |
| 2798 | policies[i] = mNetworkPolicy.valueAt(i); |
| 2799 | } |
| 2800 | return policies; |
| 2801 | } |
| 2802 | } |
| 2803 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2804 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2805 | private void normalizePoliciesNL() { |
| 2806 | normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2807 | } |
| 2808 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2809 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2810 | private void normalizePoliciesNL(NetworkPolicy[] policies) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2811 | mNetworkPolicy.clear(); |
| 2812 | for (NetworkPolicy policy : policies) { |
Annie Meng | 20b4d84 | 2018-05-18 15:00:49 +0100 | [diff] [blame] | 2813 | if (policy == null) { |
| 2814 | continue; |
| 2815 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2816 | // When two normalized templates conflict, prefer the most |
| 2817 | // restrictive policy |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 2818 | policy.template = NetworkTemplate.normalize(policy.template, mMergedSubscriberIds); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2819 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 2820 | if (existing == null || existing.compareTo(policy) > 0) { |
| 2821 | if (existing != null) { |
| 2822 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 2823 | } |
| 2824 | mNetworkPolicy.put(policy.template, policy); |
| 2825 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2830 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2831 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 2832 | |
| 2833 | final long token = Binder.clearCallingIdentity(); |
| 2834 | try { |
| 2835 | performSnooze(template, TYPE_LIMIT); |
| 2836 | } finally { |
| 2837 | Binder.restoreCallingIdentity(token); |
| 2838 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2839 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2840 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2841 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 2842 | final long currentTime = mClock.millis(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2843 | synchronized (mUidRulesFirstLock) { |
| 2844 | synchronized (mNetworkPoliciesSecondLock) { |
| 2845 | // find and snooze local policy that matches |
| 2846 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 2847 | if (policy == null) { |
| 2848 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 2849 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2850 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2851 | switch (type) { |
| 2852 | case TYPE_WARNING: |
| 2853 | policy.lastWarningSnooze = currentTime; |
| 2854 | break; |
| 2855 | case TYPE_LIMIT: |
| 2856 | policy.lastLimitSnooze = currentTime; |
| 2857 | break; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 2858 | case TYPE_RAPID: |
| 2859 | policy.lastRapidSnooze = currentTime; |
| 2860 | break; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2861 | default: |
| 2862 | throw new IllegalArgumentException("unexpected type"); |
| 2863 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2864 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2865 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2866 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2871 | public void onTetheringChanged(String iface, boolean tethering) { |
| 2872 | // No need to enforce permission because setRestrictBackground() will do it. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2873 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2874 | if (mRestrictBackground && tethering) { |
| 2875 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 2876 | setRestrictBackground(false); |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2882 | public void setRestrictBackground(boolean restrictBackground) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2883 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackground"); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2884 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2885 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2886 | final long token = Binder.clearCallingIdentity(); |
| 2887 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2888 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2889 | setRestrictBackgroundUL(restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2890 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2891 | } finally { |
| 2892 | Binder.restoreCallingIdentity(token); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2893 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2894 | } finally { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2895 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2899 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2900 | private void setRestrictBackgroundUL(boolean restrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2901 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackgroundUL"); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2902 | try { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2903 | if (restrictBackground == mRestrictBackground) { |
| 2904 | // Ideally, UI should never allow this scenario... |
| 2905 | Slog.w(TAG, "setRestrictBackgroundUL: already " + restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2906 | return; |
| 2907 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2908 | Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground); |
| 2909 | final boolean oldRestrictBackground = mRestrictBackground; |
| 2910 | mRestrictBackground = restrictBackground; |
| 2911 | // Must whitelist foreground apps before turning data saver mode on. |
| 2912 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 2913 | // so it could call AM to get the UIDs of such apps, and iterate through them instead. |
| 2914 | updateRulesForRestrictBackgroundUL(); |
| 2915 | try { |
| 2916 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2917 | Slog.e(TAG, |
| 2918 | "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2919 | mRestrictBackground = oldRestrictBackground; |
| 2920 | // TODO: if it knew the foreground apps (see TODO above), it could call |
| 2921 | // updateRulesForRestrictBackgroundUL() again to restore state. |
| 2922 | return; |
| 2923 | } |
| 2924 | } catch (RemoteException e) { |
| 2925 | // ignored; service lives in system_server |
| 2926 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2927 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2928 | sendRestrictBackgroundChangedMsg(); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2929 | mLogger.restrictBackgroundChanged(oldRestrictBackground, mRestrictBackground); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2930 | |
| 2931 | if (mRestrictBackgroundPowerState.globalBatterySaverEnabled) { |
| 2932 | mRestrictBackgroundChangedInBsm = true; |
| 2933 | } |
| 2934 | synchronized (mNetworkPoliciesSecondLock) { |
| 2935 | updateNotificationsNL(); |
| 2936 | writePolicyAL(); |
| 2937 | } |
| 2938 | } finally { |
| 2939 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2940 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | private void sendRestrictBackgroundChangedMsg() { |
| 2944 | mHandler.removeMessages(MSG_RESTRICT_BACKGROUND_CHANGED); |
| 2945 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0) |
| 2946 | .sendToTarget(); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2949 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2950 | public int getRestrictBackgroundByCaller() { |
| 2951 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2952 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2953 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2954 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2955 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2956 | final long token = Binder.clearCallingIdentity(); |
| 2957 | final int policy; |
| 2958 | try { |
| 2959 | policy = getUidPolicy(uid); |
| 2960 | } finally { |
| 2961 | Binder.restoreCallingIdentity(token); |
| 2962 | } |
| 2963 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2964 | // App is blacklisted. |
| 2965 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2966 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2967 | if (!mRestrictBackground) { |
| 2968 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2969 | } |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2970 | return (mUidPolicy.get(uid) & POLICY_ALLOW_METERED_BACKGROUND) != 0 |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2971 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2972 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2977 | public boolean getRestrictBackground() { |
| 2978 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2979 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2980 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2981 | return mRestrictBackground; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2985 | @Override |
| 2986 | public void setDeviceIdleMode(boolean enabled) { |
| 2987 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2988 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode"); |
| 2989 | try { |
| 2990 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2991 | if (mDeviceIdleMode == enabled) { |
| 2992 | return; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2993 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2994 | mDeviceIdleMode = enabled; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2995 | mLogger.deviceIdleModeEnabled(enabled); |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2996 | if (mSystemReady) { |
| 2997 | // Device idle change means we need to rebuild rules for all |
| 2998 | // known apps, so do a global refresh. |
| 2999 | updateRulesForRestrictPowerUL(); |
| 3000 | } |
| 3001 | } |
| 3002 | if (enabled) { |
| 3003 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 3004 | } else { |
| 3005 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3006 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3007 | } finally { |
| 3008 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 3012 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3013 | public void setWifiMeteredOverride(String networkId, int meteredOverride) { |
| 3014 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 3015 | final long token = Binder.clearCallingIdentity(); |
| 3016 | try { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3017 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 3018 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 3019 | for (WifiConfiguration config : configs) { |
| 3020 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 3021 | config.meteredOverride = meteredOverride; |
| 3022 | wm.updateNetwork(config); |
| 3023 | } |
| 3024 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 3025 | } finally { |
| 3026 | Binder.restoreCallingIdentity(token); |
| 3027 | } |
| 3028 | } |
| 3029 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 3030 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3031 | @Deprecated |
| 3032 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 3033 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 3034 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 3035 | return new NetworkQuotaInfo(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3038 | private void enforceSubscriptionPlanAccess(int subId, int callingUid, String callingPackage) { |
| 3039 | // Verify they're not lying about package name |
| 3040 | mAppOps.checkPackage(callingUid, callingPackage); |
| 3041 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3042 | final SubscriptionInfo si; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3043 | final PersistableBundle config; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3044 | final long token = Binder.clearCallingIdentity(); |
| 3045 | try { |
| 3046 | si = mContext.getSystemService(SubscriptionManager.class) |
| 3047 | .getActiveSubscriptionInfo(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3048 | config = mCarrierConfigManager.getConfigForSubId(subId); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3049 | } finally { |
| 3050 | Binder.restoreCallingIdentity(token); |
| 3051 | } |
| 3052 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3053 | // First check: is caller the CarrierService? |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3054 | if (si != null) { |
| 3055 | if (si.isEmbedded() && si.canManageSubscription(mContext, callingPackage)) { |
| 3056 | return; |
| 3057 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3058 | } |
| 3059 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3060 | // Second check: has the CarrierService delegated access? |
| 3061 | if (config != null) { |
| 3062 | final String overridePackage = config |
| 3063 | .getString(CarrierConfigManager.KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING, null); |
| 3064 | if (!TextUtils.isEmpty(overridePackage) |
| 3065 | && Objects.equals(overridePackage, callingPackage)) { |
| 3066 | return; |
| 3067 | } |
| 3068 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3069 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3070 | // Third check: is caller the fallback/default CarrierService? |
| 3071 | final String defaultPackage = mCarrierConfigManager.getDefaultCarrierServicePackageName(); |
| 3072 | if (!TextUtils.isEmpty(defaultPackage) |
| 3073 | && Objects.equals(defaultPackage, callingPackage)) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3074 | return; |
| 3075 | } |
| 3076 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3077 | // Fourth check: is caller a testing app? |
| 3078 | final String testPackage = SystemProperties.get(PROP_SUB_PLAN_OWNER + "." + subId, null); |
| 3079 | if (!TextUtils.isEmpty(testPackage) |
| 3080 | && Objects.equals(testPackage, callingPackage)) { |
| 3081 | return; |
| 3082 | } |
| 3083 | |
| 3084 | // Fifth check: is caller a legacy testing app? |
| 3085 | final String legacyTestPackage = SystemProperties.get("fw.sub_plan_owner." + subId, null); |
| 3086 | if (!TextUtils.isEmpty(legacyTestPackage) |
| 3087 | && Objects.equals(legacyTestPackage, callingPackage)) { |
Jeff Sharkey | a7f5046 | 2018-02-14 14:26:10 -0700 | [diff] [blame] | 3088 | return; |
| 3089 | } |
| 3090 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3091 | // Final check: does the caller hold a permission? |
| 3092 | mContext.enforceCallingOrSelfPermission(MANAGE_SUBSCRIPTION_PLANS, TAG); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | @Override |
| 3096 | public SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage) { |
| 3097 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3098 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3099 | final String fake = SystemProperties.get("fw.fake_plan"); |
| 3100 | if (!TextUtils.isEmpty(fake)) { |
| 3101 | final List<SubscriptionPlan> plans = new ArrayList<>(); |
| 3102 | if ("month_hard".equals(fake)) { |
| 3103 | plans.add(SubscriptionPlan.Builder |
| 3104 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3105 | .setTitle("G-Mobile") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3106 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3107 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3108 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3109 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3110 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3111 | plans.add(SubscriptionPlan.Builder |
| 3112 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3113 | .setTitle("G-Mobile Happy") |
| 3114 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3115 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3116 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3117 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3118 | .build()); |
| 3119 | plans.add(SubscriptionPlan.Builder |
| 3120 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3121 | .setTitle("G-Mobile, Charged after limit") |
| 3122 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3123 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3124 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3125 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3126 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3127 | } else if ("month_soft".equals(fake)) { |
| 3128 | plans.add(SubscriptionPlan.Builder |
| 3129 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3130 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3131 | .setSummary("Crazy unlimited bandwidth plan with incredibly long title " |
| 3132 | + "that should be cut off to prevent UI from looking terrible") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3133 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3134 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3135 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3136 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3137 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3138 | plans.add(SubscriptionPlan.Builder |
| 3139 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3140 | .setTitle("G-Mobile, Throttled after limit") |
| 3141 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3142 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3143 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3144 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3145 | .build()); |
| 3146 | plans.add(SubscriptionPlan.Builder |
| 3147 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3148 | .setTitle("G-Mobile, No data connection after limit") |
| 3149 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3150 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3151 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3152 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3153 | .build()); |
| 3154 | |
Sundeep Ghuman | 09e0f57 | 2018-03-14 23:20:23 -0700 | [diff] [blame] | 3155 | } else if ("month_over".equals(fake)) { |
| 3156 | plans.add(SubscriptionPlan.Builder |
| 3157 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3158 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3159 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3160 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3161 | .setDataUsage(6 * TrafficStats.GB_IN_BYTES, |
| 3162 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3163 | .build()); |
| 3164 | plans.add(SubscriptionPlan.Builder |
| 3165 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3166 | .setTitle("G-Mobile, Throttled after limit") |
| 3167 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3168 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3169 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3170 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3171 | .build()); |
| 3172 | plans.add(SubscriptionPlan.Builder |
| 3173 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3174 | .setTitle("G-Mobile, No data connection after limit") |
| 3175 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3176 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3177 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3178 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3179 | .build()); |
| 3180 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3181 | } else if ("month_none".equals(fake)) { |
| 3182 | plans.add(SubscriptionPlan.Builder |
| 3183 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3184 | .setTitle("G-Mobile") |
| 3185 | .build()); |
| 3186 | } else if ("prepaid".equals(fake)) { |
| 3187 | plans.add(SubscriptionPlan.Builder |
| 3188 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3189 | ZonedDateTime.now().plusDays(10)) |
| 3190 | .setTitle("G-Mobile") |
| 3191 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 3192 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3193 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 3194 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3195 | .build()); |
| 3196 | } else if ("prepaid_crazy".equals(fake)) { |
| 3197 | plans.add(SubscriptionPlan.Builder |
| 3198 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3199 | ZonedDateTime.now().plusDays(10)) |
| 3200 | .setTitle("G-Mobile Anytime") |
| 3201 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 3202 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3203 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 3204 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3205 | .build()); |
| 3206 | plans.add(SubscriptionPlan.Builder |
| 3207 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3208 | ZonedDateTime.now().plusDays(20)) |
| 3209 | .setTitle("G-Mobile Nickel Nights") |
| 3210 | .setSummary("5¢/GB between 1-5AM") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3211 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3212 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3213 | .setDataUsage(15 * TrafficStats.MB_IN_BYTES, |
| 3214 | ZonedDateTime.now().minusHours(30).toInstant().toEpochMilli()) |
| 3215 | .build()); |
| 3216 | plans.add(SubscriptionPlan.Builder |
| 3217 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3218 | ZonedDateTime.now().plusDays(20)) |
| 3219 | .setTitle("G-Mobile Bonus 3G") |
| 3220 | .setSummary("Unlimited 3G data") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3221 | .setDataLimit(1 * TrafficStats.GB_IN_BYTES, |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3222 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3223 | .setDataUsage(300 * TrafficStats.MB_IN_BYTES, |
| 3224 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3225 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3226 | } else if ("unlimited".equals(fake)) { |
| 3227 | plans.add(SubscriptionPlan.Builder |
| 3228 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3229 | ZonedDateTime.now().plusDays(10)) |
| 3230 | .setTitle("G-Mobile Awesome") |
| 3231 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3232 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3233 | .setDataUsage(50 * TrafficStats.MB_IN_BYTES, |
| 3234 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3235 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3236 | } |
| 3237 | return plans.toArray(new SubscriptionPlan[plans.size()]); |
| 3238 | } |
| 3239 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 3240 | synchronized (mNetworkPoliciesSecondLock) { |
| 3241 | // Only give out plan details to the package that defined them, |
| 3242 | // so that we don't risk leaking plans between apps. We always |
| 3243 | // let in core system components (like the Settings app). |
| 3244 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
| 3245 | if (Objects.equals(ownerPackage, callingPackage) |
| 3246 | || (UserHandle.getCallingAppId() == android.os.Process.SYSTEM_UID)) { |
| 3247 | return mSubscriptionPlans.get(subId); |
| 3248 | } else { |
| 3249 | Log.w(TAG, "Not returning plans because caller " + callingPackage |
| 3250 | + " doesn't match owner " + ownerPackage); |
| 3251 | return null; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3252 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | @Override |
| 3257 | public void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) { |
| 3258 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3259 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3260 | for (SubscriptionPlan plan : plans) { |
| 3261 | Preconditions.checkNotNull(plan); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | final long token = Binder.clearCallingIdentity(); |
| 3265 | try { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3266 | synchronized (mUidRulesFirstLock) { |
| 3267 | synchronized (mNetworkPoliciesSecondLock) { |
| 3268 | mSubscriptionPlans.put(subId, plans); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3269 | mSubscriptionPlansOwner.put(subId, callingPackage); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3270 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3271 | final String subscriberId = mSubIdToSubscriberId.get(subId, null); |
| 3272 | if (subscriberId != null) { |
| 3273 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 3274 | maybeUpdateMobilePolicyCycleAL(subId, subscriberId); |
| 3275 | } else { |
| 3276 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 3277 | } |
| 3278 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3279 | handleNetworkPoliciesUpdateAL(true); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3280 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3281 | } |
Jeff Sharkey | e92ed6f | 2018-01-10 20:47:42 -0700 | [diff] [blame] | 3282 | |
| 3283 | final Intent intent = new Intent(SubscriptionManager.ACTION_SUBSCRIPTION_PLANS_CHANGED); |
| 3284 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3285 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); |
| 3286 | mContext.sendBroadcast(intent, android.Manifest.permission.MANAGE_SUBSCRIPTION_PLANS); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3287 | } finally { |
| 3288 | Binder.restoreCallingIdentity(token); |
| 3289 | } |
| 3290 | } |
| 3291 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3292 | /** |
| 3293 | * Only visible for testing purposes. This doesn't give any access to |
| 3294 | * existing plans; it simply lets the debug package define new plans. |
| 3295 | */ |
| 3296 | void setSubscriptionPlansOwner(int subId, String packageName) { |
| 3297 | SystemProperties.set(PROP_SUB_PLAN_OWNER + "." + subId, packageName); |
| 3298 | } |
| 3299 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3300 | @Override |
Jeff Sharkey | 717f52f | 2018-01-04 16:04:11 -0700 | [diff] [blame] | 3301 | public String getSubscriptionPlansOwner(int subId) { |
| 3302 | if (UserHandle.getCallingAppId() != android.os.Process.SYSTEM_UID) { |
| 3303 | throw new SecurityException(); |
| 3304 | } |
| 3305 | |
| 3306 | synchronized (mNetworkPoliciesSecondLock) { |
| 3307 | return mSubscriptionPlansOwner.get(subId); |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3312 | public void setSubscriptionOverride(int subId, int overrideMask, int overrideValue, |
| 3313 | long timeoutMillis, String callingPackage) { |
| 3314 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3315 | |
| 3316 | // We can only override when carrier told us about plans |
| 3317 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3318 | final SubscriptionPlan plan = getPrimarySubscriptionPlanLocked(subId); |
| 3319 | if (plan == null |
| 3320 | || plan.getDataLimitBehavior() == SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3321 | throw new IllegalStateException( |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3322 | "Must provide valid SubscriptionPlan to enable overriding"); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 3326 | // Only allow overrides when feature is enabled. However, we always |
| 3327 | // allow disabling of overrides for safety reasons. |
| 3328 | final boolean overrideEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 3329 | NETPOLICY_OVERRIDE_ENABLED, 1) != 0; |
| 3330 | if (overrideEnabled || overrideValue == 0) { |
| 3331 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3332 | overrideMask, overrideValue, subId)); |
| 3333 | if (timeoutMillis > 0) { |
| 3334 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3335 | overrideMask, 0, subId), timeoutMillis); |
| 3336 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3341 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3342 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3343 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3344 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 3345 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3346 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3347 | for (String arg : args) { |
| 3348 | argSet.add(arg); |
| 3349 | } |
| 3350 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3351 | synchronized (mUidRulesFirstLock) { |
| 3352 | synchronized (mNetworkPoliciesSecondLock) { |
| 3353 | if (argSet.contains("--unsnooze")) { |
| 3354 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 3355 | mNetworkPolicy.valueAt(i).clearSnooze(); |
| 3356 | } |
| 3357 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 3358 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3359 | |
| 3360 | fout.println("Cleared snooze timestamps"); |
| 3361 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3362 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 3363 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3364 | fout.print("System ready: "); fout.println(mSystemReady); |
| 3365 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
| 3366 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
| 3367 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3368 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 3369 | |
| 3370 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3371 | fout.println("Network policies:"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3372 | fout.increaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3373 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 3374 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
| 3375 | } |
| 3376 | fout.decreaseIndent(); |
| 3377 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3378 | fout.println(); |
| 3379 | fout.println("Subscription plans:"); |
| 3380 | fout.increaseIndent(); |
| 3381 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 3382 | final int subId = mSubscriptionPlans.keyAt(i); |
| 3383 | fout.println("Subscriber ID " + subId + ":"); |
| 3384 | fout.increaseIndent(); |
| 3385 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 3386 | if (!ArrayUtils.isEmpty(plans)) { |
| 3387 | for (SubscriptionPlan plan : plans) { |
| 3388 | fout.println(plan); |
| 3389 | } |
| 3390 | } |
| 3391 | fout.decreaseIndent(); |
| 3392 | } |
| 3393 | fout.decreaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3394 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3395 | fout.println(); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3396 | fout.println("Active subscriptions:"); |
| 3397 | fout.increaseIndent(); |
| 3398 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 3399 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 3400 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
| 3401 | |
| 3402 | fout.println(subId + "=" + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 3403 | } |
| 3404 | fout.decreaseIndent(); |
| 3405 | |
| 3406 | fout.println(); |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 3407 | for (String[] mergedSubscribers : mMergedSubscriberIds) { |
| 3408 | fout.println("Merged subscriptions: " + Arrays.toString( |
| 3409 | NetworkIdentity.scrubSubscriberId(mergedSubscribers))); |
| 3410 | } |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3411 | |
| 3412 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3413 | fout.println("Policy for UIDs:"); |
| 3414 | fout.increaseIndent(); |
| 3415 | int size = mUidPolicy.size(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3416 | for (int i = 0; i < size; i++) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3417 | final int uid = mUidPolicy.keyAt(i); |
| 3418 | final int policy = mUidPolicy.valueAt(i); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3419 | fout.print("UID="); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3420 | fout.print(uid); |
| 3421 | fout.print(" policy="); |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 3422 | fout.print(uidPoliciesToString(policy)); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3423 | fout.println(); |
| 3424 | } |
| 3425 | fout.decreaseIndent(); |
| 3426 | |
| 3427 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 3428 | if (size > 0) { |
| 3429 | fout.println("Power save whitelist (except idle) app ids:"); |
| 3430 | fout.increaseIndent(); |
| 3431 | for (int i = 0; i < size; i++) { |
| 3432 | fout.print("UID="); |
| 3433 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 3434 | fout.print(": "); |
| 3435 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 3436 | fout.println(); |
| 3437 | } |
| 3438 | fout.decreaseIndent(); |
| 3439 | } |
| 3440 | |
| 3441 | size = mPowerSaveWhitelistAppIds.size(); |
| 3442 | if (size > 0) { |
| 3443 | fout.println("Power save whitelist app ids:"); |
| 3444 | fout.increaseIndent(); |
| 3445 | for (int i = 0; i < size; i++) { |
| 3446 | fout.print("UID="); |
| 3447 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 3448 | fout.print(": "); |
| 3449 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 3450 | fout.println(); |
| 3451 | } |
| 3452 | fout.decreaseIndent(); |
| 3453 | } |
| 3454 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3455 | size = mAppIdleTempWhitelistAppIds.size(); |
| 3456 | if (size > 0) { |
| 3457 | fout.println("App idle whitelist app ids:"); |
| 3458 | fout.increaseIndent(); |
| 3459 | for (int i = 0; i < size; i++) { |
| 3460 | fout.print("UID="); |
| 3461 | fout.print(mAppIdleTempWhitelistAppIds.keyAt(i)); |
| 3462 | fout.print(": "); |
| 3463 | fout.print(mAppIdleTempWhitelistAppIds.valueAt(i)); |
| 3464 | fout.println(); |
| 3465 | } |
| 3466 | fout.decreaseIndent(); |
| 3467 | } |
| 3468 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3469 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 3470 | if (size > 0) { |
| 3471 | fout.println("Default restrict background whitelist uids:"); |
| 3472 | fout.increaseIndent(); |
| 3473 | for (int i = 0; i < size; i++) { |
| 3474 | fout.print("UID="); |
| 3475 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 3476 | fout.println(); |
| 3477 | } |
| 3478 | fout.decreaseIndent(); |
| 3479 | } |
| 3480 | |
| 3481 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 3482 | if (size > 0) { |
| 3483 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 3484 | fout.increaseIndent(); |
| 3485 | for (int i = 0; i < size; i++) { |
| 3486 | fout.print("UID="); |
| 3487 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 3488 | fout.println(); |
| 3489 | } |
| 3490 | fout.decreaseIndent(); |
| 3491 | } |
| 3492 | |
| 3493 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 3494 | collectKeys(mUidState, knownUids); |
| 3495 | collectKeys(mUidRules, knownUids); |
| 3496 | |
| 3497 | fout.println("Status for all known UIDs:"); |
| 3498 | fout.increaseIndent(); |
| 3499 | size = knownUids.size(); |
| 3500 | for (int i = 0; i < size; i++) { |
| 3501 | final int uid = knownUids.keyAt(i); |
| 3502 | fout.print("UID="); |
| 3503 | fout.print(uid); |
| 3504 | |
| 3505 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3506 | fout.print(" state="); |
| 3507 | fout.print(state); |
| 3508 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 3509 | fout.print(" (fg)"); |
| 3510 | } else { |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 3511 | fout.print(state <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3512 | ? " (fg svc)" : " (bg)"); |
| 3513 | } |
| 3514 | |
| 3515 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3516 | fout.print(" rules="); |
| 3517 | fout.print(uidRulesToString(uidRules)); |
| 3518 | fout.println(); |
| 3519 | } |
| 3520 | fout.decreaseIndent(); |
| 3521 | |
| 3522 | fout.println("Status for just UIDs with rules:"); |
| 3523 | fout.increaseIndent(); |
| 3524 | size = mUidRules.size(); |
| 3525 | for (int i = 0; i < size; i++) { |
| 3526 | final int uid = mUidRules.keyAt(i); |
| 3527 | fout.print("UID="); |
| 3528 | fout.print(uid); |
| 3529 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3530 | fout.print(" rules="); |
| 3531 | fout.print(uidRulesToString(uidRules)); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3532 | fout.println(); |
| 3533 | } |
| 3534 | fout.decreaseIndent(); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 3535 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 3536 | fout.println("Admin restricted uids for metered data:"); |
| 3537 | fout.increaseIndent(); |
| 3538 | size = mMeteredRestrictedUids.size(); |
| 3539 | for (int i = 0; i < size; ++i) { |
| 3540 | fout.print("u" + mMeteredRestrictedUids.keyAt(i) + ": "); |
| 3541 | fout.println(mMeteredRestrictedUids.valueAt(i)); |
| 3542 | } |
| 3543 | fout.decreaseIndent(); |
| 3544 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 3545 | fout.println(); |
| 3546 | mStatLogger.dump(fout); |
| 3547 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 3548 | mLogger.dumpLogs(fout); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3549 | } |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3550 | } |
| 3551 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3552 | |
| 3553 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3554 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3555 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 3556 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3557 | this, in, out, err, args, callback, resultReceiver); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3558 | } |
| 3559 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3560 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3561 | boolean isUidForeground(int uid) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3562 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3563 | return isUidStateForeground( |
| 3564 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3565 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3566 | } |
| 3567 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3568 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3569 | private boolean isUidForegroundOnRestrictBackgroundUL(int uid) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3570 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3571 | return isProcStateAllowedWhileOnRestrictBackground(procState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3572 | } |
| 3573 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3574 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3575 | private boolean isUidForegroundOnRestrictPowerUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3576 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3577 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 3578 | } |
| 3579 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3580 | private boolean isUidStateForeground(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3581 | // only really in foreground when screen is also on |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3582 | return state <= NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3585 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3586 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3587 | * {@link #updateRulesForDataUsageRestrictionsUL(int)} and |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3588 | * {@link #updateRulesForPowerRestrictionsUL(int)}. Returns true if the state was updated. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3589 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3590 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3591 | private boolean updateUidStateUL(int uid, int uidState) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3592 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL"); |
| 3593 | try { |
| 3594 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3595 | if (oldUidState != uidState) { |
| 3596 | // state changed, push updated rules |
| 3597 | mUidState.put(uid, uidState); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3598 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState); |
| 3599 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 3600 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
Sudheer Shanka | 9e77d23 | 2017-08-14 14:43:11 -0700 | [diff] [blame] | 3601 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3602 | if (mDeviceIdleMode) { |
| 3603 | updateRuleForDeviceIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3604 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3605 | if (mRestrictPower) { |
| 3606 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3607 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3608 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3609 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3610 | return true; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3611 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3612 | } finally { |
| 3613 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3614 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3615 | return false; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3618 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3619 | private boolean removeUidStateUL(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3620 | final int index = mUidState.indexOfKey(uid); |
| 3621 | if (index >= 0) { |
| 3622 | final int oldUidState = mUidState.valueAt(index); |
| 3623 | mUidState.removeAt(index); |
| 3624 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3625 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3626 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3627 | if (mDeviceIdleMode) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3628 | updateRuleForDeviceIdleUL(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3629 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3630 | if (mRestrictPower) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3631 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3632 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3633 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3634 | return true; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3635 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3636 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3637 | return false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3638 | } |
| 3639 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3640 | // adjust stats accounting based on foreground status |
| 3641 | private void updateNetworkStats(int uid, boolean uidForeground) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3642 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3643 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3644 | "updateNetworkStats: " + uid + "/" + (uidForeground ? "F" : "B")); |
| 3645 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3646 | try { |
| 3647 | mNetworkStats.setUidForeground(uid, uidForeground); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3648 | } finally { |
| 3649 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3650 | } |
| 3651 | } |
| 3652 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3653 | private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState, |
| 3654 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3655 | final boolean oldForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3656 | isProcStateAllowedWhileOnRestrictBackground(oldUidState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3657 | final boolean newForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3658 | isProcStateAllowedWhileOnRestrictBackground(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3659 | if (oldForeground != newForeground) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3660 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3661 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3664 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3665 | void updateRulesForPowerSaveUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3666 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL"); |
| 3667 | try { |
| 3668 | updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 3669 | mUidFirewallPowerSaveRules); |
| 3670 | } finally { |
| 3671 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3672 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3673 | } |
| 3674 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3675 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3676 | void updateRuleForRestrictPowerUL(int uid) { |
| 3677 | updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3678 | } |
| 3679 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3680 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3681 | void updateRulesForDeviceIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3682 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL"); |
| 3683 | try { |
| 3684 | updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 3685 | mUidFirewallDozableRules); |
| 3686 | } finally { |
| 3687 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3688 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3689 | } |
| 3690 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3691 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3692 | void updateRuleForDeviceIdleUL(int uid) { |
| 3693 | updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3694 | } |
| 3695 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3696 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3697 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3698 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3699 | private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain, |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3700 | SparseIntArray rules) { |
| 3701 | if (enabled) { |
| 3702 | // Sync the whitelists before enabling the chain. We don't care about the rules if |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3703 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3704 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3705 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3706 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3707 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3708 | UserInfo user = users.get(ui); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3709 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveTempWhitelistAppIds, user.id); |
| 3710 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveWhitelistAppIds, user.id); |
| 3711 | if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3712 | updateRulesForWhitelistedAppIds(uidRules, |
| 3713 | mPowerSaveWhitelistExceptIdleAppIds, user.id); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3714 | } |
| 3715 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3716 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3717 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3718 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 3719 | } |
| 3720 | } |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3721 | setUidFirewallRulesUL(chain, uidRules, CHAIN_TOGGLE_ENABLE); |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3722 | } else { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3723 | setUidFirewallRulesUL(chain, null, CHAIN_TOGGLE_DISABLE); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3724 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3725 | } |
| 3726 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3727 | private void updateRulesForWhitelistedAppIds(final SparseIntArray uidRules, |
| 3728 | final SparseBooleanArray whitelistedAppIds, int userId) { |
| 3729 | for (int i = whitelistedAppIds.size() - 1; i >= 0; --i) { |
| 3730 | if (whitelistedAppIds.valueAt(i)) { |
| 3731 | final int appId = whitelistedAppIds.keyAt(i); |
| 3732 | final int uid = UserHandle.getUid(userId, appId); |
| 3733 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | /** |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3739 | * Returns whether a uid is whitelisted from power saving restrictions (eg: Battery Saver, Doze |
| 3740 | * mode, and app idle). |
| 3741 | * |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3742 | * @param deviceIdleMode if true then we don't consider |
| 3743 | * {@link #mPowerSaveWhitelistExceptIdleAppIds} for checking if the {@param uid} is |
| 3744 | * whitelisted. |
| 3745 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3746 | @GuardedBy("mUidRulesFirstLock") |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3747 | private boolean isWhitelistedFromPowerSaveUL(int uid, boolean deviceIdleMode) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3748 | final int appId = UserHandle.getAppId(uid); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3749 | boolean isWhitelisted = mPowerSaveTempWhitelistAppIds.get(appId) |
| 3750 | || mPowerSaveWhitelistAppIds.get(appId); |
| 3751 | if (!deviceIdleMode) { |
| 3752 | isWhitelisted = isWhitelisted || mPowerSaveWhitelistExceptIdleAppIds.get(appId); |
| 3753 | } |
| 3754 | return isWhitelisted; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3757 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3758 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3759 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3760 | private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3761 | if (enabled) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3762 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3763 | chain == FIREWALL_CHAIN_DOZABLE); |
| 3764 | if (isWhitelisted || isUidForegroundOnRestrictPowerUL(uid)) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3765 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3766 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3767 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | } |
| 3771 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3772 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3773 | void updateRulesForAppIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3774 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL"); |
| 3775 | try { |
| 3776 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 3777 | uidRules.clear(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3778 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3779 | // Fully update the app idle firewall chain. |
| 3780 | final List<UserInfo> users = mUserManager.getUsers(); |
| 3781 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3782 | UserInfo user = users.get(ui); |
| 3783 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 3784 | for (int uid : idleUids) { |
| 3785 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
| 3786 | // quick check: if this uid doesn't have INTERNET permission, it |
| 3787 | // doesn't have network access anyway, so it is a waste to mess |
| 3788 | // with it here. |
| 3789 | if (hasInternetPermissions(uid)) { |
| 3790 | uidRules.put(uid, FIREWALL_RULE_DENY); |
| 3791 | } |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 3792 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3793 | } |
| 3794 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3795 | |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3796 | setUidFirewallRulesUL(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3797 | } finally { |
| 3798 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3799 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3802 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3803 | void updateRuleForAppIdleUL(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3804 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3805 | |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3806 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3807 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRuleForAppIdleUL: " + uid ); |
| 3808 | } |
| 3809 | try { |
| 3810 | int appId = UserHandle.getAppId(uid); |
| 3811 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 3812 | && !isUidForegroundOnRestrictPowerUL(uid)) { |
| 3813 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3814 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3815 | } else { |
| 3816 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3817 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL " + uid + " to DEFAULT"); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3818 | } |
| 3819 | } finally { |
| 3820 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3821 | } |
| 3822 | } |
| 3823 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3824 | /** |
| 3825 | * Toggle the firewall standby chain and inform listeners if the uid rules have effectively |
| 3826 | * changed. |
| 3827 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3828 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3829 | void updateRulesForAppIdleParoleUL() { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3830 | boolean paroled = mUsageStats.isAppIdleParoleOn(); |
| 3831 | boolean enableChain = !paroled; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3832 | enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, enableChain); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3833 | |
| 3834 | int ruleCount = mUidFirewallStandbyRules.size(); |
| 3835 | for (int i = 0; i < ruleCount; i++) { |
| 3836 | int uid = mUidFirewallStandbyRules.keyAt(i); |
| 3837 | int oldRules = mUidRules.get(uid); |
| 3838 | if (enableChain) { |
| 3839 | // Chain wasn't enabled before and the other power-related |
| 3840 | // chains are whitelists, so we can clear the |
| 3841 | // MASK_ALL_NETWORKS part of the rules and re-inform listeners if |
| 3842 | // the effective rules result in blocking network access. |
| 3843 | oldRules &= MASK_METERED_NETWORKS; |
| 3844 | } else { |
| 3845 | // Skip if it had no restrictions to begin with |
| 3846 | if ((oldRules & MASK_ALL_NETWORKS) == 0) continue; |
| 3847 | } |
Sudheer Shanka | 3af0294 | 2017-04-12 14:29:14 -0700 | [diff] [blame] | 3848 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldRules, paroled); |
| 3849 | if (newUidRules == RULE_NONE) { |
| 3850 | mUidRules.delete(uid); |
| 3851 | } else { |
| 3852 | mUidRules.put(uid, newUidRules); |
| 3853 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3854 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3857 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3858 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 3859 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3860 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3861 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3862 | private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3863 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3864 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3865 | "updateRulesForGlobalChangeAL: " + (restrictedNetworksChanged ? "R" : "-")); |
| 3866 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3867 | try { |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3868 | updateRulesForAppIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3869 | updateRulesForRestrictPowerUL(); |
| 3870 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3871 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3872 | // If the set of restricted networks may have changed, re-evaluate those. |
| 3873 | if (restrictedNetworksChanged) { |
| 3874 | normalizePoliciesNL(); |
| 3875 | updateNetworkRulesNL(); |
| 3876 | } |
| 3877 | } finally { |
| 3878 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3882 | // TODO: rename / document to make it clear these are global (not app-specific) rules |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3883 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3884 | private void updateRulesForRestrictPowerUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3885 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL"); |
| 3886 | try { |
| 3887 | updateRulesForDeviceIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3888 | updateRulesForPowerSaveUL(); |
| 3889 | updateRulesForAllAppsUL(TYPE_RESTRICT_POWER); |
| 3890 | } finally { |
| 3891 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3892 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3895 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3896 | private void updateRulesForRestrictBackgroundUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3897 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL"); |
| 3898 | try { |
| 3899 | updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND); |
| 3900 | } finally { |
| 3901 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3902 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 3906 | private static final int TYPE_RESTRICT_POWER = 2; |
| 3907 | @Retention(RetentionPolicy.SOURCE) |
| 3908 | @IntDef(flag = false, value = { |
| 3909 | TYPE_RESTRICT_BACKGROUND, |
| 3910 | TYPE_RESTRICT_POWER, |
| 3911 | }) |
| 3912 | public @interface RestrictType { |
| 3913 | } |
| 3914 | |
| 3915 | // TODO: refactor / consolidate all those updateXyz methods, there are way too many of them... |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3916 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3917 | private void updateRulesForAllAppsUL(@RestrictType int type) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3918 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3919 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type); |
| 3920 | } |
| 3921 | try { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3922 | // update rules for all installed applications |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3923 | |
| 3924 | final PackageManager pm = mContext.getPackageManager(); |
| 3925 | final List<UserInfo> users; |
| 3926 | final List<ApplicationInfo> apps; |
| 3927 | |
| 3928 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-users"); |
| 3929 | try { |
| 3930 | users = mUserManager.getUsers(); |
| 3931 | } finally { |
| 3932 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3933 | } |
| 3934 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-uids"); |
| 3935 | try { |
| 3936 | apps = pm.getInstalledApplications( |
| 3937 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS |
| 3938 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 3939 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 3940 | } finally { |
| 3941 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3942 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3943 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3944 | final int usersSize = users.size(); |
| 3945 | final int appsSize = apps.size(); |
| 3946 | for (int i = 0; i < usersSize; i++) { |
| 3947 | final UserInfo user = users.get(i); |
| 3948 | for (int j = 0; j < appsSize; j++) { |
| 3949 | final ApplicationInfo app = apps.get(j); |
| 3950 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 3951 | switch (type) { |
| 3952 | case TYPE_RESTRICT_BACKGROUND: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3953 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3954 | break; |
| 3955 | case TYPE_RESTRICT_POWER: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3956 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3957 | break; |
| 3958 | default: |
| 3959 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 3960 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3961 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3962 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3963 | } finally { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3964 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3965 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3966 | } |
| 3967 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3968 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3969 | private void updateRulesForTempWhitelistChangeUL(int appId) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3970 | final List<UserInfo> users = mUserManager.getUsers(); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3971 | final int numUsers = users.size(); |
| 3972 | for (int i = 0; i < numUsers; i++) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3973 | final UserInfo user = users.get(i); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3974 | int uid = UserHandle.getUid(user.id, appId); |
| 3975 | // Update external firewall rules. |
| 3976 | updateRuleForAppIdleUL(uid); |
| 3977 | updateRuleForDeviceIdleUL(uid); |
| 3978 | updateRuleForRestrictPowerUL(uid); |
| 3979 | // Update internal rules. |
| 3980 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3984 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 3985 | // methods below could be merged into a isUidValidForRules() method. |
| 3986 | private boolean isUidValidForBlacklistRules(int uid) { |
| 3987 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3988 | if (uid == android.os.Process.MEDIA_UID || uid == android.os.Process.DRM_UID |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3989 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3990 | return true; |
| 3991 | } |
| 3992 | |
| 3993 | return false; |
| 3994 | } |
| 3995 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3996 | private boolean isUidValidForWhitelistRules(int uid) { |
| 3997 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 3998 | } |
| 3999 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4000 | /** |
| 4001 | * Set whether or not an app should be whitelisted for network access while in app idle. Other |
| 4002 | * power saving restrictions may still apply. |
| 4003 | */ |
| 4004 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4005 | void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
Sudheer Shanka | db02ccd | 2018-11-29 11:27:21 -0800 | [diff] [blame] | 4006 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 4007 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4008 | synchronized (mUidRulesFirstLock) { |
| 4009 | if (mAppIdleTempWhitelistAppIds.get(uid) == shouldWhitelist) { |
| 4010 | // No change. |
| 4011 | return; |
| 4012 | } |
| 4013 | |
| 4014 | final long token = Binder.clearCallingIdentity(); |
| 4015 | try { |
| 4016 | mLogger.appIdleWlChanged(uid, shouldWhitelist); |
| 4017 | if (shouldWhitelist) { |
| 4018 | mAppIdleTempWhitelistAppIds.put(uid, true); |
| 4019 | } else { |
| 4020 | mAppIdleTempWhitelistAppIds.delete(uid); |
| 4021 | } |
| 4022 | updateRuleForAppIdleUL(uid); |
| 4023 | updateRulesForPowerRestrictionsUL(uid); |
| 4024 | } finally { |
| 4025 | Binder.restoreCallingIdentity(token); |
| 4026 | } |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | /** Return the list of UIDs currently in the app idle whitelist. */ |
| 4031 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4032 | int[] getAppIdleWhitelist() { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4033 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 4034 | |
| 4035 | synchronized (mUidRulesFirstLock) { |
| 4036 | final int len = mAppIdleTempWhitelistAppIds.size(); |
| 4037 | int[] uids = new int[len]; |
| 4038 | for (int i = 0; i < len; ++i) { |
| 4039 | uids[i] = mAppIdleTempWhitelistAppIds.keyAt(i); |
| 4040 | } |
| 4041 | return uids; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | /** Returns if the UID is currently considered idle. */ |
| 4046 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4047 | boolean isUidIdle(int uid) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4048 | synchronized (mUidRulesFirstLock) { |
| 4049 | if (mAppIdleTempWhitelistAppIds.get(uid)) { |
| 4050 | // UID is temporarily whitelisted. |
| 4051 | return false; |
| 4052 | } |
| 4053 | } |
| 4054 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4055 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 4056 | final int userId = UserHandle.getUserId(uid); |
| 4057 | |
songjinshi | 0655edd | 2016-05-18 19:55:32 +0800 | [diff] [blame] | 4058 | if (packages != null) { |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 4059 | for (String packageName : packages) { |
| 4060 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 4061 | return false; |
| 4062 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | return true; |
| 4066 | } |
| 4067 | |
| 4068 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4069 | * Checks if an uid has INTERNET permissions. |
| 4070 | * <p> |
| 4071 | * Useful for the cases where the lack of network access can simplify the rules. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4072 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4073 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4074 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 4075 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4076 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4077 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4078 | } |
| 4079 | } catch (RemoteException e) { |
| 4080 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4081 | return true; |
| 4082 | } |
| 4083 | |
| 4084 | /** |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4085 | * Clears all state - internal and external - associated with an UID. |
| 4086 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4087 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4088 | private void onUidDeletedUL(int uid) { |
| 4089 | // First cleanup in-memory state synchronously... |
| 4090 | mUidRules.delete(uid); |
| 4091 | mUidPolicy.delete(uid); |
| 4092 | mUidFirewallStandbyRules.delete(uid); |
| 4093 | mUidFirewallDozableRules.delete(uid); |
| 4094 | mUidFirewallPowerSaveRules.delete(uid); |
| 4095 | mPowerSaveWhitelistExceptIdleAppIds.delete(uid); |
| 4096 | mPowerSaveWhitelistAppIds.delete(uid); |
| 4097 | mPowerSaveTempWhitelistAppIds.delete(uid); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4098 | mAppIdleTempWhitelistAppIds.delete(uid); |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4099 | |
| 4100 | // ...then update iptables asynchronously. |
| 4101 | mHandler.obtainMessage(MSG_RESET_FIREWALL_RULES_BY_UID, uid, 0).sendToTarget(); |
| 4102 | } |
| 4103 | |
| 4104 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4105 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4106 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4107 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4108 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4109 | * <li>Doze mode |
| 4110 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4111 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4112 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4113 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4114 | * |
| 4115 | * <p>This method changes both the external firewall rules and the internal state. |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4116 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4117 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4118 | private void updateRestrictionRulesForUidUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4119 | // Methods below only changes the firewall rules for the power-related modes. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4120 | updateRuleForDeviceIdleUL(uid); |
| 4121 | updateRuleForAppIdleUL(uid); |
| 4122 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4123 | |
| 4124 | // Update internal state for power-related modes. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4125 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4126 | |
| 4127 | // Update firewall and internal rules for Data Saver Mode. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4128 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4131 | /** |
| 4132 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 4133 | * restrictions (blacklist / whitelist). |
| 4134 | * |
| 4135 | * <p> |
| 4136 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 4137 | * networks: |
| 4138 | * <ul> |
| 4139 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 4140 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 4141 | * also blacklisted. |
| 4142 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4143 | * no UIDs other than those whitelisted will have access. |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4144 | * <ul> |
| 4145 | * |
| 4146 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 4147 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 4148 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 4149 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4150 | * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4151 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 4152 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 4153 | * following rules should also be applied: |
| 4154 | * |
| 4155 | * <ul> |
| 4156 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 4157 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 4158 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 4159 | * {@code bw_penalty_box}. |
| 4160 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 4161 | * </ul> |
| 4162 | * |
| 4163 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 4164 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 4165 | * have permission to use the internet. |
| 4166 | * |
| 4167 | * <p>The {@link #mUidRules} map is used to define the transtion of states of an UID. |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4168 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4169 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4170 | private void updateRulesForDataUsageRestrictionsUL(int uid) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4171 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4172 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4173 | "updateRulesForDataUsageRestrictionsUL: " + uid); |
| 4174 | } |
| 4175 | try { |
| 4176 | updateRulesForDataUsageRestrictionsULInner(uid); |
| 4177 | } finally { |
| 4178 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | private void updateRulesForDataUsageRestrictionsULInner(int uid) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4183 | if (!isUidValidForWhitelistRules(uid)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4184 | if (LOGD) Slog.d(TAG, "no need to update restrict data rules for uid " + uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4185 | return; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4186 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4187 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 4188 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4189 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4190 | final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid); |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4191 | final boolean isRestrictedByAdmin = isRestrictedByAdminUL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 4192 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4193 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 4194 | final boolean isWhitelisted = (uidPolicy & POLICY_ALLOW_METERED_BACKGROUND) != 0; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4195 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 4196 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4197 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4198 | // First step: define the new rule based on user restrictions and foreground state. |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4199 | if (isRestrictedByAdmin) { |
| 4200 | newRule = RULE_REJECT_METERED; |
| 4201 | } else if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4202 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 4203 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 4204 | } else if (isWhitelisted) { |
| 4205 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4206 | } |
| 4207 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4208 | if (isBlacklisted) { |
| 4209 | newRule = RULE_REJECT_METERED; |
| 4210 | } else if (mRestrictBackground && isWhitelisted) { |
| 4211 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4212 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4213 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4214 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4215 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4216 | if (LOGV) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4217 | Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4218 | + ": isForeground=" +isForeground |
| 4219 | + ", isBlacklisted=" + isBlacklisted |
| 4220 | + ", isWhitelisted=" + isWhitelisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4221 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4222 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4223 | + ", newRule=" + uidRulesToString(newRule) |
| 4224 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4225 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4226 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4227 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4228 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4229 | mUidRules.delete(uid); |
| 4230 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4231 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4232 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4233 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4234 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4235 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4236 | if (hasRule(newRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4237 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 4238 | // (since bw_penalty_box prevails over bw_happy_box). |
| 4239 | |
| 4240 | setMeteredNetworkWhitelist(uid, true); |
| 4241 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 4242 | // but ideally it should be just: |
| 4243 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4244 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4245 | setMeteredNetworkBlacklist(uid, false); |
| 4246 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4247 | } else if (hasRule(oldRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4248 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 4249 | |
| 4250 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 4251 | // but ideally they should be just: |
| 4252 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 4253 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4254 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4255 | setMeteredNetworkWhitelist(uid, false); |
| 4256 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4257 | if (isBlacklisted || isRestrictedByAdmin) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4258 | setMeteredNetworkBlacklist(uid, true); |
| 4259 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4260 | } else if (hasRule(newRule, RULE_REJECT_METERED) |
| 4261 | || hasRule(oldRule, RULE_REJECT_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4262 | // Flip state because app was explicitly added or removed to blacklist. |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4263 | setMeteredNetworkBlacklist(uid, (isBlacklisted || isRestrictedByAdmin)); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4264 | if (hasRule(oldRule, RULE_REJECT_METERED) && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4265 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 4266 | // where app is whitelisted and blacklisted at the same time (although such |
| 4267 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4268 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4269 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4270 | } else if (hasRule(newRule, RULE_ALLOW_METERED) |
| 4271 | || hasRule(oldRule, RULE_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4272 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4273 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4274 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4275 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4276 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 4277 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4278 | + ", whitelisted=" + isWhitelisted |
| 4279 | + ", blacklisted=" + isBlacklisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4280 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4281 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4282 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4283 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4284 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4285 | // Dispatch changed rule to existing listeners. |
| 4286 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 4292 | * listeners in case of change. |
| 4293 | * <p> |
| 4294 | * There are 3 power-related rules that affects whether an app has background access on |
| 4295 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 4296 | * restriction, it's added to the equivalent firewall chain: |
| 4297 | * <ul> |
| 4298 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 4299 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 4300 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 4301 | * </ul> |
| 4302 | * <p> |
| 4303 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 4304 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 4305 | * <p> |
| 4306 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 4307 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4308 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4309 | private void updateRulesForPowerRestrictionsUL(int uid) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4310 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 4311 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4312 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules, false); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4313 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4314 | if (newUidRules == RULE_NONE) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4315 | mUidRules.delete(uid); |
| 4316 | } else { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4317 | mUidRules.put(uid, newUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | |
| 4321 | /** |
| 4322 | * Similar to above but ignores idle state if app standby is currently disabled by parole. |
| 4323 | * |
| 4324 | * @param uid the uid of the app to update rules for |
| 4325 | * @param oldUidRules the current rules for the uid, in order to determine if there's a change |
| 4326 | * @param paroled whether to ignore idle state of apps and only look at other restrictions. |
| 4327 | * |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4328 | * @return the new computed rules for the uid |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4329 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4330 | private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules, boolean paroled) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4331 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4332 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4333 | "updateRulesForPowerRestrictionsUL: " + uid + "/" + oldUidRules + "/" |
| 4334 | + (paroled ? "P" : "-")); |
| 4335 | } |
| 4336 | try { |
| 4337 | return updateRulesForPowerRestrictionsULInner(uid, oldUidRules, paroled); |
| 4338 | } finally { |
| 4339 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | private int updateRulesForPowerRestrictionsULInner(int uid, int oldUidRules, boolean paroled) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4344 | if (!isUidValidForBlacklistRules(uid)) { |
| 4345 | if (LOGD) Slog.d(TAG, "no need to update restrict power rules for uid " + uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4346 | return RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4347 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4348 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4349 | final boolean isIdle = !paroled && isUidIdle(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4350 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4351 | final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4352 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4353 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, mDeviceIdleMode); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4354 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 4355 | int newRule = RULE_NONE; |
| 4356 | |
| 4357 | // First step: define the new rule based on user restrictions and foreground state. |
| 4358 | |
| 4359 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 4360 | // by considering the foreground and non-foreground states. |
| 4361 | if (isForeground) { |
| 4362 | if (restrictMode) { |
| 4363 | newRule = RULE_ALLOW_ALL; |
| 4364 | } |
| 4365 | } else if (restrictMode) { |
| 4366 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 4367 | } |
| 4368 | |
| 4369 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 4370 | |
| 4371 | if (LOGV) { |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 4372 | Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4373 | + ", isIdle: " + isIdle |
| 4374 | + ", mRestrictPower: " + mRestrictPower |
| 4375 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 4376 | + ", isForeground=" + isForeground |
| 4377 | + ", isWhitelisted=" + isWhitelisted |
| 4378 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4379 | + ", newRule=" + uidRulesToString(newRule) |
| 4380 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4381 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 4382 | } |
| 4383 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4384 | // Second step: notify listeners if state changed. |
| 4385 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4386 | if (newRule == RULE_NONE || hasRule(newRule, RULE_ALLOW_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4387 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4388 | } else if (hasRule(newRule, RULE_REJECT_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4389 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 4390 | } else { |
| 4391 | // All scenarios should have been covered above |
| 4392 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 4393 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4394 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4395 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4396 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 4397 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4398 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4399 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4400 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4401 | return newUidRules; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4404 | private class AppIdleStateChangeListener |
| 4405 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 4406 | |
| 4407 | @Override |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 4408 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, int bucket, |
| 4409 | int reason) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4410 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 4411 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 4412 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4413 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4414 | mLogger.appIdleStateChanged(uid, idle); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4415 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4416 | updateRulesForPowerRestrictionsUL(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4417 | } |
| 4418 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4419 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | @Override |
| 4423 | public void onParoleStateChanged(boolean isParoleOn) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4424 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4425 | mLogger.paroleStateChanged(isParoleOn); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4426 | updateRulesForAppIdleParoleUL(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4427 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4428 | } |
| 4429 | } |
| 4430 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4431 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 4432 | if (listener != null) { |
| 4433 | try { |
| 4434 | listener.onUidRulesChanged(uid, uidRules); |
| 4435 | } catch (RemoteException ignored) { |
| 4436 | } |
| 4437 | } |
| 4438 | } |
| 4439 | |
| 4440 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 4441 | String[] meteredIfaces) { |
| 4442 | if (listener != null) { |
| 4443 | try { |
| 4444 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 4445 | } catch (RemoteException ignored) { |
| 4446 | } |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 4451 | boolean restrictBackground) { |
| 4452 | if (listener != null) { |
| 4453 | try { |
| 4454 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 4455 | } catch (RemoteException ignored) { |
| 4456 | } |
| 4457 | } |
| 4458 | } |
| 4459 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4460 | private void dispatchUidPoliciesChanged(INetworkPolicyListener listener, int uid, |
| 4461 | int uidPolicies) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4462 | if (listener != null) { |
| 4463 | try { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4464 | listener.onUidPoliciesChanged(uid, uidPolicies); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4465 | } catch (RemoteException ignored) { |
| 4466 | } |
| 4467 | } |
| 4468 | } |
| 4469 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4470 | private void dispatchSubscriptionOverride(INetworkPolicyListener listener, int subId, |
| 4471 | int overrideMask, int overrideValue) { |
| 4472 | if (listener != null) { |
| 4473 | try { |
| 4474 | listener.onSubscriptionOverride(subId, overrideMask, overrideValue); |
| 4475 | } catch (RemoteException ignored) { |
| 4476 | } |
| 4477 | } |
| 4478 | } |
| 4479 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4480 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 4481 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4482 | public boolean handleMessage(Message msg) { |
| 4483 | switch (msg.what) { |
| 4484 | case MSG_RULES_CHANGED: { |
| 4485 | final int uid = msg.arg1; |
| 4486 | final int uidRules = msg.arg2; |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4487 | final int length = mListeners.beginBroadcast(); |
| 4488 | for (int i = 0; i < length; i++) { |
| 4489 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4490 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4491 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4492 | mListeners.finishBroadcast(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4493 | return true; |
| 4494 | } |
| 4495 | case MSG_METERED_IFACES_CHANGED: { |
| 4496 | final String[] meteredIfaces = (String[]) msg.obj; |
| 4497 | final int length = mListeners.beginBroadcast(); |
| 4498 | for (int i = 0; i < length; i++) { |
| 4499 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4500 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4501 | } |
| 4502 | mListeners.finishBroadcast(); |
| 4503 | return true; |
| 4504 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4505 | case MSG_LIMIT_REACHED: { |
| 4506 | final String iface = (String) msg.obj; |
| 4507 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4508 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4509 | if (mMeteredIfaces.contains(iface)) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4510 | // force stats update to make sure we have |
| 4511 | // numbers that caused alert to trigger. |
| 4512 | mNetworkStats.forceUpdate(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4513 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4514 | updateNetworkEnabledNL(); |
| 4515 | updateNotificationsNL(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4516 | } |
| 4517 | } |
| 4518 | return true; |
| 4519 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4520 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 4521 | final boolean restrictBackground = msg.arg1 != 0; |
| 4522 | final int length = mListeners.beginBroadcast(); |
| 4523 | for (int i = 0; i < length; i++) { |
| 4524 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4525 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4526 | } |
| 4527 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4528 | final Intent intent = |
| 4529 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4530 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4531 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 4532 | return true; |
| 4533 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4534 | case MSG_POLICIES_CHANGED: { |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4535 | final int uid = msg.arg1; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4536 | final int policy = msg.arg2; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4537 | final Boolean notifyApp = (Boolean) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4538 | // First notify internal listeners... |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4539 | final int length = mListeners.beginBroadcast(); |
| 4540 | for (int i = 0; i < length; i++) { |
| 4541 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4542 | dispatchUidPoliciesChanged(listener, uid, policy); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4543 | } |
| 4544 | mListeners.finishBroadcast(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4545 | // ...then apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
| 4546 | if (notifyApp.booleanValue()) { |
| 4547 | broadcastRestrictBackgroundChanged(uid, notifyApp); |
| 4548 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4549 | return true; |
| 4550 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4551 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 4552 | final long lowestRule = (Long) msg.obj; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4553 | // make sure stats are recorded frequently enough; we aim |
| 4554 | // for 2MB threshold for 2GB/month rules. |
| 4555 | final long persistThreshold = lowestRule / 1000; |
| 4556 | mNetworkStats.advisePersistThreshold(persistThreshold); |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4557 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4558 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 4559 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 4560 | removeInterfaceQuota((String) msg.obj); |
| 4561 | // int params need to be stitched back into a long |
| 4562 | setInterfaceQuota((String) msg.obj, |
| 4563 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 4564 | return true; |
| 4565 | } |
| 4566 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 4567 | removeInterfaceQuota((String) msg.obj); |
| 4568 | return true; |
| 4569 | } |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4570 | case MSG_RESET_FIREWALL_RULES_BY_UID: { |
| 4571 | resetUidFirewallRules(msg.arg1); |
| 4572 | return true; |
| 4573 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4574 | case MSG_SUBSCRIPTION_OVERRIDE: { |
| 4575 | final int overrideMask = msg.arg1; |
| 4576 | final int overrideValue = msg.arg2; |
| 4577 | final int subId = (int) msg.obj; |
| 4578 | final int length = mListeners.beginBroadcast(); |
| 4579 | for (int i = 0; i < length; i++) { |
| 4580 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4581 | dispatchSubscriptionOverride(listener, subId, overrideMask, overrideValue); |
| 4582 | } |
| 4583 | mListeners.finishBroadcast(); |
| 4584 | return true; |
| 4585 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4586 | case MSG_METERED_RESTRICTED_PACKAGES_CHANGED: { |
| 4587 | final int userId = msg.arg1; |
| 4588 | final Set<String> packageNames = (Set<String>) msg.obj; |
| 4589 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 4590 | return true; |
| 4591 | } |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 4592 | case MSG_SET_NETWORK_TEMPLATE_ENABLED: { |
| 4593 | final NetworkTemplate template = (NetworkTemplate) msg.obj; |
| 4594 | final boolean enabled = msg.arg1 != 0; |
| 4595 | setNetworkTemplateEnabledInner(template, enabled); |
| 4596 | return true; |
| 4597 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4598 | default: { |
| 4599 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4603 | }; |
| 4604 | |
| 4605 | private final Handler.Callback mUidEventHandlerCallback = new Handler.Callback() { |
| 4606 | @Override |
| 4607 | public boolean handleMessage(Message msg) { |
| 4608 | switch (msg.what) { |
| 4609 | case UID_MSG_STATE_CHANGED: { |
| 4610 | final int uid = msg.arg1; |
| 4611 | final int procState = msg.arg2; |
| 4612 | final long procStateSeq = (Long) msg.obj; |
| 4613 | |
| 4614 | handleUidChanged(uid, procState, procStateSeq); |
| 4615 | return true; |
| 4616 | } |
| 4617 | case UID_MSG_GONE: { |
| 4618 | final int uid = msg.arg1; |
| 4619 | handleUidGone(uid); |
| 4620 | return true; |
| 4621 | } |
| 4622 | default: { |
| 4623 | return false; |
| 4624 | } |
| 4625 | } |
| 4626 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4627 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 4628 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4629 | void handleUidChanged(int uid, int procState, long procStateSeq) { |
| 4630 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged"); |
| 4631 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4632 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4633 | synchronized (mUidRulesFirstLock) { |
| 4634 | // We received a uid state change callback, add it to the history so that it |
| 4635 | // will be useful for debugging. |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4636 | mLogger.uidStateChanged(uid, procState, procStateSeq); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4637 | // Now update the network policy rules as per the updated uid state. |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4638 | updated = updateUidStateUL(uid, procState); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4639 | // Updating the network rules is done, so notify AMS about this. |
| 4640 | mActivityManagerInternal.notifyNetworkPolicyRulesUpdated(uid, procStateSeq); |
| 4641 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4642 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4643 | // called from the handler, so there's no multi-threading issue. |
| 4644 | if (updated) { |
| 4645 | updateNetworkStats(uid, isUidStateForeground(procState)); |
| 4646 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4647 | } finally { |
| 4648 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | void handleUidGone(int uid) { |
| 4653 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidGone"); |
| 4654 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4655 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4656 | synchronized (mUidRulesFirstLock) { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4657 | updated = removeUidStateUL(uid); |
| 4658 | } |
| 4659 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4660 | // called from the handler, so there's no multi-threading issue. |
| 4661 | if (updated) { |
| 4662 | updateNetworkStats(uid, false); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4663 | } |
| 4664 | } finally { |
| 4665 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4666 | } |
| 4667 | } |
| 4668 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4669 | private void broadcastRestrictBackgroundChanged(int uid, Boolean changed) { |
| 4670 | final PackageManager pm = mContext.getPackageManager(); |
| 4671 | final String[] packages = pm.getPackagesForUid(uid); |
| 4672 | if (packages != null) { |
| 4673 | final int userId = UserHandle.getUserId(uid); |
| 4674 | for (String packageName : packages) { |
| 4675 | final Intent intent = |
| 4676 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4677 | intent.setPackage(packageName); |
| 4678 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4679 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 4680 | } |
| 4681 | } |
| 4682 | } |
| 4683 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4684 | private void setInterfaceQuotaAsync(String iface, long quotaBytes) { |
| 4685 | // long quotaBytes split up into two ints to fit in message |
| 4686 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, (int) (quotaBytes >> 32), |
| 4687 | (int) (quotaBytes & 0xFFFFFFFF), iface).sendToTarget(); |
| 4688 | } |
| 4689 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4690 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4691 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4692 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4693 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4694 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4695 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4696 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4697 | } |
| 4698 | } |
| 4699 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4700 | private void removeInterfaceQuotaAsync(String iface) { |
| 4701 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface).sendToTarget(); |
| 4702 | } |
| 4703 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4704 | private void removeInterfaceQuota(String iface) { |
| 4705 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4706 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4707 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4708 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4709 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4710 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4711 | } |
| 4712 | } |
| 4713 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4714 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 4715 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4716 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4717 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4718 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4719 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 4720 | } catch (RemoteException e) { |
| 4721 | // ignored; service lives in system_server |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 4726 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 4727 | try { |
| 4728 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 4729 | } catch (IllegalStateException e) { |
| 4730 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4731 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4732 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4736 | private static final int CHAIN_TOGGLE_NONE = 0; |
| 4737 | private static final int CHAIN_TOGGLE_ENABLE = 1; |
| 4738 | private static final int CHAIN_TOGGLE_DISABLE = 2; |
| 4739 | @Retention(RetentionPolicy.SOURCE) |
| 4740 | @IntDef(flag = false, value = { |
| 4741 | CHAIN_TOGGLE_NONE, |
| 4742 | CHAIN_TOGGLE_ENABLE, |
| 4743 | CHAIN_TOGGLE_DISABLE |
| 4744 | }) |
| 4745 | public @interface ChainToggleType { |
| 4746 | } |
| 4747 | |
| 4748 | /** |
| 4749 | * Calls {@link #setUidFirewallRules(int, SparseIntArray)} and |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4750 | * {@link #enableFirewallChainUL(int, boolean)} synchronously. |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4751 | * |
| 4752 | * @param chain firewall chain. |
| 4753 | * @param uidRules new UID rules; if {@code null}, only toggles chain state. |
| 4754 | * @param toggle whether the chain should be enabled, disabled, or not changed. |
| 4755 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4756 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4757 | private void setUidFirewallRulesUL(int chain, @Nullable SparseIntArray uidRules, |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4758 | @ChainToggleType int toggle) { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4759 | if (uidRules != null) { |
| 4760 | setUidFirewallRulesUL(chain, uidRules); |
| 4761 | } |
| 4762 | if (toggle != CHAIN_TOGGLE_NONE) { |
| 4763 | enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE); |
| 4764 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4767 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4768 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 4769 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 4770 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4771 | */ |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4772 | private void setUidFirewallRulesUL(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4773 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4774 | int size = uidRules.size(); |
| 4775 | int[] uids = new int[size]; |
| 4776 | int[] rules = new int[size]; |
| 4777 | for(int index = size - 1; index >= 0; --index) { |
| 4778 | uids[index] = uidRules.keyAt(index); |
| 4779 | rules[index] = uidRules.valueAt(index); |
| 4780 | } |
| 4781 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4782 | mLogger.firewallRulesChanged(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4783 | } catch (IllegalStateException e) { |
| 4784 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4785 | } catch (RemoteException e) { |
| 4786 | // ignored; service lives in system_server |
| 4787 | } |
| 4788 | } |
| 4789 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4790 | /** |
| 4791 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4792 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4793 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4794 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4795 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4796 | "setUidFirewallRule: " + chain + "/" + uid + "/" + rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 4797 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4798 | try { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4799 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 4800 | mUidFirewallDozableRules.put(uid, rule); |
| 4801 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 4802 | mUidFirewallStandbyRules.put(uid, rule); |
| 4803 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 4804 | mUidFirewallPowerSaveRules.put(uid, rule); |
| 4805 | } |
| 4806 | |
| 4807 | try { |
| 4808 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4809 | mLogger.uidFirewallRuleChanged(chain, uid, rule); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4810 | } catch (IllegalStateException e) { |
| 4811 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4812 | } catch (RemoteException e) { |
| 4813 | // ignored; service lives in system_server |
| 4814 | } |
| 4815 | } finally { |
| 4816 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | /** |
| 4821 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4822 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4823 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4824 | private void enableFirewallChainUL(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4825 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 4826 | mFirewallChainStates.get(chain) == enable) { |
| 4827 | // All is the same, nothing to do. |
| 4828 | return; |
| 4829 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4830 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4831 | try { |
| 4832 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4833 | mLogger.firewallChainEnabled(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4834 | } catch (IllegalStateException e) { |
| 4835 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 4836 | } catch (RemoteException e) { |
| 4837 | // ignored; service lives in system_server |
| 4838 | } |
| 4839 | } |
| 4840 | |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4841 | /** |
| 4842 | * Resets all firewall rules associated with an UID. |
| 4843 | */ |
| 4844 | private void resetUidFirewallRules(int uid) { |
| 4845 | try { |
| 4846 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_DOZABLE, uid, FIREWALL_RULE_DEFAULT); |
| 4847 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 4848 | mNetworkManager |
| 4849 | .setFirewallUidRule(FIREWALL_CHAIN_POWERSAVE, uid, FIREWALL_RULE_DEFAULT); |
| 4850 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, false); |
| 4851 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, false); |
| 4852 | } catch (IllegalStateException e) { |
| 4853 | Log.wtf(TAG, "problem resetting firewall uid rules for " + uid, e); |
| 4854 | } catch (RemoteException e) { |
| 4855 | // ignored; service lives in system_server |
| 4856 | } |
| 4857 | } |
| 4858 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4859 | @Deprecated |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4860 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4861 | return getNetworkTotalBytes(template, start, end); |
| 4862 | } |
| 4863 | |
| 4864 | private long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4865 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 4866 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4867 | } catch (RuntimeException e) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4868 | Slog.w(TAG, "Failed to read network stats: " + e); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4869 | return 0; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | |
| 4873 | private NetworkStats getNetworkUidBytes(NetworkTemplate template, long start, long end) { |
| 4874 | try { |
| 4875 | return mNetworkStats.getNetworkUidBytes(template, start, end); |
| 4876 | } catch (RuntimeException e) { |
| 4877 | Slog.w(TAG, "Failed to read network stats: " + e); |
| 4878 | return new NetworkStats(SystemClock.elapsedRealtime(), 0); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4879 | } |
| 4880 | } |
| 4881 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 4882 | private boolean isBandwidthControlEnabled() { |
| 4883 | final long token = Binder.clearCallingIdentity(); |
| 4884 | try { |
| 4885 | return mNetworkManager.isBandwidthControlEnabled(); |
| 4886 | } catch (RemoteException e) { |
| 4887 | // ignored; service lives in system_server |
| 4888 | return false; |
| 4889 | } finally { |
| 4890 | Binder.restoreCallingIdentity(token); |
| 4891 | } |
| 4892 | } |
| 4893 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 4894 | private static Intent buildAllowBackgroundDataIntent() { |
| 4895 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 4896 | } |
| 4897 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4898 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 4899 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 4900 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 4901 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4902 | return intent; |
| 4903 | } |
| 4904 | |
| 4905 | private static Intent buildSnoozeRapidIntent(NetworkTemplate template) { |
| 4906 | final Intent intent = new Intent(ACTION_SNOOZE_RAPID); |
| 4907 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4908 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4909 | return intent; |
| 4910 | } |
| 4911 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4912 | private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4913 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4914 | intent.setComponent(ComponentName.unflattenFromString( |
| 4915 | res.getString(R.string.config_networkOverLimitComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4916 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4917 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4918 | return intent; |
| 4919 | } |
| 4920 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4921 | private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4922 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4923 | intent.setComponent(ComponentName.unflattenFromString( |
| 4924 | res.getString(R.string.config_dataUsageSummaryComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4925 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4926 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4927 | return intent; |
| 4928 | } |
| 4929 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 4930 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4931 | void addIdleHandler(IdleHandler handler) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 4932 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 4933 | } |
| 4934 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4935 | @GuardedBy("mUidRulesFirstLock") |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4936 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4937 | void updateRestrictBackgroundByLowPowerModeUL(final PowerSaveState result) { |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4938 | mRestrictBackgroundPowerState = result; |
| 4939 | |
| 4940 | boolean restrictBackground = result.batterySaverEnabled; |
| 4941 | boolean shouldInvokeRestrictBackground; |
| 4942 | // store the temporary mRestrictBackgroundChangedInBsm and update it at last |
| 4943 | boolean localRestrictBgChangedInBsm = mRestrictBackgroundChangedInBsm; |
| 4944 | |
| 4945 | if (result.globalBatterySaverEnabled) { |
| 4946 | // Try to turn on restrictBackground if (1) it is off and (2) batter saver need to |
| 4947 | // turn it on. |
| 4948 | shouldInvokeRestrictBackground = !mRestrictBackground && result.batterySaverEnabled; |
| 4949 | mRestrictBackgroundBeforeBsm = mRestrictBackground; |
| 4950 | localRestrictBgChangedInBsm = false; |
| 4951 | } else { |
| 4952 | // Try to restore the restrictBackground if it doesn't change in bsm |
| 4953 | shouldInvokeRestrictBackground = !mRestrictBackgroundChangedInBsm; |
| 4954 | restrictBackground = mRestrictBackgroundBeforeBsm; |
| 4955 | } |
| 4956 | |
| 4957 | if (shouldInvokeRestrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 4958 | setRestrictBackgroundUL(restrictBackground); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | // Change it at last so setRestrictBackground() won't affect this variable |
| 4962 | mRestrictBackgroundChangedInBsm = localRestrictBgChangedInBsm; |
| 4963 | } |
| 4964 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 4965 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 4966 | final int size = source.size(); |
| 4967 | for (int i = 0; i < size; i++) { |
| 4968 | target.put(source.keyAt(i), true); |
| 4969 | } |
| 4970 | } |
| 4971 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4972 | @Override |
| 4973 | public void factoryReset(String subscriber) { |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 4974 | mContext.enforceCallingOrSelfPermission(NETWORK_SETTINGS, TAG); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4975 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4976 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 4977 | return; |
| 4978 | } |
| 4979 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4980 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 4981 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4982 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 4983 | for (NetworkPolicy policy : policies) { |
| 4984 | if (policy.template.equals(template)) { |
| 4985 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 4986 | policy.inferred = false; |
| 4987 | policy.clearSnooze(); |
| 4988 | } |
| 4989 | } |
| 4990 | setNetworkPolicies(policies); |
| 4991 | |
| 4992 | // Turn restrict background data off |
| 4993 | setRestrictBackground(false); |
| 4994 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4995 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 4996 | // Remove app's "restrict background data" flag |
| 4997 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 4998 | setUidPolicy(uid, POLICY_NONE); |
| 4999 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5000 | } |
| 5001 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 5002 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5003 | @Override |
| 5004 | public boolean isUidNetworkingBlocked(int uid, boolean isNetworkMetered) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5005 | final long startTime = mStatLogger.getTime(); |
| 5006 | |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 5007 | mContext.enforceCallingOrSelfPermission(OBSERVE_NETWORK_POLICY, TAG); |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5008 | final int uidRules; |
| 5009 | final boolean isBackgroundRestricted; |
| 5010 | synchronized (mUidRulesFirstLock) { |
| 5011 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 5012 | isBackgroundRestricted = mRestrictBackground; |
| 5013 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5014 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 5015 | isBackgroundRestricted, mLogger); |
| 5016 | |
| 5017 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 5018 | |
| 5019 | return ret; |
| 5020 | } |
| 5021 | |
| 5022 | private static boolean isSystem(int uid) { |
| 5023 | return uid < Process.FIRST_APPLICATION_UID; |
| 5024 | } |
| 5025 | |
| 5026 | static boolean isUidNetworkingBlockedInternal(int uid, int uidRules, boolean isNetworkMetered, |
| 5027 | boolean isBackgroundRestricted, @Nullable NetworkPolicyLogger logger) { |
| 5028 | final int reason; |
| 5029 | // Networks are never blocked for system components |
| 5030 | if (isSystem(uid)) { |
| 5031 | reason = NTWK_ALLOWED_SYSTEM; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5032 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5033 | else if (hasRule(uidRules, RULE_REJECT_ALL)) { |
| 5034 | reason = NTWK_BLOCKED_POWER; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5035 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5036 | else if (!isNetworkMetered) { |
| 5037 | reason = NTWK_ALLOWED_NON_METERED; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5038 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5039 | else if (hasRule(uidRules, RULE_REJECT_METERED)) { |
| 5040 | reason = NTWK_BLOCKED_BLACKLIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5041 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5042 | else if (hasRule(uidRules, RULE_ALLOW_METERED)) { |
| 5043 | reason = NTWK_ALLOWED_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5044 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5045 | else if (hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED)) { |
| 5046 | reason = NTWK_ALLOWED_TMP_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5047 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5048 | else if (isBackgroundRestricted) { |
| 5049 | reason = NTWK_BLOCKED_BG_RESTRICT; |
| 5050 | } |
| 5051 | else { |
| 5052 | reason = NTWK_ALLOWED_DEFAULT; |
| 5053 | } |
| 5054 | |
| 5055 | final boolean blocked; |
| 5056 | switch(reason) { |
| 5057 | case NTWK_ALLOWED_DEFAULT: |
| 5058 | case NTWK_ALLOWED_NON_METERED: |
| 5059 | case NTWK_ALLOWED_TMP_WHITELIST: |
| 5060 | case NTWK_ALLOWED_WHITELIST: |
| 5061 | case NTWK_ALLOWED_SYSTEM: |
| 5062 | blocked = false; |
| 5063 | break; |
| 5064 | case NTWK_BLOCKED_POWER: |
| 5065 | case NTWK_BLOCKED_BLACKLIST: |
| 5066 | case NTWK_BLOCKED_BG_RESTRICT: |
| 5067 | blocked = true; |
| 5068 | break; |
| 5069 | default: |
| 5070 | throw new IllegalArgumentException(); |
| 5071 | } |
| 5072 | if (logger != null) { |
| 5073 | logger.networkBlocked(uid, reason); |
| 5074 | } |
| 5075 | |
| 5076 | return blocked; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5077 | } |
| 5078 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5079 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 5080 | |
| 5081 | @Override |
| 5082 | public void resetUserState(int userId) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5083 | synchronized (mUidRulesFirstLock) { |
| 5084 | boolean changed = removeUserStateUL(userId, false); |
| 5085 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed; |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5086 | if (changed) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5087 | synchronized (mNetworkPoliciesSecondLock) { |
| 5088 | writePolicyAL(); |
| 5089 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5093 | |
| 5094 | /** |
| 5095 | * @return true if the given uid is restricted from doing networking on metered networks. |
| 5096 | */ |
| 5097 | @Override |
| 5098 | public boolean isUidRestrictedOnMeteredNetworks(int uid) { |
| 5099 | final int uidRules; |
| 5100 | final boolean isBackgroundRestricted; |
| 5101 | synchronized (mUidRulesFirstLock) { |
| 5102 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 5103 | isBackgroundRestricted = mRestrictBackground; |
| 5104 | } |
| 5105 | return isBackgroundRestricted |
| 5106 | && !hasRule(uidRules, RULE_ALLOW_METERED) |
| 5107 | && !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED); |
| 5108 | } |
| 5109 | |
| 5110 | /** |
| 5111 | * @return true if networking is blocked on the given interface for the given uid according |
| 5112 | * to current networking policies. |
| 5113 | */ |
| 5114 | @Override |
| 5115 | public boolean isUidNetworkingBlocked(int uid, String ifname) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5116 | final long startTime = mStatLogger.getTime(); |
| 5117 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5118 | final int uidRules; |
| 5119 | final boolean isBackgroundRestricted; |
| 5120 | synchronized (mUidRulesFirstLock) { |
| 5121 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 5122 | isBackgroundRestricted = mRestrictBackground; |
| 5123 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5124 | final boolean isNetworkMetered; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5125 | synchronized (mNetworkPoliciesSecondLock) { |
| 5126 | isNetworkMetered = mMeteredIfaces.contains(ifname); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5127 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5128 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 5129 | isBackgroundRestricted, mLogger); |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5130 | |
| 5131 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 5132 | |
| 5133 | return ret; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5134 | } |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5135 | |
| 5136 | @Override |
| 5137 | public void onTempPowerSaveWhitelistChange(int appId, boolean added) { |
| 5138 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 5139 | mLogger.tempPowerSaveWlChanged(appId, added); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5140 | if (added) { |
| 5141 | mPowerSaveTempWhitelistAppIds.put(appId, true); |
| 5142 | } else { |
| 5143 | mPowerSaveTempWhitelistAppIds.delete(appId); |
| 5144 | } |
| 5145 | updateRulesForTempWhitelistChangeUL(appId); |
| 5146 | } |
| 5147 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5148 | |
| 5149 | @Override |
| 5150 | public SubscriptionPlan getSubscriptionPlan(Network network) { |
| 5151 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5152 | final int subId = getSubIdLocked(network); |
| 5153 | return getPrimarySubscriptionPlanLocked(subId); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | @Override |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 5158 | public SubscriptionPlan getSubscriptionPlan(NetworkTemplate template) { |
| 5159 | synchronized (mNetworkPoliciesSecondLock) { |
| 5160 | final int subId = findRelevantSubIdNL(template); |
| 5161 | return getPrimarySubscriptionPlanLocked(subId); |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5166 | public long getSubscriptionOpportunisticQuota(Network network, int quotaType) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5167 | final long quotaBytes; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5168 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5169 | quotaBytes = mSubscriptionOpportunisticQuota.get(getSubIdLocked(network), |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 5170 | OPPORTUNISTIC_QUOTA_UNKNOWN); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5171 | } |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5172 | if (quotaBytes == OPPORTUNISTIC_QUOTA_UNKNOWN) { |
| 5173 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5174 | } |
| 5175 | |
| 5176 | if (quotaType == QUOTA_TYPE_JOBS) { |
| 5177 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5178 | NETPOLICY_QUOTA_FRAC_JOBS, QUOTA_FRAC_JOBS_DEFAULT)); |
| 5179 | } else if (quotaType == QUOTA_TYPE_MULTIPATH) { |
| 5180 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5181 | NETPOLICY_QUOTA_FRAC_MULTIPATH, QUOTA_FRAC_MULTIPATH_DEFAULT)); |
| 5182 | } else { |
| 5183 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5184 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5185 | } |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5186 | |
| 5187 | @Override |
| 5188 | public void onAdminDataAvailable() { |
| 5189 | mAdminDataAvailableLatch.countDown(); |
| 5190 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5191 | |
| 5192 | @Override |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 5193 | public void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
| 5194 | NetworkPolicyManagerService.this.setAppIdleWhitelist(uid, shouldWhitelist); |
| 5195 | } |
| 5196 | |
| 5197 | @Override |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5198 | public void setMeteredRestrictedPackages(Set<String> packageNames, int userId) { |
| 5199 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 5200 | } |
| 5201 | |
| 5202 | @Override |
| 5203 | public void setMeteredRestrictedPackagesAsync(Set<String> packageNames, int userId) { |
| 5204 | mHandler.obtainMessage(MSG_METERED_RESTRICTED_PACKAGES_CHANGED, |
| 5205 | userId, 0, packageNames).sendToTarget(); |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | private void setMeteredRestrictedPackagesInternal(Set<String> packageNames, int userId) { |
| 5210 | synchronized (mUidRulesFirstLock) { |
| 5211 | final Set<Integer> newRestrictedUids = new ArraySet<>(); |
| 5212 | for (String packageName : packageNames) { |
| 5213 | final int uid = getUidForPackage(packageName, userId); |
| 5214 | if (uid >= 0) { |
| 5215 | newRestrictedUids.add(uid); |
| 5216 | } |
| 5217 | } |
| 5218 | final Set<Integer> oldRestrictedUids = mMeteredRestrictedUids.get(userId); |
| 5219 | mMeteredRestrictedUids.put(userId, newRestrictedUids); |
| 5220 | handleRestrictedPackagesChangeUL(oldRestrictedUids, newRestrictedUids); |
| 5221 | mLogger.meteredRestrictedPkgsChanged(newRestrictedUids); |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | private int getUidForPackage(String packageName, int userId) { |
| 5226 | try { |
| 5227 | return mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 5228 | PackageManager.MATCH_KNOWN_PACKAGES, userId); |
| 5229 | } catch (NameNotFoundException e) { |
| 5230 | return -1; |
| 5231 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | private int parseSubId(NetworkState state) { |
| 5235 | // TODO: moved to using a legitimate NetworkSpecifier instead of string parsing |
| 5236 | int subId = INVALID_SUBSCRIPTION_ID; |
| 5237 | if (state != null && state.networkCapabilities != null |
| 5238 | && state.networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) { |
| 5239 | NetworkSpecifier spec = state.networkCapabilities.getNetworkSpecifier(); |
| 5240 | if (spec instanceof StringNetworkSpecifier) { |
| 5241 | try { |
| 5242 | subId = Integer.parseInt(((StringNetworkSpecifier) spec).specifier); |
| 5243 | } catch (NumberFormatException e) { |
| 5244 | } |
| 5245 | } |
| 5246 | } |
| 5247 | return subId; |
| 5248 | } |
| 5249 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5250 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5251 | private int getSubIdLocked(Network network) { |
| 5252 | return mNetIdToSubId.get(network.netId, INVALID_SUBSCRIPTION_ID); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5253 | } |
| 5254 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5255 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5256 | private SubscriptionPlan getPrimarySubscriptionPlanLocked(int subId) { |
| 5257 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5258 | if (!ArrayUtils.isEmpty(plans)) { |
| 5259 | for (SubscriptionPlan plan : plans) { |
| 5260 | if (plan.getCycleRule().isRecurring()) { |
| 5261 | // Recurring plans will always have an active cycle |
| 5262 | return plan; |
| 5263 | } else { |
| 5264 | // Non-recurring plans need manual test for active cycle |
| 5265 | final Range<ZonedDateTime> cycle = plan.cycleIterator().next(); |
| 5266 | if (cycle.contains(ZonedDateTime.now(mClock))) { |
| 5267 | return plan; |
| 5268 | } |
| 5269 | } |
| 5270 | } |
| 5271 | } |
| 5272 | return null; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5273 | } |
| 5274 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5275 | /** |
| 5276 | * This will only ever be called once - during device boot. |
| 5277 | */ |
| 5278 | private void waitForAdminData() { |
| 5279 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { |
| 5280 | ConcurrentUtils.waitForCountDownNoInterrupt(mAdminDataAvailableLatch, |
| 5281 | WAIT_FOR_ADMIN_DATA_TIMEOUT_MS, "Wait for admin data"); |
| 5282 | } |
| 5283 | } |
| 5284 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5285 | private void handleRestrictedPackagesChangeUL(Set<Integer> oldRestrictedUids, |
| 5286 | Set<Integer> newRestrictedUids) { |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 5287 | if (!mNetworkManagerReady) { |
| 5288 | return; |
| 5289 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5290 | if (oldRestrictedUids == null) { |
| 5291 | for (int uid : newRestrictedUids) { |
| 5292 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5293 | } |
| 5294 | return; |
| 5295 | } |
| 5296 | for (int uid : oldRestrictedUids) { |
| 5297 | if (!newRestrictedUids.contains(uid)) { |
| 5298 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5299 | } |
| 5300 | } |
| 5301 | for (int uid : newRestrictedUids) { |
| 5302 | if (!oldRestrictedUids.contains(uid)) { |
| 5303 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 5308 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5309 | private boolean isRestrictedByAdminUL(int uid) { |
| 5310 | final Set<Integer> restrictedUids = mMeteredRestrictedUids.get( |
| 5311 | UserHandle.getUserId(uid)); |
| 5312 | return restrictedUids != null && restrictedUids.contains(uid); |
| 5313 | } |
| 5314 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5315 | private static boolean hasRule(int uidRules, int rule) { |
| 5316 | return (uidRules & rule) != 0; |
| 5317 | } |
| 5318 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5319 | private static @NonNull NetworkState[] defeatNullable(@Nullable NetworkState[] val) { |
| 5320 | return (val != null) ? val : new NetworkState[0]; |
| 5321 | } |
| 5322 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5323 | private static boolean getBooleanDefeatingNullable(@Nullable PersistableBundle bundle, |
| 5324 | String key, boolean defaultValue) { |
| 5325 | return (bundle != null) ? bundle.getBoolean(key, defaultValue) : defaultValue; |
| 5326 | } |
| 5327 | |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 5328 | private static boolean isHeadlessSystemUserBuild() { |
| 5329 | return RoSystemProperties.MULTIUSER_HEADLESS_SYSTEM_USER; |
| 5330 | } |
| 5331 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 5332 | private class NotificationId { |
| 5333 | private final String mTag; |
| 5334 | private final int mId; |
| 5335 | |
| 5336 | NotificationId(NetworkPolicy policy, int type) { |
| 5337 | mTag = buildNotificationTag(policy, type); |
| 5338 | mId = type; |
| 5339 | } |
| 5340 | |
| 5341 | @Override |
| 5342 | public boolean equals(Object o) { |
| 5343 | if (this == o) return true; |
| 5344 | if (!(o instanceof NotificationId)) return false; |
| 5345 | NotificationId that = (NotificationId) o; |
| 5346 | return Objects.equals(mTag, that.mTag); |
| 5347 | } |
| 5348 | |
| 5349 | @Override |
| 5350 | public int hashCode() { |
| 5351 | return Objects.hash(mTag); |
| 5352 | } |
| 5353 | |
| 5354 | /** |
| 5355 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 5356 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 5357 | */ |
| 5358 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
| 5359 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
| 5360 | } |
| 5361 | |
| 5362 | public String getTag() { |
| 5363 | return mTag; |
| 5364 | } |
| 5365 | |
| 5366 | public int getId() { |
| 5367 | return mId; |
| 5368 | } |
| 5369 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 5370 | } |