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