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