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