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; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 23 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 24 | import static android.Manifest.permission.READ_PHONE_STATE; |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 25 | import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 26 | import static android.content.Intent.ACTION_PACKAGE_ADDED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 27 | import static android.content.Intent.ACTION_UID_REMOVED; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 28 | import static android.content.Intent.ACTION_USER_ADDED; |
| 29 | import static android.content.Intent.ACTION_USER_REMOVED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 30 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 31 | import static android.content.pm.PackageManager.MATCH_ANY_USER; |
| 32 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 33 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| 34 | import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; |
| 35 | import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES; |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 36 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 37 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 38 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 39 | import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 40 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 41 | import static android.net.INetd.FIREWALL_CHAIN_DOZABLE; |
| 42 | import static android.net.INetd.FIREWALL_CHAIN_POWERSAVE; |
| 43 | import static android.net.INetd.FIREWALL_CHAIN_STANDBY; |
| 44 | import static android.net.INetd.FIREWALL_RULE_ALLOW; |
| 45 | import static android.net.INetd.FIREWALL_RULE_DENY; |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 46 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 47 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 48 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 49 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 50 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 51 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 52 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 53 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 54 | import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS; |
| 55 | import static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 56 | import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 57 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 58 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 59 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 60 | import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 61 | import static android.net.NetworkPolicyManager.RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 62 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 63 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 64 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 65 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileIdleOrPowerSaveMode; |
| 66 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileOnRestrictBackground; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 67 | import static android.net.NetworkPolicyManager.resolveNetworkId; |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 68 | import static android.net.NetworkPolicyManager.uidPoliciesToString; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 69 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 70 | import static android.net.NetworkTemplate.MATCH_MOBILE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 71 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 72 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 73 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 74 | import static android.os.Trace.TRACE_TAG_NETWORK; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 75 | import static android.provider.Settings.Global.NETPOLICY_OVERRIDE_ENABLED; |
| 76 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_ENABLED; |
| 77 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_FRAC_JOBS; |
| 78 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_FRAC_MULTIPATH; |
| 79 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_LIMITED; |
| 80 | import static android.provider.Settings.Global.NETPOLICY_QUOTA_UNLIMITED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 81 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 82 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_THRESHOLD_DISABLED; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 83 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_USE_PLATFORM_DEFAULT; |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 84 | import static android.telephony.CarrierConfigManager.KEY_DATA_LIMIT_NOTIFICATION_BOOL; |
| 85 | import static android.telephony.CarrierConfigManager.KEY_DATA_RAPID_NOTIFICATION_BOOL; |
| 86 | import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_NOTIFICATION_BOOL; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 87 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 88 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 89 | import static com.android.internal.util.ArrayUtils.appendInt; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 90 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 91 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 92 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 93 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 94 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 95 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 96 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 97 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 98 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 99 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 100 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_DEFAULT; |
| 101 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_NON_METERED; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 102 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_SYSTEM; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 103 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_TMP_WHITELIST; |
| 104 | import static com.android.server.net.NetworkPolicyLogger.NTWK_ALLOWED_WHITELIST; |
| 105 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_BG_RESTRICT; |
| 106 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_BLACKLIST; |
| 107 | import static com.android.server.net.NetworkPolicyLogger.NTWK_BLOCKED_POWER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 108 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 109 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 110 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 111 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 112 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 113 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 114 | import android.Manifest; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 115 | import android.annotation.IntDef; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 116 | import android.annotation.NonNull; |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 117 | import android.annotation.Nullable; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 118 | import android.app.ActivityManager; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 119 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 120 | import android.app.AppGlobals; |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 121 | import android.app.AppOpsManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 122 | import android.app.IActivityManager; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 123 | import android.app.IUidObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 124 | import android.app.Notification; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 125 | import android.app.NotificationManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 126 | import android.app.PendingIntent; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 127 | import android.app.usage.UsageStatsManagerInternal; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 128 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 129 | import android.content.ComponentName; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 130 | import android.content.ContentResolver; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 131 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 132 | import android.content.Intent; |
| 133 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 134 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 135 | import android.content.pm.IPackageManager; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 136 | import android.content.pm.PackageManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 137 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 138 | import android.content.pm.UserInfo; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 139 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 140 | import android.net.ConnectivityManager; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 141 | import android.net.ConnectivityManager.NetworkCallback; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 142 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 143 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 144 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 145 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 146 | import android.net.INetworkStatsService; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 147 | import android.net.LinkProperties; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 148 | import android.net.Network; |
| 149 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 150 | import android.net.NetworkIdentity; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 151 | import android.net.NetworkPolicy; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 152 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 153 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 154 | import android.net.NetworkRequest; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 155 | import android.net.NetworkSpecifier; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 156 | import android.net.NetworkState; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 157 | import android.net.NetworkStats; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 158 | import android.net.NetworkTemplate; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 159 | import android.net.StringNetworkSpecifier; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 160 | import android.net.TrafficStats; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 161 | import android.net.wifi.WifiConfiguration; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 162 | import android.net.wifi.WifiManager; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 163 | import android.os.BestClock; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 164 | import android.os.Binder; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 165 | import android.os.Environment; |
| 166 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 167 | import android.os.HandlerThread; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 168 | import android.os.IDeviceIdleController; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 169 | import android.os.INetworkManagementService; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 170 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 171 | import android.os.MessageQueue.IdleHandler; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 172 | import android.os.PersistableBundle; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 173 | import android.os.PowerManager; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 174 | import android.os.PowerManager.ServiceType; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 175 | import android.os.PowerManagerInternal; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 176 | import android.os.PowerSaveState; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 177 | import android.os.Process; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 178 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 179 | import android.os.RemoteException; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 180 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 181 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 182 | import android.os.ShellCallback; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 183 | import android.os.SystemClock; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 184 | import android.os.SystemProperties; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 185 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 186 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 187 | import android.os.UserManager; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 188 | import android.provider.Settings; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 189 | import android.provider.Settings.Global; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 190 | import android.telephony.CarrierConfigManager; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 191 | import android.telephony.SubscriptionInfo; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 192 | import android.telephony.SubscriptionManager; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 193 | import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 194 | import android.telephony.SubscriptionPlan; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 195 | import android.telephony.TelephonyManager; |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 196 | import android.text.TextUtils; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 197 | import android.text.format.DateUtils; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 198 | import android.text.format.Formatter; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 199 | import android.util.ArrayMap; |
| 200 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 201 | import android.util.AtomicFile; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 202 | import android.util.DataUnit; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 203 | import android.util.IntArray; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 204 | import android.util.Log; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 205 | import android.util.Pair; |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 206 | import android.util.Range; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 207 | import android.util.RecurrenceRule; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 208 | import android.util.Slog; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 209 | import android.util.SparseArray; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 210 | import android.util.SparseBooleanArray; |
| 211 | import android.util.SparseIntArray; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 212 | import android.util.SparseLongArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 213 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 214 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 215 | import com.android.internal.R; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 216 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 217 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 218 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 219 | import com.android.internal.notification.SystemNotificationChannels; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 220 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 221 | import com.android.internal.util.ArrayUtils; |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 222 | import com.android.internal.util.ConcurrentUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 223 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 224 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 225 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 226 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 227 | import com.android.internal.util.StatLogger; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 228 | import com.android.server.EventLogTags; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 229 | import com.android.server.LocalServices; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 230 | import com.android.server.ServiceThread; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 231 | import com.android.server.SystemConfig; |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame^] | 232 | import com.android.server.usage.AppStandbyInternal; |
| 233 | import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 234 | |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 235 | import libcore.io.IoUtils; |
| 236 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 237 | import org.xmlpull.v1.XmlPullParser; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 238 | import org.xmlpull.v1.XmlSerializer; |
| 239 | |
| 240 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 241 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 242 | import java.io.FileInputStream; |
| 243 | import java.io.FileNotFoundException; |
| 244 | import java.io.FileOutputStream; |
| 245 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 246 | import java.io.PrintWriter; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 247 | import java.lang.annotation.Retention; |
| 248 | import java.lang.annotation.RetentionPolicy; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 249 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 250 | import java.time.Clock; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 251 | import java.time.Instant; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 252 | import java.time.ZoneId; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 253 | import java.time.ZoneOffset; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 254 | import java.time.ZonedDateTime; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 255 | import java.time.temporal.ChronoUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 256 | import java.util.ArrayList; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 257 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 258 | import java.util.Calendar; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 259 | import java.util.List; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 260 | import java.util.Objects; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 261 | import java.util.Set; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 262 | import java.util.concurrent.CountDownLatch; |
| 263 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 264 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 265 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 266 | * Service that maintains low-level network policy rules, using |
| 267 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 268 | * <p> |
| 269 | * Derives active rules by combining a given policy with other system status, |
| 270 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 271 | * enforcement. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 272 | * |
| 273 | * <p> |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 274 | * This class uses 2 locks to synchronize state: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 275 | * <ul> |
| 276 | * <li>{@code mUidRulesFirstLock}: used to guard state related to individual UIDs (such as firewall |
| 277 | * rules). |
| 278 | * <li>{@code mNetworkPoliciesSecondLock}: used to guard state related to network interfaces (such |
| 279 | * as network policies). |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 280 | * </ul> |
| 281 | * |
| 282 | * <p> |
| 283 | * As such, methods that require synchronization have the following prefixes: |
| 284 | * <ul> |
| 285 | * <li>{@code UL()}: require the "UID" lock ({@code mUidRulesFirstLock}). |
| 286 | * <li>{@code NL()}: require the "Network" lock ({@code mNetworkPoliciesSecondLock}). |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 287 | * <li>{@code AL()}: require all locks, which must be obtained in order ({@code mUidRulesFirstLock} |
| 288 | * first, then {@code mNetworkPoliciesSecondLock}, then {@code mYetAnotherGuardThirdLock}, etc.. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 289 | * </ul> |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 290 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 291 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 292 | static final String TAG = NetworkPolicyLogger.TAG; |
| 293 | private static final boolean LOGD = NetworkPolicyLogger.LOGD; |
| 294 | private static final boolean LOGV = NetworkPolicyLogger.LOGV; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 295 | |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 296 | /** |
| 297 | * No opportunistic quota could be calculated from user data plan or data settings. |
| 298 | */ |
| 299 | public static final int OPPORTUNISTIC_QUOTA_UNKNOWN = -1; |
| 300 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 301 | private static final int VERSION_INIT = 1; |
| 302 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 303 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 304 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 305 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 306 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 307 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 308 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 309 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 310 | private static final int VERSION_SWITCH_UID = 10; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 311 | private static final int VERSION_ADDED_CYCLE = 11; |
| 312 | private static final int VERSION_LATEST = VERSION_ADDED_CYCLE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 313 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 314 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 315 | public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 316 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 317 | public static final int TYPE_LIMIT = SystemMessage.NOTE_NET_LIMIT; |
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_LIMIT_SNOOZED = SystemMessage.NOTE_NET_LIMIT_SNOOZED; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 320 | @VisibleForTesting |
| 321 | public static final int TYPE_RAPID = SystemMessage.NOTE_NET_RAPID; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 322 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 323 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 324 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 325 | private static final String TAG_SUBSCRIPTION_PLAN = "subscription-plan"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 326 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 327 | private static final String TAG_APP_POLICY = "app-policy"; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 328 | private static final String TAG_WHITELIST = "whitelist"; |
| 329 | private static final String TAG_RESTRICT_BACKGROUND = "restrict-background"; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 330 | private static final String TAG_REVOKED_RESTRICT_BACKGROUND = "revoked-restrict-background"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 331 | |
| 332 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 333 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 334 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 335 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 336 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 337 | @Deprecated private static final String ATTR_CYCLE_DAY = "cycleDay"; |
| 338 | @Deprecated private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
| 339 | private static final String ATTR_CYCLE_START = "cycleStart"; |
| 340 | private static final String ATTR_CYCLE_END = "cycleEnd"; |
| 341 | private static final String ATTR_CYCLE_PERIOD = "cyclePeriod"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 342 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 343 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 344 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 345 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 346 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 347 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 348 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 349 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 350 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 351 | private static final String ATTR_POLICY = "policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 352 | private static final String ATTR_SUB_ID = "subId"; |
| 353 | private static final String ATTR_TITLE = "title"; |
| 354 | private static final String ATTR_SUMMARY = "summary"; |
| 355 | private static final String ATTR_LIMIT_BEHAVIOR = "limitBehavior"; |
| 356 | private static final String ATTR_USAGE_BYTES = "usageBytes"; |
| 357 | private static final String ATTR_USAGE_TIME = "usageTime"; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 358 | private static final String ATTR_OWNER_PACKAGE = "ownerPackage"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 359 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 360 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 361 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 362 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 363 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 364 | private static final String ACTION_SNOOZE_RAPID = |
| 365 | "com.android.server.net.action.SNOOZE_RAPID"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 366 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 367 | /** |
| 368 | * Indicates the maximum wait time for admin data to be available; |
| 369 | */ |
| 370 | private static final long WAIT_FOR_ADMIN_DATA_TIMEOUT_MS = 10_000; |
| 371 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 372 | private static final long QUOTA_UNLIMITED_DEFAULT = DataUnit.MEBIBYTES.toBytes(20); |
| 373 | private static final float QUOTA_LIMITED_DEFAULT = 0.1f; |
| 374 | private static final float QUOTA_FRAC_JOBS_DEFAULT = 0.5f; |
| 375 | private static final float QUOTA_FRAC_MULTIPATH_DEFAULT = 0.5f; |
| 376 | |
Svetoslav Ganov | 24c2775 | 2016-08-31 18:09:00 -0700 | [diff] [blame] | 377 | private static final int MSG_RULES_CHANGED = 1; |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 378 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 379 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 380 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 381 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 382 | private static final int MSG_UPDATE_INTERFACE_QUOTA = 10; |
| 383 | private static final int MSG_REMOVE_INTERFACE_QUOTA = 11; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 384 | private static final int MSG_POLICIES_CHANGED = 13; |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 385 | private static final int MSG_RESET_FIREWALL_RULES_BY_UID = 15; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 386 | private static final int MSG_SUBSCRIPTION_OVERRIDE = 16; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 387 | private static final int MSG_METERED_RESTRICTED_PACKAGES_CHANGED = 17; |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 388 | private static final int MSG_SET_NETWORK_TEMPLATE_ENABLED = 18; |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 389 | private static final int MSG_SUBSCRIPTION_PLANS_CHANGED = 19; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 390 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 391 | private static final int UID_MSG_STATE_CHANGED = 100; |
| 392 | private static final int UID_MSG_GONE = 101; |
| 393 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 394 | private static final String PROP_SUB_PLAN_OWNER = "persist.sys.sub_plan_owner"; |
| 395 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 396 | private final Context mContext; |
| 397 | private final IActivityManager mActivityManager; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 398 | private NetworkStatsManagerInternal mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 399 | private final INetworkManagementService mNetworkManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 400 | private UsageStatsManagerInternal mUsageStats; |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame^] | 401 | private AppStandbyInternal mAppStandby; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 402 | private final Clock mClock; |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 403 | private final UserManager mUserManager; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 404 | private final CarrierConfigManager mCarrierConfigManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 405 | |
| 406 | private IConnectivityManager mConnManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 407 | private PowerManagerInternal mPowerManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 408 | private IDeviceIdleController mDeviceIdleController; |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 409 | |
| 410 | /** Current cached value of the current Battery Saver mode's setting for restrict background. */ |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 411 | @GuardedBy("mUidRulesFirstLock") |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 412 | private boolean mRestrictBackgroundLowPowerMode; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 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); |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame^] | 740 | mAppStandby = LocalServices.getService(AppStandbyInternal.class); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 741 | mNetworkStats = LocalServices.getService(NetworkStatsManagerInternal.class); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 742 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 743 | synchronized (mUidRulesFirstLock) { |
| 744 | synchronized (mNetworkPoliciesSecondLock) { |
| 745 | updatePowerSaveWhitelistUL(); |
| 746 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 747 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 748 | new PowerManagerInternal.LowPowerModeListener() { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 749 | @Override |
| 750 | public int getServiceType() { |
| 751 | return ServiceType.NETWORK_FIREWALL; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 752 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 753 | |
| 754 | @Override |
| 755 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 756 | final boolean enabled = result.batterySaverEnabled; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 757 | if (LOGD) { |
| 758 | Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")"); |
| 759 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 760 | synchronized (mUidRulesFirstLock) { |
| 761 | if (mRestrictPower != enabled) { |
| 762 | mRestrictPower = enabled; |
| 763 | updateRulesForRestrictPowerUL(); |
| 764 | } |
| 765 | } |
| 766 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 767 | }); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 768 | mRestrictPower = mPowerManagerInternal.getLowPowerState( |
| 769 | ServiceType.NETWORK_FIREWALL).batterySaverEnabled; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 770 | |
| 771 | mSystemReady = true; |
| 772 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 773 | waitForAdminData(); |
| 774 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 775 | // read policy from disk |
| 776 | readPolicyAL(); |
| 777 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 778 | // Update the restrictBackground if battery saver is turned on |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 779 | mRestrictBackgroundBeforeBsm = mLoadedRestrictBackground; |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 780 | mRestrictBackgroundLowPowerMode = mPowerManagerInternal |
| 781 | .getLowPowerState(ServiceType.DATA_SAVER).batterySaverEnabled; |
| 782 | if (mRestrictBackgroundLowPowerMode && !mLoadedRestrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 783 | mLoadedRestrictBackground = true; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 784 | } |
| 785 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 786 | new PowerManagerInternal.LowPowerModeListener() { |
| 787 | @Override |
| 788 | public int getServiceType() { |
| 789 | return ServiceType.DATA_SAVER; |
| 790 | } |
| 791 | |
| 792 | @Override |
| 793 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 794 | synchronized (mUidRulesFirstLock) { |
| 795 | updateRestrictBackgroundByLowPowerModeUL(result); |
| 796 | } |
| 797 | } |
| 798 | }); |
| 799 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 800 | if (addDefaultRestrictBackgroundWhitelistUidsUL()) { |
| 801 | writePolicyAL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 802 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 803 | |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 804 | enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, true); |
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); |
| 828 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 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 | |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame^] | 875 | mAppStandby.addListener(new NetPolicyAppIdleStateChangeListener()); |
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 |
| 1130 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 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. |
Eric Jeong | e7ead1b | 2019-07-18 09:37:08 -0700 | [diff] [blame] | 1362 | if (UserManager.isHeadlessSystemUserMode()) { |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 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. |
Eric Jeong | e7ead1b | 2019-07-18 09:37:08 -0700 | [diff] [blame] | 1390 | if (UserManager.isHeadlessSystemUserMode()) { |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 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. |
Eric Jeong | e7ead1b | 2019-07-18 09:37:08 -0700 | [diff] [blame] | 1421 | if (UserManager.isHeadlessSystemUserMode()) { |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 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. |
Eric Jeong | e7ead1b | 2019-07-18 09:37:08 -0700 | [diff] [blame] | 1448 | if (UserManager.isHeadlessSystemUserMode()) { |
Heemin Seog | b9820d4 | 2019-05-21 16:17:43 -0700 | [diff] [blame] | 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) { |
| 1485 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 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 | c0f85c1 | 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 | |
| 2723 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2724 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2725 | // TODO: create permission for observing network policy |
| 2726 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2727 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | @Override |
| 2731 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2732 | // TODO: create permission for observing network policy |
| 2733 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2734 | mListeners.unregister(listener); |
| 2735 | } |
| 2736 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2737 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2738 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2739 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2740 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2741 | final long token = Binder.clearCallingIdentity(); |
| 2742 | try { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2743 | synchronized (mUidRulesFirstLock) { |
| 2744 | synchronized (mNetworkPoliciesSecondLock) { |
| 2745 | normalizePoliciesNL(policies); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2746 | handleNetworkPoliciesUpdateAL(false); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2747 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2748 | } |
| 2749 | } finally { |
| 2750 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2754 | void addNetworkPolicyAL(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2755 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2756 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 2757 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2760 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2761 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2762 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2763 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 2764 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 2765 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 2766 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2767 | } catch (SecurityException e) { |
| 2768 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2769 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2770 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 2771 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 2772 | return new NetworkPolicy[0]; |
| 2773 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2776 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2777 | final int size = mNetworkPolicy.size(); |
| 2778 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 2779 | for (int i = 0; i < size; i++) { |
| 2780 | policies[i] = mNetworkPolicy.valueAt(i); |
| 2781 | } |
| 2782 | return policies; |
| 2783 | } |
| 2784 | } |
| 2785 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2786 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2787 | private void normalizePoliciesNL() { |
| 2788 | normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2789 | } |
| 2790 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2791 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2792 | private void normalizePoliciesNL(NetworkPolicy[] policies) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2793 | mNetworkPolicy.clear(); |
| 2794 | for (NetworkPolicy policy : policies) { |
Annie Meng | 20b4d84 | 2018-05-18 15:00:49 +0100 | [diff] [blame] | 2795 | if (policy == null) { |
| 2796 | continue; |
| 2797 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2798 | // When two normalized templates conflict, prefer the most |
| 2799 | // restrictive policy |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 2800 | policy.template = NetworkTemplate.normalize(policy.template, mMergedSubscriberIds); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2801 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 2802 | if (existing == null || existing.compareTo(policy) > 0) { |
| 2803 | if (existing != null) { |
| 2804 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 2805 | } |
| 2806 | mNetworkPolicy.put(policy.template, policy); |
| 2807 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2812 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2813 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 2814 | |
| 2815 | final long token = Binder.clearCallingIdentity(); |
| 2816 | try { |
| 2817 | performSnooze(template, TYPE_LIMIT); |
| 2818 | } finally { |
| 2819 | Binder.restoreCallingIdentity(token); |
| 2820 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2821 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2822 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2823 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 2824 | final long currentTime = mClock.millis(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2825 | synchronized (mUidRulesFirstLock) { |
| 2826 | synchronized (mNetworkPoliciesSecondLock) { |
| 2827 | // find and snooze local policy that matches |
| 2828 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 2829 | if (policy == null) { |
| 2830 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 2831 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2832 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2833 | switch (type) { |
| 2834 | case TYPE_WARNING: |
| 2835 | policy.lastWarningSnooze = currentTime; |
| 2836 | break; |
| 2837 | case TYPE_LIMIT: |
| 2838 | policy.lastLimitSnooze = currentTime; |
| 2839 | break; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 2840 | case TYPE_RAPID: |
| 2841 | policy.lastRapidSnooze = currentTime; |
| 2842 | break; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2843 | default: |
| 2844 | throw new IllegalArgumentException("unexpected type"); |
| 2845 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2846 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2847 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2848 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2853 | public void onTetheringChanged(String iface, boolean tethering) { |
| 2854 | // No need to enforce permission because setRestrictBackground() will do it. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2855 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2856 | if (mRestrictBackground && tethering) { |
| 2857 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 2858 | setRestrictBackground(false); |
| 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2864 | public void setRestrictBackground(boolean restrictBackground) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2865 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackground"); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2866 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2867 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2868 | final long token = Binder.clearCallingIdentity(); |
| 2869 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2870 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2871 | setRestrictBackgroundUL(restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2872 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2873 | } finally { |
| 2874 | Binder.restoreCallingIdentity(token); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2875 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2876 | } finally { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2877 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2881 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2882 | private void setRestrictBackgroundUL(boolean restrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2883 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackgroundUL"); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2884 | try { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2885 | if (restrictBackground == mRestrictBackground) { |
| 2886 | // Ideally, UI should never allow this scenario... |
| 2887 | Slog.w(TAG, "setRestrictBackgroundUL: already " + restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2888 | return; |
| 2889 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2890 | Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground); |
| 2891 | final boolean oldRestrictBackground = mRestrictBackground; |
| 2892 | mRestrictBackground = restrictBackground; |
| 2893 | // Must whitelist foreground apps before turning data saver mode on. |
| 2894 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 2895 | // so it could call AM to get the UIDs of such apps, and iterate through them instead. |
| 2896 | updateRulesForRestrictBackgroundUL(); |
| 2897 | try { |
| 2898 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2899 | Slog.e(TAG, |
| 2900 | "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2901 | mRestrictBackground = oldRestrictBackground; |
| 2902 | // TODO: if it knew the foreground apps (see TODO above), it could call |
| 2903 | // updateRulesForRestrictBackgroundUL() again to restore state. |
| 2904 | return; |
| 2905 | } |
| 2906 | } catch (RemoteException e) { |
| 2907 | // ignored; service lives in system_server |
| 2908 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2909 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2910 | sendRestrictBackgroundChangedMsg(); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2911 | mLogger.restrictBackgroundChanged(oldRestrictBackground, mRestrictBackground); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2912 | |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 2913 | if (mRestrictBackgroundLowPowerMode) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2914 | mRestrictBackgroundChangedInBsm = true; |
| 2915 | } |
| 2916 | synchronized (mNetworkPoliciesSecondLock) { |
| 2917 | updateNotificationsNL(); |
| 2918 | writePolicyAL(); |
| 2919 | } |
| 2920 | } finally { |
| 2921 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2922 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | private void sendRestrictBackgroundChangedMsg() { |
| 2926 | mHandler.removeMessages(MSG_RESTRICT_BACKGROUND_CHANGED); |
| 2927 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0) |
| 2928 | .sendToTarget(); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2931 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2932 | public int getRestrictBackgroundByCaller() { |
| 2933 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2934 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2935 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2936 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2937 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2938 | final long token = Binder.clearCallingIdentity(); |
| 2939 | final int policy; |
| 2940 | try { |
| 2941 | policy = getUidPolicy(uid); |
| 2942 | } finally { |
| 2943 | Binder.restoreCallingIdentity(token); |
| 2944 | } |
| 2945 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2946 | // App is blacklisted. |
| 2947 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2948 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2949 | if (!mRestrictBackground) { |
| 2950 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2951 | } |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2952 | return (mUidPolicy.get(uid) & POLICY_ALLOW_METERED_BACKGROUND) != 0 |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2953 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2954 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2959 | public boolean getRestrictBackground() { |
| 2960 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2961 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2962 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2963 | return mRestrictBackground; |
| 2964 | } |
| 2965 | } |
| 2966 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2967 | @Override |
| 2968 | public void setDeviceIdleMode(boolean enabled) { |
| 2969 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2970 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode"); |
| 2971 | try { |
| 2972 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2973 | if (mDeviceIdleMode == enabled) { |
| 2974 | return; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2975 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2976 | mDeviceIdleMode = enabled; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2977 | mLogger.deviceIdleModeEnabled(enabled); |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2978 | if (mSystemReady) { |
| 2979 | // Device idle change means we need to rebuild rules for all |
| 2980 | // known apps, so do a global refresh. |
| 2981 | updateRulesForRestrictPowerUL(); |
| 2982 | } |
| 2983 | } |
| 2984 | if (enabled) { |
| 2985 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 2986 | } else { |
| 2987 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2988 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2989 | } finally { |
| 2990 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2994 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2995 | public void setWifiMeteredOverride(String networkId, int meteredOverride) { |
| 2996 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2997 | final long token = Binder.clearCallingIdentity(); |
| 2998 | try { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2999 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 3000 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 3001 | for (WifiConfiguration config : configs) { |
| 3002 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 3003 | config.meteredOverride = meteredOverride; |
| 3004 | wm.updateNetwork(config); |
| 3005 | } |
| 3006 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 3007 | } finally { |
| 3008 | Binder.restoreCallingIdentity(token); |
| 3009 | } |
| 3010 | } |
| 3011 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 3012 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3013 | @Deprecated |
| 3014 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 3015 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 3016 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 3017 | return new NetworkQuotaInfo(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3018 | } |
| 3019 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3020 | private void enforceSubscriptionPlanAccess(int subId, int callingUid, String callingPackage) { |
| 3021 | // Verify they're not lying about package name |
| 3022 | mAppOps.checkPackage(callingUid, callingPackage); |
| 3023 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3024 | final SubscriptionInfo si; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3025 | final PersistableBundle config; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3026 | final long token = Binder.clearCallingIdentity(); |
| 3027 | try { |
| 3028 | si = mContext.getSystemService(SubscriptionManager.class) |
| 3029 | .getActiveSubscriptionInfo(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3030 | config = mCarrierConfigManager.getConfigForSubId(subId); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3031 | } finally { |
| 3032 | Binder.restoreCallingIdentity(token); |
| 3033 | } |
| 3034 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3035 | // First check: is caller the CarrierService? |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3036 | if (si != null) { |
| 3037 | if (si.isEmbedded() && si.canManageSubscription(mContext, callingPackage)) { |
| 3038 | return; |
| 3039 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3040 | } |
| 3041 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3042 | // Second check: has the CarrierService delegated access? |
| 3043 | if (config != null) { |
| 3044 | final String overridePackage = config |
| 3045 | .getString(CarrierConfigManager.KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING, null); |
| 3046 | if (!TextUtils.isEmpty(overridePackage) |
| 3047 | && Objects.equals(overridePackage, callingPackage)) { |
| 3048 | return; |
| 3049 | } |
| 3050 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3051 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3052 | // Third check: is caller the fallback/default CarrierService? |
| 3053 | final String defaultPackage = mCarrierConfigManager.getDefaultCarrierServicePackageName(); |
| 3054 | if (!TextUtils.isEmpty(defaultPackage) |
| 3055 | && Objects.equals(defaultPackage, callingPackage)) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3056 | return; |
| 3057 | } |
| 3058 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3059 | // Fourth check: is caller a testing app? |
| 3060 | final String testPackage = SystemProperties.get(PROP_SUB_PLAN_OWNER + "." + subId, null); |
| 3061 | if (!TextUtils.isEmpty(testPackage) |
| 3062 | && Objects.equals(testPackage, callingPackage)) { |
| 3063 | return; |
| 3064 | } |
| 3065 | |
| 3066 | // Fifth check: is caller a legacy testing app? |
| 3067 | final String legacyTestPackage = SystemProperties.get("fw.sub_plan_owner." + subId, null); |
| 3068 | if (!TextUtils.isEmpty(legacyTestPackage) |
| 3069 | && Objects.equals(legacyTestPackage, callingPackage)) { |
Jeff Sharkey | a7f5046 | 2018-02-14 14:26:10 -0700 | [diff] [blame] | 3070 | return; |
| 3071 | } |
| 3072 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3073 | // Final check: does the caller hold a permission? |
| 3074 | mContext.enforceCallingOrSelfPermission(MANAGE_SUBSCRIPTION_PLANS, TAG); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3075 | } |
| 3076 | |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3077 | private void enforceSubscriptionPlanValidity(SubscriptionPlan[] plans) { |
| 3078 | // nothing to check if no plans |
| 3079 | if (plans.length == 0) { |
| 3080 | return; |
| 3081 | } |
| 3082 | |
| 3083 | long applicableNetworkTypes = 0; |
| 3084 | boolean allNetworks = false; |
| 3085 | for (SubscriptionPlan plan : plans) { |
| 3086 | if (plan.getNetworkTypes() == null) { |
| 3087 | allNetworks = true; |
| 3088 | } else { |
| 3089 | if ((applicableNetworkTypes & plan.getNetworkTypesBitMask()) != 0) { |
| 3090 | throw new IllegalArgumentException( |
| 3091 | "Multiple subscription plans defined for a single network type."); |
| 3092 | } else { |
| 3093 | applicableNetworkTypes |= plan.getNetworkTypesBitMask(); |
| 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | // ensure at least one plan applies for every network type |
| 3099 | if (!allNetworks) { |
| 3100 | throw new IllegalArgumentException( |
| 3101 | "No generic subscription plan that applies to all network types."); |
| 3102 | } |
| 3103 | } |
| 3104 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3105 | @Override |
| 3106 | public SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage) { |
| 3107 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3108 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3109 | final String fake = SystemProperties.get("fw.fake_plan"); |
| 3110 | if (!TextUtils.isEmpty(fake)) { |
| 3111 | final List<SubscriptionPlan> plans = new ArrayList<>(); |
| 3112 | if ("month_hard".equals(fake)) { |
| 3113 | plans.add(SubscriptionPlan.Builder |
| 3114 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3115 | .setTitle("G-Mobile") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3116 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3117 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3118 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3119 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3120 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3121 | plans.add(SubscriptionPlan.Builder |
| 3122 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3123 | .setTitle("G-Mobile Happy") |
| 3124 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3125 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3126 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3127 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3128 | .build()); |
| 3129 | plans.add(SubscriptionPlan.Builder |
| 3130 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3131 | .setTitle("G-Mobile, Charged after limit") |
| 3132 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3133 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3134 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3135 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3136 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3137 | } else if ("month_soft".equals(fake)) { |
| 3138 | plans.add(SubscriptionPlan.Builder |
| 3139 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3140 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3141 | .setSummary("Crazy unlimited bandwidth plan with incredibly long title " |
| 3142 | + "that should be cut off to prevent UI from looking terrible") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3143 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3144 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3145 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3146 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3147 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3148 | plans.add(SubscriptionPlan.Builder |
| 3149 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3150 | .setTitle("G-Mobile, Throttled after limit") |
| 3151 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3152 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3153 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3154 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3155 | .build()); |
| 3156 | plans.add(SubscriptionPlan.Builder |
| 3157 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3158 | .setTitle("G-Mobile, No data connection after limit") |
| 3159 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3160 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3161 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3162 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3163 | .build()); |
| 3164 | |
Sundeep Ghuman | 09e0f57 | 2018-03-14 23:20:23 -0700 | [diff] [blame] | 3165 | } else if ("month_over".equals(fake)) { |
| 3166 | plans.add(SubscriptionPlan.Builder |
| 3167 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3168 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3169 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3170 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3171 | .setDataUsage(6 * TrafficStats.GB_IN_BYTES, |
| 3172 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3173 | .build()); |
| 3174 | plans.add(SubscriptionPlan.Builder |
| 3175 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3176 | .setTitle("G-Mobile, Throttled after limit") |
| 3177 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3178 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3179 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3180 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3181 | .build()); |
| 3182 | plans.add(SubscriptionPlan.Builder |
| 3183 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3184 | .setTitle("G-Mobile, No data connection after limit") |
| 3185 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3186 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3187 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3188 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3189 | .build()); |
| 3190 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3191 | } else if ("month_none".equals(fake)) { |
| 3192 | plans.add(SubscriptionPlan.Builder |
| 3193 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3194 | .setTitle("G-Mobile") |
| 3195 | .build()); |
| 3196 | } else if ("prepaid".equals(fake)) { |
| 3197 | plans.add(SubscriptionPlan.Builder |
| 3198 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3199 | ZonedDateTime.now().plusDays(10)) |
| 3200 | .setTitle("G-Mobile") |
| 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 | } else if ("prepaid_crazy".equals(fake)) { |
| 3207 | plans.add(SubscriptionPlan.Builder |
| 3208 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3209 | ZonedDateTime.now().plusDays(10)) |
| 3210 | .setTitle("G-Mobile Anytime") |
| 3211 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 3212 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3213 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 3214 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3215 | .build()); |
| 3216 | plans.add(SubscriptionPlan.Builder |
| 3217 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3218 | ZonedDateTime.now().plusDays(20)) |
| 3219 | .setTitle("G-Mobile Nickel Nights") |
| 3220 | .setSummary("5¢/GB between 1-5AM") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3221 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3222 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3223 | .setDataUsage(15 * TrafficStats.MB_IN_BYTES, |
| 3224 | ZonedDateTime.now().minusHours(30).toInstant().toEpochMilli()) |
| 3225 | .build()); |
| 3226 | plans.add(SubscriptionPlan.Builder |
| 3227 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3228 | ZonedDateTime.now().plusDays(20)) |
| 3229 | .setTitle("G-Mobile Bonus 3G") |
| 3230 | .setSummary("Unlimited 3G data") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3231 | .setDataLimit(1 * TrafficStats.GB_IN_BYTES, |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3232 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3233 | .setDataUsage(300 * TrafficStats.MB_IN_BYTES, |
| 3234 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3235 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3236 | } else if ("unlimited".equals(fake)) { |
| 3237 | plans.add(SubscriptionPlan.Builder |
| 3238 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3239 | ZonedDateTime.now().plusDays(10)) |
| 3240 | .setTitle("G-Mobile Awesome") |
| 3241 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3242 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3243 | .setDataUsage(50 * TrafficStats.MB_IN_BYTES, |
| 3244 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3245 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3246 | } |
| 3247 | return plans.toArray(new SubscriptionPlan[plans.size()]); |
| 3248 | } |
| 3249 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 3250 | synchronized (mNetworkPoliciesSecondLock) { |
| 3251 | // Only give out plan details to the package that defined them, |
| 3252 | // so that we don't risk leaking plans between apps. We always |
| 3253 | // let in core system components (like the Settings app). |
| 3254 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
| 3255 | if (Objects.equals(ownerPackage, callingPackage) |
| 3256 | || (UserHandle.getCallingAppId() == android.os.Process.SYSTEM_UID)) { |
| 3257 | return mSubscriptionPlans.get(subId); |
| 3258 | } else { |
| 3259 | Log.w(TAG, "Not returning plans because caller " + callingPackage |
| 3260 | + " doesn't match owner " + ownerPackage); |
| 3261 | return null; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3262 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | @Override |
| 3267 | public void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) { |
| 3268 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3269 | enforceSubscriptionPlanValidity(plans); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3270 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3271 | for (SubscriptionPlan plan : plans) { |
| 3272 | Preconditions.checkNotNull(plan); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | final long token = Binder.clearCallingIdentity(); |
| 3276 | try { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3277 | synchronized (mUidRulesFirstLock) { |
| 3278 | synchronized (mNetworkPoliciesSecondLock) { |
| 3279 | mSubscriptionPlans.put(subId, plans); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3280 | mSubscriptionPlansOwner.put(subId, callingPackage); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3281 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3282 | final String subscriberId = mSubIdToSubscriberId.get(subId, null); |
| 3283 | if (subscriberId != null) { |
| 3284 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 3285 | maybeUpdateMobilePolicyCycleAL(subId, subscriberId); |
| 3286 | } else { |
| 3287 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 3288 | } |
| 3289 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3290 | handleNetworkPoliciesUpdateAL(true); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3291 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3292 | } |
Jeff Sharkey | e92ed6f | 2018-01-10 20:47:42 -0700 | [diff] [blame] | 3293 | |
| 3294 | final Intent intent = new Intent(SubscriptionManager.ACTION_SUBSCRIPTION_PLANS_CHANGED); |
| 3295 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3296 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); |
| 3297 | mContext.sendBroadcast(intent, android.Manifest.permission.MANAGE_SUBSCRIPTION_PLANS); |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3298 | mHandler.sendMessage( |
| 3299 | mHandler.obtainMessage(MSG_SUBSCRIPTION_PLANS_CHANGED, subId, 0, plans)); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3300 | } finally { |
| 3301 | Binder.restoreCallingIdentity(token); |
| 3302 | } |
| 3303 | } |
| 3304 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3305 | /** |
| 3306 | * Only visible for testing purposes. This doesn't give any access to |
| 3307 | * existing plans; it simply lets the debug package define new plans. |
| 3308 | */ |
| 3309 | void setSubscriptionPlansOwner(int subId, String packageName) { |
| 3310 | SystemProperties.set(PROP_SUB_PLAN_OWNER + "." + subId, packageName); |
| 3311 | } |
| 3312 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3313 | @Override |
Jeff Sharkey | 717f52f | 2018-01-04 16:04:11 -0700 | [diff] [blame] | 3314 | public String getSubscriptionPlansOwner(int subId) { |
| 3315 | if (UserHandle.getCallingAppId() != android.os.Process.SYSTEM_UID) { |
| 3316 | throw new SecurityException(); |
| 3317 | } |
| 3318 | |
| 3319 | synchronized (mNetworkPoliciesSecondLock) { |
| 3320 | return mSubscriptionPlansOwner.get(subId); |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3325 | public void setSubscriptionOverride(int subId, int overrideMask, int overrideValue, |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3326 | long timeoutMillis, String callingPackage) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3327 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3328 | |
| 3329 | // We can only override when carrier told us about plans |
| 3330 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3331 | final SubscriptionPlan plan = getPrimarySubscriptionPlanLocked(subId); |
| 3332 | if (plan == null |
| 3333 | || plan.getDataLimitBehavior() == SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3334 | throw new IllegalStateException( |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3335 | "Must provide valid SubscriptionPlan to enable overriding"); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3336 | } |
| 3337 | } |
| 3338 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 3339 | // Only allow overrides when feature is enabled. However, we always |
| 3340 | // allow disabling of overrides for safety reasons. |
| 3341 | final boolean overrideEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 3342 | NETPOLICY_OVERRIDE_ENABLED, 1) != 0; |
| 3343 | if (overrideEnabled || overrideValue == 0) { |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3344 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3345 | overrideMask, overrideValue, subId)); |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 3346 | if (timeoutMillis > 0) { |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 3347 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3348 | overrideMask, 0, subId), timeoutMillis); |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 3349 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3354 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3355 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3356 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3357 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 3358 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3359 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3360 | for (String arg : args) { |
| 3361 | argSet.add(arg); |
| 3362 | } |
| 3363 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3364 | synchronized (mUidRulesFirstLock) { |
| 3365 | synchronized (mNetworkPoliciesSecondLock) { |
| 3366 | if (argSet.contains("--unsnooze")) { |
| 3367 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 3368 | mNetworkPolicy.valueAt(i).clearSnooze(); |
| 3369 | } |
| 3370 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 3371 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3372 | |
| 3373 | fout.println("Cleared snooze timestamps"); |
| 3374 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3375 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 3376 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3377 | fout.print("System ready: "); fout.println(mSystemReady); |
| 3378 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
| 3379 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
| 3380 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3381 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 3382 | |
| 3383 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3384 | fout.println("Network policies:"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3385 | fout.increaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3386 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 3387 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
| 3388 | } |
| 3389 | fout.decreaseIndent(); |
| 3390 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3391 | fout.println(); |
| 3392 | fout.println("Subscription plans:"); |
| 3393 | fout.increaseIndent(); |
| 3394 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 3395 | final int subId = mSubscriptionPlans.keyAt(i); |
| 3396 | fout.println("Subscriber ID " + subId + ":"); |
| 3397 | fout.increaseIndent(); |
| 3398 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 3399 | if (!ArrayUtils.isEmpty(plans)) { |
| 3400 | for (SubscriptionPlan plan : plans) { |
| 3401 | fout.println(plan); |
| 3402 | } |
| 3403 | } |
| 3404 | fout.decreaseIndent(); |
| 3405 | } |
| 3406 | fout.decreaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3407 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3408 | fout.println(); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3409 | fout.println("Active subscriptions:"); |
| 3410 | fout.increaseIndent(); |
| 3411 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 3412 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 3413 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
| 3414 | |
| 3415 | fout.println(subId + "=" + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 3416 | } |
| 3417 | fout.decreaseIndent(); |
| 3418 | |
| 3419 | fout.println(); |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 3420 | for (String[] mergedSubscribers : mMergedSubscriberIds) { |
| 3421 | fout.println("Merged subscriptions: " + Arrays.toString( |
| 3422 | NetworkIdentity.scrubSubscriberId(mergedSubscribers))); |
| 3423 | } |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3424 | |
| 3425 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3426 | fout.println("Policy for UIDs:"); |
| 3427 | fout.increaseIndent(); |
| 3428 | int size = mUidPolicy.size(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3429 | for (int i = 0; i < size; i++) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3430 | final int uid = mUidPolicy.keyAt(i); |
| 3431 | final int policy = mUidPolicy.valueAt(i); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3432 | fout.print("UID="); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3433 | fout.print(uid); |
| 3434 | fout.print(" policy="); |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 3435 | fout.print(uidPoliciesToString(policy)); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3436 | fout.println(); |
| 3437 | } |
| 3438 | fout.decreaseIndent(); |
| 3439 | |
| 3440 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 3441 | if (size > 0) { |
| 3442 | fout.println("Power save whitelist (except idle) app ids:"); |
| 3443 | fout.increaseIndent(); |
| 3444 | for (int i = 0; i < size; i++) { |
| 3445 | fout.print("UID="); |
| 3446 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 3447 | fout.print(": "); |
| 3448 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 3449 | fout.println(); |
| 3450 | } |
| 3451 | fout.decreaseIndent(); |
| 3452 | } |
| 3453 | |
| 3454 | size = mPowerSaveWhitelistAppIds.size(); |
| 3455 | if (size > 0) { |
| 3456 | fout.println("Power save whitelist app ids:"); |
| 3457 | fout.increaseIndent(); |
| 3458 | for (int i = 0; i < size; i++) { |
| 3459 | fout.print("UID="); |
| 3460 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 3461 | fout.print(": "); |
| 3462 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 3463 | fout.println(); |
| 3464 | } |
| 3465 | fout.decreaseIndent(); |
| 3466 | } |
| 3467 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3468 | size = mAppIdleTempWhitelistAppIds.size(); |
| 3469 | if (size > 0) { |
| 3470 | fout.println("App idle whitelist app ids:"); |
| 3471 | fout.increaseIndent(); |
| 3472 | for (int i = 0; i < size; i++) { |
| 3473 | fout.print("UID="); |
| 3474 | fout.print(mAppIdleTempWhitelistAppIds.keyAt(i)); |
| 3475 | fout.print(": "); |
| 3476 | fout.print(mAppIdleTempWhitelistAppIds.valueAt(i)); |
| 3477 | fout.println(); |
| 3478 | } |
| 3479 | fout.decreaseIndent(); |
| 3480 | } |
| 3481 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3482 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 3483 | if (size > 0) { |
| 3484 | fout.println("Default restrict background whitelist uids:"); |
| 3485 | fout.increaseIndent(); |
| 3486 | for (int i = 0; i < size; i++) { |
| 3487 | fout.print("UID="); |
| 3488 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 3489 | fout.println(); |
| 3490 | } |
| 3491 | fout.decreaseIndent(); |
| 3492 | } |
| 3493 | |
| 3494 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 3495 | if (size > 0) { |
| 3496 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 3497 | fout.increaseIndent(); |
| 3498 | for (int i = 0; i < size; i++) { |
| 3499 | fout.print("UID="); |
| 3500 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 3501 | fout.println(); |
| 3502 | } |
| 3503 | fout.decreaseIndent(); |
| 3504 | } |
| 3505 | |
| 3506 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 3507 | collectKeys(mUidState, knownUids); |
| 3508 | collectKeys(mUidRules, knownUids); |
| 3509 | |
| 3510 | fout.println("Status for all known UIDs:"); |
| 3511 | fout.increaseIndent(); |
| 3512 | size = knownUids.size(); |
| 3513 | for (int i = 0; i < size; i++) { |
| 3514 | final int uid = knownUids.keyAt(i); |
| 3515 | fout.print("UID="); |
| 3516 | fout.print(uid); |
| 3517 | |
| 3518 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3519 | fout.print(" state="); |
| 3520 | fout.print(state); |
| 3521 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 3522 | fout.print(" (fg)"); |
| 3523 | } else { |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 3524 | fout.print(state <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3525 | ? " (fg svc)" : " (bg)"); |
| 3526 | } |
| 3527 | |
| 3528 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3529 | fout.print(" rules="); |
| 3530 | fout.print(uidRulesToString(uidRules)); |
| 3531 | fout.println(); |
| 3532 | } |
| 3533 | fout.decreaseIndent(); |
| 3534 | |
| 3535 | fout.println("Status for just UIDs with rules:"); |
| 3536 | fout.increaseIndent(); |
| 3537 | size = mUidRules.size(); |
| 3538 | for (int i = 0; i < size; i++) { |
| 3539 | final int uid = mUidRules.keyAt(i); |
| 3540 | fout.print("UID="); |
| 3541 | fout.print(uid); |
| 3542 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3543 | fout.print(" rules="); |
| 3544 | fout.print(uidRulesToString(uidRules)); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3545 | fout.println(); |
| 3546 | } |
| 3547 | fout.decreaseIndent(); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 3548 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 3549 | fout.println("Admin restricted uids for metered data:"); |
| 3550 | fout.increaseIndent(); |
| 3551 | size = mMeteredRestrictedUids.size(); |
| 3552 | for (int i = 0; i < size; ++i) { |
| 3553 | fout.print("u" + mMeteredRestrictedUids.keyAt(i) + ": "); |
| 3554 | fout.println(mMeteredRestrictedUids.valueAt(i)); |
| 3555 | } |
| 3556 | fout.decreaseIndent(); |
| 3557 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 3558 | fout.println(); |
| 3559 | mStatLogger.dump(fout); |
| 3560 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 3561 | mLogger.dumpLogs(fout); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3562 | } |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3563 | } |
| 3564 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3565 | |
| 3566 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3567 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3568 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 3569 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3570 | this, in, out, err, args, callback, resultReceiver); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3571 | } |
| 3572 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3573 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3574 | boolean isUidForeground(int uid) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3575 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3576 | return isUidStateForeground( |
| 3577 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3578 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3581 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3582 | private boolean isUidForegroundOnRestrictBackgroundUL(int uid) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3583 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3584 | return isProcStateAllowedWhileOnRestrictBackground(procState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3585 | } |
| 3586 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3587 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3588 | private boolean isUidForegroundOnRestrictPowerUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3589 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3590 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 3591 | } |
| 3592 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3593 | private boolean isUidStateForeground(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3594 | // only really in foreground when screen is also on |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3595 | return state <= NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3598 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3599 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3600 | * {@link #updateRulesForDataUsageRestrictionsUL(int)} and |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3601 | * {@link #updateRulesForPowerRestrictionsUL(int)}. Returns true if the state was updated. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3602 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3603 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3604 | private boolean updateUidStateUL(int uid, int uidState) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3605 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL"); |
| 3606 | try { |
| 3607 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3608 | if (oldUidState != uidState) { |
| 3609 | // state changed, push updated rules |
| 3610 | mUidState.put(uid, uidState); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3611 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState); |
| 3612 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 3613 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
Sudheer Shanka | 9e77d23 | 2017-08-14 14:43:11 -0700 | [diff] [blame] | 3614 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3615 | if (mDeviceIdleMode) { |
| 3616 | updateRuleForDeviceIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3617 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3618 | if (mRestrictPower) { |
| 3619 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3620 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3621 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3622 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3623 | return true; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3624 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3625 | } finally { |
| 3626 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3627 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3628 | return false; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3629 | } |
| 3630 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3631 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3632 | private boolean removeUidStateUL(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3633 | final int index = mUidState.indexOfKey(uid); |
| 3634 | if (index >= 0) { |
| 3635 | final int oldUidState = mUidState.valueAt(index); |
| 3636 | mUidState.removeAt(index); |
| 3637 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3638 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3639 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3640 | if (mDeviceIdleMode) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3641 | updateRuleForDeviceIdleUL(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3642 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3643 | if (mRestrictPower) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3644 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3645 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3646 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3647 | return true; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3648 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3649 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3650 | return false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3653 | // adjust stats accounting based on foreground status |
| 3654 | private void updateNetworkStats(int uid, boolean uidForeground) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3655 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3656 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3657 | "updateNetworkStats: " + uid + "/" + (uidForeground ? "F" : "B")); |
| 3658 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3659 | try { |
| 3660 | mNetworkStats.setUidForeground(uid, uidForeground); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3661 | } finally { |
| 3662 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3666 | private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState, |
| 3667 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3668 | final boolean oldForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3669 | isProcStateAllowedWhileOnRestrictBackground(oldUidState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3670 | final boolean newForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3671 | isProcStateAllowedWhileOnRestrictBackground(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3672 | if (oldForeground != newForeground) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3673 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3674 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3677 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3678 | void updateRulesForPowerSaveUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3679 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL"); |
| 3680 | try { |
| 3681 | updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 3682 | mUidFirewallPowerSaveRules); |
| 3683 | } finally { |
| 3684 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3685 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3686 | } |
| 3687 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3688 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3689 | void updateRuleForRestrictPowerUL(int uid) { |
| 3690 | updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3691 | } |
| 3692 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3693 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3694 | void updateRulesForDeviceIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3695 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL"); |
| 3696 | try { |
| 3697 | updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 3698 | mUidFirewallDozableRules); |
| 3699 | } finally { |
| 3700 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3701 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3702 | } |
| 3703 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3704 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3705 | void updateRuleForDeviceIdleUL(int uid) { |
| 3706 | updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3707 | } |
| 3708 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3709 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3710 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3711 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3712 | private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain, |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3713 | SparseIntArray rules) { |
| 3714 | if (enabled) { |
| 3715 | // 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] | 3716 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3717 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3718 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3719 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3720 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3721 | UserInfo user = users.get(ui); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3722 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveTempWhitelistAppIds, user.id); |
| 3723 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveWhitelistAppIds, user.id); |
| 3724 | if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3725 | updateRulesForWhitelistedAppIds(uidRules, |
| 3726 | mPowerSaveWhitelistExceptIdleAppIds, user.id); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3727 | } |
| 3728 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3729 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3730 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3731 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 3732 | } |
| 3733 | } |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3734 | setUidFirewallRulesUL(chain, uidRules, CHAIN_TOGGLE_ENABLE); |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3735 | } else { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3736 | setUidFirewallRulesUL(chain, null, CHAIN_TOGGLE_DISABLE); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3737 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3740 | private void updateRulesForWhitelistedAppIds(final SparseIntArray uidRules, |
| 3741 | final SparseBooleanArray whitelistedAppIds, int userId) { |
| 3742 | for (int i = whitelistedAppIds.size() - 1; i >= 0; --i) { |
| 3743 | if (whitelistedAppIds.valueAt(i)) { |
| 3744 | final int appId = whitelistedAppIds.keyAt(i); |
| 3745 | final int uid = UserHandle.getUid(userId, appId); |
| 3746 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /** |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3752 | * Returns whether a uid is whitelisted from power saving restrictions (eg: Battery Saver, Doze |
| 3753 | * mode, and app idle). |
| 3754 | * |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3755 | * @param deviceIdleMode if true then we don't consider |
| 3756 | * {@link #mPowerSaveWhitelistExceptIdleAppIds} for checking if the {@param uid} is |
| 3757 | * whitelisted. |
| 3758 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3759 | @GuardedBy("mUidRulesFirstLock") |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3760 | private boolean isWhitelistedFromPowerSaveUL(int uid, boolean deviceIdleMode) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3761 | final int appId = UserHandle.getAppId(uid); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3762 | boolean isWhitelisted = mPowerSaveTempWhitelistAppIds.get(appId) |
| 3763 | || mPowerSaveWhitelistAppIds.get(appId); |
| 3764 | if (!deviceIdleMode) { |
| 3765 | isWhitelisted = isWhitelisted || mPowerSaveWhitelistExceptIdleAppIds.get(appId); |
| 3766 | } |
| 3767 | return isWhitelisted; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3770 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3771 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
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 | private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3774 | if (enabled) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3775 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3776 | chain == FIREWALL_CHAIN_DOZABLE); |
| 3777 | if (isWhitelisted || isUidForegroundOnRestrictPowerUL(uid)) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3778 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3779 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3780 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3785 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3786 | void updateRulesForAppIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3787 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL"); |
| 3788 | try { |
| 3789 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 3790 | uidRules.clear(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3791 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3792 | // Fully update the app idle firewall chain. |
| 3793 | final List<UserInfo> users = mUserManager.getUsers(); |
| 3794 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3795 | UserInfo user = users.get(ui); |
| 3796 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 3797 | for (int uid : idleUids) { |
| 3798 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
| 3799 | // quick check: if this uid doesn't have INTERNET permission, it |
| 3800 | // doesn't have network access anyway, so it is a waste to mess |
| 3801 | // with it here. |
| 3802 | if (hasInternetPermissions(uid)) { |
| 3803 | uidRules.put(uid, FIREWALL_RULE_DENY); |
| 3804 | } |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 3805 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3806 | } |
| 3807 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3808 | |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3809 | setUidFirewallRulesUL(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3810 | } finally { |
| 3811 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3812 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3815 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3816 | void updateRuleForAppIdleUL(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3817 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3818 | |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3819 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3820 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRuleForAppIdleUL: " + uid ); |
| 3821 | } |
| 3822 | try { |
| 3823 | int appId = UserHandle.getAppId(uid); |
| 3824 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 3825 | && !isUidForegroundOnRestrictPowerUL(uid)) { |
| 3826 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3827 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3828 | } else { |
| 3829 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3830 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL " + uid + " to DEFAULT"); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3831 | } |
| 3832 | } finally { |
| 3833 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3834 | } |
| 3835 | } |
| 3836 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3837 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3838 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 3839 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3840 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3841 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3842 | private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3843 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3844 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3845 | "updateRulesForGlobalChangeAL: " + (restrictedNetworksChanged ? "R" : "-")); |
| 3846 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3847 | try { |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3848 | updateRulesForAppIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3849 | updateRulesForRestrictPowerUL(); |
| 3850 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3851 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3852 | // If the set of restricted networks may have changed, re-evaluate those. |
| 3853 | if (restrictedNetworksChanged) { |
| 3854 | normalizePoliciesNL(); |
| 3855 | updateNetworkRulesNL(); |
| 3856 | } |
| 3857 | } finally { |
| 3858 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3862 | // 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] | 3863 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3864 | private void updateRulesForRestrictPowerUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3865 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL"); |
| 3866 | try { |
| 3867 | updateRulesForDeviceIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3868 | updateRulesForPowerSaveUL(); |
| 3869 | updateRulesForAllAppsUL(TYPE_RESTRICT_POWER); |
| 3870 | } finally { |
| 3871 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3872 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3875 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3876 | private void updateRulesForRestrictBackgroundUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3877 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL"); |
| 3878 | try { |
| 3879 | updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND); |
| 3880 | } finally { |
| 3881 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3882 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 3886 | private static final int TYPE_RESTRICT_POWER = 2; |
| 3887 | @Retention(RetentionPolicy.SOURCE) |
| 3888 | @IntDef(flag = false, value = { |
| 3889 | TYPE_RESTRICT_BACKGROUND, |
| 3890 | TYPE_RESTRICT_POWER, |
| 3891 | }) |
| 3892 | public @interface RestrictType { |
| 3893 | } |
| 3894 | |
| 3895 | // 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] | 3896 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3897 | private void updateRulesForAllAppsUL(@RestrictType int type) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3898 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3899 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type); |
| 3900 | } |
| 3901 | try { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3902 | // update rules for all installed applications |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3903 | |
| 3904 | final PackageManager pm = mContext.getPackageManager(); |
| 3905 | final List<UserInfo> users; |
| 3906 | final List<ApplicationInfo> apps; |
| 3907 | |
| 3908 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-users"); |
| 3909 | try { |
| 3910 | users = mUserManager.getUsers(); |
| 3911 | } finally { |
| 3912 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3913 | } |
| 3914 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-uids"); |
| 3915 | try { |
| 3916 | apps = pm.getInstalledApplications( |
| 3917 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS |
| 3918 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 3919 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 3920 | } finally { |
| 3921 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3922 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3923 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3924 | final int usersSize = users.size(); |
| 3925 | final int appsSize = apps.size(); |
| 3926 | for (int i = 0; i < usersSize; i++) { |
| 3927 | final UserInfo user = users.get(i); |
| 3928 | for (int j = 0; j < appsSize; j++) { |
| 3929 | final ApplicationInfo app = apps.get(j); |
| 3930 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 3931 | switch (type) { |
| 3932 | case TYPE_RESTRICT_BACKGROUND: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3933 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3934 | break; |
| 3935 | case TYPE_RESTRICT_POWER: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3936 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3937 | break; |
| 3938 | default: |
| 3939 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 3940 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3941 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3942 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3943 | } finally { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3944 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3945 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3948 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3949 | private void updateRulesForTempWhitelistChangeUL(int appId) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3950 | final List<UserInfo> users = mUserManager.getUsers(); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3951 | final int numUsers = users.size(); |
| 3952 | for (int i = 0; i < numUsers; i++) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3953 | final UserInfo user = users.get(i); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3954 | int uid = UserHandle.getUid(user.id, appId); |
| 3955 | // Update external firewall rules. |
| 3956 | updateRuleForAppIdleUL(uid); |
| 3957 | updateRuleForDeviceIdleUL(uid); |
| 3958 | updateRuleForRestrictPowerUL(uid); |
| 3959 | // Update internal rules. |
| 3960 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3961 | } |
| 3962 | } |
| 3963 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3964 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 3965 | // methods below could be merged into a isUidValidForRules() method. |
| 3966 | private boolean isUidValidForBlacklistRules(int uid) { |
| 3967 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3968 | 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] | 3969 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3970 | return true; |
| 3971 | } |
| 3972 | |
| 3973 | return false; |
| 3974 | } |
| 3975 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3976 | private boolean isUidValidForWhitelistRules(int uid) { |
| 3977 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 3978 | } |
| 3979 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3980 | /** |
| 3981 | * Set whether or not an app should be whitelisted for network access while in app idle. Other |
| 3982 | * power saving restrictions may still apply. |
| 3983 | */ |
| 3984 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3985 | void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
Sudheer Shanka | db02ccd | 2018-11-29 11:27:21 -0800 | [diff] [blame] | 3986 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 3987 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3988 | synchronized (mUidRulesFirstLock) { |
| 3989 | if (mAppIdleTempWhitelistAppIds.get(uid) == shouldWhitelist) { |
| 3990 | // No change. |
| 3991 | return; |
| 3992 | } |
| 3993 | |
| 3994 | final long token = Binder.clearCallingIdentity(); |
| 3995 | try { |
| 3996 | mLogger.appIdleWlChanged(uid, shouldWhitelist); |
| 3997 | if (shouldWhitelist) { |
| 3998 | mAppIdleTempWhitelistAppIds.put(uid, true); |
| 3999 | } else { |
| 4000 | mAppIdleTempWhitelistAppIds.delete(uid); |
| 4001 | } |
| 4002 | updateRuleForAppIdleUL(uid); |
| 4003 | updateRulesForPowerRestrictionsUL(uid); |
| 4004 | } finally { |
| 4005 | Binder.restoreCallingIdentity(token); |
| 4006 | } |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | /** Return the list of UIDs currently in the app idle whitelist. */ |
| 4011 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4012 | int[] getAppIdleWhitelist() { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4013 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 4014 | |
| 4015 | synchronized (mUidRulesFirstLock) { |
| 4016 | final int len = mAppIdleTempWhitelistAppIds.size(); |
| 4017 | int[] uids = new int[len]; |
| 4018 | for (int i = 0; i < len; ++i) { |
| 4019 | uids[i] = mAppIdleTempWhitelistAppIds.keyAt(i); |
| 4020 | } |
| 4021 | return uids; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | /** Returns if the UID is currently considered idle. */ |
| 4026 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4027 | boolean isUidIdle(int uid) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4028 | synchronized (mUidRulesFirstLock) { |
| 4029 | if (mAppIdleTempWhitelistAppIds.get(uid)) { |
| 4030 | // UID is temporarily whitelisted. |
| 4031 | return false; |
| 4032 | } |
| 4033 | } |
| 4034 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4035 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 4036 | final int userId = UserHandle.getUserId(uid); |
| 4037 | |
songjinshi | 0655edd | 2016-05-18 19:55:32 +0800 | [diff] [blame] | 4038 | if (packages != null) { |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 4039 | for (String packageName : packages) { |
| 4040 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 4041 | return false; |
| 4042 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | return true; |
| 4046 | } |
| 4047 | |
| 4048 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4049 | * Checks if an uid has INTERNET permissions. |
| 4050 | * <p> |
| 4051 | * 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] | 4052 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4053 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4054 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 4055 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4056 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4057 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4058 | } |
| 4059 | } catch (RemoteException e) { |
| 4060 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4061 | return true; |
| 4062 | } |
| 4063 | |
| 4064 | /** |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4065 | * Clears all state - internal and external - associated with an UID. |
| 4066 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4067 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4068 | private void onUidDeletedUL(int uid) { |
| 4069 | // First cleanup in-memory state synchronously... |
| 4070 | mUidRules.delete(uid); |
| 4071 | mUidPolicy.delete(uid); |
| 4072 | mUidFirewallStandbyRules.delete(uid); |
| 4073 | mUidFirewallDozableRules.delete(uid); |
| 4074 | mUidFirewallPowerSaveRules.delete(uid); |
| 4075 | mPowerSaveWhitelistExceptIdleAppIds.delete(uid); |
| 4076 | mPowerSaveWhitelistAppIds.delete(uid); |
| 4077 | mPowerSaveTempWhitelistAppIds.delete(uid); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4078 | mAppIdleTempWhitelistAppIds.delete(uid); |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4079 | |
| 4080 | // ...then update iptables asynchronously. |
| 4081 | mHandler.obtainMessage(MSG_RESET_FIREWALL_RULES_BY_UID, uid, 0).sendToTarget(); |
| 4082 | } |
| 4083 | |
| 4084 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4085 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4086 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4087 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4088 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4089 | * <li>Doze mode |
| 4090 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4091 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4092 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4093 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4094 | * |
| 4095 | * <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] | 4096 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4097 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4098 | private void updateRestrictionRulesForUidUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4099 | // Methods below only changes the firewall rules for the power-related modes. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4100 | updateRuleForDeviceIdleUL(uid); |
| 4101 | updateRuleForAppIdleUL(uid); |
| 4102 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4103 | |
| 4104 | // Update internal state for power-related modes. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4105 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4106 | |
| 4107 | // Update firewall and internal rules for Data Saver Mode. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4108 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4109 | } |
| 4110 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4111 | /** |
| 4112 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 4113 | * restrictions (blacklist / whitelist). |
| 4114 | * |
| 4115 | * <p> |
| 4116 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 4117 | * networks: |
| 4118 | * <ul> |
| 4119 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 4120 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 4121 | * also blacklisted. |
| 4122 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4123 | * no UIDs other than those whitelisted will have access. |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4124 | * <ul> |
| 4125 | * |
| 4126 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 4127 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 4128 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 4129 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4130 | * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4131 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 4132 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 4133 | * following rules should also be applied: |
| 4134 | * |
| 4135 | * <ul> |
| 4136 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 4137 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 4138 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 4139 | * {@code bw_penalty_box}. |
| 4140 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 4141 | * </ul> |
| 4142 | * |
| 4143 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 4144 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 4145 | * have permission to use the internet. |
| 4146 | * |
| 4147 | * <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] | 4148 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4149 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4150 | private void updateRulesForDataUsageRestrictionsUL(int uid) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4151 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4152 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4153 | "updateRulesForDataUsageRestrictionsUL: " + uid); |
| 4154 | } |
| 4155 | try { |
| 4156 | updateRulesForDataUsageRestrictionsULInner(uid); |
| 4157 | } finally { |
| 4158 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | private void updateRulesForDataUsageRestrictionsULInner(int uid) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4163 | if (!isUidValidForWhitelistRules(uid)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4164 | 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] | 4165 | return; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4166 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4167 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 4168 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4169 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4170 | final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid); |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4171 | final boolean isRestrictedByAdmin = isRestrictedByAdminUL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 4172 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4173 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 4174 | final boolean isWhitelisted = (uidPolicy & POLICY_ALLOW_METERED_BACKGROUND) != 0; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4175 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 4176 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4177 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4178 | // 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] | 4179 | if (isRestrictedByAdmin) { |
| 4180 | newRule = RULE_REJECT_METERED; |
| 4181 | } else if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4182 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 4183 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 4184 | } else if (isWhitelisted) { |
| 4185 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4186 | } |
| 4187 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4188 | if (isBlacklisted) { |
| 4189 | newRule = RULE_REJECT_METERED; |
| 4190 | } else if (mRestrictBackground && isWhitelisted) { |
| 4191 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4192 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4193 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4194 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4195 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4196 | if (LOGV) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4197 | Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4198 | + ": isForeground=" +isForeground |
| 4199 | + ", isBlacklisted=" + isBlacklisted |
| 4200 | + ", isWhitelisted=" + isWhitelisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4201 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4202 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4203 | + ", newRule=" + uidRulesToString(newRule) |
| 4204 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4205 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4206 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4207 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4208 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4209 | mUidRules.delete(uid); |
| 4210 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4211 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4212 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4213 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4214 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4215 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4216 | if (hasRule(newRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4217 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 4218 | // (since bw_penalty_box prevails over bw_happy_box). |
| 4219 | |
| 4220 | setMeteredNetworkWhitelist(uid, true); |
| 4221 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 4222 | // but ideally it should be just: |
| 4223 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4224 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4225 | setMeteredNetworkBlacklist(uid, false); |
| 4226 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4227 | } else if (hasRule(oldRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4228 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 4229 | |
| 4230 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 4231 | // but ideally they should be just: |
| 4232 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 4233 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4234 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4235 | setMeteredNetworkWhitelist(uid, false); |
| 4236 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4237 | if (isBlacklisted || isRestrictedByAdmin) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4238 | setMeteredNetworkBlacklist(uid, true); |
| 4239 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4240 | } else if (hasRule(newRule, RULE_REJECT_METERED) |
| 4241 | || hasRule(oldRule, RULE_REJECT_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4242 | // Flip state because app was explicitly added or removed to blacklist. |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4243 | setMeteredNetworkBlacklist(uid, (isBlacklisted || isRestrictedByAdmin)); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4244 | if (hasRule(oldRule, RULE_REJECT_METERED) && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4245 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 4246 | // where app is whitelisted and blacklisted at the same time (although such |
| 4247 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4248 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4249 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4250 | } else if (hasRule(newRule, RULE_ALLOW_METERED) |
| 4251 | || hasRule(oldRule, RULE_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4252 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4253 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4254 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4255 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4256 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 4257 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4258 | + ", whitelisted=" + isWhitelisted |
| 4259 | + ", blacklisted=" + isBlacklisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4260 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4261 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4262 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4263 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4264 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4265 | // Dispatch changed rule to existing listeners. |
| 4266 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | /** |
| 4271 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 4272 | * listeners in case of change. |
| 4273 | * <p> |
| 4274 | * There are 3 power-related rules that affects whether an app has background access on |
| 4275 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 4276 | * restriction, it's added to the equivalent firewall chain: |
| 4277 | * <ul> |
| 4278 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 4279 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 4280 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 4281 | * </ul> |
| 4282 | * <p> |
| 4283 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 4284 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 4285 | * <p> |
| 4286 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 4287 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4288 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4289 | private void updateRulesForPowerRestrictionsUL(int uid) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4290 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 4291 | |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4292 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4293 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4294 | if (newUidRules == RULE_NONE) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4295 | mUidRules.delete(uid); |
| 4296 | } else { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4297 | mUidRules.put(uid, newUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | /** |
| 4302 | * Similar to above but ignores idle state if app standby is currently disabled by parole. |
| 4303 | * |
| 4304 | * @param uid the uid of the app to update rules for |
| 4305 | * @param oldUidRules the current rules for the uid, in order to determine if there's a change |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4306 | * |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4307 | * @return the new computed rules for the uid |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4308 | */ |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4309 | private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4310 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4311 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4312 | "updateRulesForPowerRestrictionsUL: " + uid + "/" + oldUidRules); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4313 | } |
| 4314 | try { |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4315 | return updateRulesForPowerRestrictionsULInner(uid, oldUidRules); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4316 | } finally { |
| 4317 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4318 | } |
| 4319 | } |
| 4320 | |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4321 | private int updateRulesForPowerRestrictionsULInner(int uid, int oldUidRules) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4322 | if (!isUidValidForBlacklistRules(uid)) { |
| 4323 | 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] | 4324 | return RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4325 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4326 | |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4327 | final boolean isIdle = isUidIdle(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4328 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4329 | final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4330 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4331 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, mDeviceIdleMode); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4332 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 4333 | int newRule = RULE_NONE; |
| 4334 | |
| 4335 | // First step: define the new rule based on user restrictions and foreground state. |
| 4336 | |
| 4337 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 4338 | // by considering the foreground and non-foreground states. |
| 4339 | if (isForeground) { |
| 4340 | if (restrictMode) { |
| 4341 | newRule = RULE_ALLOW_ALL; |
| 4342 | } |
| 4343 | } else if (restrictMode) { |
| 4344 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 4345 | } |
| 4346 | |
| 4347 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 4348 | |
| 4349 | if (LOGV) { |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 4350 | Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4351 | + ", isIdle: " + isIdle |
| 4352 | + ", mRestrictPower: " + mRestrictPower |
| 4353 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 4354 | + ", isForeground=" + isForeground |
| 4355 | + ", isWhitelisted=" + isWhitelisted |
| 4356 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4357 | + ", newRule=" + uidRulesToString(newRule) |
| 4358 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4359 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 4360 | } |
| 4361 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4362 | // Second step: notify listeners if state changed. |
| 4363 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4364 | if (newRule == RULE_NONE || hasRule(newRule, RULE_ALLOW_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4365 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4366 | } else if (hasRule(newRule, RULE_REJECT_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4367 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 4368 | } else { |
| 4369 | // All scenarios should have been covered above |
| 4370 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 4371 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4372 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4373 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4374 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 4375 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4376 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4377 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4378 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4379 | return newUidRules; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame^] | 4382 | private class NetPolicyAppIdleStateChangeListener extends AppIdleStateChangeListener { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4383 | @Override |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 4384 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, int bucket, |
| 4385 | int reason) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4386 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 4387 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 4388 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4389 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4390 | mLogger.appIdleStateChanged(uid, idle); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4391 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4392 | updateRulesForPowerRestrictionsUL(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4393 | } |
| 4394 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4395 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4396 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4397 | } |
| 4398 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4399 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 4400 | if (listener != null) { |
| 4401 | try { |
| 4402 | listener.onUidRulesChanged(uid, uidRules); |
| 4403 | } catch (RemoteException ignored) { |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 4409 | String[] meteredIfaces) { |
| 4410 | if (listener != null) { |
| 4411 | try { |
| 4412 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 4413 | } catch (RemoteException ignored) { |
| 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 4419 | boolean restrictBackground) { |
| 4420 | if (listener != null) { |
| 4421 | try { |
| 4422 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 4423 | } catch (RemoteException ignored) { |
| 4424 | } |
| 4425 | } |
| 4426 | } |
| 4427 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4428 | private void dispatchUidPoliciesChanged(INetworkPolicyListener listener, int uid, |
| 4429 | int uidPolicies) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4430 | if (listener != null) { |
| 4431 | try { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4432 | listener.onUidPoliciesChanged(uid, uidPolicies); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4433 | } catch (RemoteException ignored) { |
| 4434 | } |
| 4435 | } |
| 4436 | } |
| 4437 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4438 | private void dispatchSubscriptionOverride(INetworkPolicyListener listener, int subId, |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 4439 | int overrideMask, int overrideValue) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4440 | if (listener != null) { |
| 4441 | try { |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 4442 | listener.onSubscriptionOverride(subId, overrideMask, overrideValue); |
| 4443 | } catch (RemoteException ignored) { |
| 4444 | } |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | private void dispatchSubscriptionPlansChanged(INetworkPolicyListener listener, int subId, |
| 4449 | SubscriptionPlan[] plans) { |
| 4450 | if (listener != null) { |
| 4451 | try { |
| 4452 | listener.onSubscriptionPlansChanged(subId, plans); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4453 | } catch (RemoteException ignored) { |
| 4454 | } |
| 4455 | } |
| 4456 | } |
| 4457 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4458 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 4459 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4460 | public boolean handleMessage(Message msg) { |
| 4461 | switch (msg.what) { |
| 4462 | case MSG_RULES_CHANGED: { |
| 4463 | final int uid = msg.arg1; |
| 4464 | final int uidRules = msg.arg2; |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4465 | final int length = mListeners.beginBroadcast(); |
| 4466 | for (int i = 0; i < length; i++) { |
| 4467 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4468 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4469 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4470 | mListeners.finishBroadcast(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4471 | return true; |
| 4472 | } |
| 4473 | case MSG_METERED_IFACES_CHANGED: { |
| 4474 | final String[] meteredIfaces = (String[]) msg.obj; |
| 4475 | final int length = mListeners.beginBroadcast(); |
| 4476 | for (int i = 0; i < length; i++) { |
| 4477 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4478 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4479 | } |
| 4480 | mListeners.finishBroadcast(); |
| 4481 | return true; |
| 4482 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4483 | case MSG_LIMIT_REACHED: { |
| 4484 | final String iface = (String) msg.obj; |
| 4485 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4486 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4487 | if (mMeteredIfaces.contains(iface)) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4488 | // force stats update to make sure we have |
| 4489 | // numbers that caused alert to trigger. |
| 4490 | mNetworkStats.forceUpdate(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4491 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4492 | updateNetworkEnabledNL(); |
| 4493 | updateNotificationsNL(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | return true; |
| 4497 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4498 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 4499 | final boolean restrictBackground = msg.arg1 != 0; |
| 4500 | final int length = mListeners.beginBroadcast(); |
| 4501 | for (int i = 0; i < length; i++) { |
| 4502 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4503 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4504 | } |
| 4505 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4506 | final Intent intent = |
| 4507 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4508 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4509 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 4510 | return true; |
| 4511 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4512 | case MSG_POLICIES_CHANGED: { |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4513 | final int uid = msg.arg1; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4514 | final int policy = msg.arg2; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4515 | final Boolean notifyApp = (Boolean) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4516 | // First notify internal listeners... |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4517 | final int length = mListeners.beginBroadcast(); |
| 4518 | for (int i = 0; i < length; i++) { |
| 4519 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4520 | dispatchUidPoliciesChanged(listener, uid, policy); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4521 | } |
| 4522 | mListeners.finishBroadcast(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4523 | // ...then apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
| 4524 | if (notifyApp.booleanValue()) { |
| 4525 | broadcastRestrictBackgroundChanged(uid, notifyApp); |
| 4526 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4527 | return true; |
| 4528 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4529 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 4530 | final long lowestRule = (Long) msg.obj; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4531 | // make sure stats are recorded frequently enough; we aim |
| 4532 | // for 2MB threshold for 2GB/month rules. |
| 4533 | final long persistThreshold = lowestRule / 1000; |
| 4534 | mNetworkStats.advisePersistThreshold(persistThreshold); |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4535 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4536 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 4537 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 4538 | removeInterfaceQuota((String) msg.obj); |
| 4539 | // int params need to be stitched back into a long |
| 4540 | setInterfaceQuota((String) msg.obj, |
| 4541 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 4542 | return true; |
| 4543 | } |
| 4544 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 4545 | removeInterfaceQuota((String) msg.obj); |
| 4546 | return true; |
| 4547 | } |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4548 | case MSG_RESET_FIREWALL_RULES_BY_UID: { |
| 4549 | resetUidFirewallRules(msg.arg1); |
| 4550 | return true; |
| 4551 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4552 | case MSG_SUBSCRIPTION_OVERRIDE: { |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 4553 | final int overrideMask = msg.arg1; |
| 4554 | final int overrideValue = msg.arg2; |
| 4555 | final int subId = (int) msg.obj; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4556 | final int length = mListeners.beginBroadcast(); |
| 4557 | for (int i = 0; i < length; i++) { |
| 4558 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 4559 | dispatchSubscriptionOverride(listener, subId, overrideMask, overrideValue); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4560 | } |
| 4561 | mListeners.finishBroadcast(); |
| 4562 | return true; |
| 4563 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4564 | case MSG_METERED_RESTRICTED_PACKAGES_CHANGED: { |
| 4565 | final int userId = msg.arg1; |
| 4566 | final Set<String> packageNames = (Set<String>) msg.obj; |
| 4567 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 4568 | return true; |
| 4569 | } |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 4570 | case MSG_SET_NETWORK_TEMPLATE_ENABLED: { |
| 4571 | final NetworkTemplate template = (NetworkTemplate) msg.obj; |
| 4572 | final boolean enabled = msg.arg1 != 0; |
| 4573 | setNetworkTemplateEnabledInner(template, enabled); |
| 4574 | return true; |
| 4575 | } |
Sarah Chin | 7af1fd0 | 2019-09-26 11:37:13 -0700 | [diff] [blame] | 4576 | case MSG_SUBSCRIPTION_PLANS_CHANGED: { |
| 4577 | final SubscriptionPlan[] plans = (SubscriptionPlan[]) msg.obj; |
| 4578 | final int subId = msg.arg1; |
| 4579 | final int length = mListeners.beginBroadcast(); |
| 4580 | for (int i = 0; i < length; i++) { |
| 4581 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4582 | dispatchSubscriptionPlansChanged(listener, subId, plans); |
| 4583 | } |
| 4584 | mListeners.finishBroadcast(); |
| 4585 | return true; |
| 4586 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4587 | default: { |
| 4588 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4592 | }; |
| 4593 | |
| 4594 | private final Handler.Callback mUidEventHandlerCallback = new Handler.Callback() { |
| 4595 | @Override |
| 4596 | public boolean handleMessage(Message msg) { |
| 4597 | switch (msg.what) { |
| 4598 | case UID_MSG_STATE_CHANGED: { |
| 4599 | final int uid = msg.arg1; |
| 4600 | final int procState = msg.arg2; |
| 4601 | final long procStateSeq = (Long) msg.obj; |
| 4602 | |
| 4603 | handleUidChanged(uid, procState, procStateSeq); |
| 4604 | return true; |
| 4605 | } |
| 4606 | case UID_MSG_GONE: { |
| 4607 | final int uid = msg.arg1; |
| 4608 | handleUidGone(uid); |
| 4609 | return true; |
| 4610 | } |
| 4611 | default: { |
| 4612 | return false; |
| 4613 | } |
| 4614 | } |
| 4615 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4616 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 4617 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4618 | void handleUidChanged(int uid, int procState, long procStateSeq) { |
| 4619 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged"); |
| 4620 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4621 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4622 | synchronized (mUidRulesFirstLock) { |
| 4623 | // We received a uid state change callback, add it to the history so that it |
| 4624 | // will be useful for debugging. |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4625 | mLogger.uidStateChanged(uid, procState, procStateSeq); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4626 | // Now update the network policy rules as per the updated uid state. |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4627 | updated = updateUidStateUL(uid, procState); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4628 | // Updating the network rules is done, so notify AMS about this. |
| 4629 | mActivityManagerInternal.notifyNetworkPolicyRulesUpdated(uid, procStateSeq); |
| 4630 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4631 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4632 | // called from the handler, so there's no multi-threading issue. |
| 4633 | if (updated) { |
| 4634 | updateNetworkStats(uid, isUidStateForeground(procState)); |
| 4635 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4636 | } finally { |
| 4637 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | void handleUidGone(int uid) { |
| 4642 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidGone"); |
| 4643 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4644 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4645 | synchronized (mUidRulesFirstLock) { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4646 | updated = removeUidStateUL(uid); |
| 4647 | } |
| 4648 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4649 | // called from the handler, so there's no multi-threading issue. |
| 4650 | if (updated) { |
| 4651 | updateNetworkStats(uid, false); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4652 | } |
| 4653 | } finally { |
| 4654 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4655 | } |
| 4656 | } |
| 4657 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4658 | private void broadcastRestrictBackgroundChanged(int uid, Boolean changed) { |
| 4659 | final PackageManager pm = mContext.getPackageManager(); |
| 4660 | final String[] packages = pm.getPackagesForUid(uid); |
| 4661 | if (packages != null) { |
| 4662 | final int userId = UserHandle.getUserId(uid); |
| 4663 | for (String packageName : packages) { |
| 4664 | final Intent intent = |
| 4665 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4666 | intent.setPackage(packageName); |
| 4667 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4668 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 4669 | } |
| 4670 | } |
| 4671 | } |
| 4672 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4673 | private void setInterfaceQuotaAsync(String iface, long quotaBytes) { |
| 4674 | // long quotaBytes split up into two ints to fit in message |
| 4675 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, (int) (quotaBytes >> 32), |
| 4676 | (int) (quotaBytes & 0xFFFFFFFF), iface).sendToTarget(); |
| 4677 | } |
| 4678 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4679 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4680 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4681 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4682 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4683 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4684 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4685 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4689 | private void removeInterfaceQuotaAsync(String iface) { |
| 4690 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface).sendToTarget(); |
| 4691 | } |
| 4692 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4693 | private void removeInterfaceQuota(String iface) { |
| 4694 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4695 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4696 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4697 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4698 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4699 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4700 | } |
| 4701 | } |
| 4702 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4703 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 4704 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4705 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4706 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4707 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4708 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 4709 | } catch (RemoteException e) { |
| 4710 | // ignored; service lives in system_server |
| 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 4715 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 4716 | try { |
| 4717 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 4718 | } catch (IllegalStateException e) { |
| 4719 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4720 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4721 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4725 | private static final int CHAIN_TOGGLE_NONE = 0; |
| 4726 | private static final int CHAIN_TOGGLE_ENABLE = 1; |
| 4727 | private static final int CHAIN_TOGGLE_DISABLE = 2; |
| 4728 | @Retention(RetentionPolicy.SOURCE) |
| 4729 | @IntDef(flag = false, value = { |
| 4730 | CHAIN_TOGGLE_NONE, |
| 4731 | CHAIN_TOGGLE_ENABLE, |
| 4732 | CHAIN_TOGGLE_DISABLE |
| 4733 | }) |
| 4734 | public @interface ChainToggleType { |
| 4735 | } |
| 4736 | |
| 4737 | /** |
Kweku Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 4738 | * Calls {@link #setUidFirewallRulesUL(int, SparseIntArray)} and |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4739 | * {@link #enableFirewallChainUL(int, boolean)} synchronously. |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4740 | * |
| 4741 | * @param chain firewall chain. |
| 4742 | * @param uidRules new UID rules; if {@code null}, only toggles chain state. |
| 4743 | * @param toggle whether the chain should be enabled, disabled, or not changed. |
| 4744 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4745 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4746 | private void setUidFirewallRulesUL(int chain, @Nullable SparseIntArray uidRules, |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4747 | @ChainToggleType int toggle) { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4748 | if (uidRules != null) { |
| 4749 | setUidFirewallRulesUL(chain, uidRules); |
| 4750 | } |
| 4751 | if (toggle != CHAIN_TOGGLE_NONE) { |
| 4752 | enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE); |
| 4753 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4756 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4757 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 4758 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 4759 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4760 | */ |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4761 | private void setUidFirewallRulesUL(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4762 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4763 | int size = uidRules.size(); |
| 4764 | int[] uids = new int[size]; |
| 4765 | int[] rules = new int[size]; |
| 4766 | for(int index = size - 1; index >= 0; --index) { |
| 4767 | uids[index] = uidRules.keyAt(index); |
| 4768 | rules[index] = uidRules.valueAt(index); |
| 4769 | } |
| 4770 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4771 | mLogger.firewallRulesChanged(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4772 | } catch (IllegalStateException e) { |
| 4773 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4774 | } catch (RemoteException e) { |
| 4775 | // ignored; service lives in system_server |
| 4776 | } |
| 4777 | } |
| 4778 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4779 | /** |
| 4780 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4781 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4782 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4783 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4784 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4785 | "setUidFirewallRule: " + chain + "/" + uid + "/" + rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 4786 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4787 | try { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4788 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 4789 | mUidFirewallDozableRules.put(uid, rule); |
| 4790 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 4791 | mUidFirewallStandbyRules.put(uid, rule); |
| 4792 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 4793 | mUidFirewallPowerSaveRules.put(uid, rule); |
| 4794 | } |
| 4795 | |
| 4796 | try { |
| 4797 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4798 | mLogger.uidFirewallRuleChanged(chain, uid, rule); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4799 | } catch (IllegalStateException e) { |
| 4800 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4801 | } catch (RemoteException e) { |
| 4802 | // ignored; service lives in system_server |
| 4803 | } |
| 4804 | } finally { |
| 4805 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4806 | } |
| 4807 | } |
| 4808 | |
| 4809 | /** |
| 4810 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4811 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4812 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4813 | private void enableFirewallChainUL(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4814 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 4815 | mFirewallChainStates.get(chain) == enable) { |
| 4816 | // All is the same, nothing to do. |
| 4817 | return; |
| 4818 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4819 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4820 | try { |
| 4821 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4822 | mLogger.firewallChainEnabled(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4823 | } catch (IllegalStateException e) { |
| 4824 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 4825 | } catch (RemoteException e) { |
| 4826 | // ignored; service lives in system_server |
| 4827 | } |
| 4828 | } |
| 4829 | |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4830 | /** |
| 4831 | * Resets all firewall rules associated with an UID. |
| 4832 | */ |
| 4833 | private void resetUidFirewallRules(int uid) { |
| 4834 | try { |
| 4835 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_DOZABLE, uid, FIREWALL_RULE_DEFAULT); |
| 4836 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 4837 | mNetworkManager |
| 4838 | .setFirewallUidRule(FIREWALL_CHAIN_POWERSAVE, uid, FIREWALL_RULE_DEFAULT); |
| 4839 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, false); |
| 4840 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, false); |
| 4841 | } catch (IllegalStateException e) { |
| 4842 | Log.wtf(TAG, "problem resetting firewall uid rules for " + uid, e); |
| 4843 | } catch (RemoteException e) { |
| 4844 | // ignored; service lives in system_server |
| 4845 | } |
| 4846 | } |
| 4847 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4848 | @Deprecated |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4849 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4850 | return getNetworkTotalBytes(template, start, end); |
| 4851 | } |
| 4852 | |
| 4853 | private long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4854 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 4855 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4856 | } catch (RuntimeException e) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4857 | Slog.w(TAG, "Failed to read network stats: " + e); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4858 | return 0; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | private NetworkStats getNetworkUidBytes(NetworkTemplate template, long start, long end) { |
| 4863 | try { |
| 4864 | return mNetworkStats.getNetworkUidBytes(template, start, end); |
| 4865 | } catch (RuntimeException e) { |
| 4866 | Slog.w(TAG, "Failed to read network stats: " + e); |
| 4867 | return new NetworkStats(SystemClock.elapsedRealtime(), 0); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4868 | } |
| 4869 | } |
| 4870 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 4871 | private boolean isBandwidthControlEnabled() { |
| 4872 | final long token = Binder.clearCallingIdentity(); |
| 4873 | try { |
| 4874 | return mNetworkManager.isBandwidthControlEnabled(); |
| 4875 | } catch (RemoteException e) { |
| 4876 | // ignored; service lives in system_server |
| 4877 | return false; |
| 4878 | } finally { |
| 4879 | Binder.restoreCallingIdentity(token); |
| 4880 | } |
| 4881 | } |
| 4882 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 4883 | private static Intent buildAllowBackgroundDataIntent() { |
| 4884 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 4885 | } |
| 4886 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4887 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 4888 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 4889 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 4890 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4891 | return intent; |
| 4892 | } |
| 4893 | |
| 4894 | private static Intent buildSnoozeRapidIntent(NetworkTemplate template) { |
| 4895 | final Intent intent = new Intent(ACTION_SNOOZE_RAPID); |
| 4896 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4897 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4898 | return intent; |
| 4899 | } |
| 4900 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4901 | private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4902 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4903 | intent.setComponent(ComponentName.unflattenFromString( |
| 4904 | res.getString(R.string.config_networkOverLimitComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4905 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4906 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4907 | return intent; |
| 4908 | } |
| 4909 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4910 | private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4911 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4912 | intent.setComponent(ComponentName.unflattenFromString( |
| 4913 | res.getString(R.string.config_dataUsageSummaryComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4914 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4915 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4916 | return intent; |
| 4917 | } |
| 4918 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 4919 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4920 | void addIdleHandler(IdleHandler handler) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 4921 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 4922 | } |
| 4923 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4924 | @GuardedBy("mUidRulesFirstLock") |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4925 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4926 | void updateRestrictBackgroundByLowPowerModeUL(final PowerSaveState result) { |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 4927 | if (mRestrictBackgroundLowPowerMode == result.batterySaverEnabled) { |
| 4928 | // Nothing changed. Nothing to do. |
| 4929 | return; |
| 4930 | } |
| 4931 | mRestrictBackgroundLowPowerMode = result.batterySaverEnabled; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4932 | |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 4933 | boolean restrictBackground = mRestrictBackgroundLowPowerMode; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4934 | boolean shouldInvokeRestrictBackground; |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 4935 | // store the temporary mRestrictBackgroundChangedInBsm and update it at the end. |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4936 | boolean localRestrictBgChangedInBsm = mRestrictBackgroundChangedInBsm; |
| 4937 | |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 4938 | if (mRestrictBackgroundLowPowerMode) { |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4939 | // Try to turn on restrictBackground if (1) it is off and (2) batter saver need to |
| 4940 | // turn it on. |
Kweku Adams | 25fa3a7 | 2019-07-12 17:00:17 -0700 | [diff] [blame] | 4941 | shouldInvokeRestrictBackground = !mRestrictBackground; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4942 | mRestrictBackgroundBeforeBsm = mRestrictBackground; |
| 4943 | localRestrictBgChangedInBsm = false; |
| 4944 | } else { |
| 4945 | // Try to restore the restrictBackground if it doesn't change in bsm |
| 4946 | shouldInvokeRestrictBackground = !mRestrictBackgroundChangedInBsm; |
| 4947 | restrictBackground = mRestrictBackgroundBeforeBsm; |
| 4948 | } |
| 4949 | |
| 4950 | if (shouldInvokeRestrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 4951 | setRestrictBackgroundUL(restrictBackground); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | // Change it at last so setRestrictBackground() won't affect this variable |
| 4955 | mRestrictBackgroundChangedInBsm = localRestrictBgChangedInBsm; |
| 4956 | } |
| 4957 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 4958 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 4959 | final int size = source.size(); |
| 4960 | for (int i = 0; i < size; i++) { |
| 4961 | target.put(source.keyAt(i), true); |
| 4962 | } |
| 4963 | } |
| 4964 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4965 | @Override |
| 4966 | public void factoryReset(String subscriber) { |
| 4967 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 4968 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4969 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 4970 | return; |
| 4971 | } |
| 4972 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4973 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 4974 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4975 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 4976 | for (NetworkPolicy policy : policies) { |
| 4977 | if (policy.template.equals(template)) { |
| 4978 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 4979 | policy.inferred = false; |
| 4980 | policy.clearSnooze(); |
| 4981 | } |
| 4982 | } |
| 4983 | setNetworkPolicies(policies); |
| 4984 | |
| 4985 | // Turn restrict background data off |
| 4986 | setRestrictBackground(false); |
| 4987 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4988 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 4989 | // Remove app's "restrict background data" flag |
| 4990 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 4991 | setUidPolicy(uid, POLICY_NONE); |
| 4992 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4993 | } |
| 4994 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 4995 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4996 | @Override |
| 4997 | public boolean isUidNetworkingBlocked(int uid, boolean isNetworkMetered) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 4998 | final long startTime = mStatLogger.getTime(); |
| 4999 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5000 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5001 | final int uidRules; |
| 5002 | final boolean isBackgroundRestricted; |
| 5003 | synchronized (mUidRulesFirstLock) { |
| 5004 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 5005 | isBackgroundRestricted = mRestrictBackground; |
| 5006 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5007 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 5008 | isBackgroundRestricted, mLogger); |
| 5009 | |
| 5010 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 5011 | |
| 5012 | return ret; |
| 5013 | } |
| 5014 | |
| 5015 | private static boolean isSystem(int uid) { |
| 5016 | return uid < Process.FIRST_APPLICATION_UID; |
| 5017 | } |
| 5018 | |
| 5019 | static boolean isUidNetworkingBlockedInternal(int uid, int uidRules, boolean isNetworkMetered, |
| 5020 | boolean isBackgroundRestricted, @Nullable NetworkPolicyLogger logger) { |
| 5021 | final int reason; |
| 5022 | // Networks are never blocked for system components |
| 5023 | if (isSystem(uid)) { |
| 5024 | reason = NTWK_ALLOWED_SYSTEM; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5025 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5026 | else if (hasRule(uidRules, RULE_REJECT_ALL)) { |
| 5027 | reason = NTWK_BLOCKED_POWER; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5028 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5029 | else if (!isNetworkMetered) { |
| 5030 | reason = NTWK_ALLOWED_NON_METERED; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5031 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5032 | else if (hasRule(uidRules, RULE_REJECT_METERED)) { |
| 5033 | reason = NTWK_BLOCKED_BLACKLIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5034 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5035 | else if (hasRule(uidRules, RULE_ALLOW_METERED)) { |
| 5036 | reason = NTWK_ALLOWED_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5037 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5038 | else if (hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED)) { |
| 5039 | reason = NTWK_ALLOWED_TMP_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5040 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5041 | else if (isBackgroundRestricted) { |
| 5042 | reason = NTWK_BLOCKED_BG_RESTRICT; |
| 5043 | } |
| 5044 | else { |
| 5045 | reason = NTWK_ALLOWED_DEFAULT; |
| 5046 | } |
| 5047 | |
| 5048 | final boolean blocked; |
| 5049 | switch(reason) { |
| 5050 | case NTWK_ALLOWED_DEFAULT: |
| 5051 | case NTWK_ALLOWED_NON_METERED: |
| 5052 | case NTWK_ALLOWED_TMP_WHITELIST: |
| 5053 | case NTWK_ALLOWED_WHITELIST: |
| 5054 | case NTWK_ALLOWED_SYSTEM: |
| 5055 | blocked = false; |
| 5056 | break; |
| 5057 | case NTWK_BLOCKED_POWER: |
| 5058 | case NTWK_BLOCKED_BLACKLIST: |
| 5059 | case NTWK_BLOCKED_BG_RESTRICT: |
| 5060 | blocked = true; |
| 5061 | break; |
| 5062 | default: |
| 5063 | throw new IllegalArgumentException(); |
| 5064 | } |
| 5065 | if (logger != null) { |
| 5066 | logger.networkBlocked(uid, reason); |
| 5067 | } |
| 5068 | |
| 5069 | return blocked; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5070 | } |
| 5071 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5072 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 5073 | |
| 5074 | @Override |
| 5075 | public void resetUserState(int userId) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5076 | synchronized (mUidRulesFirstLock) { |
| 5077 | boolean changed = removeUserStateUL(userId, false); |
| 5078 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed; |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5079 | if (changed) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5080 | synchronized (mNetworkPoliciesSecondLock) { |
| 5081 | writePolicyAL(); |
| 5082 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5083 | } |
| 5084 | } |
| 5085 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5086 | |
| 5087 | /** |
| 5088 | * @return true if the given uid is restricted from doing networking on metered networks. |
| 5089 | */ |
| 5090 | @Override |
| 5091 | public boolean isUidRestrictedOnMeteredNetworks(int uid) { |
| 5092 | final int uidRules; |
| 5093 | final boolean isBackgroundRestricted; |
| 5094 | synchronized (mUidRulesFirstLock) { |
| 5095 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 5096 | isBackgroundRestricted = mRestrictBackground; |
| 5097 | } |
| 5098 | return isBackgroundRestricted |
| 5099 | && !hasRule(uidRules, RULE_ALLOW_METERED) |
| 5100 | && !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED); |
| 5101 | } |
| 5102 | |
| 5103 | /** |
| 5104 | * @return true if networking is blocked on the given interface for the given uid according |
| 5105 | * to current networking policies. |
| 5106 | */ |
| 5107 | @Override |
| 5108 | public boolean isUidNetworkingBlocked(int uid, String ifname) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5109 | final long startTime = mStatLogger.getTime(); |
| 5110 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5111 | final int uidRules; |
| 5112 | final boolean isBackgroundRestricted; |
| 5113 | synchronized (mUidRulesFirstLock) { |
| 5114 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 5115 | isBackgroundRestricted = mRestrictBackground; |
| 5116 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5117 | final boolean isNetworkMetered; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5118 | synchronized (mNetworkPoliciesSecondLock) { |
| 5119 | isNetworkMetered = mMeteredIfaces.contains(ifname); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5120 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5121 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 5122 | isBackgroundRestricted, mLogger); |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5123 | |
| 5124 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 5125 | |
| 5126 | return ret; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5127 | } |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5128 | |
| 5129 | @Override |
| 5130 | public void onTempPowerSaveWhitelistChange(int appId, boolean added) { |
| 5131 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 5132 | mLogger.tempPowerSaveWlChanged(appId, added); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5133 | if (added) { |
| 5134 | mPowerSaveTempWhitelistAppIds.put(appId, true); |
| 5135 | } else { |
| 5136 | mPowerSaveTempWhitelistAppIds.delete(appId); |
| 5137 | } |
| 5138 | updateRulesForTempWhitelistChangeUL(appId); |
| 5139 | } |
| 5140 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5141 | |
| 5142 | @Override |
| 5143 | public SubscriptionPlan getSubscriptionPlan(Network network) { |
| 5144 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5145 | final int subId = getSubIdLocked(network); |
| 5146 | return getPrimarySubscriptionPlanLocked(subId); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | @Override |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 5151 | public SubscriptionPlan getSubscriptionPlan(NetworkTemplate template) { |
| 5152 | synchronized (mNetworkPoliciesSecondLock) { |
| 5153 | final int subId = findRelevantSubIdNL(template); |
| 5154 | return getPrimarySubscriptionPlanLocked(subId); |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5159 | public long getSubscriptionOpportunisticQuota(Network network, int quotaType) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5160 | final long quotaBytes; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5161 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5162 | quotaBytes = mSubscriptionOpportunisticQuota.get(getSubIdLocked(network), |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 5163 | OPPORTUNISTIC_QUOTA_UNKNOWN); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5164 | } |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5165 | if (quotaBytes == OPPORTUNISTIC_QUOTA_UNKNOWN) { |
| 5166 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5167 | } |
| 5168 | |
| 5169 | if (quotaType == QUOTA_TYPE_JOBS) { |
| 5170 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5171 | NETPOLICY_QUOTA_FRAC_JOBS, QUOTA_FRAC_JOBS_DEFAULT)); |
| 5172 | } else if (quotaType == QUOTA_TYPE_MULTIPATH) { |
| 5173 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5174 | NETPOLICY_QUOTA_FRAC_MULTIPATH, QUOTA_FRAC_MULTIPATH_DEFAULT)); |
| 5175 | } else { |
| 5176 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5177 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5178 | } |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5179 | |
| 5180 | @Override |
| 5181 | public void onAdminDataAvailable() { |
| 5182 | mAdminDataAvailableLatch.countDown(); |
| 5183 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5184 | |
| 5185 | @Override |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 5186 | public void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
| 5187 | NetworkPolicyManagerService.this.setAppIdleWhitelist(uid, shouldWhitelist); |
| 5188 | } |
| 5189 | |
| 5190 | @Override |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5191 | public void setMeteredRestrictedPackages(Set<String> packageNames, int userId) { |
| 5192 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 5193 | } |
| 5194 | |
| 5195 | @Override |
| 5196 | public void setMeteredRestrictedPackagesAsync(Set<String> packageNames, int userId) { |
| 5197 | mHandler.obtainMessage(MSG_METERED_RESTRICTED_PACKAGES_CHANGED, |
| 5198 | userId, 0, packageNames).sendToTarget(); |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | private void setMeteredRestrictedPackagesInternal(Set<String> packageNames, int userId) { |
| 5203 | synchronized (mUidRulesFirstLock) { |
| 5204 | final Set<Integer> newRestrictedUids = new ArraySet<>(); |
| 5205 | for (String packageName : packageNames) { |
| 5206 | final int uid = getUidForPackage(packageName, userId); |
| 5207 | if (uid >= 0) { |
| 5208 | newRestrictedUids.add(uid); |
| 5209 | } |
| 5210 | } |
| 5211 | final Set<Integer> oldRestrictedUids = mMeteredRestrictedUids.get(userId); |
| 5212 | mMeteredRestrictedUids.put(userId, newRestrictedUids); |
| 5213 | handleRestrictedPackagesChangeUL(oldRestrictedUids, newRestrictedUids); |
| 5214 | mLogger.meteredRestrictedPkgsChanged(newRestrictedUids); |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | private int getUidForPackage(String packageName, int userId) { |
| 5219 | try { |
| 5220 | return mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 5221 | PackageManager.MATCH_KNOWN_PACKAGES, userId); |
| 5222 | } catch (NameNotFoundException e) { |
| 5223 | return -1; |
| 5224 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | private int parseSubId(NetworkState state) { |
| 5228 | // TODO: moved to using a legitimate NetworkSpecifier instead of string parsing |
| 5229 | int subId = INVALID_SUBSCRIPTION_ID; |
| 5230 | if (state != null && state.networkCapabilities != null |
| 5231 | && state.networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) { |
| 5232 | NetworkSpecifier spec = state.networkCapabilities.getNetworkSpecifier(); |
| 5233 | if (spec instanceof StringNetworkSpecifier) { |
| 5234 | try { |
| 5235 | subId = Integer.parseInt(((StringNetworkSpecifier) spec).specifier); |
| 5236 | } catch (NumberFormatException e) { |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | return subId; |
| 5241 | } |
| 5242 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5243 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5244 | private int getSubIdLocked(Network network) { |
| 5245 | return mNetIdToSubId.get(network.netId, INVALID_SUBSCRIPTION_ID); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5246 | } |
| 5247 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5248 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5249 | private SubscriptionPlan getPrimarySubscriptionPlanLocked(int subId) { |
| 5250 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5251 | if (!ArrayUtils.isEmpty(plans)) { |
| 5252 | for (SubscriptionPlan plan : plans) { |
| 5253 | if (plan.getCycleRule().isRecurring()) { |
| 5254 | // Recurring plans will always have an active cycle |
| 5255 | return plan; |
| 5256 | } else { |
| 5257 | // Non-recurring plans need manual test for active cycle |
| 5258 | final Range<ZonedDateTime> cycle = plan.cycleIterator().next(); |
| 5259 | if (cycle.contains(ZonedDateTime.now(mClock))) { |
| 5260 | return plan; |
| 5261 | } |
| 5262 | } |
| 5263 | } |
| 5264 | } |
| 5265 | return null; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5266 | } |
| 5267 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5268 | /** |
| 5269 | * This will only ever be called once - during device boot. |
| 5270 | */ |
| 5271 | private void waitForAdminData() { |
| 5272 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { |
| 5273 | ConcurrentUtils.waitForCountDownNoInterrupt(mAdminDataAvailableLatch, |
| 5274 | WAIT_FOR_ADMIN_DATA_TIMEOUT_MS, "Wait for admin data"); |
| 5275 | } |
| 5276 | } |
| 5277 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5278 | private void handleRestrictedPackagesChangeUL(Set<Integer> oldRestrictedUids, |
| 5279 | Set<Integer> newRestrictedUids) { |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 5280 | if (!mNetworkManagerReady) { |
| 5281 | return; |
| 5282 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5283 | if (oldRestrictedUids == null) { |
| 5284 | for (int uid : newRestrictedUids) { |
| 5285 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5286 | } |
| 5287 | return; |
| 5288 | } |
| 5289 | for (int uid : oldRestrictedUids) { |
| 5290 | if (!newRestrictedUids.contains(uid)) { |
| 5291 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5292 | } |
| 5293 | } |
| 5294 | for (int uid : newRestrictedUids) { |
| 5295 | if (!oldRestrictedUids.contains(uid)) { |
| 5296 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 5301 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5302 | private boolean isRestrictedByAdminUL(int uid) { |
| 5303 | final Set<Integer> restrictedUids = mMeteredRestrictedUids.get( |
| 5304 | UserHandle.getUserId(uid)); |
| 5305 | return restrictedUids != null && restrictedUids.contains(uid); |
| 5306 | } |
| 5307 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5308 | private static boolean hasRule(int uidRules, int rule) { |
| 5309 | return (uidRules & rule) != 0; |
| 5310 | } |
| 5311 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5312 | private static @NonNull NetworkState[] defeatNullable(@Nullable NetworkState[] val) { |
| 5313 | return (val != null) ? val : new NetworkState[0]; |
| 5314 | } |
| 5315 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5316 | private static boolean getBooleanDefeatingNullable(@Nullable PersistableBundle bundle, |
| 5317 | String key, boolean defaultValue) { |
| 5318 | return (bundle != null) ? bundle.getBoolean(key, defaultValue) : defaultValue; |
| 5319 | } |
| 5320 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 5321 | private class NotificationId { |
| 5322 | private final String mTag; |
| 5323 | private final int mId; |
| 5324 | |
| 5325 | NotificationId(NetworkPolicy policy, int type) { |
| 5326 | mTag = buildNotificationTag(policy, type); |
| 5327 | mId = type; |
| 5328 | } |
| 5329 | |
| 5330 | @Override |
| 5331 | public boolean equals(Object o) { |
| 5332 | if (this == o) return true; |
| 5333 | if (!(o instanceof NotificationId)) return false; |
| 5334 | NotificationId that = (NotificationId) o; |
| 5335 | return Objects.equals(mTag, that.mTag); |
| 5336 | } |
| 5337 | |
| 5338 | @Override |
| 5339 | public int hashCode() { |
| 5340 | return Objects.hash(mTag); |
| 5341 | } |
| 5342 | |
| 5343 | /** |
| 5344 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 5345 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 5346 | */ |
| 5347 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
| 5348 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
| 5349 | } |
| 5350 | |
| 5351 | public String getTag() { |
| 5352 | return mTag; |
| 5353 | } |
| 5354 | |
| 5355 | public int getId() { |
| 5356 | return mId; |
| 5357 | } |
| 5358 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 5359 | } |