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