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