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