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