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