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