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