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