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