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