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