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