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