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