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