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; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 232 | |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 233 | import libcore.io.IoUtils; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 234 | import libcore.util.EmptyArray; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 235 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 236 | import org.xmlpull.v1.XmlPullParser; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 237 | import org.xmlpull.v1.XmlSerializer; |
| 238 | |
| 239 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 240 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 241 | import java.io.FileInputStream; |
| 242 | import java.io.FileNotFoundException; |
| 243 | import java.io.FileOutputStream; |
| 244 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 245 | import java.io.PrintWriter; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 246 | import java.lang.annotation.Retention; |
| 247 | import java.lang.annotation.RetentionPolicy; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 248 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 249 | import java.time.Clock; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 250 | import java.time.Instant; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 251 | import java.time.ZoneId; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 252 | import java.time.ZoneOffset; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 253 | import java.time.ZonedDateTime; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 254 | import java.time.temporal.ChronoUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 255 | import java.util.ArrayList; |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 256 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 257 | import java.util.Calendar; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 258 | import java.util.List; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 259 | import java.util.Objects; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 260 | import java.util.Set; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 261 | import java.util.concurrent.CountDownLatch; |
| 262 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 263 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 264 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 265 | * Service that maintains low-level network policy rules, using |
| 266 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 267 | * <p> |
| 268 | * Derives active rules by combining a given policy with other system status, |
| 269 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 270 | * enforcement. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 271 | * |
| 272 | * <p> |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 273 | * This class uses 2 locks to synchronize state: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 274 | * <ul> |
| 275 | * <li>{@code mUidRulesFirstLock}: used to guard state related to individual UIDs (such as firewall |
| 276 | * rules). |
| 277 | * <li>{@code mNetworkPoliciesSecondLock}: used to guard state related to network interfaces (such |
| 278 | * as network policies). |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 279 | * </ul> |
| 280 | * |
| 281 | * <p> |
| 282 | * As such, methods that require synchronization have the following prefixes: |
| 283 | * <ul> |
| 284 | * <li>{@code UL()}: require the "UID" lock ({@code mUidRulesFirstLock}). |
| 285 | * <li>{@code NL()}: require the "Network" lock ({@code mNetworkPoliciesSecondLock}). |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 286 | * <li>{@code AL()}: require all locks, which must be obtained in order ({@code mUidRulesFirstLock} |
| 287 | * first, then {@code mNetworkPoliciesSecondLock}, then {@code mYetAnotherGuardThirdLock}, etc.. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 288 | * </ul> |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 289 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 290 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 291 | static final String TAG = NetworkPolicyLogger.TAG; |
| 292 | private static final boolean LOGD = NetworkPolicyLogger.LOGD; |
| 293 | private static final boolean LOGV = NetworkPolicyLogger.LOGV; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 294 | |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 295 | /** |
| 296 | * No opportunistic quota could be calculated from user data plan or data settings. |
| 297 | */ |
| 298 | public static final int OPPORTUNISTIC_QUOTA_UNKNOWN = -1; |
| 299 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 300 | private static final int VERSION_INIT = 1; |
| 301 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 302 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 303 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 304 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 305 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 306 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 307 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 308 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 309 | private static final int VERSION_SWITCH_UID = 10; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 310 | private static final int VERSION_ADDED_CYCLE = 11; |
| 311 | private static final int VERSION_LATEST = VERSION_ADDED_CYCLE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 312 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 313 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 314 | public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 315 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 316 | public static final int TYPE_LIMIT = SystemMessage.NOTE_NET_LIMIT; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 317 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 318 | public static final int TYPE_LIMIT_SNOOZED = SystemMessage.NOTE_NET_LIMIT_SNOOZED; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 319 | @VisibleForTesting |
| 320 | public static final int TYPE_RAPID = SystemMessage.NOTE_NET_RAPID; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 321 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 322 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 323 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 324 | private static final String TAG_SUBSCRIPTION_PLAN = "subscription-plan"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 325 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 326 | private static final String TAG_APP_POLICY = "app-policy"; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 327 | private static final String TAG_WHITELIST = "whitelist"; |
| 328 | private static final String TAG_RESTRICT_BACKGROUND = "restrict-background"; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 329 | private static final String TAG_REVOKED_RESTRICT_BACKGROUND = "revoked-restrict-background"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 330 | |
| 331 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 332 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 333 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 334 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 335 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 336 | @Deprecated private static final String ATTR_CYCLE_DAY = "cycleDay"; |
| 337 | @Deprecated private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
| 338 | private static final String ATTR_CYCLE_START = "cycleStart"; |
| 339 | private static final String ATTR_CYCLE_END = "cycleEnd"; |
| 340 | private static final String ATTR_CYCLE_PERIOD = "cyclePeriod"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 341 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 342 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 343 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 344 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 345 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 346 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 347 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 348 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 349 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 350 | private static final String ATTR_POLICY = "policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 351 | private static final String ATTR_SUB_ID = "subId"; |
| 352 | private static final String ATTR_TITLE = "title"; |
| 353 | private static final String ATTR_SUMMARY = "summary"; |
| 354 | private static final String ATTR_LIMIT_BEHAVIOR = "limitBehavior"; |
| 355 | private static final String ATTR_USAGE_BYTES = "usageBytes"; |
| 356 | private static final String ATTR_USAGE_TIME = "usageTime"; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 357 | private static final String ATTR_OWNER_PACKAGE = "ownerPackage"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 358 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 359 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 360 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 361 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 362 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 363 | private static final String ACTION_SNOOZE_RAPID = |
| 364 | "com.android.server.net.action.SNOOZE_RAPID"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 365 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 366 | /** |
| 367 | * Indicates the maximum wait time for admin data to be available; |
| 368 | */ |
| 369 | private static final long WAIT_FOR_ADMIN_DATA_TIMEOUT_MS = 10_000; |
| 370 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 371 | private static final long QUOTA_UNLIMITED_DEFAULT = DataUnit.MEBIBYTES.toBytes(20); |
| 372 | private static final float QUOTA_LIMITED_DEFAULT = 0.1f; |
| 373 | private static final float QUOTA_FRAC_JOBS_DEFAULT = 0.5f; |
| 374 | private static final float QUOTA_FRAC_MULTIPATH_DEFAULT = 0.5f; |
| 375 | |
Svetoslav Ganov | 24c2775 | 2016-08-31 18:09:00 -0700 | [diff] [blame] | 376 | private static final int MSG_RULES_CHANGED = 1; |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 377 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 378 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 379 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 380 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 381 | private static final int MSG_UPDATE_INTERFACE_QUOTA = 10; |
| 382 | private static final int MSG_REMOVE_INTERFACE_QUOTA = 11; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 383 | private static final int MSG_POLICIES_CHANGED = 13; |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 384 | private static final int MSG_RESET_FIREWALL_RULES_BY_UID = 15; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 385 | private static final int MSG_SUBSCRIPTION_OVERRIDE = 16; |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 386 | private static final int MSG_METERED_RESTRICTED_PACKAGES_CHANGED = 17; |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 387 | private static final int MSG_SET_NETWORK_TEMPLATE_ENABLED = 18; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 388 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 389 | private static final int UID_MSG_STATE_CHANGED = 100; |
| 390 | private static final int UID_MSG_GONE = 101; |
| 391 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 392 | private static final String PROP_SUB_PLAN_OWNER = "persist.sys.sub_plan_owner"; |
| 393 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 394 | private final Context mContext; |
| 395 | private final IActivityManager mActivityManager; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 396 | private NetworkStatsManagerInternal mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 397 | private final INetworkManagementService mNetworkManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 398 | private UsageStatsManagerInternal mUsageStats; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 399 | private final Clock mClock; |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 400 | private final UserManager mUserManager; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 401 | private final CarrierConfigManager mCarrierConfigManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 402 | |
| 403 | private IConnectivityManager mConnManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 404 | private PowerManagerInternal mPowerManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 405 | private IDeviceIdleController mDeviceIdleController; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 406 | @GuardedBy("mUidRulesFirstLock") |
| 407 | private PowerSaveState mRestrictBackgroundPowerState; |
| 408 | |
| 409 | // Store the status of restrict background before turning on battery saver. |
| 410 | // Used to restore mRestrictBackground when battery saver is turned off. |
| 411 | private boolean mRestrictBackgroundBeforeBsm; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 412 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 413 | // Denotes the status of restrict background read from disk. |
| 414 | private boolean mLoadedRestrictBackground; |
| 415 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 416 | // See main javadoc for instructions on how to use these locks. |
| 417 | final Object mUidRulesFirstLock = new Object(); |
| 418 | final Object mNetworkPoliciesSecondLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 419 | |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 420 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
| 421 | volatile boolean mSystemReady; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 422 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 423 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackground; |
| 424 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictPower; |
| 425 | @GuardedBy("mUidRulesFirstLock") volatile boolean mDeviceIdleMode; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 426 | // Store whether user flipped restrict background in battery saver mode |
| 427 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackgroundChangedInBsm; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 428 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 429 | private final boolean mSuppressDefaultPolicy; |
| 430 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 431 | private final CountDownLatch mAdminDataAvailableLatch = new CountDownLatch(1); |
| 432 | |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 433 | private volatile boolean mNetworkManagerReady; |
| 434 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 435 | /** Defined network policies. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 436 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 437 | final ArrayMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = new ArrayMap<>(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 438 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 439 | /** Map from subId to subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 440 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 441 | final SparseArray<SubscriptionPlan[]> mSubscriptionPlans = new SparseArray<>(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 442 | /** Map from subId to package name that owns subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 443 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 444 | final SparseArray<String> mSubscriptionPlansOwner = new SparseArray<>(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 445 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 446 | /** Map from subId to daily opportunistic quota. */ |
| 447 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 448 | final SparseLongArray mSubscriptionOpportunisticQuota = new SparseLongArray(); |
| 449 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 450 | /** Defined UID policies. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 451 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 452 | /** Currently derived rules for each UID. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 453 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidRules = new SparseIntArray(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 454 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 455 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 456 | final SparseIntArray mUidFirewallStandbyRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 457 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 458 | final SparseIntArray mUidFirewallDozableRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 459 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 460 | final SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 461 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 462 | /** 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] | 463 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 464 | final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray(); |
| 465 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 466 | // "Power save mode" is the concept used in the DeviceIdleController that includes various |
| 467 | // features including Doze and Battery Saver. It include Battery Saver, but "power save mode" |
| 468 | // and "battery saver" are not equivalent. |
| 469 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 470 | /** |
| 471 | * 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] | 472 | * in power save mode, except device idle (doze) still applies. |
| 473 | * TODO: An int array might be sufficient |
| 474 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 475 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 476 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 477 | |
| 478 | /** |
| 479 | * 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] | 480 | * in power save mode. |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 481 | * TODO: An int array might be sufficient |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 482 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 483 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 484 | private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 485 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 486 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 487 | private final SparseBooleanArray mPowerSaveTempWhitelistAppIds = new SparseBooleanArray(); |
| 488 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 489 | /** |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 490 | * UIDs that have been white-listed temporarily to be able to have network access despite being |
| 491 | * idle. Other power saving restrictions still apply. |
| 492 | */ |
| 493 | @GuardedBy("mUidRulesFirstLock") |
| 494 | private final SparseBooleanArray mAppIdleTempWhitelistAppIds = new SparseBooleanArray(); |
| 495 | |
| 496 | /** |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 497 | * UIDs that have been initially white-listed by system to avoid restricted background. |
| 498 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 499 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 500 | private final SparseBooleanArray mDefaultRestrictBackgroundWhitelistUids = |
| 501 | new SparseBooleanArray(); |
| 502 | |
| 503 | /** |
| 504 | * UIDs that have been initially white-listed by system to avoid restricted background, |
| 505 | * but later revoked by user. |
| 506 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 507 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 508 | private final SparseBooleanArray mRestrictBackgroundWhitelistRevokedUids = |
| 509 | new SparseBooleanArray(); |
| 510 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 511 | /** Set of ifaces that are metered. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 512 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 513 | private ArraySet<String> mMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 514 | /** Set of over-limit templates that have been notified. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 515 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 516 | private final ArraySet<NetworkTemplate> mOverLimitNotified = new ArraySet<>(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 517 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 518 | /** Set of currently active {@link Notification} tags. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 519 | @GuardedBy("mNetworkPoliciesSecondLock") |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 520 | private final ArraySet<NotificationId> mActiveNotifs = new ArraySet<>(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 521 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 522 | /** Foreground at UID granularity. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 523 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 524 | final SparseIntArray mUidState = new SparseIntArray(); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 525 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 526 | /** Map from network ID to last observed meteredness state */ |
| 527 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 528 | private final SparseBooleanArray mNetworkMetered = new SparseBooleanArray(); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 529 | /** Map from network ID to last observed roaming state */ |
| 530 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 531 | private final SparseBooleanArray mNetworkRoaming = new SparseBooleanArray(); |
| 532 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 533 | /** Map from netId to subId as of last update */ |
| 534 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 535 | private final SparseIntArray mNetIdToSubId = new SparseIntArray(); |
| 536 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 537 | /** Map from subId to subscriberId as of last update */ |
| 538 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 539 | private final SparseArray<String> mSubIdToSubscriberId = new SparseArray<>(); |
| 540 | /** Set of all merged subscriberId as of last update */ |
| 541 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 542 | private String[] mMergedSubscriberIds = EmptyArray.STRING; |
| 543 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 544 | /** |
| 545 | * Indicates the uids restricted by admin from accessing metered data. It's a mapping from |
| 546 | * userId to restricted uids which belong to that user. |
| 547 | */ |
| 548 | @GuardedBy("mUidRulesFirstLock") |
| 549 | private final SparseArray<Set<Integer>> mMeteredRestrictedUids = new SparseArray<>(); |
| 550 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 551 | private final RemoteCallbackList<INetworkPolicyListener> |
| 552 | mListeners = new RemoteCallbackList<>(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 553 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 554 | final Handler mHandler; |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 555 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 556 | final Handler mUidEventHandler; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 557 | |
| 558 | private final ServiceThread mUidEventThread; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 559 | |
Andreas Gampe | e8e2ebd | 2018-10-24 19:42:52 -0700 | [diff] [blame] | 560 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 561 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 562 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 563 | private final AppOpsManager mAppOps; |
| 564 | |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 565 | private final IPackageManager mIPm; |
| 566 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 567 | private ActivityManagerInternal mActivityManagerInternal; |
| 568 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 569 | private final NetworkPolicyLogger mLogger = new NetworkPolicyLogger(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 570 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 571 | // TODO: keep whitelist of system-critical services that should never have |
| 572 | // rules enforced, such as system, phone, and radio UIDs. |
| 573 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 574 | // TODO: migrate notifications to SystemUI |
| 575 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 576 | |
| 577 | interface Stats { |
| 578 | int UPDATE_NETWORK_ENABLED = 0; |
| 579 | int IS_UID_NETWORKING_BLOCKED = 1; |
| 580 | |
| 581 | int COUNT = IS_UID_NETWORKING_BLOCKED + 1; |
| 582 | } |
| 583 | |
| 584 | public final StatLogger mStatLogger = new StatLogger(new String[] { |
| 585 | "updateNetworkEnabledNL()", |
| 586 | "isUidNetworkingBlocked()", |
| 587 | }); |
| 588 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 589 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 590 | INetworkManagementService networkManagement) { |
| 591 | this(context, activityManager, networkManagement, AppGlobals.getPackageManager(), |
| 592 | getDefaultClock(), getDefaultSystemDir(), false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 595 | private static @NonNull File getDefaultSystemDir() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 596 | return new File(Environment.getDataDirectory(), "system"); |
| 597 | } |
| 598 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 599 | private static @NonNull Clock getDefaultClock() { |
| 600 | return new BestClock(ZoneOffset.UTC, SystemClock.currentNetworkTimeClock(), |
| 601 | Clock.systemUTC()); |
| 602 | } |
| 603 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 604 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 605 | INetworkManagementService networkManagement, IPackageManager pm, Clock clock, |
| 606 | File systemDir, boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 607 | mContext = checkNotNull(context, "missing context"); |
| 608 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 609 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 610 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService( |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 611 | Context.DEVICE_IDLE_CONTROLLER)); |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 612 | mClock = checkNotNull(clock, "missing Clock"); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 613 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 614 | mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 615 | mIPm = pm; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 616 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 617 | HandlerThread thread = new HandlerThread(TAG); |
| 618 | thread.start(); |
| 619 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 620 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 621 | // We create another thread for the UID events, which are more time-critical. |
| 622 | mUidEventThread = new ServiceThread(TAG + ".uid", Process.THREAD_PRIORITY_FOREGROUND, |
| 623 | /*allowIo=*/ false); |
| 624 | mUidEventThread.start(); |
| 625 | mUidEventHandler = new Handler(mUidEventThread.getLooper(), mUidEventHandlerCallback); |
| 626 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 627 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 628 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 629 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml"), "net-policy"); |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 630 | |
| 631 | mAppOps = context.getSystemService(AppOpsManager.class); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 632 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 633 | // Expose private service for system components to use. |
| 634 | LocalServices.addService(NetworkPolicyManagerInternal.class, |
| 635 | new NetworkPolicyManagerInternalImpl()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 639 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 640 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 641 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 642 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 643 | void updatePowerSaveWhitelistUL() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 644 | try { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 645 | int[] whitelist = mDeviceIdleController.getAppIdWhitelistExceptIdle(); |
| 646 | mPowerSaveWhitelistExceptIdleAppIds.clear(); |
| 647 | if (whitelist != null) { |
| 648 | for (int uid : whitelist) { |
| 649 | mPowerSaveWhitelistExceptIdleAppIds.put(uid, true); |
| 650 | } |
| 651 | } |
| 652 | whitelist = mDeviceIdleController.getAppIdWhitelist(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 653 | mPowerSaveWhitelistAppIds.clear(); |
| 654 | if (whitelist != null) { |
| 655 | for (int uid : whitelist) { |
| 656 | mPowerSaveWhitelistAppIds.put(uid, true); |
| 657 | } |
| 658 | } |
| 659 | } catch (RemoteException e) { |
| 660 | } |
| 661 | } |
| 662 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 663 | /** |
| 664 | * Whitelists pre-defined apps for restrict background, but only if the user didn't already |
| 665 | * revoke the whitelist. |
| 666 | * |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 667 | * @return whether any uid has been whitelisted. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 668 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 669 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 670 | boolean addDefaultRestrictBackgroundWhitelistUidsUL() { |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 671 | final List<UserInfo> users = mUserManager.getUsers(); |
| 672 | final int numberUsers = users.size(); |
| 673 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 674 | boolean changed = false; |
| 675 | for (int i = 0; i < numberUsers; i++) { |
| 676 | final UserInfo user = users.get(i); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 677 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(user.id) || changed; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 678 | } |
| 679 | return changed; |
| 680 | } |
| 681 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 682 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 683 | private boolean addDefaultRestrictBackgroundWhitelistUidsUL(int userId) { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 684 | final SystemConfig sysConfig = SystemConfig.getInstance(); |
| 685 | final PackageManager pm = mContext.getPackageManager(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 686 | final ArraySet<String> allowDataUsage = sysConfig.getAllowInDataUsageSave(); |
| 687 | boolean changed = false; |
| 688 | for (int i = 0; i < allowDataUsage.size(); i++) { |
| 689 | final String pkg = allowDataUsage.valueAt(i); |
| 690 | if (LOGD) |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 691 | Slog.d(TAG, "checking restricted background whitelisting for package " + pkg |
| 692 | + " and user " + userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 693 | final ApplicationInfo app; |
| 694 | try { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 695 | app = pm.getApplicationInfoAsUser(pkg, PackageManager.MATCH_SYSTEM_ONLY, userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 696 | } catch (PackageManager.NameNotFoundException e) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 697 | if (LOGD) Slog.d(TAG, "No ApplicationInfo for package " + pkg); |
| 698 | // Ignore it - some apps on allow-in-data-usage-save are optional. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 699 | continue; |
| 700 | } |
| 701 | if (!app.isPrivilegedApp()) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 702 | Slog.e(TAG, "addDefaultRestrictBackgroundWhitelistUidsUL(): " |
| 703 | + "skipping non-privileged app " + pkg); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 704 | continue; |
| 705 | } |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 706 | final int uid = UserHandle.getUid(userId, app.uid); |
| 707 | mDefaultRestrictBackgroundWhitelistUids.append(uid, true); |
| 708 | if (LOGD) |
| 709 | Slog.d(TAG, "Adding uid " + uid + " (user " + userId + ") to default restricted " |
| 710 | + "background whitelist. Revoked status: " |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 711 | + mRestrictBackgroundWhitelistRevokedUids.get(uid)); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 712 | if (!mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 713 | if (LOGD) |
| 714 | Slog.d(TAG, "adding default package " + pkg + " (uid " + uid + " for user " |
| 715 | + userId + ") to restrict background whitelist"); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 716 | setUidPolicyUncheckedUL(uid, POLICY_ALLOW_METERED_BACKGROUND, false); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 717 | changed = true; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | return changed; |
| 721 | } |
| 722 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 723 | private void initService(CountDownLatch initCompleteSignal) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 724 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "systemReady"); |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 725 | final int oldPriority = Process.getThreadPriority(Process.myTid()); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 726 | try { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 727 | // Boost thread's priority during system server init |
| 728 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 729 | if (!isBandwidthControlEnabled()) { |
| 730 | Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy"); |
| 731 | return; |
| 732 | } |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 733 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 734 | mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 735 | mNetworkStats = LocalServices.getService(NetworkStatsManagerInternal.class); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 736 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 737 | synchronized (mUidRulesFirstLock) { |
| 738 | synchronized (mNetworkPoliciesSecondLock) { |
| 739 | updatePowerSaveWhitelistUL(); |
| 740 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 741 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 742 | new PowerManagerInternal.LowPowerModeListener() { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 743 | @Override |
| 744 | public int getServiceType() { |
| 745 | return ServiceType.NETWORK_FIREWALL; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 746 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 747 | |
| 748 | @Override |
| 749 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 750 | final boolean enabled = result.batterySaverEnabled; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 751 | if (LOGD) { |
| 752 | Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")"); |
| 753 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 754 | synchronized (mUidRulesFirstLock) { |
| 755 | if (mRestrictPower != enabled) { |
| 756 | mRestrictPower = enabled; |
| 757 | updateRulesForRestrictPowerUL(); |
| 758 | } |
| 759 | } |
| 760 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 761 | }); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 762 | mRestrictPower = mPowerManagerInternal.getLowPowerState( |
| 763 | ServiceType.NETWORK_FIREWALL).batterySaverEnabled; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 764 | |
| 765 | mSystemReady = true; |
| 766 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 767 | waitForAdminData(); |
| 768 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 769 | // read policy from disk |
| 770 | readPolicyAL(); |
| 771 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 772 | // Update the restrictBackground if battery saver is turned on |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 773 | mRestrictBackgroundBeforeBsm = mLoadedRestrictBackground; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 774 | mRestrictBackgroundPowerState = mPowerManagerInternal |
| 775 | .getLowPowerState(ServiceType.DATA_SAVER); |
| 776 | final boolean localRestrictBackground = |
| 777 | mRestrictBackgroundPowerState.batterySaverEnabled; |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 778 | if (localRestrictBackground && !mLoadedRestrictBackground) { |
| 779 | mLoadedRestrictBackground = true; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 780 | } |
| 781 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 782 | new PowerManagerInternal.LowPowerModeListener() { |
| 783 | @Override |
| 784 | public int getServiceType() { |
| 785 | return ServiceType.DATA_SAVER; |
| 786 | } |
| 787 | |
| 788 | @Override |
| 789 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 790 | synchronized (mUidRulesFirstLock) { |
| 791 | updateRestrictBackgroundByLowPowerModeUL(result); |
| 792 | } |
| 793 | } |
| 794 | }); |
| 795 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 796 | if (addDefaultRestrictBackgroundWhitelistUidsUL()) { |
| 797 | writePolicyAL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 798 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 799 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 800 | setRestrictBackgroundUL(mLoadedRestrictBackground); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 801 | updateRulesForGlobalChangeAL(false); |
| 802 | updateNotificationsNL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 803 | } |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 804 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 805 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 806 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 807 | try { |
| 808 | mActivityManager.registerUidObserver(mUidObserver, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 809 | ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE, |
Sudheer Shanka | 6a3c07e | 2018-09-11 15:21:22 -0700 | [diff] [blame] | 810 | NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE, "android"); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 811 | mNetworkManager.registerObserver(mAlertObserver); |
| 812 | } catch (RemoteException e) { |
| 813 | // ignored; both services live in system_server |
| 814 | } |
| 815 | |
| 816 | // listen for changes to power save whitelist |
| 817 | final IntentFilter whitelistFilter = new IntentFilter( |
| 818 | PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 819 | mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler); |
| 820 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 821 | // watch for network interfaces to be claimed |
| 822 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); |
| 823 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 824 | |
| 825 | // listen for package changes to update policy |
| 826 | final IntentFilter packageFilter = new IntentFilter(); |
| 827 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
| 828 | packageFilter.addDataScheme("package"); |
| 829 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
| 830 | |
| 831 | // listen for UID changes to update policy |
| 832 | mContext.registerReceiver( |
| 833 | mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler); |
| 834 | |
| 835 | // listen for user changes to update policy |
| 836 | final IntentFilter userFilter = new IntentFilter(); |
| 837 | userFilter.addAction(ACTION_USER_ADDED); |
| 838 | userFilter.addAction(ACTION_USER_REMOVED); |
| 839 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 840 | |
| 841 | // listen for stats update events |
| 842 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 843 | mContext.registerReceiver( |
| 844 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 845 | |
| 846 | // listen for restrict background changes from notifications |
| 847 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 848 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 849 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 850 | // Listen for snooze from notifications |
| 851 | mContext.registerReceiver(mSnoozeReceiver, |
| 852 | new IntentFilter(ACTION_SNOOZE_WARNING), MANAGE_NETWORK_POLICY, mHandler); |
| 853 | mContext.registerReceiver(mSnoozeReceiver, |
| 854 | new IntentFilter(ACTION_SNOOZE_RAPID), MANAGE_NETWORK_POLICY, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 855 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 856 | // listen for configured wifi networks to be loaded |
| 857 | final IntentFilter wifiFilter = |
| 858 | new IntentFilter(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 859 | mContext.registerReceiver(mWifiReceiver, wifiFilter, null, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 860 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 861 | // listen for carrier config changes to update data cycle information |
| 862 | final IntentFilter carrierConfigFilter = new IntentFilter( |
| 863 | ACTION_CARRIER_CONFIG_CHANGED); |
| 864 | mContext.registerReceiver(mCarrierConfigReceiver, carrierConfigFilter, null, mHandler); |
| 865 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 866 | // listen for meteredness changes |
| 867 | mContext.getSystemService(ConnectivityManager.class).registerNetworkCallback( |
| 868 | new NetworkRequest.Builder().build(), mNetworkCallback); |
| 869 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 870 | mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener()); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 871 | |
| 872 | // Listen for subscriber changes |
| 873 | mContext.getSystemService(SubscriptionManager.class).addOnSubscriptionsChangedListener( |
| 874 | new OnSubscriptionsChangedListener(mHandler.getLooper()) { |
| 875 | @Override |
| 876 | public void onSubscriptionsChanged() { |
| 877 | updateNetworksInternal(); |
| 878 | } |
| 879 | }); |
| 880 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 881 | // tell systemReady() that the service has been initialized |
| 882 | initCompleteSignal.countDown(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 883 | } finally { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 884 | // Restore the default priority after init is done |
| 885 | Process.setThreadPriority(oldPriority); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 886 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 887 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 890 | public CountDownLatch networkScoreAndNetworkManagementServiceReady() { |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 891 | mNetworkManagerReady = true; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 892 | final CountDownLatch initCompleteSignal = new CountDownLatch(1); |
| 893 | mHandler.post(() -> initService(initCompleteSignal)); |
| 894 | return initCompleteSignal; |
| 895 | } |
| 896 | |
| 897 | public void systemReady(CountDownLatch initCompleteSignal) { |
| 898 | // wait for initService to complete |
| 899 | try { |
| 900 | if (!initCompleteSignal.await(30, TimeUnit.SECONDS)) { |
| 901 | throw new IllegalStateException("Service " + TAG +" init timeout"); |
| 902 | } |
| 903 | } catch (InterruptedException e) { |
| 904 | Thread.currentThread().interrupt(); |
| 905 | throw new IllegalStateException("Service " + TAG + " init interrupted", e); |
| 906 | } |
| 907 | } |
| 908 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 909 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 910 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 911 | mUidEventHandler.obtainMessage(UID_MSG_STATE_CHANGED, |
| 912 | uid, procState, procStateSeq).sendToTarget(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 915 | @Override public void onUidGone(int uid, boolean disabled) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 916 | mUidEventHandler.obtainMessage(UID_MSG_GONE, uid, 0).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 917 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 918 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 919 | @Override public void onUidActive(int uid) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 922 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 923 | } |
| 924 | |
| 925 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 926 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 927 | }; |
| 928 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 929 | final private BroadcastReceiver mPowerSaveWhitelistReceiver = new BroadcastReceiver() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 930 | @Override |
| 931 | public void onReceive(Context context, Intent intent) { |
| 932 | // on background handler thread, and POWER_SAVE_WHITELIST_CHANGED is protected |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 933 | synchronized (mUidRulesFirstLock) { |
| 934 | updatePowerSaveWhitelistUL(); |
| 935 | updateRulesForRestrictPowerUL(); |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 936 | updateRulesForAppIdleUL(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | }; |
| 940 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 941 | final private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 942 | @Override |
| 943 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 944 | // on background handler thread, and PACKAGE_ADDED is protected |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 945 | |
| 946 | final String action = intent.getAction(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 947 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 948 | if (uid == -1) return; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 949 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 950 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 951 | // update rules for UID, since it might be subject to |
| 952 | // global background data policy |
| 953 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 954 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 955 | updateRestrictionRulesForUidUL(uid); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 956 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | }; |
| 960 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 961 | final private BroadcastReceiver mUidRemovedReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 962 | @Override |
| 963 | public void onReceive(Context context, Intent intent) { |
| 964 | // on background handler thread, and UID_REMOVED is protected |
| 965 | |
| 966 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 967 | if (uid == -1) return; |
| 968 | |
| 969 | // remove any policy and update rules to clean up |
| 970 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 971 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 972 | onUidDeletedUL(uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 973 | synchronized (mNetworkPoliciesSecondLock) { |
| 974 | writePolicyAL(); |
| 975 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | }; |
| 979 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 980 | final private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 981 | @Override |
| 982 | public void onReceive(Context context, Intent intent) { |
| 983 | // on background handler thread, and USER_ADDED and USER_REMOVED |
| 984 | // broadcasts are protected |
| 985 | |
| 986 | final String action = intent.getAction(); |
| 987 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 988 | if (userId == -1) return; |
| 989 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 990 | switch (action) { |
| 991 | case ACTION_USER_REMOVED: |
| 992 | case ACTION_USER_ADDED: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 993 | synchronized (mUidRulesFirstLock) { |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 994 | // 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] | 995 | // USER_REMOVED, and one last sanity check during USER_ADDED |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 996 | removeUserStateUL(userId, true); |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 997 | // Removing outside removeUserStateUL since that can also be called when |
| 998 | // user resets app preferences. |
| 999 | mMeteredRestrictedUids.remove(userId); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1000 | if (action == ACTION_USER_ADDED) { |
| 1001 | // Add apps that are whitelisted by default. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1002 | addDefaultRestrictBackgroundWhitelistUidsUL(userId); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1003 | } |
| 1004 | // Update global restrict for that user |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1005 | synchronized (mNetworkPoliciesSecondLock) { |
| 1006 | updateRulesForGlobalChangeAL(true); |
| 1007 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 1008 | } |
| 1009 | break; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | }; |
| 1013 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1014 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1015 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 1016 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 1017 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1018 | final private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1019 | @Override |
| 1020 | public void onReceive(Context context, Intent intent) { |
| 1021 | // on background handler thread, and verified |
| 1022 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 1023 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1024 | synchronized (mNetworkPoliciesSecondLock) { |
| 1025 | updateNetworkEnabledNL(); |
| 1026 | updateNotificationsNL(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | }; |
| 1030 | |
| 1031 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1032 | * Receiver that watches for {@link Notification} control of |
| 1033 | * {@link #mRestrictBackground}. |
| 1034 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1035 | final private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1036 | @Override |
| 1037 | public void onReceive(Context context, Intent intent) { |
| 1038 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 1039 | // permission above. |
| 1040 | |
| 1041 | setRestrictBackground(false); |
| 1042 | } |
| 1043 | }; |
| 1044 | |
| 1045 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1046 | * Receiver that watches for {@link Notification} control of |
| 1047 | * {@link NetworkPolicy#lastWarningSnooze}. |
| 1048 | */ |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1049 | final private BroadcastReceiver mSnoozeReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1050 | @Override |
| 1051 | public void onReceive(Context context, Intent intent) { |
| 1052 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 1053 | // permission above. |
| 1054 | |
| 1055 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1056 | if (ACTION_SNOOZE_WARNING.equals(intent.getAction())) { |
| 1057 | performSnooze(template, TYPE_WARNING); |
| 1058 | } else if (ACTION_SNOOZE_RAPID.equals(intent.getAction())) { |
| 1059 | performSnooze(template, TYPE_RAPID); |
| 1060 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1061 | } |
| 1062 | }; |
| 1063 | |
| 1064 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1065 | * Receiver that watches for {@link WifiConfiguration} to be loaded so that |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1066 | * we can perform upgrade logic. After initial upgrade logic, it updates |
| 1067 | * {@link #mMeteredIfaces} based on configuration changes. |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1068 | */ |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1069 | final private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1070 | @Override |
| 1071 | public void onReceive(Context context, Intent intent) { |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1072 | synchronized (mUidRulesFirstLock) { |
| 1073 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1074 | upgradeWifiMeteredOverrideAL(); |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | // Only need to perform upgrade logic once |
| 1078 | mContext.unregisterReceiver(this); |
| 1079 | } |
| 1080 | }; |
| 1081 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1082 | private static boolean updateCapabilityChange(SparseBooleanArray lastValues, boolean newValue, |
| 1083 | Network network) { |
| 1084 | final boolean lastValue = lastValues.get(network.netId, false); |
| 1085 | final boolean changed = (lastValue != newValue) || lastValues.indexOfKey(network.netId) < 0; |
| 1086 | if (changed) { |
| 1087 | lastValues.put(network.netId, newValue); |
| 1088 | } |
| 1089 | return changed; |
| 1090 | } |
| 1091 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1092 | private final NetworkCallback mNetworkCallback = new NetworkCallback() { |
| 1093 | @Override |
| 1094 | public void onCapabilitiesChanged(Network network, |
| 1095 | NetworkCapabilities networkCapabilities) { |
| 1096 | if (network == null || networkCapabilities == null) return; |
| 1097 | |
| 1098 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1099 | final boolean newMetered = !networkCapabilities |
| 1100 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1101 | final boolean meteredChanged = updateCapabilityChange( |
| 1102 | mNetworkMetered, newMetered, network); |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 1103 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1104 | final boolean newRoaming = !networkCapabilities |
| 1105 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); |
| 1106 | final boolean roamingChanged = updateCapabilityChange( |
| 1107 | mNetworkRoaming, newRoaming, network); |
| 1108 | |
| 1109 | if (meteredChanged || roamingChanged) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 1110 | mLogger.meterednessChanged(network.netId, newMetered); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1111 | updateNetworkRulesNL(); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | }; |
| 1116 | |
| 1117 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1118 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 1119 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1120 | final private INetworkManagementEventObserver mAlertObserver |
| 1121 | = new BaseNetworkObserver() { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1122 | @Override |
| 1123 | public void limitReached(String limitName, String iface) { |
| 1124 | // only someone like NMS should be calling us |
| 1125 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1126 | |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 1127 | if (!LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 1128 | mHandler.obtainMessage(MSG_LIMIT_REACHED, iface).sendToTarget(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | }; |
| 1132 | |
| 1133 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1134 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 1135 | * to show visible notifications as needed. |
| 1136 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1137 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1138 | void updateNotificationsNL() { |
| 1139 | if (LOGV) Slog.v(TAG, "updateNotificationsNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1140 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNotificationsNL"); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1141 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1142 | // keep track of previously active notifications |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1143 | final ArraySet<NotificationId> beforeNotifs = new ArraySet<NotificationId>(mActiveNotifs); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1144 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1145 | |
| 1146 | // TODO: when switching to kernel notifications, compute next future |
| 1147 | // cycle boundary to recompute notifications. |
| 1148 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1149 | // examine stats for each active policy |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1150 | final long now = mClock.millis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1151 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1152 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1153 | final int subId = findRelevantSubIdNL(policy.template); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1154 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1155 | // ignore policies that aren't relevant to user |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1156 | if (subId == INVALID_SUBSCRIPTION_ID) continue; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1157 | if (!policy.hasCycle()) continue; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1158 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1159 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1160 | .cycleIterator(policy).next(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1161 | final long cycleStart = cycle.first.toInstant().toEpochMilli(); |
| 1162 | final long cycleEnd = cycle.second.toInstant().toEpochMilli(); |
| 1163 | final long totalBytes = getTotalBytes(policy.template, cycleStart, cycleEnd); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1164 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1165 | // Carrier might want to manage notifications themselves |
| 1166 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame^] | 1167 | if (!CarrierConfigManager.isConfigForIdentifiedCarrier(config)) { |
| 1168 | if (LOGV) Slog.v(TAG, "isConfigForIdentifiedCarrier returned false"); |
| 1169 | // Don't show notifications until we confirm that the loaded config is from an |
| 1170 | // identified carrier, which may want to manage their own notifications. This method |
| 1171 | // should be called every time the carrier config changes anyways, and there's no |
| 1172 | // reason to alert if there isn't a carrier. |
| 1173 | return; |
| 1174 | } |
| 1175 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1176 | final boolean notifyWarning = getBooleanDefeatingNullable(config, |
| 1177 | KEY_DATA_WARNING_NOTIFICATION_BOOL, true); |
| 1178 | final boolean notifyLimit = getBooleanDefeatingNullable(config, |
| 1179 | KEY_DATA_LIMIT_NOTIFICATION_BOOL, true); |
| 1180 | final boolean notifyRapid = getBooleanDefeatingNullable(config, |
| 1181 | KEY_DATA_RAPID_NOTIFICATION_BOOL, true); |
| 1182 | |
| 1183 | // Notify when data usage is over warning |
| 1184 | if (notifyWarning) { |
| 1185 | if (policy.isOverWarning(totalBytes) && !policy.isOverLimit(totalBytes)) { |
| 1186 | final boolean snoozedThisCycle = policy.lastWarningSnooze >= cycleStart; |
| 1187 | if (!snoozedThisCycle) { |
| 1188 | enqueueNotification(policy, TYPE_WARNING, totalBytes, null); |
| 1189 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1190 | } |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1191 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1192 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1193 | // Notify when data usage is over limit |
| 1194 | if (notifyLimit) { |
| 1195 | if (policy.isOverLimit(totalBytes)) { |
| 1196 | final boolean snoozedThisCycle = policy.lastLimitSnooze >= cycleStart; |
| 1197 | if (snoozedThisCycle) { |
| 1198 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes, null); |
| 1199 | } else { |
| 1200 | enqueueNotification(policy, TYPE_LIMIT, totalBytes, null); |
| 1201 | notifyOverLimitNL(policy.template); |
| 1202 | } |
| 1203 | } else { |
| 1204 | notifyUnderLimitNL(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1207 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1208 | // Warn if average usage over last 4 days is on track to blow pretty |
| 1209 | // far past the plan limits. |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1210 | if (notifyRapid && policy.limitBytes != LIMIT_DISABLED) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1211 | final long recentDuration = TimeUnit.DAYS.toMillis(4); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1212 | final long recentStart = now - recentDuration; |
| 1213 | final long recentEnd = now; |
| 1214 | final long recentBytes = getTotalBytes(policy.template, recentStart, recentEnd); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1215 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1216 | final long cycleDuration = cycleEnd - cycleStart; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1217 | final long projectedBytes = (recentBytes * cycleDuration) / recentDuration; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1218 | final long alertBytes = (policy.limitBytes * 3) / 2; |
| 1219 | |
| 1220 | if (LOGD) { |
| 1221 | Slog.d(TAG, "Rapid usage considering recent " + recentBytes + " projected " |
| 1222 | + projectedBytes + " alert " + alertBytes); |
| 1223 | } |
| 1224 | |
| 1225 | final boolean snoozedRecently = policy.lastRapidSnooze >= now |
| 1226 | - DateUtils.DAY_IN_MILLIS; |
| 1227 | if (projectedBytes > alertBytes && !snoozedRecently) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1228 | enqueueNotification(policy, TYPE_RAPID, 0, |
| 1229 | findRapidBlame(policy.template, recentStart, recentEnd)); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1234 | // cancel stale notifications that we didn't renew above |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1235 | for (int i = beforeNotifs.size()-1; i >= 0; i--) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1236 | final NotificationId notificationId = beforeNotifs.valueAt(i); |
| 1237 | if (!mActiveNotifs.contains(notificationId)) { |
| 1238 | cancelNotification(notificationId); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1241 | |
| 1242 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /** |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1246 | * Attempt to find a specific app to blame for rapid data usage during the |
| 1247 | * given time period. |
| 1248 | */ |
| 1249 | private @Nullable ApplicationInfo findRapidBlame(NetworkTemplate template, |
| 1250 | long start, long end) { |
| 1251 | long totalBytes = 0; |
| 1252 | long maxBytes = 0; |
| 1253 | int maxUid = 0; |
| 1254 | |
| 1255 | final NetworkStats stats = getNetworkUidBytes(template, start, end); |
| 1256 | NetworkStats.Entry entry = null; |
| 1257 | for (int i = 0; i < stats.size(); i++) { |
| 1258 | entry = stats.getValues(i, entry); |
| 1259 | final long bytes = entry.rxBytes + entry.txBytes; |
| 1260 | totalBytes += bytes; |
| 1261 | if (bytes > maxBytes) { |
| 1262 | maxBytes = bytes; |
| 1263 | maxUid = entry.uid; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // Only point blame if the majority of usage was done by a single app. |
| 1268 | // TODO: support shared UIDs |
| 1269 | if (maxBytes > 0 && maxBytes > totalBytes / 2) { |
| 1270 | final String[] packageNames = mContext.getPackageManager().getPackagesForUid(maxUid); |
Jeff Sharkey | d37154e | 2018-03-26 16:50:59 -0600 | [diff] [blame] | 1271 | if (packageNames != null && packageNames.length == 1) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1272 | try { |
| 1273 | return mContext.getPackageManager().getApplicationInfo(packageNames[0], |
| 1274 | MATCH_ANY_USER | MATCH_DISABLED_COMPONENTS | MATCH_DIRECT_BOOT_AWARE |
| 1275 | | MATCH_DIRECT_BOOT_UNAWARE | MATCH_UNINSTALLED_PACKAGES); |
| 1276 | } catch (NameNotFoundException ignored) { |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | return null; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1285 | * Test if given {@link NetworkTemplate} is relevant to user based on |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1286 | * current device state, such as when |
| 1287 | * {@link TelephonyManager#getSubscriberId()} matches. This is regardless of |
| 1288 | * data connection status. |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1289 | * |
| 1290 | * @return relevant subId, or {@link #INVALID_SUBSCRIPTION_ID} when no |
| 1291 | * matching subId found. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1292 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1293 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1294 | private int findRelevantSubIdNL(NetworkTemplate template) { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1295 | // Mobile template is relevant when any active subscriber matches |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1296 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 1297 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 1298 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1299 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1300 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, |
| 1301 | true); |
| 1302 | if (template.matches(probeIdent)) { |
| 1303 | return subId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1304 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1305 | } |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1306 | return INVALID_SUBSCRIPTION_ID; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1310 | * Notify that given {@link NetworkTemplate} is over |
| 1311 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 1312 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1313 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1314 | private void notifyOverLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1315 | if (!mOverLimitNotified.contains(template)) { |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1316 | mContext.startActivity(buildNetworkOverLimitIntent(mContext.getResources(), template)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1317 | mOverLimitNotified.add(template); |
| 1318 | } |
| 1319 | } |
| 1320 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1321 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1322 | private void notifyUnderLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1323 | mOverLimitNotified.remove(template); |
| 1324 | } |
| 1325 | |
| 1326 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1327 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 1328 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 1329 | */ |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1330 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes, |
| 1331 | ApplicationInfo rapidBlame) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1332 | final NotificationId notificationId = new NotificationId(policy, type); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1333 | final Notification.Builder builder = |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1334 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_ALERTS); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1335 | builder.setOnlyAlertOnce(true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1336 | builder.setWhen(0L); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 1337 | builder.setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 1338 | com.android.internal.R.color.system_notification_accent_color)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1339 | |
| 1340 | final Resources res = mContext.getResources(); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1341 | final CharSequence title; |
| 1342 | final CharSequence body; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1343 | switch (type) { |
| 1344 | case TYPE_WARNING: { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1345 | title = res.getText(R.string.data_usage_warning_title); |
| 1346 | body = res.getString(R.string.data_usage_warning_body, |
Anton Hansson | a6101e8 | 2019-03-29 15:50:09 +0000 | [diff] [blame] | 1347 | Formatter.formatFileSize(mContext, totalBytes, Formatter.FLAG_IEC_UNITS)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1348 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1349 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1350 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1351 | final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template); |
| 1352 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1353 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1354 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1355 | final Intent viewIntent = buildViewDataUsageIntent(res, policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1356 | builder.setContentIntent(PendingIntent.getActivity( |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1357 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1358 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1359 | break; |
| 1360 | } |
| 1361 | case TYPE_LIMIT: { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1362 | switch (policy.template.getMatchRule()) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1363 | case MATCH_MOBILE: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1364 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1365 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1366 | case MATCH_WIFI: |
| 1367 | title = res.getText(R.string.data_usage_wifi_limit_title); |
| 1368 | break; |
| 1369 | default: |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1370 | return; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1371 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1372 | body = res.getText(R.string.data_usage_limit_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1373 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1374 | builder.setOngoing(true); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1375 | builder.setSmallIcon(R.drawable.stat_notify_disabled_data); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1376 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1377 | final Intent intent = buildNetworkOverLimitIntent(res, policy.template); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1378 | builder.setContentIntent(PendingIntent.getActivity( |
| 1379 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1380 | break; |
| 1381 | } |
| 1382 | case TYPE_LIMIT_SNOOZED: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1383 | switch (policy.template.getMatchRule()) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1384 | case MATCH_MOBILE: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1385 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 1386 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1387 | case MATCH_WIFI: |
| 1388 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 1389 | break; |
| 1390 | default: |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1391 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1392 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1393 | final long overBytes = totalBytes - policy.limitBytes; |
| 1394 | body = res.getString(R.string.data_usage_limit_snoozed_body, |
Anton Hansson | a6101e8 | 2019-03-29 15:50:09 +0000 | [diff] [blame] | 1395 | Formatter.formatFileSize(mContext, overBytes, Formatter.FLAG_IEC_UNITS)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1396 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1397 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1398 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1399 | builder.setChannelId(SystemNotificationChannels.NETWORK_STATUS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1400 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1401 | final Intent intent = buildViewDataUsageIntent(res, policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1402 | builder.setContentIntent(PendingIntent.getActivity( |
| 1403 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1406 | case TYPE_RAPID: { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1407 | title = res.getText(R.string.data_usage_rapid_title); |
| 1408 | if (rapidBlame != null) { |
| 1409 | body = res.getString(R.string.data_usage_rapid_app_body, |
| 1410 | rapidBlame.loadLabel(mContext.getPackageManager())); |
| 1411 | } else { |
| 1412 | body = res.getString(R.string.data_usage_rapid_body); |
| 1413 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1414 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1415 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1416 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1417 | final Intent snoozeIntent = buildSnoozeRapidIntent(policy.template); |
| 1418 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1419 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1420 | |
| 1421 | final Intent viewIntent = buildViewDataUsageIntent(res, policy.template); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1422 | builder.setContentIntent(PendingIntent.getActivity( |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1423 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1424 | break; |
| 1425 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1426 | default: { |
| 1427 | return; |
| 1428 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1431 | builder.setTicker(title); |
| 1432 | builder.setContentTitle(title); |
| 1433 | builder.setContentText(body); |
| 1434 | builder.setStyle(new Notification.BigTextStyle().bigText(body)); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1435 | |
| 1436 | mContext.getSystemService(NotificationManager.class).notifyAsUser(notificationId.getTag(), |
| 1437 | notificationId.getId(), builder.build(), UserHandle.ALL); |
| 1438 | mActiveNotifs.add(notificationId); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1441 | private void cancelNotification(NotificationId notificationId) { |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1442 | mContext.getSystemService(NotificationManager.class).cancel(notificationId.getTag(), |
| 1443 | notificationId.getId()); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1447 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1448 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1449 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1450 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1451 | @Override |
| 1452 | public void onReceive(Context context, Intent intent) { |
| 1453 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 1454 | // permission above. |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1455 | updateNetworksInternal(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1456 | } |
| 1457 | }; |
| 1458 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1459 | private void updateNetworksInternal() { |
| 1460 | // Get all of our cross-process communication with telephony out of |
| 1461 | // the way before we acquire internal locks. |
| 1462 | updateSubscriptions(); |
| 1463 | |
| 1464 | synchronized (mUidRulesFirstLock) { |
| 1465 | synchronized (mNetworkPoliciesSecondLock) { |
| 1466 | ensureActiveMobilePolicyAL(); |
| 1467 | normalizePoliciesNL(); |
| 1468 | updateNetworkEnabledNL(); |
| 1469 | updateNetworkRulesNL(); |
| 1470 | updateNotificationsNL(); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1475 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1476 | void updateNetworks() throws InterruptedException { |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1477 | updateNetworksInternal(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1478 | final CountDownLatch latch = new CountDownLatch(1); |
| 1479 | mHandler.post(() -> { |
| 1480 | latch.countDown(); |
| 1481 | }); |
| 1482 | latch.await(5, TimeUnit.SECONDS); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1483 | } |
| 1484 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1485 | /** |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1486 | * Update mobile policies with data cycle information from {@link CarrierConfigManager} |
| 1487 | * if necessary. |
| 1488 | * |
| 1489 | * @param subId that has its associated NetworkPolicy updated if necessary |
| 1490 | * @return if any policies were updated |
| 1491 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1492 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1493 | private boolean maybeUpdateMobilePolicyCycleAL(int subId, String subscriberId) { |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1494 | if (LOGV) Slog.v(TAG, "maybeUpdateMobilePolicyCycleAL()"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1495 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1496 | // find and update the mobile NetworkPolicy for this subscriber id |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1497 | boolean policyUpdated = false; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1498 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1499 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1500 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1501 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1502 | if (template.matches(probeIdent)) { |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1503 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1504 | policyUpdated |= updateDefaultMobilePolicyAL(subId, policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1505 | } |
| 1506 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1507 | return policyUpdated; |
| 1508 | } |
| 1509 | |
| 1510 | /** |
| 1511 | * Returns the cycle day that should be used for a mobile NetworkPolicy. |
| 1512 | * |
| 1513 | * It attempts to get an appropriate cycle day from the passed in CarrierConfig. If it's unable |
| 1514 | * to do so, it returns the fallback value. |
| 1515 | * |
| 1516 | * @param config The CarrierConfig to read the value from. |
| 1517 | * @param fallbackCycleDay to return if the CarrierConfig can't be read. |
| 1518 | * @return cycleDay to use in the mobile NetworkPolicy. |
| 1519 | */ |
| 1520 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1521 | int getCycleDayFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1522 | int fallbackCycleDay) { |
| 1523 | if (config == null) { |
| 1524 | return fallbackCycleDay; |
| 1525 | } |
| 1526 | int cycleDay = |
| 1527 | config.getInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT); |
| 1528 | if (cycleDay == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1529 | return fallbackCycleDay; |
| 1530 | } |
| 1531 | // validate cycleDay value |
| 1532 | final Calendar cal = Calendar.getInstance(); |
| 1533 | if (cycleDay < cal.getMinimum(Calendar.DAY_OF_MONTH) || |
| 1534 | cycleDay > cal.getMaximum(Calendar.DAY_OF_MONTH)) { |
| 1535 | Slog.e(TAG, "Invalid date in " |
| 1536 | + "CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT: " + cycleDay); |
| 1537 | return fallbackCycleDay; |
| 1538 | } |
| 1539 | return cycleDay; |
| 1540 | } |
| 1541 | |
| 1542 | /** |
| 1543 | * Returns the warning bytes that should be used for a mobile NetworkPolicy. |
| 1544 | * |
| 1545 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1546 | * to do so, it returns the fallback value. |
| 1547 | * |
| 1548 | * @param config The CarrierConfig to read the value from. |
| 1549 | * @param fallbackWarningBytes to return if the CarrierConfig can't be read. |
| 1550 | * @return warningBytes to use in the mobile NetworkPolicy. |
| 1551 | */ |
| 1552 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1553 | long getWarningBytesFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1554 | long fallbackWarningBytes) { |
| 1555 | if (config == null) { |
| 1556 | return fallbackWarningBytes; |
| 1557 | } |
| 1558 | long warningBytes = |
| 1559 | config.getLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG); |
| 1560 | |
| 1561 | if (warningBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1562 | return WARNING_DISABLED; |
| 1563 | } else if (warningBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1564 | return getPlatformDefaultWarningBytes(); |
| 1565 | } else if (warningBytes < 0) { |
| 1566 | Slog.e(TAG, "Invalid value in " |
| 1567 | + "CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; expected a " |
| 1568 | + "non-negative value but got: " + warningBytes); |
| 1569 | return fallbackWarningBytes; |
| 1570 | } |
| 1571 | |
| 1572 | return warningBytes; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Returns the limit bytes that should be used for a mobile NetworkPolicy. |
| 1577 | * |
| 1578 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1579 | * to do so, it returns the fallback value. |
| 1580 | * |
| 1581 | * @param config The CarrierConfig to read the value from. |
| 1582 | * @param fallbackLimitBytes to return if the CarrierConfig can't be read. |
| 1583 | * @return limitBytes to use in the mobile NetworkPolicy. |
| 1584 | */ |
| 1585 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1586 | long getLimitBytesFromCarrierConfig(@Nullable PersistableBundle config, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1587 | long fallbackLimitBytes) { |
| 1588 | if (config == null) { |
| 1589 | return fallbackLimitBytes; |
| 1590 | } |
| 1591 | long limitBytes = |
| 1592 | config.getLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG); |
| 1593 | |
| 1594 | if (limitBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1595 | return LIMIT_DISABLED; |
| 1596 | } else if (limitBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1597 | return getPlatformDefaultLimitBytes(); |
| 1598 | } else if (limitBytes < 0) { |
| 1599 | Slog.e(TAG, "Invalid value in " |
| 1600 | + "CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; expected a " |
| 1601 | + "non-negative value but got: " + limitBytes); |
| 1602 | return fallbackLimitBytes; |
| 1603 | } |
| 1604 | return limitBytes; |
| 1605 | } |
| 1606 | |
| 1607 | /** |
| 1608 | * Receiver that watches for {@link CarrierConfigManager} to be changed. |
| 1609 | */ |
| 1610 | private BroadcastReceiver mCarrierConfigReceiver = new BroadcastReceiver() { |
| 1611 | @Override |
| 1612 | public void onReceive(Context context, Intent intent) { |
| 1613 | // No need to do a permission check, because the ACTION_CARRIER_CONFIG_CHANGED |
| 1614 | // broadcast is protected and can't be spoofed. Runs on a background handler thread. |
| 1615 | |
| 1616 | if (!intent.hasExtra(PhoneConstants.SUBSCRIPTION_KEY)) { |
| 1617 | return; |
| 1618 | } |
| 1619 | final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, -1); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1620 | |
| 1621 | // Get all of our cross-process communication with telephony out of |
| 1622 | // the way before we acquire internal locks. |
| 1623 | updateSubscriptions(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1624 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1625 | synchronized (mUidRulesFirstLock) { |
| 1626 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1627 | final String subscriberId = mSubIdToSubscriberId.get(subId, null); |
| 1628 | if (subscriberId != null) { |
| 1629 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 1630 | maybeUpdateMobilePolicyCycleAL(subId, subscriberId); |
| 1631 | } else { |
| 1632 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 1633 | } |
| 1634 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1635 | // update network and notification rules, as the data cycle changed and it's |
| 1636 | // possible that we should be triggering warnings/limits now |
| 1637 | handleNetworkPoliciesUpdateAL(true); |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | }; |
| 1642 | |
| 1643 | /** |
| 1644 | * Handles all tasks that need to be run after a new network policy has been set, or an existing |
| 1645 | * one has been updated. |
| 1646 | * |
| 1647 | * @param shouldNormalizePolicies true iff network policies need to be normalized after the |
| 1648 | * update. |
| 1649 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1650 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1651 | void handleNetworkPoliciesUpdateAL(boolean shouldNormalizePolicies) { |
| 1652 | if (shouldNormalizePolicies) { |
| 1653 | normalizePoliciesNL(); |
| 1654 | } |
| 1655 | updateNetworkEnabledNL(); |
| 1656 | updateNetworkRulesNL(); |
| 1657 | updateNotificationsNL(); |
| 1658 | writePolicyAL(); |
| 1659 | } |
| 1660 | |
| 1661 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1662 | * Proactively control network data connections when they exceed |
| 1663 | * {@link NetworkPolicy#limitBytes}. |
| 1664 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1665 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1666 | void updateNetworkEnabledNL() { |
| 1667 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1668 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNetworkEnabledNL"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1669 | |
| 1670 | // TODO: reset any policy-disabled networks when any policy is removed |
| 1671 | // completely, which is currently rare case. |
| 1672 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 1673 | final long startTime = mStatLogger.getTime(); |
| 1674 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1675 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1676 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1677 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1678 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1679 | setNetworkTemplateEnabled(policy.template, true); |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1683 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1684 | .cycleIterator(policy).next(); |
| 1685 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1686 | final long end = cycle.second.toInstant().toEpochMilli(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1687 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1688 | |
| 1689 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1690 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 1691 | && policy.lastLimitSnooze < start; |
| 1692 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1693 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1694 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1695 | } |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 1696 | |
| 1697 | mStatLogger.logDurationStat(Stats.UPDATE_NETWORK_ENABLED, startTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1698 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1702 | * Proactively disable networks that match the given |
| 1703 | * {@link NetworkTemplate}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1704 | */ |
| 1705 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 1706 | // Don't call setNetworkTemplateEnabledInner() directly because we may have a lock |
| 1707 | // held. Call it via the handler. |
| 1708 | mHandler.obtainMessage(MSG_SET_NETWORK_TEMPLATE_ENABLED, enabled ? 1 : 0, 0, template) |
| 1709 | .sendToTarget(); |
| 1710 | } |
| 1711 | |
| 1712 | private void setNetworkTemplateEnabledInner(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1713 | // TODO: reach into ConnectivityManager to proactively disable bringing |
| 1714 | // up this network, since we know that traffic will be blocked. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1715 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1716 | if (template.getMatchRule() == MATCH_MOBILE) { |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1717 | // If mobile data usage hits the limit or if the user resumes the data, we need to |
| 1718 | // notify telephony. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1719 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1720 | final IntArray matchingSubIds = new IntArray(); |
| 1721 | synchronized (mNetworkPoliciesSecondLock) { |
| 1722 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 1723 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 1724 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
| 1725 | |
| 1726 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1727 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, |
| 1728 | true); |
| 1729 | // Template is matched when subscriber id matches. |
| 1730 | if (template.matches(probeIdent)) { |
| 1731 | matchingSubIds.add(subId); |
| 1732 | } |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1733 | } |
| 1734 | } |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1735 | |
| 1736 | // Only talk with telephony outside of locks |
| 1737 | final TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 1738 | for (int i = 0; i < matchingSubIds.size(); i++) { |
| 1739 | final int subId = matchingSubIds.get(i); |
| 1740 | tm.setPolicyDataEnabled(enabled, subId); |
| 1741 | } |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1742 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | /** |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1746 | * Collect all ifaces from a {@link NetworkState} into the given set. |
| 1747 | */ |
| 1748 | private static void collectIfaces(ArraySet<String> ifaces, NetworkState state) { |
| 1749 | final String baseIface = state.linkProperties.getInterfaceName(); |
| 1750 | if (baseIface != null) { |
| 1751 | ifaces.add(baseIface); |
| 1752 | } |
| 1753 | for (LinkProperties stackedLink : state.linkProperties.getStackedLinks()) { |
| 1754 | final String stackedIface = stackedLink.getInterfaceName(); |
| 1755 | if (stackedIface != null) { |
| 1756 | ifaces.add(stackedIface); |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | /** |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1762 | * Examine all currently active subscriptions from |
| 1763 | * {@link SubscriptionManager#getActiveSubscriptionIdList()} and update |
| 1764 | * internal data structures. |
| 1765 | * <p> |
| 1766 | * Callers <em>must not</em> hold any locks when this method called. |
| 1767 | */ |
| 1768 | void updateSubscriptions() { |
| 1769 | if (LOGV) Slog.v(TAG, "updateSubscriptions()"); |
| 1770 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateSubscriptions"); |
| 1771 | |
| 1772 | final TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 1773 | final SubscriptionManager sm = mContext.getSystemService(SubscriptionManager.class); |
| 1774 | |
| 1775 | final int[] subIds = ArrayUtils.defeatNullable(sm.getActiveSubscriptionIdList()); |
| 1776 | final String[] mergedSubscriberIds = ArrayUtils.defeatNullable(tm.getMergedSubscriberIds()); |
| 1777 | |
| 1778 | final SparseArray<String> subIdToSubscriberId = new SparseArray<>(subIds.length); |
| 1779 | for (int subId : subIds) { |
| 1780 | final String subscriberId = tm.getSubscriberId(subId); |
| 1781 | if (!TextUtils.isEmpty(subscriberId)) { |
| 1782 | subIdToSubscriberId.put(subId, subscriberId); |
| 1783 | } else { |
| 1784 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | synchronized (mNetworkPoliciesSecondLock) { |
| 1789 | mSubIdToSubscriberId.clear(); |
| 1790 | for (int i = 0; i < subIdToSubscriberId.size(); i++) { |
| 1791 | mSubIdToSubscriberId.put(subIdToSubscriberId.keyAt(i), |
| 1792 | subIdToSubscriberId.valueAt(i)); |
| 1793 | } |
| 1794 | |
| 1795 | mMergedSubscriberIds = mergedSubscriberIds; |
| 1796 | } |
| 1797 | |
| 1798 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1799 | } |
| 1800 | |
| 1801 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1802 | * Examine all connected {@link NetworkState}, looking for |
| 1803 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1804 | * remaining quota based on usage cycle and historical stats. |
| 1805 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1806 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1807 | void updateNetworkRulesNL() { |
| 1808 | if (LOGV) Slog.v(TAG, "updateNetworkRulesNL()"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1809 | Trace.traceBegin(TRACE_TAG_NETWORK, "updateNetworkRulesNL"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1810 | |
| 1811 | final NetworkState[] states; |
| 1812 | try { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1813 | states = defeatNullable(mConnManager.getAllNetworkState()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1814 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1815 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1816 | return; |
| 1817 | } |
| 1818 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1819 | // First, generate identities of all connected networks so we can |
| 1820 | // quickly compare them against all defined policies below. |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1821 | mNetIdToSubId.clear(); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1822 | final ArrayMap<NetworkState, NetworkIdentity> identified = new ArrayMap<>(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1823 | for (NetworkState state : states) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1824 | if (state.network != null) { |
| 1825 | mNetIdToSubId.put(state.network.netId, parseSubId(state)); |
| 1826 | } |
Wei Liu | b8eaf45 | 2016-01-25 10:32:27 -0800 | [diff] [blame] | 1827 | if (state.networkInfo != null && state.networkInfo.isConnected()) { |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1828 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, |
| 1829 | true); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1830 | identified.put(state, ident); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1834 | final ArraySet<String> newMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1835 | long lowestRule = Long.MAX_VALUE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1836 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1837 | // For every well-defined policy, compute remaining data based on |
| 1838 | // current cycle and historical stats, and push to kernel. |
| 1839 | final ArraySet<String> matchingIfaces = new ArraySet<>(); |
| 1840 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1841 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1842 | |
| 1843 | // Collect all ifaces that match this policy |
| 1844 | matchingIfaces.clear(); |
| 1845 | for (int j = identified.size() - 1; j >= 0; j--) { |
| 1846 | if (policy.template.matches(identified.valueAt(j))) { |
| 1847 | collectIfaces(matchingIfaces, identified.keyAt(j)); |
| 1848 | } |
| 1849 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1850 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1851 | if (LOGD) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1852 | Slog.d(TAG, "Applying " + policy + " to ifaces " + matchingIfaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1855 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1856 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1857 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1858 | final long quotaBytes; |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1859 | if (hasLimit && policy.hasCycle()) { |
| 1860 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1861 | .cycleIterator(policy).next(); |
| 1862 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1863 | final long end = cycle.second.toInstant().toEpochMilli(); |
| 1864 | final long totalBytes = getTotalBytes(policy.template, start, end); |
| 1865 | |
| 1866 | if (policy.lastLimitSnooze >= start) { |
| 1867 | // snoozing past quota, but we still need to restrict apps, |
| 1868 | // so push really high quota. |
| 1869 | quotaBytes = Long.MAX_VALUE; |
| 1870 | } else { |
| 1871 | // remaining "quota" bytes are based on total usage in |
| 1872 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1873 | // set 1-byte quota and disable the radio later. |
| 1874 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1875 | } |
| 1876 | } else { |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1877 | // metered network, but no policy limit; we still need to |
| 1878 | // restrict apps, so push really high quota. |
| 1879 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1880 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1881 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1882 | if (matchingIfaces.size() > 1) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1883 | // TODO: switch to shared quota once NMS supports |
| 1884 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1887 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1888 | final String iface = matchingIfaces.valueAt(j); |
| 1889 | setInterfaceQuotaAsync(iface, quotaBytes); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1890 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1891 | } |
| 1892 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1893 | |
| 1894 | // keep track of lowest warning or limit of active policies |
| 1895 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1896 | lowestRule = policy.warningBytes; |
| 1897 | } |
| 1898 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1899 | lowestRule = policy.limitBytes; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1903 | // One final pass to catch any metered ifaces that don't have explicitly |
| 1904 | // defined policies; typically Wi-Fi networks. |
| 1905 | for (NetworkState state : states) { |
| 1906 | if (state.networkInfo != null && state.networkInfo.isConnected() |
| 1907 | && !state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 1908 | matchingIfaces.clear(); |
| 1909 | collectIfaces(matchingIfaces, state); |
| 1910 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1911 | final String iface = matchingIfaces.valueAt(j); |
| 1912 | if (!newMeteredIfaces.contains(iface)) { |
| 1913 | setInterfaceQuotaAsync(iface, Long.MAX_VALUE); |
| 1914 | newMeteredIfaces.add(iface); |
| 1915 | } |
| 1916 | } |
| 1917 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1920 | // Remove quota from any interfaces that are no longer metered. |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1921 | for (int i = mMeteredIfaces.size() - 1; i >= 0; i--) { |
| 1922 | final String iface = mMeteredIfaces.valueAt(i); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1923 | if (!newMeteredIfaces.contains(iface)) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1924 | removeInterfaceQuotaAsync(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | mMeteredIfaces = newMeteredIfaces; |
| 1928 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1929 | final ContentResolver cr = mContext.getContentResolver(); |
| 1930 | final boolean quotaEnabled = Settings.Global.getInt(cr, |
| 1931 | NETPOLICY_QUOTA_ENABLED, 1) != 0; |
| 1932 | final long quotaUnlimited = Settings.Global.getLong(cr, |
| 1933 | NETPOLICY_QUOTA_UNLIMITED, QUOTA_UNLIMITED_DEFAULT); |
| 1934 | final float quotaLimited = Settings.Global.getFloat(cr, |
| 1935 | NETPOLICY_QUOTA_LIMITED, QUOTA_LIMITED_DEFAULT); |
| 1936 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1937 | // Finally, calculate our opportunistic quotas |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1938 | mSubscriptionOpportunisticQuota.clear(); |
| 1939 | for (NetworkState state : states) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1940 | if (!quotaEnabled) continue; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1941 | if (state.network == null) continue; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1942 | final int subId = getSubIdLocked(state.network); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1943 | final SubscriptionPlan plan = getPrimarySubscriptionPlanLocked(subId); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1944 | if (plan == null) continue; |
| 1945 | |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 1946 | final long quotaBytes; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1947 | final long limitBytes = plan.getDataLimitBytes(); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1948 | if (!state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_ROAMING)) { |
| 1949 | // Clamp to 0 when roaming |
| 1950 | quotaBytes = 0; |
| 1951 | } else if (limitBytes == SubscriptionPlan.BYTES_UNKNOWN) { |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 1952 | quotaBytes = OPPORTUNISTIC_QUOTA_UNKNOWN; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1953 | } else if (limitBytes == SubscriptionPlan.BYTES_UNLIMITED) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1954 | // Unlimited data; let's use 20MiB/day (600MiB/month) |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1955 | quotaBytes = quotaUnlimited; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1956 | } else { |
| 1957 | // Limited data; let's only use 10% of remaining budget |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 1958 | final Range<ZonedDateTime> cycle = plan.cycleIterator().next(); |
| 1959 | final long start = cycle.getLower().toInstant().toEpochMilli(); |
| 1960 | final long end = cycle.getUpper().toInstant().toEpochMilli(); |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 1961 | final Instant now = mClock.instant(); |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 1962 | final long startOfDay = ZonedDateTime.ofInstant(now, cycle.getLower().getZone()) |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 1963 | .truncatedTo(ChronoUnit.DAYS) |
| 1964 | .toInstant().toEpochMilli(); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1965 | final long totalBytes = getTotalBytes( |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 1966 | NetworkTemplate.buildTemplateMobileAll(state.subscriberId), |
| 1967 | start, startOfDay); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1968 | final long remainingBytes = limitBytes - totalBytes; |
Remi NGUYEN VAN | bed7b97 | 2018-04-02 15:48:19 +0900 | [diff] [blame] | 1969 | // Number of remaining days including current day |
| 1970 | final long remainingDays = |
| 1971 | 1 + ((end - now.toEpochMilli() - 1) / TimeUnit.DAYS.toMillis(1)); |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 1972 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1973 | quotaBytes = Math.max(0, (long) ((remainingBytes / remainingDays) * quotaLimited)); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | mSubscriptionOpportunisticQuota.put(subId, quotaBytes); |
| 1977 | } |
| 1978 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1979 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1980 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1981 | |
| 1982 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1983 | |
| 1984 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | /** |
| 1988 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1989 | * have at least a default mobile policy defined. |
| 1990 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 1991 | @GuardedBy("mNetworkPoliciesSecondLock") |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1992 | private void ensureActiveMobilePolicyAL() { |
| 1993 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyAL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1994 | if (mSuppressDefaultPolicy) return; |
| 1995 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 1996 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 1997 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 1998 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1999 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2000 | ensureActiveMobilePolicyAL(subId, subscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2001 | } |
| 2002 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2003 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2004 | /** |
| 2005 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 2006 | * have at least a default mobile policy defined. |
| 2007 | * |
| 2008 | * @param subId to build a default policy for |
| 2009 | * @param subscriberId that we check for an existing policy |
| 2010 | * @return true if a mobile network policy was added, or false one already existed. |
| 2011 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2012 | @GuardedBy("mNetworkPoliciesSecondLock") |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2013 | private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2014 | // Poke around to see if we already have a policy |
| 2015 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 2016 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2017 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 2018 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 2019 | if (template.matches(probeIdent)) { |
| 2020 | if (LOGD) { |
| 2021 | Slog.d(TAG, "Found template " + template + " which matches subscriber " |
| 2022 | + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 2023 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2024 | return false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2028 | Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 2029 | + "; generating default policy"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2030 | final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2031 | addNetworkPolicyAL(policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2032 | return true; |
| 2033 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2034 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2035 | private long getPlatformDefaultWarningBytes() { |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2036 | final int dataWarningConfig = mContext.getResources().getInteger( |
| 2037 | com.android.internal.R.integer.config_networkPolicyDefaultWarning); |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2038 | if (dataWarningConfig == WARNING_DISABLED) { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2039 | return WARNING_DISABLED; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2040 | } else { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2041 | return dataWarningConfig * MB_IN_BYTES; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 2042 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2043 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2044 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2045 | private long getPlatformDefaultLimitBytes() { |
| 2046 | return LIMIT_DISABLED; |
| 2047 | } |
| 2048 | |
| 2049 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 2050 | NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2051 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2052 | final RecurrenceRule cycleRule = NetworkPolicy |
| 2053 | .buildRule(ZonedDateTime.now().getDayOfMonth(), ZoneId.systemDefault()); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2054 | final NetworkPolicy policy = new NetworkPolicy(template, cycleRule, |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2055 | getPlatformDefaultWarningBytes(), getPlatformDefaultLimitBytes(), |
| 2056 | SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 2057 | synchronized (mUidRulesFirstLock) { |
| 2058 | synchronized (mNetworkPoliciesSecondLock) { |
| 2059 | updateDefaultMobilePolicyAL(subId, policy); |
| 2060 | } |
| 2061 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2062 | return policy; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2065 | /** |
| 2066 | * Update the given {@link NetworkPolicy} based on any carrier-provided |
| 2067 | * defaults via {@link SubscriptionPlan} or {@link CarrierConfigManager}. |
| 2068 | * Leaves policy untouched if the user has modified it. |
| 2069 | * |
| 2070 | * @return if the policy was modified |
| 2071 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2072 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2073 | private boolean updateDefaultMobilePolicyAL(int subId, NetworkPolicy policy) { |
| 2074 | if (!policy.inferred) { |
| 2075 | if (LOGD) Slog.d(TAG, "Ignoring user-defined policy " + policy); |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | final NetworkPolicy original = new NetworkPolicy(policy.template, policy.cycleRule, |
| 2080 | policy.warningBytes, policy.limitBytes, policy.lastWarningSnooze, |
| 2081 | policy.lastLimitSnooze, policy.metered, policy.inferred); |
| 2082 | |
| 2083 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
| 2084 | if (!ArrayUtils.isEmpty(plans)) { |
| 2085 | final SubscriptionPlan plan = plans[0]; |
| 2086 | policy.cycleRule = plan.getCycleRule(); |
| 2087 | final long planLimitBytes = plan.getDataLimitBytes(); |
| 2088 | if (planLimitBytes == SubscriptionPlan.BYTES_UNKNOWN) { |
| 2089 | policy.warningBytes = getPlatformDefaultWarningBytes(); |
| 2090 | policy.limitBytes = getPlatformDefaultLimitBytes(); |
| 2091 | } else if (planLimitBytes == SubscriptionPlan.BYTES_UNLIMITED) { |
| 2092 | policy.warningBytes = NetworkPolicy.WARNING_DISABLED; |
| 2093 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 2094 | } else { |
| 2095 | policy.warningBytes = (planLimitBytes * 9) / 10; |
| 2096 | switch (plan.getDataLimitBehavior()) { |
| 2097 | case SubscriptionPlan.LIMIT_BEHAVIOR_BILLED: |
| 2098 | case SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED: |
| 2099 | policy.limitBytes = planLimitBytes; |
| 2100 | break; |
| 2101 | default: |
| 2102 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 2103 | break; |
| 2104 | } |
| 2105 | } |
| 2106 | } else { |
| 2107 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
| 2108 | final int currentCycleDay; |
| 2109 | if (policy.cycleRule.isMonthly()) { |
| 2110 | currentCycleDay = policy.cycleRule.start.getDayOfMonth(); |
| 2111 | } else { |
| 2112 | currentCycleDay = NetworkPolicy.CYCLE_NONE; |
| 2113 | } |
| 2114 | final int cycleDay = getCycleDayFromCarrierConfig(config, currentCycleDay); |
| 2115 | policy.cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.systemDefault()); |
| 2116 | policy.warningBytes = getWarningBytesFromCarrierConfig(config, policy.warningBytes); |
| 2117 | policy.limitBytes = getLimitBytesFromCarrierConfig(config, policy.limitBytes); |
| 2118 | } |
| 2119 | |
| 2120 | if (policy.equals(original)) { |
| 2121 | return false; |
| 2122 | } else { |
| 2123 | Slog.d(TAG, "Updated " + original + " to " + policy); |
| 2124 | return true; |
| 2125 | } |
| 2126 | } |
| 2127 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2128 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2129 | private void readPolicyAL() { |
| 2130 | if (LOGV) Slog.v(TAG, "readPolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2131 | |
| 2132 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2133 | mNetworkPolicy.clear(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2134 | mSubscriptionPlans.clear(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2135 | mSubscriptionPlansOwner.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2136 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2137 | |
| 2138 | FileInputStream fis = null; |
| 2139 | try { |
| 2140 | fis = mPolicyFile.openRead(); |
| 2141 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2142 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2143 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2144 | // Must save the <restrict-background> tags and convert them to <uid-policy> later, |
| 2145 | // to skip UIDs that were explicitly blacklisted. |
| 2146 | final SparseBooleanArray whitelistedRestrictBackground = new SparseBooleanArray(); |
| 2147 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2148 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2149 | int version = VERSION_INIT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2150 | boolean insideWhitelist = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2151 | while ((type = in.next()) != END_DOCUMENT) { |
| 2152 | final String tag = in.getName(); |
| 2153 | if (type == START_TAG) { |
| 2154 | if (TAG_POLICY_LIST.equals(tag)) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 2155 | final boolean oldValue = mRestrictBackground; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2156 | version = readIntAttribute(in, ATTR_VERSION); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2157 | mLoadedRestrictBackground = (version >= VERSION_ADDED_RESTRICT_BACKGROUND) |
| 2158 | && readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2159 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 2160 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 2161 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2162 | final String networkId; |
| 2163 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 2164 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 2165 | } else { |
| 2166 | networkId = null; |
| 2167 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2168 | final RecurrenceRule cycleRule; |
| 2169 | if (version >= VERSION_ADDED_CYCLE) { |
| 2170 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 2171 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 2172 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 2173 | cycleRule = new RecurrenceRule( |
| 2174 | RecurrenceRule.convertZonedDateTime(start), |
| 2175 | RecurrenceRule.convertZonedDateTime(end), |
| 2176 | RecurrenceRule.convertPeriod(period)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 2177 | } else { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2178 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
| 2179 | final String cycleTimezone; |
| 2180 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 2181 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 2182 | } else { |
| 2183 | cycleTimezone = "UTC"; |
| 2184 | } |
| 2185 | cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.of(cycleTimezone)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 2186 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2187 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 2188 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2189 | final long lastLimitSnooze; |
| 2190 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 2191 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 2192 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 2193 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2194 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2195 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2196 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2197 | final boolean metered; |
| 2198 | if (version >= VERSION_ADDED_METERED) { |
| 2199 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 2200 | } else { |
| 2201 | switch (networkTemplate) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 2202 | case MATCH_MOBILE: |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2203 | metered = true; |
| 2204 | break; |
| 2205 | default: |
| 2206 | metered = false; |
| 2207 | } |
| 2208 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2209 | final long lastWarningSnooze; |
| 2210 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 2211 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 2212 | } else { |
| 2213 | lastWarningSnooze = SNOOZE_NEVER; |
| 2214 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2215 | final boolean inferred; |
| 2216 | if (version >= VERSION_ADDED_INFERRED) { |
| 2217 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 2218 | } else { |
| 2219 | inferred = false; |
| 2220 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2221 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2222 | final NetworkTemplate template = new NetworkTemplate(networkTemplate, |
| 2223 | subscriberId, networkId); |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2224 | if (template.isPersistable()) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2225 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleRule, |
| 2226 | warningBytes, limitBytes, lastWarningSnooze, |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2227 | lastLimitSnooze, metered, inferred)); |
| 2228 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2229 | |
| 2230 | } else if (TAG_SUBSCRIPTION_PLAN.equals(tag)) { |
| 2231 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 2232 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 2233 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 2234 | final SubscriptionPlan.Builder builder = new SubscriptionPlan.Builder( |
| 2235 | RecurrenceRule.convertZonedDateTime(start), |
| 2236 | RecurrenceRule.convertZonedDateTime(end), |
| 2237 | RecurrenceRule.convertPeriod(period)); |
| 2238 | builder.setTitle(readStringAttribute(in, ATTR_TITLE)); |
| 2239 | builder.setSummary(readStringAttribute(in, ATTR_SUMMARY)); |
| 2240 | |
| 2241 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES, |
| 2242 | SubscriptionPlan.BYTES_UNKNOWN); |
| 2243 | final int limitBehavior = readIntAttribute(in, ATTR_LIMIT_BEHAVIOR, |
| 2244 | SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN); |
| 2245 | if (limitBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 2246 | && limitBehavior != SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
| 2247 | builder.setDataLimit(limitBytes, limitBehavior); |
| 2248 | } |
| 2249 | |
| 2250 | final long usageBytes = readLongAttribute(in, ATTR_USAGE_BYTES, |
| 2251 | SubscriptionPlan.BYTES_UNKNOWN); |
| 2252 | final long usageTime = readLongAttribute(in, ATTR_USAGE_TIME, |
| 2253 | SubscriptionPlan.TIME_UNKNOWN); |
| 2254 | if (usageBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 2255 | && usageTime != SubscriptionPlan.TIME_UNKNOWN) { |
| 2256 | builder.setDataUsage(usageBytes, usageTime); |
| 2257 | } |
| 2258 | |
| 2259 | final int subId = readIntAttribute(in, ATTR_SUB_ID); |
| 2260 | final SubscriptionPlan plan = builder.build(); |
| 2261 | mSubscriptionPlans.put(subId, ArrayUtils.appendElement( |
| 2262 | SubscriptionPlan.class, mSubscriptionPlans.get(subId), plan)); |
| 2263 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2264 | final String ownerPackage = readStringAttribute(in, ATTR_OWNER_PACKAGE); |
| 2265 | mSubscriptionPlansOwner.put(subId, ownerPackage); |
| 2266 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2267 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2268 | final int uid = readIntAttribute(in, ATTR_UID); |
| 2269 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 2270 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2271 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2272 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2273 | } else { |
| 2274 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 2275 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2276 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2277 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 2278 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 2279 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2280 | // TODO: set for other users during upgrade |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 2281 | // app policy is deprecated so this is only used in pre system user split. |
| 2282 | final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2283 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2284 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2285 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2286 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2287 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2288 | } else if (TAG_WHITELIST.equals(tag)) { |
| 2289 | insideWhitelist = true; |
| 2290 | } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 2291 | final int uid = readIntAttribute(in, ATTR_UID); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2292 | whitelistedRestrictBackground.append(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2293 | } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 2294 | final int uid = readIntAttribute(in, ATTR_UID); |
| 2295 | mRestrictBackgroundWhitelistRevokedUids.put(uid, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2296 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2297 | } else if (type == END_TAG) { |
| 2298 | if (TAG_WHITELIST.equals(tag)) { |
| 2299 | insideWhitelist = false; |
| 2300 | } |
| 2301 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2305 | final int size = whitelistedRestrictBackground.size(); |
| 2306 | for (int i = 0; i < size; i++) { |
| 2307 | final int uid = whitelistedRestrictBackground.keyAt(i); |
| 2308 | final int policy = mUidPolicy.get(uid, POLICY_NONE); |
| 2309 | if ((policy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
| 2310 | Slog.w(TAG, "ignoring restrict-background-whitelist for " + uid |
| 2311 | + " because its policy is " + uidPoliciesToString(policy)); |
| 2312 | continue; |
| 2313 | } |
| 2314 | if (UserHandle.isApp(uid)) { |
| 2315 | final int newPolicy = policy | POLICY_ALLOW_METERED_BACKGROUND; |
| 2316 | if (LOGV) |
| 2317 | Log.v(TAG, "new policy for " + uid + ": " + uidPoliciesToString(newPolicy)); |
| 2318 | setUidPolicyUncheckedUL(uid, newPolicy, false); |
| 2319 | } else { |
| 2320 | Slog.w(TAG, "unable to update policy on UID " + uid); |
| 2321 | } |
| 2322 | } |
| 2323 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2324 | } catch (FileNotFoundException e) { |
| 2325 | // missing policy is okay, probably first boot |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2326 | upgradeDefaultBackgroundDataUL(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2327 | } catch (Exception e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2328 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2329 | } finally { |
| 2330 | IoUtils.closeQuietly(fis); |
| 2331 | } |
| 2332 | } |
| 2333 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2334 | /** |
| 2335 | * Upgrade legacy background data flags, notifying listeners of one last |
| 2336 | * change to always-true. |
| 2337 | */ |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2338 | private void upgradeDefaultBackgroundDataUL() { |
| 2339 | // This method is only called when we're unable to find the network policy flag, which |
| 2340 | // 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] | 2341 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2342 | // Seed from the default value configured for this device. |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2343 | mLoadedRestrictBackground = Settings.Global.getInt( |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 2344 | mContext.getContentResolver(), Global.DEFAULT_RESTRICT_BACKGROUND_DATA, 0) == 1; |
| 2345 | |
| 2346 | // NOTE: We used to read the legacy setting here : |
| 2347 | // |
| 2348 | // final int legacyFlagValue = Settings.Secure.getInt( |
| 2349 | // mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, ..); |
| 2350 | // |
| 2351 | // This is no longer necessary because we will never upgrade directly from Gingerbread |
| 2352 | // to O+. Devices upgrading from ICS onwards to O will have a netpolicy.xml file that |
| 2353 | // contains the correct value that we will continue to use. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2356 | /** |
| 2357 | * Perform upgrade step of moving any user-defined meterness overrides over |
| 2358 | * into {@link WifiConfiguration}. |
| 2359 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2360 | @GuardedBy({"mNetworkPoliciesSecondLock", "mUidRulesFirstLock"}) |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2361 | private void upgradeWifiMeteredOverrideAL() { |
| 2362 | boolean modified = false; |
| 2363 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2364 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2365 | for (int i = 0; i < mNetworkPolicy.size(); ) { |
| 2366 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 2367 | if (policy.template.getMatchRule() == NetworkTemplate.MATCH_WIFI |
| 2368 | && !policy.inferred) { |
| 2369 | mNetworkPolicy.removeAt(i); |
| 2370 | modified = true; |
| 2371 | |
| 2372 | final String networkId = resolveNetworkId(policy.template.getNetworkId()); |
| 2373 | for (WifiConfiguration config : configs) { |
| 2374 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2375 | Slog.d(TAG, "Found network " + networkId + "; upgrading metered hint"); |
| 2376 | config.meteredOverride = policy.metered |
| 2377 | ? WifiConfiguration.METERED_OVERRIDE_METERED |
| 2378 | : WifiConfiguration.METERED_OVERRIDE_NOT_METERED; |
| 2379 | wm.updateNetwork(config); |
| 2380 | } |
| 2381 | } |
| 2382 | } else { |
| 2383 | i++; |
| 2384 | } |
| 2385 | } |
| 2386 | if (modified) { |
| 2387 | writePolicyAL(); |
| 2388 | } |
| 2389 | } |
| 2390 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2391 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2392 | void writePolicyAL() { |
| 2393 | if (LOGV) Slog.v(TAG, "writePolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2394 | |
| 2395 | FileOutputStream fos = null; |
| 2396 | try { |
| 2397 | fos = mPolicyFile.startWrite(); |
| 2398 | |
| 2399 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2400 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2401 | out.startDocument(null, true); |
| 2402 | |
| 2403 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2404 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2405 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2406 | |
| 2407 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2408 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2409 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2410 | final NetworkTemplate template = policy.template; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2411 | if (!template.isPersistable()) continue; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2412 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2413 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2414 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 2415 | final String subscriberId = template.getSubscriberId(); |
| 2416 | if (subscriberId != null) { |
| 2417 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2418 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2419 | final String networkId = template.getNetworkId(); |
| 2420 | if (networkId != null) { |
| 2421 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 2422 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2423 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2424 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.start)); |
| 2425 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2426 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.end)); |
| 2427 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2428 | RecurrenceRule.convertPeriod(policy.cycleRule.period)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2429 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 2430 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2431 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 2432 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2433 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2434 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2435 | out.endTag(null, TAG_NETWORK_POLICY); |
| 2436 | } |
| 2437 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2438 | // write all known subscription plans |
| 2439 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 2440 | final int subId = mSubscriptionPlans.keyAt(i); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2441 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2442 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 2443 | if (ArrayUtils.isEmpty(plans)) continue; |
| 2444 | |
| 2445 | for (SubscriptionPlan plan : plans) { |
| 2446 | out.startTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2447 | writeIntAttribute(out, ATTR_SUB_ID, subId); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2448 | writeStringAttribute(out, ATTR_OWNER_PACKAGE, ownerPackage); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2449 | final RecurrenceRule cycleRule = plan.getCycleRule(); |
| 2450 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2451 | RecurrenceRule.convertZonedDateTime(cycleRule.start)); |
| 2452 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2453 | RecurrenceRule.convertZonedDateTime(cycleRule.end)); |
| 2454 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2455 | RecurrenceRule.convertPeriod(cycleRule.period)); |
| 2456 | writeStringAttribute(out, ATTR_TITLE, plan.getTitle()); |
| 2457 | writeStringAttribute(out, ATTR_SUMMARY, plan.getSummary()); |
| 2458 | writeLongAttribute(out, ATTR_LIMIT_BYTES, plan.getDataLimitBytes()); |
| 2459 | writeIntAttribute(out, ATTR_LIMIT_BEHAVIOR, plan.getDataLimitBehavior()); |
| 2460 | writeLongAttribute(out, ATTR_USAGE_BYTES, plan.getDataUsageBytes()); |
| 2461 | writeLongAttribute(out, ATTR_USAGE_TIME, plan.getDataUsageTime()); |
| 2462 | out.endTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2463 | } |
| 2464 | } |
| 2465 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2466 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2467 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2468 | final int uid = mUidPolicy.keyAt(i); |
| 2469 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2470 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2471 | // skip writing empty policies |
| 2472 | if (policy == POLICY_NONE) continue; |
| 2473 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2474 | out.startTag(null, TAG_UID_POLICY); |
| 2475 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2476 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2477 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | out.endTag(null, TAG_POLICY_LIST); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2481 | |
| 2482 | // write all whitelists |
| 2483 | out.startTag(null, TAG_WHITELIST); |
| 2484 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2485 | // revoked restrict background whitelist |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2486 | int size = mRestrictBackgroundWhitelistRevokedUids.size(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2487 | for (int i = 0; i < size; i++) { |
| 2488 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2489 | out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2490 | writeIntAttribute(out, ATTR_UID, uid); |
| 2491 | out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2492 | } |
| 2493 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2494 | out.endTag(null, TAG_WHITELIST); |
| 2495 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2496 | out.endDocument(); |
| 2497 | |
| 2498 | mPolicyFile.finishWrite(fos); |
| 2499 | } catch (IOException e) { |
| 2500 | if (fos != null) { |
| 2501 | mPolicyFile.failWrite(fos); |
| 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2506 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2507 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2508 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2509 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2510 | if (!UserHandle.isApp(uid)) { |
| 2511 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2512 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2513 | synchronized (mUidRulesFirstLock) { |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2514 | final long token = Binder.clearCallingIdentity(); |
| 2515 | try { |
| 2516 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2517 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2518 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2519 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2520 | } |
| 2521 | } finally { |
| 2522 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2523 | } |
| 2524 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2527 | @Override |
| 2528 | public void addUidPolicy(int uid, int policy) { |
| 2529 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2530 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2531 | if (!UserHandle.isApp(uid)) { |
| 2532 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2533 | } |
| 2534 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2535 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2536 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2537 | policy |= oldPolicy; |
| 2538 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2539 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2540 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2541 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2542 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2546 | public void removeUidPolicy(int uid, int policy) { |
| 2547 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2548 | |
| 2549 | if (!UserHandle.isApp(uid)) { |
| 2550 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2551 | } |
| 2552 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2553 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2554 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2555 | policy = oldPolicy & ~policy; |
| 2556 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2557 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2558 | mLogger.uidPolicyChanged(uid, oldPolicy, policy); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2563 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2564 | private void setUidPolicyUncheckedUL(int uid, int oldPolicy, int policy, boolean persist) { |
Sudheer Shanka | 5723ccb | 2018-02-13 11:08:19 -0800 | [diff] [blame] | 2565 | setUidPolicyUncheckedUL(uid, policy, false); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2566 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2567 | final boolean notifyApp; |
| 2568 | if (!isUidValidForWhitelistRules(uid)) { |
| 2569 | notifyApp = false; |
| 2570 | } else { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2571 | final boolean wasBlacklisted = oldPolicy == POLICY_REJECT_METERED_BACKGROUND; |
| 2572 | final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND; |
| 2573 | final boolean wasWhitelisted = oldPolicy == POLICY_ALLOW_METERED_BACKGROUND; |
| 2574 | final boolean isWhitelisted = policy == POLICY_ALLOW_METERED_BACKGROUND; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2575 | final boolean wasBlocked = wasBlacklisted || (mRestrictBackground && !wasWhitelisted); |
| 2576 | final boolean isBlocked = isBlacklisted || (mRestrictBackground && !isWhitelisted); |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2577 | if ((wasWhitelisted && (!isWhitelisted || isBlacklisted)) |
| 2578 | && mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2579 | && !mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2580 | if (LOGD) |
| 2581 | Slog.d(TAG, "Adding uid " + uid + " to revoked restrict background whitelist"); |
| 2582 | mRestrictBackgroundWhitelistRevokedUids.append(uid, true); |
| 2583 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2584 | notifyApp = wasBlocked != isBlocked; |
| 2585 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2586 | mHandler.obtainMessage(MSG_POLICIES_CHANGED, uid, policy, Boolean.valueOf(notifyApp)) |
| 2587 | .sendToTarget(); |
Sudheer Shanka | 5723ccb | 2018-02-13 11:08:19 -0800 | [diff] [blame] | 2588 | if (persist) { |
| 2589 | synchronized (mNetworkPoliciesSecondLock) { |
| 2590 | writePolicyAL(); |
| 2591 | } |
| 2592 | } |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2593 | } |
| 2594 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2595 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2596 | private void setUidPolicyUncheckedUL(int uid, int policy, boolean persist) { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2597 | if (policy == POLICY_NONE) { |
| 2598 | mUidPolicy.delete(uid); |
| 2599 | } else { |
| 2600 | mUidPolicy.put(uid, policy); |
| 2601 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2602 | |
| 2603 | // uid policy changed, recompute rules and persist policy. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2604 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2605 | if (persist) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2606 | synchronized (mNetworkPoliciesSecondLock) { |
| 2607 | writePolicyAL(); |
| 2608 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2613 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2614 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2615 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2616 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2617 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2618 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2621 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2622 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2623 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2624 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2625 | int[] uids = new int[0]; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2626 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2627 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2628 | final int uid = mUidPolicy.keyAt(i); |
| 2629 | final int uidPolicy = mUidPolicy.valueAt(i); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 2630 | if ((policy == POLICY_NONE && uidPolicy == POLICY_NONE) || |
| 2631 | (uidPolicy & policy) != 0) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2632 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2636 | return uids; |
| 2637 | } |
| 2638 | |
| 2639 | /** |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2640 | * Removes any persistable state associated with given {@link UserHandle}, persisting |
| 2641 | * if any changes that are made. |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2642 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2643 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2644 | boolean removeUserStateUL(int userId, boolean writePolicy) { |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2645 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2646 | mLogger.removingUserState(userId); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2647 | boolean changed = false; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2648 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2649 | // Remove entries from revoked default restricted background UID whitelist |
| 2650 | for (int i = mRestrictBackgroundWhitelistRevokedUids.size() - 1; i >= 0; i--) { |
| 2651 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2652 | if (UserHandle.getUserId(uid) == userId) { |
| 2653 | mRestrictBackgroundWhitelistRevokedUids.removeAt(i); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2654 | changed = true; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2658 | // Remove associated UID policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2659 | int[] uids = new int[0]; |
| 2660 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2661 | final int uid = mUidPolicy.keyAt(i); |
| 2662 | if (UserHandle.getUserId(uid) == userId) { |
| 2663 | uids = appendInt(uids, uid); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | if (uids.length > 0) { |
| 2668 | for (int uid : uids) { |
| 2669 | mUidPolicy.delete(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2670 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2671 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2672 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2673 | synchronized (mNetworkPoliciesSecondLock) { |
| 2674 | updateRulesForGlobalChangeAL(true); |
| 2675 | if (writePolicy && changed) { |
| 2676 | writePolicyAL(); |
| 2677 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2678 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2679 | return changed; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2683 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2684 | // TODO: create permission for observing network policy |
| 2685 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2686 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | @Override |
| 2690 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2691 | // TODO: create permission for observing network policy |
| 2692 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2693 | mListeners.unregister(listener); |
| 2694 | } |
| 2695 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2696 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2697 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2698 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2699 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2700 | final long token = Binder.clearCallingIdentity(); |
| 2701 | try { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2702 | synchronized (mUidRulesFirstLock) { |
| 2703 | synchronized (mNetworkPoliciesSecondLock) { |
| 2704 | normalizePoliciesNL(policies); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2705 | handleNetworkPoliciesUpdateAL(false); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2706 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2707 | } |
| 2708 | } finally { |
| 2709 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2713 | void addNetworkPolicyAL(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2714 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2715 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 2716 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2719 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2720 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2721 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2722 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 2723 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 2724 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 2725 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2726 | } catch (SecurityException e) { |
| 2727 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2728 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2729 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 2730 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 2731 | return new NetworkPolicy[0]; |
| 2732 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2735 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2736 | final int size = mNetworkPolicy.size(); |
| 2737 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 2738 | for (int i = 0; i < size; i++) { |
| 2739 | policies[i] = mNetworkPolicy.valueAt(i); |
| 2740 | } |
| 2741 | return policies; |
| 2742 | } |
| 2743 | } |
| 2744 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2745 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2746 | private void normalizePoliciesNL() { |
| 2747 | normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2748 | } |
| 2749 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2750 | @GuardedBy("mNetworkPoliciesSecondLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2751 | private void normalizePoliciesNL(NetworkPolicy[] policies) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2752 | mNetworkPolicy.clear(); |
| 2753 | for (NetworkPolicy policy : policies) { |
Annie Meng | 20b4d84 | 2018-05-18 15:00:49 +0100 | [diff] [blame] | 2754 | if (policy == null) { |
| 2755 | continue; |
| 2756 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2757 | // When two normalized templates conflict, prefer the most |
| 2758 | // restrictive policy |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 2759 | policy.template = NetworkTemplate.normalize(policy.template, mMergedSubscriberIds); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2760 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 2761 | if (existing == null || existing.compareTo(policy) > 0) { |
| 2762 | if (existing != null) { |
| 2763 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 2764 | } |
| 2765 | mNetworkPolicy.put(policy.template, policy); |
| 2766 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2771 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2772 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 2773 | |
| 2774 | final long token = Binder.clearCallingIdentity(); |
| 2775 | try { |
| 2776 | performSnooze(template, TYPE_LIMIT); |
| 2777 | } finally { |
| 2778 | Binder.restoreCallingIdentity(token); |
| 2779 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2780 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2781 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2782 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 2783 | final long currentTime = mClock.millis(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2784 | synchronized (mUidRulesFirstLock) { |
| 2785 | synchronized (mNetworkPoliciesSecondLock) { |
| 2786 | // find and snooze local policy that matches |
| 2787 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 2788 | if (policy == null) { |
| 2789 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 2790 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2791 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2792 | switch (type) { |
| 2793 | case TYPE_WARNING: |
| 2794 | policy.lastWarningSnooze = currentTime; |
| 2795 | break; |
| 2796 | case TYPE_LIMIT: |
| 2797 | policy.lastLimitSnooze = currentTime; |
| 2798 | break; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 2799 | case TYPE_RAPID: |
| 2800 | policy.lastRapidSnooze = currentTime; |
| 2801 | break; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2802 | default: |
| 2803 | throw new IllegalArgumentException("unexpected type"); |
| 2804 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2805 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2806 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2807 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2812 | public void onTetheringChanged(String iface, boolean tethering) { |
| 2813 | // No need to enforce permission because setRestrictBackground() will do it. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2814 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2815 | if (mRestrictBackground && tethering) { |
| 2816 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 2817 | setRestrictBackground(false); |
| 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2823 | public void setRestrictBackground(boolean restrictBackground) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2824 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackground"); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2825 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2826 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2827 | final long token = Binder.clearCallingIdentity(); |
| 2828 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2829 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2830 | setRestrictBackgroundUL(restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2831 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2832 | } finally { |
| 2833 | Binder.restoreCallingIdentity(token); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2834 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2835 | } finally { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2836 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 2840 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2841 | private void setRestrictBackgroundUL(boolean restrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2842 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackgroundUL"); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2843 | try { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2844 | if (restrictBackground == mRestrictBackground) { |
| 2845 | // Ideally, UI should never allow this scenario... |
| 2846 | Slog.w(TAG, "setRestrictBackgroundUL: already " + restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2847 | return; |
| 2848 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2849 | Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground); |
| 2850 | final boolean oldRestrictBackground = mRestrictBackground; |
| 2851 | mRestrictBackground = restrictBackground; |
| 2852 | // Must whitelist foreground apps before turning data saver mode on. |
| 2853 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 2854 | // so it could call AM to get the UIDs of such apps, and iterate through them instead. |
| 2855 | updateRulesForRestrictBackgroundUL(); |
| 2856 | try { |
| 2857 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2858 | Slog.e(TAG, |
| 2859 | "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2860 | mRestrictBackground = oldRestrictBackground; |
| 2861 | // TODO: if it knew the foreground apps (see TODO above), it could call |
| 2862 | // updateRulesForRestrictBackgroundUL() again to restore state. |
| 2863 | return; |
| 2864 | } |
| 2865 | } catch (RemoteException e) { |
| 2866 | // ignored; service lives in system_server |
| 2867 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2868 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2869 | sendRestrictBackgroundChangedMsg(); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2870 | mLogger.restrictBackgroundChanged(oldRestrictBackground, mRestrictBackground); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2871 | |
| 2872 | if (mRestrictBackgroundPowerState.globalBatterySaverEnabled) { |
| 2873 | mRestrictBackgroundChangedInBsm = true; |
| 2874 | } |
| 2875 | synchronized (mNetworkPoliciesSecondLock) { |
| 2876 | updateNotificationsNL(); |
| 2877 | writePolicyAL(); |
| 2878 | } |
| 2879 | } finally { |
| 2880 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2881 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | private void sendRestrictBackgroundChangedMsg() { |
| 2885 | mHandler.removeMessages(MSG_RESTRICT_BACKGROUND_CHANGED); |
| 2886 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0) |
| 2887 | .sendToTarget(); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2890 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2891 | public int getRestrictBackgroundByCaller() { |
| 2892 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2893 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2894 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2895 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2896 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2897 | final long token = Binder.clearCallingIdentity(); |
| 2898 | final int policy; |
| 2899 | try { |
| 2900 | policy = getUidPolicy(uid); |
| 2901 | } finally { |
| 2902 | Binder.restoreCallingIdentity(token); |
| 2903 | } |
| 2904 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2905 | // App is blacklisted. |
| 2906 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2907 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2908 | if (!mRestrictBackground) { |
| 2909 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2910 | } |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2911 | return (mUidPolicy.get(uid) & POLICY_ALLOW_METERED_BACKGROUND) != 0 |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2912 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2913 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2918 | public boolean getRestrictBackground() { |
| 2919 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2920 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2921 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2922 | return mRestrictBackground; |
| 2923 | } |
| 2924 | } |
| 2925 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2926 | @Override |
| 2927 | public void setDeviceIdleMode(boolean enabled) { |
| 2928 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2929 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode"); |
| 2930 | try { |
| 2931 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2932 | if (mDeviceIdleMode == enabled) { |
| 2933 | return; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2934 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2935 | mDeviceIdleMode = enabled; |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 2936 | mLogger.deviceIdleModeEnabled(enabled); |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2937 | if (mSystemReady) { |
| 2938 | // Device idle change means we need to rebuild rules for all |
| 2939 | // known apps, so do a global refresh. |
| 2940 | updateRulesForRestrictPowerUL(); |
| 2941 | } |
| 2942 | } |
| 2943 | if (enabled) { |
| 2944 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 2945 | } else { |
| 2946 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2947 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2948 | } finally { |
| 2949 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2953 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2954 | public void setWifiMeteredOverride(String networkId, int meteredOverride) { |
| 2955 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2956 | final long token = Binder.clearCallingIdentity(); |
| 2957 | try { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2958 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2959 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2960 | for (WifiConfiguration config : configs) { |
| 2961 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2962 | config.meteredOverride = meteredOverride; |
| 2963 | wm.updateNetwork(config); |
| 2964 | } |
| 2965 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2966 | } finally { |
| 2967 | Binder.restoreCallingIdentity(token); |
| 2968 | } |
| 2969 | } |
| 2970 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2971 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2972 | @Deprecated |
| 2973 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 2974 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 2975 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 2976 | return new NetworkQuotaInfo(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2977 | } |
| 2978 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2979 | private void enforceSubscriptionPlanAccess(int subId, int callingUid, String callingPackage) { |
| 2980 | // Verify they're not lying about package name |
| 2981 | mAppOps.checkPackage(callingUid, callingPackage); |
| 2982 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2983 | final SubscriptionInfo si; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2984 | final PersistableBundle config; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2985 | final long token = Binder.clearCallingIdentity(); |
| 2986 | try { |
| 2987 | si = mContext.getSystemService(SubscriptionManager.class) |
| 2988 | .getActiveSubscriptionInfo(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2989 | config = mCarrierConfigManager.getConfigForSubId(subId); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2990 | } finally { |
| 2991 | Binder.restoreCallingIdentity(token); |
| 2992 | } |
| 2993 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2994 | // First check: is caller the CarrierService? |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2995 | if (si != null) { |
| 2996 | if (si.isEmbedded() && si.canManageSubscription(mContext, callingPackage)) { |
| 2997 | return; |
| 2998 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2999 | } |
| 3000 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3001 | // Second check: has the CarrierService delegated access? |
| 3002 | if (config != null) { |
| 3003 | final String overridePackage = config |
| 3004 | .getString(CarrierConfigManager.KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING, null); |
| 3005 | if (!TextUtils.isEmpty(overridePackage) |
| 3006 | && Objects.equals(overridePackage, callingPackage)) { |
| 3007 | return; |
| 3008 | } |
| 3009 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3010 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3011 | // Third check: is caller the fallback/default CarrierService? |
| 3012 | final String defaultPackage = mCarrierConfigManager.getDefaultCarrierServicePackageName(); |
| 3013 | if (!TextUtils.isEmpty(defaultPackage) |
| 3014 | && Objects.equals(defaultPackage, callingPackage)) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3015 | return; |
| 3016 | } |
| 3017 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3018 | // Fourth check: is caller a testing app? |
| 3019 | final String testPackage = SystemProperties.get(PROP_SUB_PLAN_OWNER + "." + subId, null); |
| 3020 | if (!TextUtils.isEmpty(testPackage) |
| 3021 | && Objects.equals(testPackage, callingPackage)) { |
| 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | // Fifth check: is caller a legacy testing app? |
| 3026 | final String legacyTestPackage = SystemProperties.get("fw.sub_plan_owner." + subId, null); |
| 3027 | if (!TextUtils.isEmpty(legacyTestPackage) |
| 3028 | && Objects.equals(legacyTestPackage, callingPackage)) { |
Jeff Sharkey | a7f5046 | 2018-02-14 14:26:10 -0700 | [diff] [blame] | 3029 | return; |
| 3030 | } |
| 3031 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3032 | // Final check: does the caller hold a permission? |
| 3033 | mContext.enforceCallingOrSelfPermission(MANAGE_SUBSCRIPTION_PLANS, TAG); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | @Override |
| 3037 | public SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage) { |
| 3038 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3039 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3040 | final String fake = SystemProperties.get("fw.fake_plan"); |
| 3041 | if (!TextUtils.isEmpty(fake)) { |
| 3042 | final List<SubscriptionPlan> plans = new ArrayList<>(); |
| 3043 | if ("month_hard".equals(fake)) { |
| 3044 | plans.add(SubscriptionPlan.Builder |
| 3045 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3046 | .setTitle("G-Mobile") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3047 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3048 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3049 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3050 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3051 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3052 | plans.add(SubscriptionPlan.Builder |
| 3053 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3054 | .setTitle("G-Mobile Happy") |
| 3055 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3056 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3057 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3058 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3059 | .build()); |
| 3060 | plans.add(SubscriptionPlan.Builder |
| 3061 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3062 | .setTitle("G-Mobile, Charged after limit") |
| 3063 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3064 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 3065 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3066 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 3067 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3068 | } else if ("month_soft".equals(fake)) { |
| 3069 | plans.add(SubscriptionPlan.Builder |
| 3070 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3071 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3072 | .setSummary("Crazy unlimited bandwidth plan with incredibly long title " |
| 3073 | + "that should be cut off to prevent UI from looking terrible") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3074 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3075 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3076 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 3077 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3078 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3079 | plans.add(SubscriptionPlan.Builder |
| 3080 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3081 | .setTitle("G-Mobile, Throttled after limit") |
| 3082 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3083 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3084 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3085 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3086 | .build()); |
| 3087 | plans.add(SubscriptionPlan.Builder |
| 3088 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3089 | .setTitle("G-Mobile, No data connection after limit") |
| 3090 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3091 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3092 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3093 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3094 | .build()); |
| 3095 | |
Sundeep Ghuman | 09e0f57 | 2018-03-14 23:20:23 -0700 | [diff] [blame] | 3096 | } else if ("month_over".equals(fake)) { |
| 3097 | plans.add(SubscriptionPlan.Builder |
| 3098 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3099 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 3100 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3101 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3102 | .setDataUsage(6 * TrafficStats.GB_IN_BYTES, |
| 3103 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3104 | .build()); |
| 3105 | plans.add(SubscriptionPlan.Builder |
| 3106 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3107 | .setTitle("G-Mobile, Throttled after limit") |
| 3108 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3109 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3110 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3111 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3112 | .build()); |
| 3113 | plans.add(SubscriptionPlan.Builder |
| 3114 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 3115 | .setTitle("G-Mobile, No data connection after limit") |
| 3116 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3117 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3118 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 3119 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3120 | .build()); |
| 3121 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3122 | } else if ("month_none".equals(fake)) { |
| 3123 | plans.add(SubscriptionPlan.Builder |
| 3124 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 3125 | .setTitle("G-Mobile") |
| 3126 | .build()); |
| 3127 | } else if ("prepaid".equals(fake)) { |
| 3128 | plans.add(SubscriptionPlan.Builder |
| 3129 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3130 | ZonedDateTime.now().plusDays(10)) |
| 3131 | .setTitle("G-Mobile") |
| 3132 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 3133 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3134 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 3135 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3136 | .build()); |
| 3137 | } else if ("prepaid_crazy".equals(fake)) { |
| 3138 | plans.add(SubscriptionPlan.Builder |
| 3139 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3140 | ZonedDateTime.now().plusDays(10)) |
| 3141 | .setTitle("G-Mobile Anytime") |
| 3142 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 3143 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 3144 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 3145 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3146 | .build()); |
| 3147 | plans.add(SubscriptionPlan.Builder |
| 3148 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3149 | ZonedDateTime.now().plusDays(20)) |
| 3150 | .setTitle("G-Mobile Nickel Nights") |
| 3151 | .setSummary("5¢/GB between 1-5AM") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3152 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 3153 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3154 | .setDataUsage(15 * TrafficStats.MB_IN_BYTES, |
| 3155 | ZonedDateTime.now().minusHours(30).toInstant().toEpochMilli()) |
| 3156 | .build()); |
| 3157 | plans.add(SubscriptionPlan.Builder |
| 3158 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 3159 | ZonedDateTime.now().plusDays(20)) |
| 3160 | .setTitle("G-Mobile Bonus 3G") |
| 3161 | .setSummary("Unlimited 3G data") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3162 | .setDataLimit(1 * TrafficStats.GB_IN_BYTES, |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3163 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3164 | .setDataUsage(300 * TrafficStats.MB_IN_BYTES, |
| 3165 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 3166 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 3167 | } else if ("unlimited".equals(fake)) { |
| 3168 | plans.add(SubscriptionPlan.Builder |
| 3169 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 3170 | ZonedDateTime.now().plusDays(10)) |
| 3171 | .setTitle("G-Mobile Awesome") |
| 3172 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 3173 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 3174 | .setDataUsage(50 * TrafficStats.MB_IN_BYTES, |
| 3175 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 3176 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3177 | } |
| 3178 | return plans.toArray(new SubscriptionPlan[plans.size()]); |
| 3179 | } |
| 3180 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 3181 | synchronized (mNetworkPoliciesSecondLock) { |
| 3182 | // Only give out plan details to the package that defined them, |
| 3183 | // so that we don't risk leaking plans between apps. We always |
| 3184 | // let in core system components (like the Settings app). |
| 3185 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
| 3186 | if (Objects.equals(ownerPackage, callingPackage) |
| 3187 | || (UserHandle.getCallingAppId() == android.os.Process.SYSTEM_UID)) { |
| 3188 | return mSubscriptionPlans.get(subId); |
| 3189 | } else { |
| 3190 | Log.w(TAG, "Not returning plans because caller " + callingPackage |
| 3191 | + " doesn't match owner " + ownerPackage); |
| 3192 | return null; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3193 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | @Override |
| 3198 | public void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) { |
| 3199 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3200 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3201 | for (SubscriptionPlan plan : plans) { |
| 3202 | Preconditions.checkNotNull(plan); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | final long token = Binder.clearCallingIdentity(); |
| 3206 | try { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3207 | synchronized (mUidRulesFirstLock) { |
| 3208 | synchronized (mNetworkPoliciesSecondLock) { |
| 3209 | mSubscriptionPlans.put(subId, plans); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 3210 | mSubscriptionPlansOwner.put(subId, callingPackage); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3211 | |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3212 | final String subscriberId = mSubIdToSubscriberId.get(subId, null); |
| 3213 | if (subscriberId != null) { |
| 3214 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 3215 | maybeUpdateMobilePolicyCycleAL(subId, subscriberId); |
| 3216 | } else { |
| 3217 | Slog.wtf(TAG, "Missing subscriberId for subId " + subId); |
| 3218 | } |
| 3219 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3220 | handleNetworkPoliciesUpdateAL(true); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 3221 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3222 | } |
Jeff Sharkey | e92ed6f | 2018-01-10 20:47:42 -0700 | [diff] [blame] | 3223 | |
| 3224 | final Intent intent = new Intent(SubscriptionManager.ACTION_SUBSCRIPTION_PLANS_CHANGED); |
| 3225 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3226 | intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); |
| 3227 | mContext.sendBroadcast(intent, android.Manifest.permission.MANAGE_SUBSCRIPTION_PLANS); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3228 | } finally { |
| 3229 | Binder.restoreCallingIdentity(token); |
| 3230 | } |
| 3231 | } |
| 3232 | |
Jeff Sharkey | 003d3e6 | 2018-03-30 14:35:04 -0600 | [diff] [blame] | 3233 | /** |
| 3234 | * Only visible for testing purposes. This doesn't give any access to |
| 3235 | * existing plans; it simply lets the debug package define new plans. |
| 3236 | */ |
| 3237 | void setSubscriptionPlansOwner(int subId, String packageName) { |
| 3238 | SystemProperties.set(PROP_SUB_PLAN_OWNER + "." + subId, packageName); |
| 3239 | } |
| 3240 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 3241 | @Override |
Jeff Sharkey | 717f52f | 2018-01-04 16:04:11 -0700 | [diff] [blame] | 3242 | public String getSubscriptionPlansOwner(int subId) { |
| 3243 | if (UserHandle.getCallingAppId() != android.os.Process.SYSTEM_UID) { |
| 3244 | throw new SecurityException(); |
| 3245 | } |
| 3246 | |
| 3247 | synchronized (mNetworkPoliciesSecondLock) { |
| 3248 | return mSubscriptionPlansOwner.get(subId); |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3253 | public void setSubscriptionOverride(int subId, int overrideMask, int overrideValue, |
| 3254 | long timeoutMillis, String callingPackage) { |
| 3255 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 3256 | |
| 3257 | // We can only override when carrier told us about plans |
| 3258 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3259 | final SubscriptionPlan plan = getPrimarySubscriptionPlanLocked(subId); |
| 3260 | if (plan == null |
| 3261 | || plan.getDataLimitBehavior() == SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3262 | throw new IllegalStateException( |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 3263 | "Must provide valid SubscriptionPlan to enable overriding"); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3264 | } |
| 3265 | } |
| 3266 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 3267 | // Only allow overrides when feature is enabled. However, we always |
| 3268 | // allow disabling of overrides for safety reasons. |
| 3269 | final boolean overrideEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 3270 | NETPOLICY_OVERRIDE_ENABLED, 1) != 0; |
| 3271 | if (overrideEnabled || overrideValue == 0) { |
| 3272 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3273 | overrideMask, overrideValue, subId)); |
| 3274 | if (timeoutMillis > 0) { |
| 3275 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SUBSCRIPTION_OVERRIDE, |
| 3276 | overrideMask, 0, subId), timeoutMillis); |
| 3277 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3282 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3283 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3284 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 3285 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 3286 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3287 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3288 | for (String arg : args) { |
| 3289 | argSet.add(arg); |
| 3290 | } |
| 3291 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3292 | synchronized (mUidRulesFirstLock) { |
| 3293 | synchronized (mNetworkPoliciesSecondLock) { |
| 3294 | if (argSet.contains("--unsnooze")) { |
| 3295 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 3296 | mNetworkPolicy.valueAt(i).clearSnooze(); |
| 3297 | } |
| 3298 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 3299 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3300 | |
| 3301 | fout.println("Cleared snooze timestamps"); |
| 3302 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3303 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 3304 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3305 | fout.print("System ready: "); fout.println(mSystemReady); |
| 3306 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
| 3307 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
| 3308 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3309 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 3310 | |
| 3311 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3312 | fout.println("Network policies:"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3313 | fout.increaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3314 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 3315 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
| 3316 | } |
| 3317 | fout.decreaseIndent(); |
| 3318 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3319 | fout.println(); |
| 3320 | fout.println("Subscription plans:"); |
| 3321 | fout.increaseIndent(); |
| 3322 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 3323 | final int subId = mSubscriptionPlans.keyAt(i); |
| 3324 | fout.println("Subscriber ID " + subId + ":"); |
| 3325 | fout.increaseIndent(); |
| 3326 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 3327 | if (!ArrayUtils.isEmpty(plans)) { |
| 3328 | for (SubscriptionPlan plan : plans) { |
| 3329 | fout.println(plan); |
| 3330 | } |
| 3331 | } |
| 3332 | fout.decreaseIndent(); |
| 3333 | } |
| 3334 | fout.decreaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3335 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 3336 | fout.println(); |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 3337 | fout.println("Active subscriptions:"); |
| 3338 | fout.increaseIndent(); |
| 3339 | for (int i = 0; i < mSubIdToSubscriberId.size(); i++) { |
| 3340 | final int subId = mSubIdToSubscriberId.keyAt(i); |
| 3341 | final String subscriberId = mSubIdToSubscriberId.valueAt(i); |
| 3342 | |
| 3343 | fout.println(subId + "=" + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 3344 | } |
| 3345 | fout.decreaseIndent(); |
| 3346 | |
| 3347 | fout.println(); |
| 3348 | fout.println("Merged subscriptions: " |
| 3349 | + Arrays.toString(NetworkIdentity.scrubSubscriberId(mMergedSubscriberIds))); |
| 3350 | |
| 3351 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3352 | fout.println("Policy for UIDs:"); |
| 3353 | fout.increaseIndent(); |
| 3354 | int size = mUidPolicy.size(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3355 | for (int i = 0; i < size; i++) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3356 | final int uid = mUidPolicy.keyAt(i); |
| 3357 | final int policy = mUidPolicy.valueAt(i); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3358 | fout.print("UID="); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3359 | fout.print(uid); |
| 3360 | fout.print(" policy="); |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 3361 | fout.print(uidPoliciesToString(policy)); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3362 | fout.println(); |
| 3363 | } |
| 3364 | fout.decreaseIndent(); |
| 3365 | |
| 3366 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 3367 | if (size > 0) { |
| 3368 | fout.println("Power save whitelist (except idle) app ids:"); |
| 3369 | fout.increaseIndent(); |
| 3370 | for (int i = 0; i < size; i++) { |
| 3371 | fout.print("UID="); |
| 3372 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 3373 | fout.print(": "); |
| 3374 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 3375 | fout.println(); |
| 3376 | } |
| 3377 | fout.decreaseIndent(); |
| 3378 | } |
| 3379 | |
| 3380 | size = mPowerSaveWhitelistAppIds.size(); |
| 3381 | if (size > 0) { |
| 3382 | fout.println("Power save whitelist app ids:"); |
| 3383 | fout.increaseIndent(); |
| 3384 | for (int i = 0; i < size; i++) { |
| 3385 | fout.print("UID="); |
| 3386 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 3387 | fout.print(": "); |
| 3388 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 3389 | fout.println(); |
| 3390 | } |
| 3391 | fout.decreaseIndent(); |
| 3392 | } |
| 3393 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3394 | size = mAppIdleTempWhitelistAppIds.size(); |
| 3395 | if (size > 0) { |
| 3396 | fout.println("App idle whitelist app ids:"); |
| 3397 | fout.increaseIndent(); |
| 3398 | for (int i = 0; i < size; i++) { |
| 3399 | fout.print("UID="); |
| 3400 | fout.print(mAppIdleTempWhitelistAppIds.keyAt(i)); |
| 3401 | fout.print(": "); |
| 3402 | fout.print(mAppIdleTempWhitelistAppIds.valueAt(i)); |
| 3403 | fout.println(); |
| 3404 | } |
| 3405 | fout.decreaseIndent(); |
| 3406 | } |
| 3407 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3408 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 3409 | if (size > 0) { |
| 3410 | fout.println("Default restrict background whitelist uids:"); |
| 3411 | fout.increaseIndent(); |
| 3412 | for (int i = 0; i < size; i++) { |
| 3413 | fout.print("UID="); |
| 3414 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 3415 | fout.println(); |
| 3416 | } |
| 3417 | fout.decreaseIndent(); |
| 3418 | } |
| 3419 | |
| 3420 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 3421 | if (size > 0) { |
| 3422 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 3423 | fout.increaseIndent(); |
| 3424 | for (int i = 0; i < size; i++) { |
| 3425 | fout.print("UID="); |
| 3426 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 3427 | fout.println(); |
| 3428 | } |
| 3429 | fout.decreaseIndent(); |
| 3430 | } |
| 3431 | |
| 3432 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 3433 | collectKeys(mUidState, knownUids); |
| 3434 | collectKeys(mUidRules, knownUids); |
| 3435 | |
| 3436 | fout.println("Status for all known UIDs:"); |
| 3437 | fout.increaseIndent(); |
| 3438 | size = knownUids.size(); |
| 3439 | for (int i = 0; i < size; i++) { |
| 3440 | final int uid = knownUids.keyAt(i); |
| 3441 | fout.print("UID="); |
| 3442 | fout.print(uid); |
| 3443 | |
| 3444 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3445 | fout.print(" state="); |
| 3446 | fout.print(state); |
| 3447 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 3448 | fout.print(" (fg)"); |
| 3449 | } else { |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 3450 | fout.print(state <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3451 | ? " (fg svc)" : " (bg)"); |
| 3452 | } |
| 3453 | |
| 3454 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3455 | fout.print(" rules="); |
| 3456 | fout.print(uidRulesToString(uidRules)); |
| 3457 | fout.println(); |
| 3458 | } |
| 3459 | fout.decreaseIndent(); |
| 3460 | |
| 3461 | fout.println("Status for just UIDs with rules:"); |
| 3462 | fout.increaseIndent(); |
| 3463 | size = mUidRules.size(); |
| 3464 | for (int i = 0; i < size; i++) { |
| 3465 | final int uid = mUidRules.keyAt(i); |
| 3466 | fout.print("UID="); |
| 3467 | fout.print(uid); |
| 3468 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 3469 | fout.print(" rules="); |
| 3470 | fout.print(uidRulesToString(uidRules)); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3471 | fout.println(); |
| 3472 | } |
| 3473 | fout.decreaseIndent(); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 3474 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 3475 | fout.println("Admin restricted uids for metered data:"); |
| 3476 | fout.increaseIndent(); |
| 3477 | size = mMeteredRestrictedUids.size(); |
| 3478 | for (int i = 0; i < size; ++i) { |
| 3479 | fout.print("u" + mMeteredRestrictedUids.keyAt(i) + ": "); |
| 3480 | fout.println(mMeteredRestrictedUids.valueAt(i)); |
| 3481 | } |
| 3482 | fout.decreaseIndent(); |
| 3483 | |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 3484 | fout.println(); |
| 3485 | mStatLogger.dump(fout); |
| 3486 | |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 3487 | mLogger.dumpLogs(fout); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3488 | } |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3489 | } |
| 3490 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3491 | |
| 3492 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3493 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3494 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 3495 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 3496 | this, in, out, err, args, callback, resultReceiver); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3499 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3500 | boolean isUidForeground(int uid) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3501 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3502 | return isUidStateForeground( |
| 3503 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 3504 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3507 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3508 | private boolean isUidForegroundOnRestrictBackgroundUL(int uid) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3509 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3510 | return isProcStateAllowedWhileOnRestrictBackground(procState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3513 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3514 | private boolean isUidForegroundOnRestrictPowerUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3515 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3516 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 3517 | } |
| 3518 | |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3519 | private boolean isUidStateForeground(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3520 | // only really in foreground when screen is also on |
Sudheer Shanka | d993dcf | 2018-02-11 12:22:16 -0800 | [diff] [blame] | 3521 | return state <= NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3524 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3525 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3526 | * {@link #updateRulesForDataUsageRestrictionsUL(int)} and |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3527 | * {@link #updateRulesForPowerRestrictionsUL(int)}. Returns true if the state was updated. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3528 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3529 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3530 | private boolean updateUidStateUL(int uid, int uidState) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3531 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL"); |
| 3532 | try { |
| 3533 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3534 | if (oldUidState != uidState) { |
| 3535 | // state changed, push updated rules |
| 3536 | mUidState.put(uid, uidState); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3537 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState); |
| 3538 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 3539 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
Sudheer Shanka | 9e77d23 | 2017-08-14 14:43:11 -0700 | [diff] [blame] | 3540 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3541 | if (mDeviceIdleMode) { |
| 3542 | updateRuleForDeviceIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3543 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3544 | if (mRestrictPower) { |
| 3545 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3546 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3547 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3548 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3549 | return true; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3550 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3551 | } finally { |
| 3552 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3553 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3554 | return false; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3557 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3558 | private boolean removeUidStateUL(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3559 | final int index = mUidState.indexOfKey(uid); |
| 3560 | if (index >= 0) { |
| 3561 | final int oldUidState = mUidState.valueAt(index); |
| 3562 | mUidState.removeAt(index); |
| 3563 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3564 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3565 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3566 | if (mDeviceIdleMode) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3567 | updateRuleForDeviceIdleUL(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3568 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3569 | if (mRestrictPower) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3570 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3571 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3572 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3573 | return true; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3574 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3575 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 3576 | return false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3579 | // adjust stats accounting based on foreground status |
| 3580 | private void updateNetworkStats(int uid, boolean uidForeground) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3581 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3582 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3583 | "updateNetworkStats: " + uid + "/" + (uidForeground ? "F" : "B")); |
| 3584 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3585 | try { |
| 3586 | mNetworkStats.setUidForeground(uid, uidForeground); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3587 | } finally { |
| 3588 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3592 | private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState, |
| 3593 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3594 | final boolean oldForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3595 | isProcStateAllowedWhileOnRestrictBackground(oldUidState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3596 | final boolean newForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3597 | isProcStateAllowedWhileOnRestrictBackground(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3598 | if (oldForeground != newForeground) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3599 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3600 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3603 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3604 | void updateRulesForPowerSaveUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3605 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL"); |
| 3606 | try { |
| 3607 | updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 3608 | mUidFirewallPowerSaveRules); |
| 3609 | } finally { |
| 3610 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3611 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3612 | } |
| 3613 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3614 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3615 | void updateRuleForRestrictPowerUL(int uid) { |
| 3616 | updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3619 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3620 | void updateRulesForDeviceIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3621 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL"); |
| 3622 | try { |
| 3623 | updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 3624 | mUidFirewallDozableRules); |
| 3625 | } finally { |
| 3626 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3627 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3628 | } |
| 3629 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3630 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3631 | void updateRuleForDeviceIdleUL(int uid) { |
| 3632 | updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3633 | } |
| 3634 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3635 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3636 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3637 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3638 | private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain, |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3639 | SparseIntArray rules) { |
| 3640 | if (enabled) { |
| 3641 | // 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] | 3642 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3643 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3644 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3645 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3646 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3647 | UserInfo user = users.get(ui); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3648 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveTempWhitelistAppIds, user.id); |
| 3649 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveWhitelistAppIds, user.id); |
| 3650 | if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3651 | updateRulesForWhitelistedAppIds(uidRules, |
| 3652 | mPowerSaveWhitelistExceptIdleAppIds, user.id); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3653 | } |
| 3654 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3655 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3656 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3657 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 3658 | } |
| 3659 | } |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3660 | setUidFirewallRulesUL(chain, uidRules, CHAIN_TOGGLE_ENABLE); |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3661 | } else { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3662 | setUidFirewallRulesUL(chain, null, CHAIN_TOGGLE_DISABLE); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3663 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3666 | private void updateRulesForWhitelistedAppIds(final SparseIntArray uidRules, |
| 3667 | final SparseBooleanArray whitelistedAppIds, int userId) { |
| 3668 | for (int i = whitelistedAppIds.size() - 1; i >= 0; --i) { |
| 3669 | if (whitelistedAppIds.valueAt(i)) { |
| 3670 | final int appId = whitelistedAppIds.keyAt(i); |
| 3671 | final int uid = UserHandle.getUid(userId, appId); |
| 3672 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /** |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3678 | * Returns whether a uid is whitelisted from power saving restrictions (eg: Battery Saver, Doze |
| 3679 | * mode, and app idle). |
| 3680 | * |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3681 | * @param deviceIdleMode if true then we don't consider |
| 3682 | * {@link #mPowerSaveWhitelistExceptIdleAppIds} for checking if the {@param uid} is |
| 3683 | * whitelisted. |
| 3684 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3685 | @GuardedBy("mUidRulesFirstLock") |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3686 | private boolean isWhitelistedFromPowerSaveUL(int uid, boolean deviceIdleMode) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3687 | final int appId = UserHandle.getAppId(uid); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3688 | boolean isWhitelisted = mPowerSaveTempWhitelistAppIds.get(appId) |
| 3689 | || mPowerSaveWhitelistAppIds.get(appId); |
| 3690 | if (!deviceIdleMode) { |
| 3691 | isWhitelisted = isWhitelisted || mPowerSaveWhitelistExceptIdleAppIds.get(appId); |
| 3692 | } |
| 3693 | return isWhitelisted; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3696 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3697 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3698 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3699 | private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3700 | if (enabled) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3701 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3702 | chain == FIREWALL_CHAIN_DOZABLE); |
| 3703 | if (isWhitelisted || isUidForegroundOnRestrictPowerUL(uid)) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3704 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3705 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3706 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
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 | void updateRulesForAppIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3713 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL"); |
| 3714 | try { |
| 3715 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 3716 | uidRules.clear(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3717 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3718 | // Fully update the app idle firewall chain. |
| 3719 | final List<UserInfo> users = mUserManager.getUsers(); |
| 3720 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3721 | UserInfo user = users.get(ui); |
| 3722 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 3723 | for (int uid : idleUids) { |
| 3724 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
| 3725 | // quick check: if this uid doesn't have INTERNET permission, it |
| 3726 | // doesn't have network access anyway, so it is a waste to mess |
| 3727 | // with it here. |
| 3728 | if (hasInternetPermissions(uid)) { |
| 3729 | uidRules.put(uid, FIREWALL_RULE_DENY); |
| 3730 | } |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 3731 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3732 | } |
| 3733 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3734 | |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3735 | setUidFirewallRulesUL(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3736 | } finally { |
| 3737 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3738 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3739 | } |
| 3740 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3741 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3742 | void updateRuleForAppIdleUL(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3743 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3744 | |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3745 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3746 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRuleForAppIdleUL: " + uid ); |
| 3747 | } |
| 3748 | try { |
| 3749 | int appId = UserHandle.getAppId(uid); |
| 3750 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 3751 | && !isUidForegroundOnRestrictPowerUL(uid)) { |
| 3752 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3753 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL DENY " + uid); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3754 | } else { |
| 3755 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3756 | if (LOGD) Log.d(TAG, "updateRuleForAppIdleUL " + uid + " to DEFAULT"); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3757 | } |
| 3758 | } finally { |
| 3759 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3760 | } |
| 3761 | } |
| 3762 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3763 | /** |
| 3764 | * Toggle the firewall standby chain and inform listeners if the uid rules have effectively |
| 3765 | * changed. |
| 3766 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3767 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3768 | void updateRulesForAppIdleParoleUL() { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3769 | boolean paroled = mUsageStats.isAppIdleParoleOn(); |
| 3770 | boolean enableChain = !paroled; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3771 | enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, enableChain); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3772 | |
| 3773 | int ruleCount = mUidFirewallStandbyRules.size(); |
| 3774 | for (int i = 0; i < ruleCount; i++) { |
| 3775 | int uid = mUidFirewallStandbyRules.keyAt(i); |
| 3776 | int oldRules = mUidRules.get(uid); |
| 3777 | if (enableChain) { |
| 3778 | // Chain wasn't enabled before and the other power-related |
| 3779 | // chains are whitelists, so we can clear the |
| 3780 | // MASK_ALL_NETWORKS part of the rules and re-inform listeners if |
| 3781 | // the effective rules result in blocking network access. |
| 3782 | oldRules &= MASK_METERED_NETWORKS; |
| 3783 | } else { |
| 3784 | // Skip if it had no restrictions to begin with |
| 3785 | if ((oldRules & MASK_ALL_NETWORKS) == 0) continue; |
| 3786 | } |
Sudheer Shanka | 3af0294 | 2017-04-12 14:29:14 -0700 | [diff] [blame] | 3787 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldRules, paroled); |
| 3788 | if (newUidRules == RULE_NONE) { |
| 3789 | mUidRules.delete(uid); |
| 3790 | } else { |
| 3791 | mUidRules.put(uid, newUidRules); |
| 3792 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3793 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3796 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3797 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 3798 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3799 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3800 | @GuardedBy({"mUidRulesFirstLock", "mNetworkPoliciesSecondLock"}) |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3801 | private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3802 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3803 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3804 | "updateRulesForGlobalChangeAL: " + (restrictedNetworksChanged ? "R" : "-")); |
| 3805 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3806 | try { |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3807 | updateRulesForAppIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3808 | updateRulesForRestrictPowerUL(); |
| 3809 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3810 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3811 | // If the set of restricted networks may have changed, re-evaluate those. |
| 3812 | if (restrictedNetworksChanged) { |
| 3813 | normalizePoliciesNL(); |
| 3814 | updateNetworkRulesNL(); |
| 3815 | } |
| 3816 | } finally { |
| 3817 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3821 | // 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] | 3822 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3823 | private void updateRulesForRestrictPowerUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3824 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL"); |
| 3825 | try { |
| 3826 | updateRulesForDeviceIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3827 | updateRulesForPowerSaveUL(); |
| 3828 | updateRulesForAllAppsUL(TYPE_RESTRICT_POWER); |
| 3829 | } finally { |
| 3830 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3831 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3832 | } |
| 3833 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3834 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3835 | private void updateRulesForRestrictBackgroundUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3836 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL"); |
| 3837 | try { |
| 3838 | updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND); |
| 3839 | } finally { |
| 3840 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3841 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 3845 | private static final int TYPE_RESTRICT_POWER = 2; |
| 3846 | @Retention(RetentionPolicy.SOURCE) |
| 3847 | @IntDef(flag = false, value = { |
| 3848 | TYPE_RESTRICT_BACKGROUND, |
| 3849 | TYPE_RESTRICT_POWER, |
| 3850 | }) |
| 3851 | public @interface RestrictType { |
| 3852 | } |
| 3853 | |
| 3854 | // 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] | 3855 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3856 | private void updateRulesForAllAppsUL(@RestrictType int type) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3857 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3858 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type); |
| 3859 | } |
| 3860 | try { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3861 | // update rules for all installed applications |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3862 | |
| 3863 | final PackageManager pm = mContext.getPackageManager(); |
| 3864 | final List<UserInfo> users; |
| 3865 | final List<ApplicationInfo> apps; |
| 3866 | |
| 3867 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-users"); |
| 3868 | try { |
| 3869 | users = mUserManager.getUsers(); |
| 3870 | } finally { |
| 3871 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3872 | } |
| 3873 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-uids"); |
| 3874 | try { |
| 3875 | apps = pm.getInstalledApplications( |
| 3876 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS |
| 3877 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 3878 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 3879 | } finally { |
| 3880 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3881 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3882 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3883 | final int usersSize = users.size(); |
| 3884 | final int appsSize = apps.size(); |
| 3885 | for (int i = 0; i < usersSize; i++) { |
| 3886 | final UserInfo user = users.get(i); |
| 3887 | for (int j = 0; j < appsSize; j++) { |
| 3888 | final ApplicationInfo app = apps.get(j); |
| 3889 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 3890 | switch (type) { |
| 3891 | case TYPE_RESTRICT_BACKGROUND: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3892 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3893 | break; |
| 3894 | case TYPE_RESTRICT_POWER: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3895 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3896 | break; |
| 3897 | default: |
| 3898 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 3899 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3900 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3901 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3902 | } finally { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3903 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3904 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 3907 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3908 | private void updateRulesForTempWhitelistChangeUL(int appId) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3909 | final List<UserInfo> users = mUserManager.getUsers(); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3910 | final int numUsers = users.size(); |
| 3911 | for (int i = 0; i < numUsers; i++) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3912 | final UserInfo user = users.get(i); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3913 | int uid = UserHandle.getUid(user.id, appId); |
| 3914 | // Update external firewall rules. |
| 3915 | updateRuleForAppIdleUL(uid); |
| 3916 | updateRuleForDeviceIdleUL(uid); |
| 3917 | updateRuleForRestrictPowerUL(uid); |
| 3918 | // Update internal rules. |
| 3919 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3920 | } |
| 3921 | } |
| 3922 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3923 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 3924 | // methods below could be merged into a isUidValidForRules() method. |
| 3925 | private boolean isUidValidForBlacklistRules(int uid) { |
| 3926 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3927 | 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] | 3928 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3929 | return true; |
| 3930 | } |
| 3931 | |
| 3932 | return false; |
| 3933 | } |
| 3934 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3935 | private boolean isUidValidForWhitelistRules(int uid) { |
| 3936 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 3937 | } |
| 3938 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3939 | /** |
| 3940 | * Set whether or not an app should be whitelisted for network access while in app idle. Other |
| 3941 | * power saving restrictions may still apply. |
| 3942 | */ |
| 3943 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3944 | void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
Sudheer Shanka | db02ccd | 2018-11-29 11:27:21 -0800 | [diff] [blame] | 3945 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 3946 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3947 | synchronized (mUidRulesFirstLock) { |
| 3948 | if (mAppIdleTempWhitelistAppIds.get(uid) == shouldWhitelist) { |
| 3949 | // No change. |
| 3950 | return; |
| 3951 | } |
| 3952 | |
| 3953 | final long token = Binder.clearCallingIdentity(); |
| 3954 | try { |
| 3955 | mLogger.appIdleWlChanged(uid, shouldWhitelist); |
| 3956 | if (shouldWhitelist) { |
| 3957 | mAppIdleTempWhitelistAppIds.put(uid, true); |
| 3958 | } else { |
| 3959 | mAppIdleTempWhitelistAppIds.delete(uid); |
| 3960 | } |
| 3961 | updateRuleForAppIdleUL(uid); |
| 3962 | updateRulesForPowerRestrictionsUL(uid); |
| 3963 | } finally { |
| 3964 | Binder.restoreCallingIdentity(token); |
| 3965 | } |
| 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | /** Return the list of UIDs currently in the app idle whitelist. */ |
| 3970 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3971 | int[] getAppIdleWhitelist() { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3972 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 3973 | |
| 3974 | synchronized (mUidRulesFirstLock) { |
| 3975 | final int len = mAppIdleTempWhitelistAppIds.size(); |
| 3976 | int[] uids = new int[len]; |
| 3977 | for (int i = 0; i < len; ++i) { |
| 3978 | uids[i] = mAppIdleTempWhitelistAppIds.keyAt(i); |
| 3979 | } |
| 3980 | return uids; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /** Returns if the UID is currently considered idle. */ |
| 3985 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 3986 | boolean isUidIdle(int uid) { |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 3987 | synchronized (mUidRulesFirstLock) { |
| 3988 | if (mAppIdleTempWhitelistAppIds.get(uid)) { |
| 3989 | // UID is temporarily whitelisted. |
| 3990 | return false; |
| 3991 | } |
| 3992 | } |
| 3993 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3994 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 3995 | final int userId = UserHandle.getUserId(uid); |
| 3996 | |
songjinshi | 0655edd | 2016-05-18 19:55:32 +0800 | [diff] [blame] | 3997 | if (packages != null) { |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 3998 | for (String packageName : packages) { |
| 3999 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 4000 | return false; |
| 4001 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4002 | } |
| 4003 | } |
| 4004 | return true; |
| 4005 | } |
| 4006 | |
| 4007 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4008 | * Checks if an uid has INTERNET permissions. |
| 4009 | * <p> |
| 4010 | * 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] | 4011 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4012 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4013 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 4014 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4015 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4016 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4017 | } |
| 4018 | } catch (RemoteException e) { |
| 4019 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 4020 | return true; |
| 4021 | } |
| 4022 | |
| 4023 | /** |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4024 | * Clears all state - internal and external - associated with an UID. |
| 4025 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4026 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4027 | private void onUidDeletedUL(int uid) { |
| 4028 | // First cleanup in-memory state synchronously... |
| 4029 | mUidRules.delete(uid); |
| 4030 | mUidPolicy.delete(uid); |
| 4031 | mUidFirewallStandbyRules.delete(uid); |
| 4032 | mUidFirewallDozableRules.delete(uid); |
| 4033 | mUidFirewallPowerSaveRules.delete(uid); |
| 4034 | mPowerSaveWhitelistExceptIdleAppIds.delete(uid); |
| 4035 | mPowerSaveWhitelistAppIds.delete(uid); |
| 4036 | mPowerSaveTempWhitelistAppIds.delete(uid); |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4037 | mAppIdleTempWhitelistAppIds.delete(uid); |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4038 | |
| 4039 | // ...then update iptables asynchronously. |
| 4040 | mHandler.obtainMessage(MSG_RESET_FIREWALL_RULES_BY_UID, uid, 0).sendToTarget(); |
| 4041 | } |
| 4042 | |
| 4043 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4044 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4045 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4046 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4047 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4048 | * <li>Doze mode |
| 4049 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4050 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4051 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4052 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4053 | * |
| 4054 | * <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] | 4055 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4056 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4057 | private void updateRestrictionRulesForUidUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4058 | // Methods below only changes the firewall rules for the power-related modes. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4059 | updateRuleForDeviceIdleUL(uid); |
| 4060 | updateRuleForAppIdleUL(uid); |
| 4061 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4062 | |
| 4063 | // Update internal state for power-related modes. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4064 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4065 | |
| 4066 | // Update firewall and internal rules for Data Saver Mode. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4067 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4068 | } |
| 4069 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4070 | /** |
| 4071 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 4072 | * restrictions (blacklist / whitelist). |
| 4073 | * |
| 4074 | * <p> |
| 4075 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 4076 | * networks: |
| 4077 | * <ul> |
| 4078 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 4079 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 4080 | * also blacklisted. |
| 4081 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4082 | * no UIDs other than those whitelisted will have access. |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4083 | * <ul> |
| 4084 | * |
| 4085 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 4086 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 4087 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 4088 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4089 | * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4090 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 4091 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 4092 | * following rules should also be applied: |
| 4093 | * |
| 4094 | * <ul> |
| 4095 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 4096 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 4097 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 4098 | * {@code bw_penalty_box}. |
| 4099 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 4100 | * </ul> |
| 4101 | * |
| 4102 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 4103 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 4104 | * have permission to use the internet. |
| 4105 | * |
| 4106 | * <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] | 4107 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4108 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4109 | private void updateRulesForDataUsageRestrictionsUL(int uid) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4110 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4111 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4112 | "updateRulesForDataUsageRestrictionsUL: " + uid); |
| 4113 | } |
| 4114 | try { |
| 4115 | updateRulesForDataUsageRestrictionsULInner(uid); |
| 4116 | } finally { |
| 4117 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4118 | } |
| 4119 | } |
| 4120 | |
| 4121 | private void updateRulesForDataUsageRestrictionsULInner(int uid) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4122 | if (!isUidValidForWhitelistRules(uid)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4123 | 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] | 4124 | return; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4125 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4126 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 4127 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4128 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4129 | final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid); |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4130 | final boolean isRestrictedByAdmin = isRestrictedByAdminUL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 4131 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4132 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 4133 | final boolean isWhitelisted = (uidPolicy & POLICY_ALLOW_METERED_BACKGROUND) != 0; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4134 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 4135 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 4136 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4137 | // 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] | 4138 | if (isRestrictedByAdmin) { |
| 4139 | newRule = RULE_REJECT_METERED; |
| 4140 | } else if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4141 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 4142 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 4143 | } else if (isWhitelisted) { |
| 4144 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4145 | } |
| 4146 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4147 | if (isBlacklisted) { |
| 4148 | newRule = RULE_REJECT_METERED; |
| 4149 | } else if (mRestrictBackground && isWhitelisted) { |
| 4150 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4151 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4152 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4153 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4154 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4155 | if (LOGV) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4156 | Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4157 | + ": isForeground=" +isForeground |
| 4158 | + ", isBlacklisted=" + isBlacklisted |
| 4159 | + ", isWhitelisted=" + isWhitelisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4160 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4161 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4162 | + ", newRule=" + uidRulesToString(newRule) |
| 4163 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4164 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4165 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4166 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4167 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4168 | mUidRules.delete(uid); |
| 4169 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4170 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 4171 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 4172 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4173 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4174 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4175 | if (hasRule(newRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4176 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 4177 | // (since bw_penalty_box prevails over bw_happy_box). |
| 4178 | |
| 4179 | setMeteredNetworkWhitelist(uid, true); |
| 4180 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 4181 | // but ideally it should be just: |
| 4182 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4183 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4184 | setMeteredNetworkBlacklist(uid, false); |
| 4185 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4186 | } else if (hasRule(oldRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4187 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 4188 | |
| 4189 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 4190 | // but ideally they should be just: |
| 4191 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 4192 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4193 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4194 | setMeteredNetworkWhitelist(uid, false); |
| 4195 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4196 | if (isBlacklisted || isRestrictedByAdmin) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4197 | setMeteredNetworkBlacklist(uid, true); |
| 4198 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4199 | } else if (hasRule(newRule, RULE_REJECT_METERED) |
| 4200 | || hasRule(oldRule, RULE_REJECT_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4201 | // Flip state because app was explicitly added or removed to blacklist. |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4202 | setMeteredNetworkBlacklist(uid, (isBlacklisted || isRestrictedByAdmin)); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4203 | if (hasRule(oldRule, RULE_REJECT_METERED) && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4204 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 4205 | // where app is whitelisted and blacklisted at the same time (although such |
| 4206 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4207 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4208 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4209 | } else if (hasRule(newRule, RULE_ALLOW_METERED) |
| 4210 | || hasRule(oldRule, RULE_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4211 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4212 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4213 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4214 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 4215 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 4216 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4217 | + ", whitelisted=" + isWhitelisted |
| 4218 | + ", blacklisted=" + isBlacklisted |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4219 | + ", isRestrictedByAdmin=" + isRestrictedByAdmin |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4220 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4221 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4222 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4223 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4224 | // Dispatch changed rule to existing listeners. |
| 4225 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | /** |
| 4230 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 4231 | * listeners in case of change. |
| 4232 | * <p> |
| 4233 | * There are 3 power-related rules that affects whether an app has background access on |
| 4234 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 4235 | * restriction, it's added to the equivalent firewall chain: |
| 4236 | * <ul> |
| 4237 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 4238 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 4239 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 4240 | * </ul> |
| 4241 | * <p> |
| 4242 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 4243 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 4244 | * <p> |
| 4245 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 4246 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4247 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4248 | private void updateRulesForPowerRestrictionsUL(int uid) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4249 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 4250 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4251 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules, false); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4252 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4253 | if (newUidRules == RULE_NONE) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4254 | mUidRules.delete(uid); |
| 4255 | } else { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4256 | mUidRules.put(uid, newUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | /** |
| 4261 | * Similar to above but ignores idle state if app standby is currently disabled by parole. |
| 4262 | * |
| 4263 | * @param uid the uid of the app to update rules for |
| 4264 | * @param oldUidRules the current rules for the uid, in order to determine if there's a change |
| 4265 | * @param paroled whether to ignore idle state of apps and only look at other restrictions. |
| 4266 | * |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4267 | * @return the new computed rules for the uid |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4268 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4269 | private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules, boolean paroled) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4270 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4271 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4272 | "updateRulesForPowerRestrictionsUL: " + uid + "/" + oldUidRules + "/" |
| 4273 | + (paroled ? "P" : "-")); |
| 4274 | } |
| 4275 | try { |
| 4276 | return updateRulesForPowerRestrictionsULInner(uid, oldUidRules, paroled); |
| 4277 | } finally { |
| 4278 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | private int updateRulesForPowerRestrictionsULInner(int uid, int oldUidRules, boolean paroled) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4283 | if (!isUidValidForBlacklistRules(uid)) { |
| 4284 | 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] | 4285 | return RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4286 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 4287 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4288 | final boolean isIdle = !paroled && isUidIdle(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4289 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4290 | final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4291 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 4292 | final boolean isWhitelisted = isWhitelistedFromPowerSaveUL(uid, mDeviceIdleMode); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4293 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 4294 | int newRule = RULE_NONE; |
| 4295 | |
| 4296 | // First step: define the new rule based on user restrictions and foreground state. |
| 4297 | |
| 4298 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 4299 | // by considering the foreground and non-foreground states. |
| 4300 | if (isForeground) { |
| 4301 | if (restrictMode) { |
| 4302 | newRule = RULE_ALLOW_ALL; |
| 4303 | } |
| 4304 | } else if (restrictMode) { |
| 4305 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 4306 | } |
| 4307 | |
| 4308 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 4309 | |
| 4310 | if (LOGV) { |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 4311 | Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4312 | + ", isIdle: " + isIdle |
| 4313 | + ", mRestrictPower: " + mRestrictPower |
| 4314 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 4315 | + ", isForeground=" + isForeground |
| 4316 | + ", isWhitelisted=" + isWhitelisted |
| 4317 | + ", oldRule=" + uidRulesToString(oldRule) |
| 4318 | + ", newRule=" + uidRulesToString(newRule) |
| 4319 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 4320 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 4321 | } |
| 4322 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4323 | // Second step: notify listeners if state changed. |
| 4324 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4325 | if (newRule == RULE_NONE || hasRule(newRule, RULE_ALLOW_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4326 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 4327 | } else if (hasRule(newRule, RULE_REJECT_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4328 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 4329 | } else { |
| 4330 | // All scenarios should have been covered above |
| 4331 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 4332 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 4333 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4334 | + ", newRule=" + uidRulesToString(newUidRules) |
| 4335 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 4336 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4337 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4338 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 4339 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4340 | return newUidRules; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4343 | private class AppIdleStateChangeListener |
| 4344 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 4345 | |
| 4346 | @Override |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 4347 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, int bucket, |
| 4348 | int reason) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4349 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 4350 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 4351 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4352 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4353 | mLogger.appIdleStateChanged(uid, idle); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4354 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4355 | updateRulesForPowerRestrictionsUL(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4356 | } |
| 4357 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4358 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | @Override |
| 4362 | public void onParoleStateChanged(boolean isParoleOn) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4363 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4364 | mLogger.paroleStateChanged(isParoleOn); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4365 | updateRulesForAppIdleParoleUL(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4366 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4370 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 4371 | if (listener != null) { |
| 4372 | try { |
| 4373 | listener.onUidRulesChanged(uid, uidRules); |
| 4374 | } catch (RemoteException ignored) { |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 4380 | String[] meteredIfaces) { |
| 4381 | if (listener != null) { |
| 4382 | try { |
| 4383 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 4384 | } catch (RemoteException ignored) { |
| 4385 | } |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 4390 | boolean restrictBackground) { |
| 4391 | if (listener != null) { |
| 4392 | try { |
| 4393 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 4394 | } catch (RemoteException ignored) { |
| 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4399 | private void dispatchUidPoliciesChanged(INetworkPolicyListener listener, int uid, |
| 4400 | int uidPolicies) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4401 | if (listener != null) { |
| 4402 | try { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4403 | listener.onUidPoliciesChanged(uid, uidPolicies); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4404 | } catch (RemoteException ignored) { |
| 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4409 | private void dispatchSubscriptionOverride(INetworkPolicyListener listener, int subId, |
| 4410 | int overrideMask, int overrideValue) { |
| 4411 | if (listener != null) { |
| 4412 | try { |
| 4413 | listener.onSubscriptionOverride(subId, overrideMask, overrideValue); |
| 4414 | } catch (RemoteException ignored) { |
| 4415 | } |
| 4416 | } |
| 4417 | } |
| 4418 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4419 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 4420 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4421 | public boolean handleMessage(Message msg) { |
| 4422 | switch (msg.what) { |
| 4423 | case MSG_RULES_CHANGED: { |
| 4424 | final int uid = msg.arg1; |
| 4425 | final int uidRules = msg.arg2; |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4426 | final int length = mListeners.beginBroadcast(); |
| 4427 | for (int i = 0; i < length; i++) { |
| 4428 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4429 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4430 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 4431 | mListeners.finishBroadcast(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4432 | return true; |
| 4433 | } |
| 4434 | case MSG_METERED_IFACES_CHANGED: { |
| 4435 | final String[] meteredIfaces = (String[]) msg.obj; |
| 4436 | final int length = mListeners.beginBroadcast(); |
| 4437 | for (int i = 0; i < length; i++) { |
| 4438 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4439 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4440 | } |
| 4441 | mListeners.finishBroadcast(); |
| 4442 | return true; |
| 4443 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4444 | case MSG_LIMIT_REACHED: { |
| 4445 | final String iface = (String) msg.obj; |
| 4446 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4447 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4448 | if (mMeteredIfaces.contains(iface)) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4449 | // force stats update to make sure we have |
| 4450 | // numbers that caused alert to trigger. |
| 4451 | mNetworkStats.forceUpdate(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4452 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4453 | updateNetworkEnabledNL(); |
| 4454 | updateNotificationsNL(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 4455 | } |
| 4456 | } |
| 4457 | return true; |
| 4458 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4459 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 4460 | final boolean restrictBackground = msg.arg1 != 0; |
| 4461 | final int length = mListeners.beginBroadcast(); |
| 4462 | for (int i = 0; i < length; i++) { |
| 4463 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4464 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4465 | } |
| 4466 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4467 | final Intent intent = |
| 4468 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4469 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4470 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 4471 | return true; |
| 4472 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4473 | case MSG_POLICIES_CHANGED: { |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4474 | final int uid = msg.arg1; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4475 | final int policy = msg.arg2; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4476 | final Boolean notifyApp = (Boolean) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 4477 | // First notify internal listeners... |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4478 | final int length = mListeners.beginBroadcast(); |
| 4479 | for (int i = 0; i < length; i++) { |
| 4480 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 4481 | dispatchUidPoliciesChanged(listener, uid, policy); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4482 | } |
| 4483 | mListeners.finishBroadcast(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4484 | // ...then apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
| 4485 | if (notifyApp.booleanValue()) { |
| 4486 | broadcastRestrictBackgroundChanged(uid, notifyApp); |
| 4487 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 4488 | return true; |
| 4489 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4490 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 4491 | final long lowestRule = (Long) msg.obj; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4492 | // make sure stats are recorded frequently enough; we aim |
| 4493 | // for 2MB threshold for 2GB/month rules. |
| 4494 | final long persistThreshold = lowestRule / 1000; |
| 4495 | mNetworkStats.advisePersistThreshold(persistThreshold); |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 4496 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 4497 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 4498 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 4499 | removeInterfaceQuota((String) msg.obj); |
| 4500 | // int params need to be stitched back into a long |
| 4501 | setInterfaceQuota((String) msg.obj, |
| 4502 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 4503 | return true; |
| 4504 | } |
| 4505 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 4506 | removeInterfaceQuota((String) msg.obj); |
| 4507 | return true; |
| 4508 | } |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4509 | case MSG_RESET_FIREWALL_RULES_BY_UID: { |
| 4510 | resetUidFirewallRules(msg.arg1); |
| 4511 | return true; |
| 4512 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 4513 | case MSG_SUBSCRIPTION_OVERRIDE: { |
| 4514 | final int overrideMask = msg.arg1; |
| 4515 | final int overrideValue = msg.arg2; |
| 4516 | final int subId = (int) msg.obj; |
| 4517 | final int length = mListeners.beginBroadcast(); |
| 4518 | for (int i = 0; i < length; i++) { |
| 4519 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 4520 | dispatchSubscriptionOverride(listener, subId, overrideMask, overrideValue); |
| 4521 | } |
| 4522 | mListeners.finishBroadcast(); |
| 4523 | return true; |
| 4524 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 4525 | case MSG_METERED_RESTRICTED_PACKAGES_CHANGED: { |
| 4526 | final int userId = msg.arg1; |
| 4527 | final Set<String> packageNames = (Set<String>) msg.obj; |
| 4528 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 4529 | return true; |
| 4530 | } |
Makoto Onuki | 3f1bf5f | 2018-04-04 15:29:00 -0700 | [diff] [blame] | 4531 | case MSG_SET_NETWORK_TEMPLATE_ENABLED: { |
| 4532 | final NetworkTemplate template = (NetworkTemplate) msg.obj; |
| 4533 | final boolean enabled = msg.arg1 != 0; |
| 4534 | setNetworkTemplateEnabledInner(template, enabled); |
| 4535 | return true; |
| 4536 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4537 | default: { |
| 4538 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4542 | }; |
| 4543 | |
| 4544 | private final Handler.Callback mUidEventHandlerCallback = new Handler.Callback() { |
| 4545 | @Override |
| 4546 | public boolean handleMessage(Message msg) { |
| 4547 | switch (msg.what) { |
| 4548 | case UID_MSG_STATE_CHANGED: { |
| 4549 | final int uid = msg.arg1; |
| 4550 | final int procState = msg.arg2; |
| 4551 | final long procStateSeq = (Long) msg.obj; |
| 4552 | |
| 4553 | handleUidChanged(uid, procState, procStateSeq); |
| 4554 | return true; |
| 4555 | } |
| 4556 | case UID_MSG_GONE: { |
| 4557 | final int uid = msg.arg1; |
| 4558 | handleUidGone(uid); |
| 4559 | return true; |
| 4560 | } |
| 4561 | default: { |
| 4562 | return false; |
| 4563 | } |
| 4564 | } |
| 4565 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 4566 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 4567 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4568 | void handleUidChanged(int uid, int procState, long procStateSeq) { |
| 4569 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged"); |
| 4570 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4571 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4572 | synchronized (mUidRulesFirstLock) { |
| 4573 | // We received a uid state change callback, add it to the history so that it |
| 4574 | // will be useful for debugging. |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4575 | mLogger.uidStateChanged(uid, procState, procStateSeq); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4576 | // Now update the network policy rules as per the updated uid state. |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4577 | updated = updateUidStateUL(uid, procState); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4578 | // Updating the network rules is done, so notify AMS about this. |
| 4579 | mActivityManagerInternal.notifyNetworkPolicyRulesUpdated(uid, procStateSeq); |
| 4580 | } |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4581 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4582 | // called from the handler, so there's no multi-threading issue. |
| 4583 | if (updated) { |
| 4584 | updateNetworkStats(uid, isUidStateForeground(procState)); |
| 4585 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4586 | } finally { |
| 4587 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | void handleUidGone(int uid) { |
| 4592 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidGone"); |
| 4593 | try { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4594 | boolean updated; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4595 | synchronized (mUidRulesFirstLock) { |
Amith Yamasani | d78542b | 2019-02-19 09:57:32 -0800 | [diff] [blame] | 4596 | updated = removeUidStateUL(uid); |
| 4597 | } |
| 4598 | // Do this without the lock held. handleUidChanged() and handleUidGone() are |
| 4599 | // called from the handler, so there's no multi-threading issue. |
| 4600 | if (updated) { |
| 4601 | updateNetworkStats(uid, false); |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 4602 | } |
| 4603 | } finally { |
| 4604 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 4605 | } |
| 4606 | } |
| 4607 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 4608 | private void broadcastRestrictBackgroundChanged(int uid, Boolean changed) { |
| 4609 | final PackageManager pm = mContext.getPackageManager(); |
| 4610 | final String[] packages = pm.getPackagesForUid(uid); |
| 4611 | if (packages != null) { |
| 4612 | final int userId = UserHandle.getUserId(uid); |
| 4613 | for (String packageName : packages) { |
| 4614 | final Intent intent = |
| 4615 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 4616 | intent.setPackage(packageName); |
| 4617 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 4618 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 4619 | } |
| 4620 | } |
| 4621 | } |
| 4622 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4623 | private void setInterfaceQuotaAsync(String iface, long quotaBytes) { |
| 4624 | // long quotaBytes split up into two ints to fit in message |
| 4625 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, (int) (quotaBytes >> 32), |
| 4626 | (int) (quotaBytes & 0xFFFFFFFF), iface).sendToTarget(); |
| 4627 | } |
| 4628 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4629 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4630 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4631 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4632 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4633 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4634 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4635 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4636 | } |
| 4637 | } |
| 4638 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4639 | private void removeInterfaceQuotaAsync(String iface) { |
| 4640 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface).sendToTarget(); |
| 4641 | } |
| 4642 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4643 | private void removeInterfaceQuota(String iface) { |
| 4644 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4645 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4646 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4647 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4648 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4649 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4653 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 4654 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4655 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4656 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4657 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4658 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 4659 | } catch (RemoteException e) { |
| 4660 | // ignored; service lives in system_server |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 4665 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 4666 | try { |
| 4667 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 4668 | } catch (IllegalStateException e) { |
| 4669 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4670 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4671 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4672 | } |
| 4673 | } |
| 4674 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4675 | private static final int CHAIN_TOGGLE_NONE = 0; |
| 4676 | private static final int CHAIN_TOGGLE_ENABLE = 1; |
| 4677 | private static final int CHAIN_TOGGLE_DISABLE = 2; |
| 4678 | @Retention(RetentionPolicy.SOURCE) |
| 4679 | @IntDef(flag = false, value = { |
| 4680 | CHAIN_TOGGLE_NONE, |
| 4681 | CHAIN_TOGGLE_ENABLE, |
| 4682 | CHAIN_TOGGLE_DISABLE |
| 4683 | }) |
| 4684 | public @interface ChainToggleType { |
| 4685 | } |
| 4686 | |
| 4687 | /** |
| 4688 | * Calls {@link #setUidFirewallRules(int, SparseIntArray)} and |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4689 | * {@link #enableFirewallChainUL(int, boolean)} synchronously. |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4690 | * |
| 4691 | * @param chain firewall chain. |
| 4692 | * @param uidRules new UID rules; if {@code null}, only toggles chain state. |
| 4693 | * @param toggle whether the chain should be enabled, disabled, or not changed. |
| 4694 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4695 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4696 | private void setUidFirewallRulesUL(int chain, @Nullable SparseIntArray uidRules, |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4697 | @ChainToggleType int toggle) { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4698 | if (uidRules != null) { |
| 4699 | setUidFirewallRulesUL(chain, uidRules); |
| 4700 | } |
| 4701 | if (toggle != CHAIN_TOGGLE_NONE) { |
| 4702 | enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE); |
| 4703 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4706 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4707 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 4708 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 4709 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4710 | */ |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4711 | private void setUidFirewallRulesUL(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4712 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4713 | int size = uidRules.size(); |
| 4714 | int[] uids = new int[size]; |
| 4715 | int[] rules = new int[size]; |
| 4716 | for(int index = size - 1; index >= 0; --index) { |
| 4717 | uids[index] = uidRules.keyAt(index); |
| 4718 | rules[index] = uidRules.valueAt(index); |
| 4719 | } |
| 4720 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4721 | mLogger.firewallRulesChanged(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4722 | } catch (IllegalStateException e) { |
| 4723 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4724 | } catch (RemoteException e) { |
| 4725 | // ignored; service lives in system_server |
| 4726 | } |
| 4727 | } |
| 4728 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4729 | /** |
| 4730 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4731 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4732 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4733 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4734 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4735 | "setUidFirewallRule: " + chain + "/" + uid + "/" + rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 4736 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4737 | try { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4738 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 4739 | mUidFirewallDozableRules.put(uid, rule); |
| 4740 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 4741 | mUidFirewallStandbyRules.put(uid, rule); |
| 4742 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 4743 | mUidFirewallPowerSaveRules.put(uid, rule); |
| 4744 | } |
| 4745 | |
| 4746 | try { |
| 4747 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4748 | mLogger.uidFirewallRuleChanged(chain, uid, rule); |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4749 | } catch (IllegalStateException e) { |
| 4750 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4751 | } catch (RemoteException e) { |
| 4752 | // ignored; service lives in system_server |
| 4753 | } |
| 4754 | } finally { |
| 4755 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | /** |
| 4760 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4761 | */ |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4762 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4763 | private void enableFirewallChainUL(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4764 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 4765 | mFirewallChainStates.get(chain) == enable) { |
| 4766 | // All is the same, nothing to do. |
| 4767 | return; |
| 4768 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4769 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4770 | try { |
| 4771 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 4772 | mLogger.firewallChainEnabled(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4773 | } catch (IllegalStateException e) { |
| 4774 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 4775 | } catch (RemoteException e) { |
| 4776 | // ignored; service lives in system_server |
| 4777 | } |
| 4778 | } |
| 4779 | |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4780 | /** |
| 4781 | * Resets all firewall rules associated with an UID. |
| 4782 | */ |
| 4783 | private void resetUidFirewallRules(int uid) { |
| 4784 | try { |
| 4785 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_DOZABLE, uid, FIREWALL_RULE_DEFAULT); |
| 4786 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 4787 | mNetworkManager |
| 4788 | .setFirewallUidRule(FIREWALL_CHAIN_POWERSAVE, uid, FIREWALL_RULE_DEFAULT); |
| 4789 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, false); |
| 4790 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, false); |
| 4791 | } catch (IllegalStateException e) { |
| 4792 | Log.wtf(TAG, "problem resetting firewall uid rules for " + uid, e); |
| 4793 | } catch (RemoteException e) { |
| 4794 | // ignored; service lives in system_server |
| 4795 | } |
| 4796 | } |
| 4797 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4798 | @Deprecated |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4799 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4800 | return getNetworkTotalBytes(template, start, end); |
| 4801 | } |
| 4802 | |
| 4803 | private long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4804 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 4805 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4806 | } catch (RuntimeException e) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4807 | Slog.w(TAG, "Failed to read network stats: " + e); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4808 | return 0; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | private NetworkStats getNetworkUidBytes(NetworkTemplate template, long start, long end) { |
| 4813 | try { |
| 4814 | return mNetworkStats.getNetworkUidBytes(template, start, end); |
| 4815 | } catch (RuntimeException e) { |
| 4816 | Slog.w(TAG, "Failed to read network stats: " + e); |
| 4817 | return new NetworkStats(SystemClock.elapsedRealtime(), 0); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 4821 | private boolean isBandwidthControlEnabled() { |
| 4822 | final long token = Binder.clearCallingIdentity(); |
| 4823 | try { |
| 4824 | return mNetworkManager.isBandwidthControlEnabled(); |
| 4825 | } catch (RemoteException e) { |
| 4826 | // ignored; service lives in system_server |
| 4827 | return false; |
| 4828 | } finally { |
| 4829 | Binder.restoreCallingIdentity(token); |
| 4830 | } |
| 4831 | } |
| 4832 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 4833 | private static Intent buildAllowBackgroundDataIntent() { |
| 4834 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 4835 | } |
| 4836 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4837 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 4838 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 4839 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 4840 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4841 | return intent; |
| 4842 | } |
| 4843 | |
| 4844 | private static Intent buildSnoozeRapidIntent(NetworkTemplate template) { |
| 4845 | final Intent intent = new Intent(ACTION_SNOOZE_RAPID); |
| 4846 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4847 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4848 | return intent; |
| 4849 | } |
| 4850 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4851 | private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4852 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4853 | intent.setComponent(ComponentName.unflattenFromString( |
| 4854 | res.getString(R.string.config_networkOverLimitComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4855 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4856 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4857 | return intent; |
| 4858 | } |
| 4859 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4860 | private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4861 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4862 | intent.setComponent(ComponentName.unflattenFromString( |
| 4863 | res.getString(R.string.config_dataUsageSummaryComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4864 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4865 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4866 | return intent; |
| 4867 | } |
| 4868 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 4869 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4870 | void addIdleHandler(IdleHandler handler) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 4871 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 4872 | } |
| 4873 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 4874 | @GuardedBy("mUidRulesFirstLock") |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4875 | @VisibleForTesting |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 4876 | void updateRestrictBackgroundByLowPowerModeUL(final PowerSaveState result) { |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4877 | mRestrictBackgroundPowerState = result; |
| 4878 | |
| 4879 | boolean restrictBackground = result.batterySaverEnabled; |
| 4880 | boolean shouldInvokeRestrictBackground; |
| 4881 | // store the temporary mRestrictBackgroundChangedInBsm and update it at last |
| 4882 | boolean localRestrictBgChangedInBsm = mRestrictBackgroundChangedInBsm; |
| 4883 | |
| 4884 | if (result.globalBatterySaverEnabled) { |
| 4885 | // Try to turn on restrictBackground if (1) it is off and (2) batter saver need to |
| 4886 | // turn it on. |
| 4887 | shouldInvokeRestrictBackground = !mRestrictBackground && result.batterySaverEnabled; |
| 4888 | mRestrictBackgroundBeforeBsm = mRestrictBackground; |
| 4889 | localRestrictBgChangedInBsm = false; |
| 4890 | } else { |
| 4891 | // Try to restore the restrictBackground if it doesn't change in bsm |
| 4892 | shouldInvokeRestrictBackground = !mRestrictBackgroundChangedInBsm; |
| 4893 | restrictBackground = mRestrictBackgroundBeforeBsm; |
| 4894 | } |
| 4895 | |
| 4896 | if (shouldInvokeRestrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 4897 | setRestrictBackgroundUL(restrictBackground); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | // Change it at last so setRestrictBackground() won't affect this variable |
| 4901 | mRestrictBackgroundChangedInBsm = localRestrictBgChangedInBsm; |
| 4902 | } |
| 4903 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 4904 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 4905 | final int size = source.size(); |
| 4906 | for (int i = 0; i < size; i++) { |
| 4907 | target.put(source.keyAt(i), true); |
| 4908 | } |
| 4909 | } |
| 4910 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4911 | @Override |
| 4912 | public void factoryReset(String subscriber) { |
| 4913 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 4914 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4915 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 4916 | return; |
| 4917 | } |
| 4918 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4919 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 4920 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4921 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 4922 | for (NetworkPolicy policy : policies) { |
| 4923 | if (policy.template.equals(template)) { |
| 4924 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 4925 | policy.inferred = false; |
| 4926 | policy.clearSnooze(); |
| 4927 | } |
| 4928 | } |
| 4929 | setNetworkPolicies(policies); |
| 4930 | |
| 4931 | // Turn restrict background data off |
| 4932 | setRestrictBackground(false); |
| 4933 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4934 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 4935 | // Remove app's "restrict background data" flag |
| 4936 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 4937 | setUidPolicy(uid, POLICY_NONE); |
| 4938 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4939 | } |
| 4940 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 4941 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4942 | @Override |
| 4943 | public boolean isUidNetworkingBlocked(int uid, boolean isNetworkMetered) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 4944 | final long startTime = mStatLogger.getTime(); |
| 4945 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4946 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4947 | final int uidRules; |
| 4948 | final boolean isBackgroundRestricted; |
| 4949 | synchronized (mUidRulesFirstLock) { |
| 4950 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 4951 | isBackgroundRestricted = mRestrictBackground; |
| 4952 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4953 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 4954 | isBackgroundRestricted, mLogger); |
| 4955 | |
| 4956 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 4957 | |
| 4958 | return ret; |
| 4959 | } |
| 4960 | |
| 4961 | private static boolean isSystem(int uid) { |
| 4962 | return uid < Process.FIRST_APPLICATION_UID; |
| 4963 | } |
| 4964 | |
| 4965 | static boolean isUidNetworkingBlockedInternal(int uid, int uidRules, boolean isNetworkMetered, |
| 4966 | boolean isBackgroundRestricted, @Nullable NetworkPolicyLogger logger) { |
| 4967 | final int reason; |
| 4968 | // Networks are never blocked for system components |
| 4969 | if (isSystem(uid)) { |
| 4970 | reason = NTWK_ALLOWED_SYSTEM; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4971 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4972 | else if (hasRule(uidRules, RULE_REJECT_ALL)) { |
| 4973 | reason = NTWK_BLOCKED_POWER; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4974 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4975 | else if (!isNetworkMetered) { |
| 4976 | reason = NTWK_ALLOWED_NON_METERED; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4977 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4978 | else if (hasRule(uidRules, RULE_REJECT_METERED)) { |
| 4979 | reason = NTWK_BLOCKED_BLACKLIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4980 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4981 | else if (hasRule(uidRules, RULE_ALLOW_METERED)) { |
| 4982 | reason = NTWK_ALLOWED_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4983 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4984 | else if (hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED)) { |
| 4985 | reason = NTWK_ALLOWED_TMP_WHITELIST; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4986 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4987 | else if (isBackgroundRestricted) { |
| 4988 | reason = NTWK_BLOCKED_BG_RESTRICT; |
| 4989 | } |
| 4990 | else { |
| 4991 | reason = NTWK_ALLOWED_DEFAULT; |
| 4992 | } |
| 4993 | |
| 4994 | final boolean blocked; |
| 4995 | switch(reason) { |
| 4996 | case NTWK_ALLOWED_DEFAULT: |
| 4997 | case NTWK_ALLOWED_NON_METERED: |
| 4998 | case NTWK_ALLOWED_TMP_WHITELIST: |
| 4999 | case NTWK_ALLOWED_WHITELIST: |
| 5000 | case NTWK_ALLOWED_SYSTEM: |
| 5001 | blocked = false; |
| 5002 | break; |
| 5003 | case NTWK_BLOCKED_POWER: |
| 5004 | case NTWK_BLOCKED_BLACKLIST: |
| 5005 | case NTWK_BLOCKED_BG_RESTRICT: |
| 5006 | blocked = true; |
| 5007 | break; |
| 5008 | default: |
| 5009 | throw new IllegalArgumentException(); |
| 5010 | } |
| 5011 | if (logger != null) { |
| 5012 | logger.networkBlocked(uid, reason); |
| 5013 | } |
| 5014 | |
| 5015 | return blocked; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5018 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 5019 | |
| 5020 | @Override |
| 5021 | public void resetUserState(int userId) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5022 | synchronized (mUidRulesFirstLock) { |
| 5023 | boolean changed = removeUserStateUL(userId, false); |
| 5024 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed; |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5025 | if (changed) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 5026 | synchronized (mNetworkPoliciesSecondLock) { |
| 5027 | writePolicyAL(); |
| 5028 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5032 | |
| 5033 | /** |
| 5034 | * @return true if the given uid is restricted from doing networking on metered networks. |
| 5035 | */ |
| 5036 | @Override |
| 5037 | public boolean isUidRestrictedOnMeteredNetworks(int uid) { |
| 5038 | final int uidRules; |
| 5039 | final boolean isBackgroundRestricted; |
| 5040 | synchronized (mUidRulesFirstLock) { |
| 5041 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 5042 | isBackgroundRestricted = mRestrictBackground; |
| 5043 | } |
| 5044 | return isBackgroundRestricted |
| 5045 | && !hasRule(uidRules, RULE_ALLOW_METERED) |
| 5046 | && !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED); |
| 5047 | } |
| 5048 | |
| 5049 | /** |
| 5050 | * @return true if networking is blocked on the given interface for the given uid according |
| 5051 | * to current networking policies. |
| 5052 | */ |
| 5053 | @Override |
| 5054 | public boolean isUidNetworkingBlocked(int uid, String ifname) { |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5055 | final long startTime = mStatLogger.getTime(); |
| 5056 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5057 | final int uidRules; |
| 5058 | final boolean isBackgroundRestricted; |
| 5059 | synchronized (mUidRulesFirstLock) { |
| 5060 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 5061 | isBackgroundRestricted = mRestrictBackground; |
| 5062 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5063 | final boolean isNetworkMetered; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 5064 | synchronized (mNetworkPoliciesSecondLock) { |
| 5065 | isNetworkMetered = mMeteredIfaces.contains(ifname); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5066 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5067 | final boolean ret = isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered, |
| 5068 | isBackgroundRestricted, mLogger); |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 5069 | |
| 5070 | mStatLogger.logDurationStat(Stats.IS_UID_NETWORKING_BLOCKED, startTime); |
| 5071 | |
| 5072 | return ret; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5073 | } |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5074 | |
| 5075 | @Override |
| 5076 | public void onTempPowerSaveWhitelistChange(int appId, boolean added) { |
| 5077 | synchronized (mUidRulesFirstLock) { |
Sudheer Shanka | 352dc57 | 2017-09-22 17:09:38 -0700 | [diff] [blame] | 5078 | mLogger.tempPowerSaveWlChanged(appId, added); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 5079 | if (added) { |
| 5080 | mPowerSaveTempWhitelistAppIds.put(appId, true); |
| 5081 | } else { |
| 5082 | mPowerSaveTempWhitelistAppIds.delete(appId); |
| 5083 | } |
| 5084 | updateRulesForTempWhitelistChangeUL(appId); |
| 5085 | } |
| 5086 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5087 | |
| 5088 | @Override |
| 5089 | public SubscriptionPlan getSubscriptionPlan(Network network) { |
| 5090 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5091 | final int subId = getSubIdLocked(network); |
| 5092 | return getPrimarySubscriptionPlanLocked(subId); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | @Override |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 5097 | public SubscriptionPlan getSubscriptionPlan(NetworkTemplate template) { |
| 5098 | synchronized (mNetworkPoliciesSecondLock) { |
| 5099 | final int subId = findRelevantSubIdNL(template); |
| 5100 | return getPrimarySubscriptionPlanLocked(subId); |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | @Override |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5105 | public long getSubscriptionOpportunisticQuota(Network network, int quotaType) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5106 | final long quotaBytes; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5107 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5108 | quotaBytes = mSubscriptionOpportunisticQuota.get(getSubIdLocked(network), |
Remi NGUYEN VAN | 5a89f94 | 2018-03-30 21:17:42 +0900 | [diff] [blame] | 5109 | OPPORTUNISTIC_QUOTA_UNKNOWN); |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5110 | } |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 5111 | if (quotaBytes == OPPORTUNISTIC_QUOTA_UNKNOWN) { |
| 5112 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5113 | } |
| 5114 | |
| 5115 | if (quotaType == QUOTA_TYPE_JOBS) { |
| 5116 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5117 | NETPOLICY_QUOTA_FRAC_JOBS, QUOTA_FRAC_JOBS_DEFAULT)); |
| 5118 | } else if (quotaType == QUOTA_TYPE_MULTIPATH) { |
| 5119 | return (long) (quotaBytes * Settings.Global.getFloat(mContext.getContentResolver(), |
| 5120 | NETPOLICY_QUOTA_FRAC_MULTIPATH, QUOTA_FRAC_MULTIPATH_DEFAULT)); |
| 5121 | } else { |
| 5122 | return OPPORTUNISTIC_QUOTA_UNKNOWN; |
| 5123 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5124 | } |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5125 | |
| 5126 | @Override |
| 5127 | public void onAdminDataAvailable() { |
| 5128 | mAdminDataAvailableLatch.countDown(); |
| 5129 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5130 | |
| 5131 | @Override |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 5132 | public void setAppIdleWhitelist(int uid, boolean shouldWhitelist) { |
| 5133 | NetworkPolicyManagerService.this.setAppIdleWhitelist(uid, shouldWhitelist); |
| 5134 | } |
| 5135 | |
| 5136 | @Override |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5137 | public void setMeteredRestrictedPackages(Set<String> packageNames, int userId) { |
| 5138 | setMeteredRestrictedPackagesInternal(packageNames, userId); |
| 5139 | } |
| 5140 | |
| 5141 | @Override |
| 5142 | public void setMeteredRestrictedPackagesAsync(Set<String> packageNames, int userId) { |
| 5143 | mHandler.obtainMessage(MSG_METERED_RESTRICTED_PACKAGES_CHANGED, |
| 5144 | userId, 0, packageNames).sendToTarget(); |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | private void setMeteredRestrictedPackagesInternal(Set<String> packageNames, int userId) { |
| 5149 | synchronized (mUidRulesFirstLock) { |
| 5150 | final Set<Integer> newRestrictedUids = new ArraySet<>(); |
| 5151 | for (String packageName : packageNames) { |
| 5152 | final int uid = getUidForPackage(packageName, userId); |
| 5153 | if (uid >= 0) { |
| 5154 | newRestrictedUids.add(uid); |
| 5155 | } |
| 5156 | } |
| 5157 | final Set<Integer> oldRestrictedUids = mMeteredRestrictedUids.get(userId); |
| 5158 | mMeteredRestrictedUids.put(userId, newRestrictedUids); |
| 5159 | handleRestrictedPackagesChangeUL(oldRestrictedUids, newRestrictedUids); |
| 5160 | mLogger.meteredRestrictedPkgsChanged(newRestrictedUids); |
| 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | private int getUidForPackage(String packageName, int userId) { |
| 5165 | try { |
| 5166 | return mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 5167 | PackageManager.MATCH_KNOWN_PACKAGES, userId); |
| 5168 | } catch (NameNotFoundException e) { |
| 5169 | return -1; |
| 5170 | } |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | private int parseSubId(NetworkState state) { |
| 5174 | // TODO: moved to using a legitimate NetworkSpecifier instead of string parsing |
| 5175 | int subId = INVALID_SUBSCRIPTION_ID; |
| 5176 | if (state != null && state.networkCapabilities != null |
| 5177 | && state.networkCapabilities.hasTransport(TRANSPORT_CELLULAR)) { |
| 5178 | NetworkSpecifier spec = state.networkCapabilities.getNetworkSpecifier(); |
| 5179 | if (spec instanceof StringNetworkSpecifier) { |
| 5180 | try { |
| 5181 | subId = Integer.parseInt(((StringNetworkSpecifier) spec).specifier); |
| 5182 | } catch (NumberFormatException e) { |
| 5183 | } |
| 5184 | } |
| 5185 | } |
| 5186 | return subId; |
| 5187 | } |
| 5188 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5189 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 5190 | private int getSubIdLocked(Network network) { |
| 5191 | return mNetIdToSubId.get(network.netId, INVALID_SUBSCRIPTION_ID); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5192 | } |
| 5193 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5194 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5195 | private SubscriptionPlan getPrimarySubscriptionPlanLocked(int subId) { |
| 5196 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5197 | if (!ArrayUtils.isEmpty(plans)) { |
| 5198 | for (SubscriptionPlan plan : plans) { |
| 5199 | if (plan.getCycleRule().isRecurring()) { |
| 5200 | // Recurring plans will always have an active cycle |
| 5201 | return plan; |
| 5202 | } else { |
| 5203 | // Non-recurring plans need manual test for active cycle |
| 5204 | final Range<ZonedDateTime> cycle = plan.cycleIterator().next(); |
| 5205 | if (cycle.contains(ZonedDateTime.now(mClock))) { |
| 5206 | return plan; |
| 5207 | } |
| 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | return null; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5212 | } |
| 5213 | |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 5214 | /** |
| 5215 | * This will only ever be called once - during device boot. |
| 5216 | */ |
| 5217 | private void waitForAdminData() { |
| 5218 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { |
| 5219 | ConcurrentUtils.waitForCountDownNoInterrupt(mAdminDataAvailableLatch, |
| 5220 | WAIT_FOR_ADMIN_DATA_TIMEOUT_MS, "Wait for admin data"); |
| 5221 | } |
| 5222 | } |
| 5223 | |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5224 | private void handleRestrictedPackagesChangeUL(Set<Integer> oldRestrictedUids, |
| 5225 | Set<Integer> newRestrictedUids) { |
Sudheer Shanka | 1536fb6 | 2018-07-05 11:52:36 -0700 | [diff] [blame] | 5226 | if (!mNetworkManagerReady) { |
| 5227 | return; |
| 5228 | } |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5229 | if (oldRestrictedUids == null) { |
| 5230 | for (int uid : newRestrictedUids) { |
| 5231 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5232 | } |
| 5233 | return; |
| 5234 | } |
| 5235 | for (int uid : oldRestrictedUids) { |
| 5236 | if (!newRestrictedUids.contains(uid)) { |
| 5237 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5238 | } |
| 5239 | } |
| 5240 | for (int uid : newRestrictedUids) { |
| 5241 | if (!oldRestrictedUids.contains(uid)) { |
| 5242 | updateRulesForDataUsageRestrictionsUL(uid); |
| 5243 | } |
| 5244 | } |
| 5245 | } |
| 5246 | |
Andreas Gampe | aae5aa3 | 2018-07-20 12:55:38 -0700 | [diff] [blame] | 5247 | @GuardedBy("mUidRulesFirstLock") |
Sudheer Shanka | 04d61ae | 2018-01-17 12:16:57 -0800 | [diff] [blame] | 5248 | private boolean isRestrictedByAdminUL(int uid) { |
| 5249 | final Set<Integer> restrictedUids = mMeteredRestrictedUids.get( |
| 5250 | UserHandle.getUserId(uid)); |
| 5251 | return restrictedUids != null && restrictedUids.contains(uid); |
| 5252 | } |
| 5253 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5254 | private static boolean hasRule(int uidRules, int rule) { |
| 5255 | return (uidRules & rule) != 0; |
| 5256 | } |
| 5257 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 5258 | private static @NonNull NetworkState[] defeatNullable(@Nullable NetworkState[] val) { |
| 5259 | return (val != null) ? val : new NetworkState[0]; |
| 5260 | } |
| 5261 | |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 5262 | private static boolean getBooleanDefeatingNullable(@Nullable PersistableBundle bundle, |
| 5263 | String key, boolean defaultValue) { |
| 5264 | return (bundle != null) ? bundle.getBoolean(key, defaultValue) : defaultValue; |
| 5265 | } |
| 5266 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 5267 | private class NotificationId { |
| 5268 | private final String mTag; |
| 5269 | private final int mId; |
| 5270 | |
| 5271 | NotificationId(NetworkPolicy policy, int type) { |
| 5272 | mTag = buildNotificationTag(policy, type); |
| 5273 | mId = type; |
| 5274 | } |
| 5275 | |
| 5276 | @Override |
| 5277 | public boolean equals(Object o) { |
| 5278 | if (this == o) return true; |
| 5279 | if (!(o instanceof NotificationId)) return false; |
| 5280 | NotificationId that = (NotificationId) o; |
| 5281 | return Objects.equals(mTag, that.mTag); |
| 5282 | } |
| 5283 | |
| 5284 | @Override |
| 5285 | public int hashCode() { |
| 5286 | return Objects.hash(mTag); |
| 5287 | } |
| 5288 | |
| 5289 | /** |
| 5290 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 5291 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 5292 | */ |
| 5293 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
| 5294 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
| 5295 | } |
| 5296 | |
| 5297 | public String getTag() { |
| 5298 | return mTag; |
| 5299 | } |
| 5300 | |
| 5301 | public int getId() { |
| 5302 | return mId; |
| 5303 | } |
| 5304 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 5305 | } |