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