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