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 | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 22 | import static android.Manifest.permission.MANAGE_SUBSCRIPTION_PLANS; |
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; |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 36 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 37 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 38 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 39 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 40 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_DOZABLE; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 42 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_POWERSAVE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 43 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_STANDBY; |
| 44 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_ALLOW; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 45 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 46 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DENY; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 47 | import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS; |
| 48 | import static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS; |
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.RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 55 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 56 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 57 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Sudheer Shanka | e359c3d | 2017-02-22 18:41:29 -0800 | [diff] [blame] | 58 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileIdleOrPowerSaveMode; |
| 59 | import static android.net.NetworkPolicyManager.isProcStateAllowedWhileOnRestrictBackground; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 60 | import static android.net.NetworkPolicyManager.resolveNetworkId; |
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; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 69 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 70 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_THRESHOLD_DISABLED; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 71 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_USE_PLATFORM_DEFAULT; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 72 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 73 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 74 | import static com.android.internal.util.ArrayUtils.appendInt; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 75 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 76 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 77 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 78 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 79 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 80 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 81 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 82 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 83 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 84 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 85 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 86 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 87 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 88 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 89 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 90 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 91 | import android.Manifest; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 92 | import android.annotation.IntDef; |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 93 | import android.annotation.Nullable; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 94 | import android.app.ActivityManager; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 95 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 96 | import android.app.AppGlobals; |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 97 | import android.app.AppOpsManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 98 | import android.app.IActivityManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 99 | import android.app.INotificationManager; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 100 | import android.app.IUidObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 101 | import android.app.Notification; |
| 102 | import android.app.PendingIntent; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 103 | import android.app.usage.UsageStatsManagerInternal; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 104 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 105 | import android.content.ComponentName; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 106 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 107 | import android.content.Intent; |
| 108 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 109 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 110 | import android.content.pm.IPackageManager; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 111 | import android.content.pm.PackageManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 112 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 113 | import android.content.pm.UserInfo; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 114 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 115 | import android.net.ConnectivityManager; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 116 | import android.net.ConnectivityManager.NetworkCallback; |
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 | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 123 | import android.net.Network; |
| 124 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 125 | import android.net.NetworkIdentity; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 126 | import android.net.NetworkPolicy; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 127 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 128 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 129 | import android.net.NetworkRequest; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 130 | import android.net.NetworkState; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 131 | import android.net.NetworkTemplate; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 132 | import android.net.TrafficStats; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 133 | import android.net.wifi.WifiConfiguration; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 134 | import android.net.wifi.WifiManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 135 | import android.os.Binder; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 136 | import android.os.Environment; |
| 137 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 138 | import android.os.HandlerThread; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 139 | import android.os.IDeviceIdleController; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 140 | import android.os.INetworkManagementService; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 141 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 142 | import android.os.MessageQueue.IdleHandler; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 143 | import android.os.PersistableBundle; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 144 | import android.os.PowerManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 145 | import android.os.PowerManagerInternal; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 146 | import android.os.PowerSaveState; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 147 | import android.os.Process; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 148 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 149 | import android.os.RemoteException; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 150 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 151 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 152 | import android.os.ShellCallback; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 153 | import android.os.SystemProperties; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 154 | import android.os.Trace; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 155 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 156 | import android.os.UserManager; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 157 | import android.provider.Settings; |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 158 | import android.provider.Settings.Global; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 159 | import android.telephony.CarrierConfigManager; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 160 | import android.telephony.SubscriptionInfo; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 161 | import android.telephony.SubscriptionManager; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 162 | import android.telephony.SubscriptionPlan; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 163 | import android.telephony.TelephonyManager; |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 164 | import android.text.TextUtils; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 165 | import android.text.format.Formatter; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 166 | import android.util.ArrayMap; |
| 167 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 168 | import android.util.AtomicFile; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 169 | import android.util.Log; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 170 | import android.util.NtpTrustedTime; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 171 | import android.util.Pair; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 172 | import android.util.RecurrenceRule; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 173 | import android.util.Slog; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 174 | import android.util.SparseArray; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 175 | import android.util.SparseBooleanArray; |
| 176 | import android.util.SparseIntArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 177 | import android.util.TrustedTime; |
| 178 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 179 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 180 | import com.android.internal.R; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 181 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 182 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 183 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 184 | import com.android.internal.notification.SystemNotificationChannels; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 185 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 186 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 187 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 188 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 189 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 190 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 191 | import com.android.server.EventLogTags; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 192 | import com.android.server.LocalServices; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 193 | import com.android.server.ServiceThread; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 194 | import com.android.server.SystemConfig; |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 195 | import com.android.server.power.BatterySaverPolicy.ServiceType; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 196 | |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 197 | import libcore.io.IoUtils; |
| 198 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 199 | import org.xmlpull.v1.XmlPullParser; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 200 | import org.xmlpull.v1.XmlSerializer; |
| 201 | |
| 202 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 203 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 204 | import java.io.FileInputStream; |
| 205 | import java.io.FileNotFoundException; |
| 206 | import java.io.FileOutputStream; |
| 207 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 208 | import java.io.PrintWriter; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 209 | import java.lang.annotation.Retention; |
| 210 | import java.lang.annotation.RetentionPolicy; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 211 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 212 | import java.time.ZoneId; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 213 | import java.time.ZonedDateTime; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 214 | import java.util.ArrayList; |
| 215 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 216 | import java.util.Calendar; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 217 | import java.util.List; |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 218 | import java.util.Objects; |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 219 | import java.util.concurrent.CountDownLatch; |
| 220 | import java.util.concurrent.TimeUnit; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 222 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 223 | * Service that maintains low-level network policy rules, using |
| 224 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 225 | * <p> |
| 226 | * Derives active rules by combining a given policy with other system status, |
| 227 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 228 | * enforcement. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 229 | * |
| 230 | * <p> |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 231 | * This class uses 2-3 locks to synchronize state: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 232 | * <ul> |
| 233 | * <li>{@code mUidRulesFirstLock}: used to guard state related to individual UIDs (such as firewall |
| 234 | * rules). |
| 235 | * <li>{@code mNetworkPoliciesSecondLock}: used to guard state related to network interfaces (such |
| 236 | * as network policies). |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 237 | * <li>{@code allLocks}: not a "real" lock, but an indication (through @GuardedBy) that all locks |
| 238 | * must be held. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 239 | * </ul> |
| 240 | * |
| 241 | * <p> |
| 242 | * As such, methods that require synchronization have the following prefixes: |
| 243 | * <ul> |
| 244 | * <li>{@code UL()}: require the "UID" lock ({@code mUidRulesFirstLock}). |
| 245 | * <li>{@code NL()}: require the "Network" lock ({@code mNetworkPoliciesSecondLock}). |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 246 | * <li>{@code AL()}: require all locks, which must be obtained in order ({@code mUidRulesFirstLock} |
| 247 | * first, then {@code mNetworkPoliciesSecondLock}, then {@code mYetAnotherGuardThirdLock}, etc.. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 248 | * </ul> |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 249 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 250 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 251 | static final String TAG = "NetworkPolicy"; |
Hugo Benichi | 5d2f2b5 | 2017-04-07 08:53:25 +0900 | [diff] [blame] | 252 | private static final boolean LOGD = false; |
| 253 | private static final boolean LOGV = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 254 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 255 | private static final int VERSION_INIT = 1; |
| 256 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 257 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 258 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 259 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 260 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 261 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 262 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 263 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 264 | private static final int VERSION_SWITCH_UID = 10; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 265 | private static final int VERSION_ADDED_CYCLE = 11; |
| 266 | private static final int VERSION_LATEST = VERSION_ADDED_CYCLE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 267 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 268 | /** |
| 269 | * Max items written to {@link #ProcStateSeqHistory}. |
| 270 | */ |
| 271 | @VisibleForTesting |
| 272 | public static final int MAX_PROC_STATE_SEQ_HISTORY = |
| 273 | ActivityManager.isLowRamDeviceStatic() ? 50 : 200; |
| 274 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 275 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 276 | public static final int TYPE_WARNING = SystemMessage.NOTE_NET_WARNING; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 277 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 278 | public static final int TYPE_LIMIT = SystemMessage.NOTE_NET_LIMIT; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 279 | @VisibleForTesting |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 280 | public static final int TYPE_LIMIT_SNOOZED = SystemMessage.NOTE_NET_LIMIT_SNOOZED; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 281 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 282 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 283 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 284 | private static final String TAG_SUBSCRIPTION_PLAN = "subscription-plan"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 285 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 286 | private static final String TAG_APP_POLICY = "app-policy"; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 287 | private static final String TAG_WHITELIST = "whitelist"; |
| 288 | private static final String TAG_RESTRICT_BACKGROUND = "restrict-background"; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 289 | private static final String TAG_REVOKED_RESTRICT_BACKGROUND = "revoked-restrict-background"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 290 | |
| 291 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 292 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 293 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 294 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 295 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 296 | @Deprecated private static final String ATTR_CYCLE_DAY = "cycleDay"; |
| 297 | @Deprecated private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
| 298 | private static final String ATTR_CYCLE_START = "cycleStart"; |
| 299 | private static final String ATTR_CYCLE_END = "cycleEnd"; |
| 300 | private static final String ATTR_CYCLE_PERIOD = "cyclePeriod"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 301 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 302 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 303 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 304 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 305 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 306 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 307 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 308 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 309 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 310 | private static final String ATTR_POLICY = "policy"; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 311 | private static final String ATTR_SUB_ID = "subId"; |
| 312 | private static final String ATTR_TITLE = "title"; |
| 313 | private static final String ATTR_SUMMARY = "summary"; |
| 314 | private static final String ATTR_LIMIT_BEHAVIOR = "limitBehavior"; |
| 315 | private static final String ATTR_USAGE_BYTES = "usageBytes"; |
| 316 | private static final String ATTR_USAGE_TIME = "usageTime"; |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 317 | private static final String ATTR_OWNER_PACKAGE = "ownerPackage"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 318 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 319 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 320 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 321 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 322 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 323 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 324 | private static final long TIME_CACHE_MAX_AGE = DAY_IN_MILLIS; |
| 325 | |
Svetoslav Ganov | 24c2775 | 2016-08-31 18:09:00 -0700 | [diff] [blame] | 326 | private static final int MSG_RULES_CHANGED = 1; |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 327 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 328 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 329 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 330 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 331 | private static final int MSG_UPDATE_INTERFACE_QUOTA = 10; |
| 332 | private static final int MSG_REMOVE_INTERFACE_QUOTA = 11; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 333 | private static final int MSG_POLICIES_CHANGED = 13; |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 334 | private static final int MSG_RESET_FIREWALL_RULES_BY_UID = 15; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 335 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 336 | private static final int UID_MSG_STATE_CHANGED = 100; |
| 337 | private static final int UID_MSG_GONE = 101; |
| 338 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 339 | private final Context mContext; |
| 340 | private final IActivityManager mActivityManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 341 | private final INetworkStatsService mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 342 | private final INetworkManagementService mNetworkManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 343 | private UsageStatsManagerInternal mUsageStats; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 344 | private final TrustedTime mTime; |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 345 | private final UserManager mUserManager; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 346 | private final CarrierConfigManager mCarrierConfigManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 347 | |
| 348 | private IConnectivityManager mConnManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 349 | private INotificationManager mNotifManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 350 | private PowerManagerInternal mPowerManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 351 | private IDeviceIdleController mDeviceIdleController; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 352 | @GuardedBy("mUidRulesFirstLock") |
| 353 | private PowerSaveState mRestrictBackgroundPowerState; |
| 354 | |
| 355 | // Store the status of restrict background before turning on battery saver. |
| 356 | // Used to restore mRestrictBackground when battery saver is turned off. |
| 357 | private boolean mRestrictBackgroundBeforeBsm; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 358 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 359 | // Denotes the status of restrict background read from disk. |
| 360 | private boolean mLoadedRestrictBackground; |
| 361 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 362 | // See main javadoc for instructions on how to use these locks. |
| 363 | final Object mUidRulesFirstLock = new Object(); |
| 364 | final Object mNetworkPoliciesSecondLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 365 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 366 | @GuardedBy("allLocks") volatile boolean mSystemReady; |
| 367 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 368 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackground; |
| 369 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictPower; |
| 370 | @GuardedBy("mUidRulesFirstLock") volatile boolean mDeviceIdleMode; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 371 | // Store whether user flipped restrict background in battery saver mode |
| 372 | @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackgroundChangedInBsm; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 373 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 374 | private final boolean mSuppressDefaultPolicy; |
| 375 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 376 | /** Defined network policies. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 377 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 378 | final ArrayMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = new ArrayMap<>(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 379 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 380 | /** Map from subId to subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 381 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 382 | final SparseArray<SubscriptionPlan[]> mSubscriptionPlans = new SparseArray<>(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 383 | /** Map from subId to package name that owns subscription plans. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 384 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 385 | final SparseArray<String> mSubscriptionPlansOwner = new SparseArray<>(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 386 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 387 | /** Defined UID policies. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 388 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 389 | /** Currently derived rules for each UID. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 390 | @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidRules = new SparseIntArray(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 391 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 392 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 393 | final SparseIntArray mUidFirewallStandbyRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 394 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 395 | final SparseIntArray mUidFirewallDozableRules = new SparseIntArray(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 396 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 397 | final SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 398 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 399 | /** 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] | 400 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 401 | final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray(); |
| 402 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 403 | /** |
| 404 | * 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] | 405 | * in power save mode, except device idle (doze) still applies. |
| 406 | * TODO: An int array might be sufficient |
| 407 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 408 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 409 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 410 | |
| 411 | /** |
| 412 | * 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] | 413 | * in power save mode. |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 414 | * TODO: An int array might be sufficient |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 415 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 416 | @GuardedBy("mUidRulesFirstLock") |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 417 | private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 418 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 419 | @GuardedBy("mUidRulesFirstLock") |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 420 | private final SparseBooleanArray mPowerSaveTempWhitelistAppIds = new SparseBooleanArray(); |
| 421 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 422 | /** |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 423 | * UIDs that have been initially white-listed by system to avoid restricted background. |
| 424 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 425 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 426 | private final SparseBooleanArray mDefaultRestrictBackgroundWhitelistUids = |
| 427 | new SparseBooleanArray(); |
| 428 | |
| 429 | /** |
| 430 | * UIDs that have been initially white-listed by system to avoid restricted background, |
| 431 | * but later revoked by user. |
| 432 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 433 | @GuardedBy("mUidRulesFirstLock") |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 434 | private final SparseBooleanArray mRestrictBackgroundWhitelistRevokedUids = |
| 435 | new SparseBooleanArray(); |
| 436 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 437 | /** Set of ifaces that are metered. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 438 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 439 | private ArraySet<String> mMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 440 | /** Set of over-limit templates that have been notified. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 441 | @GuardedBy("mNetworkPoliciesSecondLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 442 | private final ArraySet<NetworkTemplate> mOverLimitNotified = new ArraySet<>(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 443 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 444 | /** Set of currently active {@link Notification} tags. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 445 | @GuardedBy("mNetworkPoliciesSecondLock") |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 446 | private final ArraySet<NotificationId> mActiveNotifs = new ArraySet<>(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 447 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 448 | /** Foreground at UID granularity. */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 449 | @GuardedBy("mUidRulesFirstLock") |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 450 | final SparseIntArray mUidState = new SparseIntArray(); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 451 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 452 | /** Map from network ID to last observed meteredness state */ |
| 453 | @GuardedBy("mNetworkPoliciesSecondLock") |
| 454 | private final SparseBooleanArray mNetworkMetered = new SparseBooleanArray(); |
| 455 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 456 | private final RemoteCallbackList<INetworkPolicyListener> |
| 457 | mListeners = new RemoteCallbackList<>(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 458 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 459 | final Handler mHandler; |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 460 | @VisibleForTesting |
| 461 | public final Handler mUidEventHandler; |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 462 | |
| 463 | private final ServiceThread mUidEventThread; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 464 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 465 | @GuardedBy("allLocks") |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 466 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 467 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 468 | private final AppOpsManager mAppOps; |
| 469 | |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 470 | private final IPackageManager mIPm; |
| 471 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 472 | private ActivityManagerInternal mActivityManagerInternal; |
| 473 | |
| 474 | /** |
| 475 | * This is used for debugging purposes. Whenever the IUidObserver.onUidStateChanged is called, |
| 476 | * the uid and procStateSeq will be written to this and will be printed as part of dump. |
| 477 | */ |
| 478 | @VisibleForTesting |
| 479 | public ProcStateSeqHistory mObservedHistory |
| 480 | = new ProcStateSeqHistory(MAX_PROC_STATE_SEQ_HISTORY); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 481 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 482 | // TODO: keep whitelist of system-critical services that should never have |
| 483 | // rules enforced, such as system, phone, and radio UIDs. |
| 484 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 485 | // TODO: migrate notifications to SystemUI |
| 486 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 487 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 488 | INetworkStatsService networkStats, INetworkManagementService networkManagement) { |
| 489 | this(context, activityManager, networkStats, networkManagement, |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 490 | AppGlobals.getPackageManager(), NtpTrustedTime.getInstance(context), getSystemDir(), |
| 491 | false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | private static File getSystemDir() { |
| 495 | return new File(Environment.getDataDirectory(), "system"); |
| 496 | } |
| 497 | |
| 498 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 499 | INetworkStatsService networkStats, INetworkManagementService networkManagement, |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 500 | IPackageManager pm, TrustedTime time, File systemDir, boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 501 | mContext = checkNotNull(context, "missing context"); |
| 502 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 503 | mNetworkStats = checkNotNull(networkStats, "missing networkStats"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 504 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 505 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService( |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 506 | Context.DEVICE_IDLE_CONTROLLER)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 507 | mTime = checkNotNull(time, "missing TrustedTime"); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 508 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 509 | mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 510 | mIPm = pm; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 511 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 512 | HandlerThread thread = new HandlerThread(TAG); |
| 513 | thread.start(); |
| 514 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 515 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 516 | // We create another thread for the UID events, which are more time-critical. |
| 517 | mUidEventThread = new ServiceThread(TAG + ".uid", Process.THREAD_PRIORITY_FOREGROUND, |
| 518 | /*allowIo=*/ false); |
| 519 | mUidEventThread.start(); |
| 520 | mUidEventHandler = new Handler(mUidEventThread.getLooper(), mUidEventHandlerCallback); |
| 521 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 522 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 523 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 524 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml")); |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 525 | |
| 526 | mAppOps = context.getSystemService(AppOpsManager.class); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 527 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 528 | // Expose private service for system components to use. |
| 529 | LocalServices.addService(NetworkPolicyManagerInternal.class, |
| 530 | new NetworkPolicyManagerInternalImpl()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 534 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 535 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 536 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 537 | public void bindNotificationManager(INotificationManager notifManager) { |
| 538 | mNotifManager = checkNotNull(notifManager, "missing INotificationManager"); |
| 539 | } |
| 540 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 541 | void updatePowerSaveWhitelistUL() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 542 | try { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 543 | int[] whitelist = mDeviceIdleController.getAppIdWhitelistExceptIdle(); |
| 544 | mPowerSaveWhitelistExceptIdleAppIds.clear(); |
| 545 | if (whitelist != null) { |
| 546 | for (int uid : whitelist) { |
| 547 | mPowerSaveWhitelistExceptIdleAppIds.put(uid, true); |
| 548 | } |
| 549 | } |
| 550 | whitelist = mDeviceIdleController.getAppIdWhitelist(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 551 | mPowerSaveWhitelistAppIds.clear(); |
| 552 | if (whitelist != null) { |
| 553 | for (int uid : whitelist) { |
| 554 | mPowerSaveWhitelistAppIds.put(uid, true); |
| 555 | } |
| 556 | } |
| 557 | } catch (RemoteException e) { |
| 558 | } |
| 559 | } |
| 560 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 561 | /** |
| 562 | * Whitelists pre-defined apps for restrict background, but only if the user didn't already |
| 563 | * revoke the whitelist. |
| 564 | * |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 565 | * @return whether any uid has been whitelisted. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 566 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 567 | boolean addDefaultRestrictBackgroundWhitelistUidsUL() { |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 568 | final List<UserInfo> users = mUserManager.getUsers(); |
| 569 | final int numberUsers = users.size(); |
| 570 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 571 | boolean changed = false; |
| 572 | for (int i = 0; i < numberUsers; i++) { |
| 573 | final UserInfo user = users.get(i); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 574 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(user.id) || changed; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 575 | } |
| 576 | return changed; |
| 577 | } |
| 578 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 579 | private boolean addDefaultRestrictBackgroundWhitelistUidsUL(int userId) { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 580 | final SystemConfig sysConfig = SystemConfig.getInstance(); |
| 581 | final PackageManager pm = mContext.getPackageManager(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 582 | final ArraySet<String> allowDataUsage = sysConfig.getAllowInDataUsageSave(); |
| 583 | boolean changed = false; |
| 584 | for (int i = 0; i < allowDataUsage.size(); i++) { |
| 585 | final String pkg = allowDataUsage.valueAt(i); |
| 586 | if (LOGD) |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 587 | Slog.d(TAG, "checking restricted background whitelisting for package " + pkg |
| 588 | + " and user " + userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 589 | final ApplicationInfo app; |
| 590 | try { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 591 | app = pm.getApplicationInfoAsUser(pkg, PackageManager.MATCH_SYSTEM_ONLY, userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 592 | } catch (PackageManager.NameNotFoundException e) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 593 | if (LOGD) Slog.d(TAG, "No ApplicationInfo for package " + pkg); |
| 594 | // Ignore it - some apps on allow-in-data-usage-save are optional. |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 595 | continue; |
| 596 | } |
| 597 | if (!app.isPrivilegedApp()) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 598 | Slog.e(TAG, "addDefaultRestrictBackgroundWhitelistUidsUL(): " |
| 599 | + "skipping non-privileged app " + pkg); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 600 | continue; |
| 601 | } |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 602 | final int uid = UserHandle.getUid(userId, app.uid); |
| 603 | mDefaultRestrictBackgroundWhitelistUids.append(uid, true); |
| 604 | if (LOGD) |
| 605 | Slog.d(TAG, "Adding uid " + uid + " (user " + userId + ") to default restricted " |
| 606 | + "background whitelist. Revoked status: " |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 607 | + mRestrictBackgroundWhitelistRevokedUids.get(uid)); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 608 | if (!mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
Felipe Leme | a1252b2 | 2016-08-31 08:47:50 -0700 | [diff] [blame] | 609 | if (LOGD) |
| 610 | Slog.d(TAG, "adding default package " + pkg + " (uid " + uid + " for user " |
| 611 | + userId + ") to restrict background whitelist"); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 612 | setUidPolicyUncheckedUL(uid, POLICY_ALLOW_METERED_BACKGROUND, false); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 613 | changed = true; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | return changed; |
| 617 | } |
| 618 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 619 | private void initService(CountDownLatch initCompleteSignal) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 620 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "systemReady"); |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 621 | final int oldPriority = Process.getThreadPriority(Process.myTid()); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 622 | try { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 623 | // Boost thread's priority during system server init |
| 624 | Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 625 | if (!isBandwidthControlEnabled()) { |
| 626 | Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy"); |
| 627 | return; |
| 628 | } |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 629 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 630 | mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 631 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 632 | synchronized (mUidRulesFirstLock) { |
| 633 | synchronized (mNetworkPoliciesSecondLock) { |
| 634 | updatePowerSaveWhitelistUL(); |
| 635 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 636 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 637 | new PowerManagerInternal.LowPowerModeListener() { |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 638 | @Override |
| 639 | public int getServiceType() { |
| 640 | return ServiceType.NETWORK_FIREWALL; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 641 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 642 | |
| 643 | @Override |
| 644 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 645 | final boolean enabled = result.batterySaverEnabled; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 646 | if (LOGD) { |
| 647 | Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")"); |
| 648 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 649 | synchronized (mUidRulesFirstLock) { |
| 650 | if (mRestrictPower != enabled) { |
| 651 | mRestrictPower = enabled; |
| 652 | updateRulesForRestrictPowerUL(); |
| 653 | } |
| 654 | } |
| 655 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 656 | }); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 657 | mRestrictPower = mPowerManagerInternal.getLowPowerState( |
| 658 | ServiceType.NETWORK_FIREWALL).batterySaverEnabled; |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 659 | |
| 660 | mSystemReady = true; |
| 661 | |
| 662 | // read policy from disk |
| 663 | readPolicyAL(); |
| 664 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 665 | // Update the restrictBackground if battery saver is turned on |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 666 | mRestrictBackgroundBeforeBsm = mLoadedRestrictBackground; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 667 | mRestrictBackgroundPowerState = mPowerManagerInternal |
| 668 | .getLowPowerState(ServiceType.DATA_SAVER); |
| 669 | final boolean localRestrictBackground = |
| 670 | mRestrictBackgroundPowerState.batterySaverEnabled; |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 671 | if (localRestrictBackground && !mLoadedRestrictBackground) { |
| 672 | mLoadedRestrictBackground = true; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 673 | } |
| 674 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 675 | new PowerManagerInternal.LowPowerModeListener() { |
| 676 | @Override |
| 677 | public int getServiceType() { |
| 678 | return ServiceType.DATA_SAVER; |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public void onLowPowerModeChanged(PowerSaveState result) { |
| 683 | synchronized (mUidRulesFirstLock) { |
| 684 | updateRestrictBackgroundByLowPowerModeUL(result); |
| 685 | } |
| 686 | } |
| 687 | }); |
| 688 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 689 | if (addDefaultRestrictBackgroundWhitelistUidsUL()) { |
| 690 | writePolicyAL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 691 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 692 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 693 | setRestrictBackgroundUL(mLoadedRestrictBackground); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 694 | updateRulesForGlobalChangeAL(false); |
| 695 | updateNotificationsNL(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 696 | } |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 697 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 698 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 699 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 700 | try { |
| 701 | mActivityManager.registerUidObserver(mUidObserver, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 702 | ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE, |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 703 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 704 | mNetworkManager.registerObserver(mAlertObserver); |
| 705 | } catch (RemoteException e) { |
| 706 | // ignored; both services live in system_server |
| 707 | } |
| 708 | |
| 709 | // listen for changes to power save whitelist |
| 710 | final IntentFilter whitelistFilter = new IntentFilter( |
| 711 | PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 712 | mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler); |
| 713 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 714 | // watch for network interfaces to be claimed |
| 715 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); |
| 716 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 717 | |
| 718 | // listen for package changes to update policy |
| 719 | final IntentFilter packageFilter = new IntentFilter(); |
| 720 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
| 721 | packageFilter.addDataScheme("package"); |
| 722 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
| 723 | |
| 724 | // listen for UID changes to update policy |
| 725 | mContext.registerReceiver( |
| 726 | mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler); |
| 727 | |
| 728 | // listen for user changes to update policy |
| 729 | final IntentFilter userFilter = new IntentFilter(); |
| 730 | userFilter.addAction(ACTION_USER_ADDED); |
| 731 | userFilter.addAction(ACTION_USER_REMOVED); |
| 732 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 733 | |
| 734 | // listen for stats update events |
| 735 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 736 | mContext.registerReceiver( |
| 737 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 738 | |
| 739 | // listen for restrict background changes from notifications |
| 740 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 741 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 742 | |
| 743 | // listen for snooze warning from notifications |
| 744 | final IntentFilter snoozeWarningFilter = new IntentFilter(ACTION_SNOOZE_WARNING); |
| 745 | mContext.registerReceiver(mSnoozeWarningReceiver, snoozeWarningFilter, |
| 746 | MANAGE_NETWORK_POLICY, mHandler); |
| 747 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 748 | // listen for configured wifi networks to be loaded |
| 749 | final IntentFilter wifiFilter = |
| 750 | new IntentFilter(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); |
| 751 | mContext.registerReceiver(mWifiReceiver, wifiFilter, null, mHandler); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 752 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 753 | // listen for carrier config changes to update data cycle information |
| 754 | final IntentFilter carrierConfigFilter = new IntentFilter( |
| 755 | ACTION_CARRIER_CONFIG_CHANGED); |
| 756 | mContext.registerReceiver(mCarrierConfigReceiver, carrierConfigFilter, null, mHandler); |
| 757 | |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 758 | // listen for meteredness changes |
| 759 | mContext.getSystemService(ConnectivityManager.class).registerNetworkCallback( |
| 760 | new NetworkRequest.Builder().build(), mNetworkCallback); |
| 761 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 762 | mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener()); |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 763 | // tell systemReady() that the service has been initialized |
| 764 | initCompleteSignal.countDown(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 765 | } finally { |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 766 | // Restore the default priority after init is done |
| 767 | Process.setThreadPriority(oldPriority); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 768 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 769 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 772 | public CountDownLatch networkScoreAndNetworkManagementServiceReady() { |
| 773 | final CountDownLatch initCompleteSignal = new CountDownLatch(1); |
| 774 | mHandler.post(() -> initService(initCompleteSignal)); |
| 775 | return initCompleteSignal; |
| 776 | } |
| 777 | |
| 778 | public void systemReady(CountDownLatch initCompleteSignal) { |
| 779 | // wait for initService to complete |
| 780 | try { |
| 781 | if (!initCompleteSignal.await(30, TimeUnit.SECONDS)) { |
| 782 | throw new IllegalStateException("Service " + TAG +" init timeout"); |
| 783 | } |
| 784 | } catch (InterruptedException e) { |
| 785 | Thread.currentThread().interrupt(); |
| 786 | throw new IllegalStateException("Service " + TAG + " init interrupted", e); |
| 787 | } |
| 788 | } |
| 789 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 790 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 791 | @Override public void onUidStateChanged(int uid, int procState, long procStateSeq) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 792 | mUidEventHandler.obtainMessage(UID_MSG_STATE_CHANGED, |
| 793 | uid, procState, procStateSeq).sendToTarget(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 796 | @Override public void onUidGone(int uid, boolean disabled) { |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 797 | mUidEventHandler.obtainMessage(UID_MSG_GONE, uid, 0).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 798 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 799 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 800 | @Override public void onUidActive(int uid) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 803 | @Override public void onUidIdle(int uid, boolean disabled) { |
| 804 | } |
| 805 | |
| 806 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 807 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 808 | }; |
| 809 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 810 | final private BroadcastReceiver mPowerSaveWhitelistReceiver = new BroadcastReceiver() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 811 | @Override |
| 812 | public void onReceive(Context context, Intent intent) { |
| 813 | // on background handler thread, and POWER_SAVE_WHITELIST_CHANGED is protected |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 814 | synchronized (mUidRulesFirstLock) { |
| 815 | updatePowerSaveWhitelistUL(); |
| 816 | updateRulesForRestrictPowerUL(); |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 817 | updateRulesForAppIdleUL(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | }; |
| 821 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 822 | final private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 823 | @Override |
| 824 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 825 | // on background handler thread, and PACKAGE_ADDED is protected |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 826 | |
| 827 | final String action = intent.getAction(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 828 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 829 | if (uid == -1) return; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 830 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 831 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 832 | // update rules for UID, since it might be subject to |
| 833 | // global background data policy |
| 834 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 835 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 836 | updateRestrictionRulesForUidUL(uid); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 837 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | }; |
| 841 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 842 | final private BroadcastReceiver mUidRemovedReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 843 | @Override |
| 844 | public void onReceive(Context context, Intent intent) { |
| 845 | // on background handler thread, and UID_REMOVED is protected |
| 846 | |
| 847 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 848 | if (uid == -1) return; |
| 849 | |
| 850 | // remove any policy and update rules to clean up |
| 851 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 852 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 853 | onUidDeletedUL(uid); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 854 | synchronized (mNetworkPoliciesSecondLock) { |
| 855 | writePolicyAL(); |
| 856 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | }; |
| 860 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 861 | final private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 862 | @Override |
| 863 | public void onReceive(Context context, Intent intent) { |
| 864 | // on background handler thread, and USER_ADDED and USER_REMOVED |
| 865 | // broadcasts are protected |
| 866 | |
| 867 | final String action = intent.getAction(); |
| 868 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 869 | if (userId == -1) return; |
| 870 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 871 | switch (action) { |
| 872 | case ACTION_USER_REMOVED: |
| 873 | case ACTION_USER_ADDED: |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 874 | synchronized (mUidRulesFirstLock) { |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 875 | // 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] | 876 | // USER_REMOVED, and one last sanity check during USER_ADDED |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 877 | removeUserStateUL(userId, true); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 878 | if (action == ACTION_USER_ADDED) { |
| 879 | // Add apps that are whitelisted by default. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 880 | addDefaultRestrictBackgroundWhitelistUidsUL(userId); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 881 | } |
| 882 | // Update global restrict for that user |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 883 | synchronized (mNetworkPoliciesSecondLock) { |
| 884 | updateRulesForGlobalChangeAL(true); |
| 885 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 886 | } |
| 887 | break; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | }; |
| 891 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 892 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 893 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 894 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 895 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 896 | final private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 897 | @Override |
| 898 | public void onReceive(Context context, Intent intent) { |
| 899 | // on background handler thread, and verified |
| 900 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 901 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 902 | maybeRefreshTrustedTime(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 903 | synchronized (mNetworkPoliciesSecondLock) { |
| 904 | updateNetworkEnabledNL(); |
| 905 | updateNotificationsNL(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | }; |
| 909 | |
| 910 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 911 | * Receiver that watches for {@link Notification} control of |
| 912 | * {@link #mRestrictBackground}. |
| 913 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 914 | final private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 915 | @Override |
| 916 | public void onReceive(Context context, Intent intent) { |
| 917 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 918 | // permission above. |
| 919 | |
| 920 | setRestrictBackground(false); |
| 921 | } |
| 922 | }; |
| 923 | |
| 924 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 925 | * Receiver that watches for {@link Notification} control of |
| 926 | * {@link NetworkPolicy#lastWarningSnooze}. |
| 927 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 928 | final private BroadcastReceiver mSnoozeWarningReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 929 | @Override |
| 930 | public void onReceive(Context context, Intent intent) { |
| 931 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 932 | // permission above. |
| 933 | |
| 934 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 935 | performSnooze(template, TYPE_WARNING); |
| 936 | } |
| 937 | }; |
| 938 | |
| 939 | /** |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 940 | * Receiver that watches for {@link WifiConfiguration} to be loaded so that |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 941 | * we can perform upgrade logic. After initial upgrade logic, it updates |
| 942 | * {@link #mMeteredIfaces} based on configuration changes. |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 943 | */ |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 944 | final private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 945 | @Override |
| 946 | public void onReceive(Context context, Intent intent) { |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 947 | synchronized (mUidRulesFirstLock) { |
| 948 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 949 | upgradeWifiMeteredOverrideAL(); |
Jeff Sharkey | b43a292 | 2017-09-13 17:30:45 -0600 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | // Only need to perform upgrade logic once |
| 953 | mContext.unregisterReceiver(this); |
| 954 | } |
| 955 | }; |
| 956 | |
| 957 | private final NetworkCallback mNetworkCallback = new NetworkCallback() { |
| 958 | @Override |
| 959 | public void onCapabilitiesChanged(Network network, |
| 960 | NetworkCapabilities networkCapabilities) { |
| 961 | if (network == null || networkCapabilities == null) return; |
| 962 | |
| 963 | synchronized (mNetworkPoliciesSecondLock) { |
| 964 | final boolean oldMetered = mNetworkMetered.get(network.netId, false); |
| 965 | final boolean newMetered = !networkCapabilities |
| 966 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 967 | |
| 968 | if ((oldMetered != newMetered) || mNetworkMetered.indexOfKey(network.netId) < 0) { |
| 969 | mNetworkMetered.put(network.netId, newMetered); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 970 | updateNetworkRulesNL(); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | }; |
| 975 | |
| 976 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 977 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 978 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 979 | final private INetworkManagementEventObserver mAlertObserver |
| 980 | = new BaseNetworkObserver() { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 981 | @Override |
| 982 | public void limitReached(String limitName, String iface) { |
| 983 | // only someone like NMS should be calling us |
| 984 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 985 | |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 986 | if (!LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 987 | mHandler.obtainMessage(MSG_LIMIT_REACHED, iface).sendToTarget(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | }; |
| 991 | |
| 992 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 993 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 994 | * to show visible notifications as needed. |
| 995 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 996 | void updateNotificationsNL() { |
| 997 | if (LOGV) Slog.v(TAG, "updateNotificationsNL()"); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 998 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 999 | // keep track of previously active notifications |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1000 | final ArraySet<NotificationId> beforeNotifs = new ArraySet<NotificationId>(mActiveNotifs); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1001 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1002 | |
| 1003 | // TODO: when switching to kernel notifications, compute next future |
| 1004 | // cycle boundary to recompute notifications. |
| 1005 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1006 | // examine stats for each active policy |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1007 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1008 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1009 | // ignore policies that aren't relevant to user |
| 1010 | if (!isTemplateRelevant(policy.template)) continue; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1011 | if (!policy.hasCycle()) continue; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1012 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1013 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1014 | .cycleIterator(policy).next(); |
| 1015 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1016 | final long end = cycle.second.toInstant().toEpochMilli(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1017 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1018 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1019 | if (policy.isOverLimit(totalBytes)) { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1020 | if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1021 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes); |
| 1022 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1023 | enqueueNotification(policy, TYPE_LIMIT, totalBytes); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1024 | notifyOverLimitNL(policy.template); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1027 | } else { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1028 | notifyUnderLimitNL(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1029 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1030 | if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1031 | enqueueNotification(policy, TYPE_WARNING, totalBytes); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1036 | // cancel stale notifications that we didn't renew above |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1037 | for (int i = beforeNotifs.size()-1; i >= 0; i--) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1038 | final NotificationId notificationId = beforeNotifs.valueAt(i); |
| 1039 | if (!mActiveNotifs.contains(notificationId)) { |
| 1040 | cancelNotification(notificationId); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Test if given {@link NetworkTemplate} is relevant to user based on |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1047 | * current device state, such as when |
| 1048 | * {@link TelephonyManager#getSubscriberId()} matches. This is regardless of |
| 1049 | * data connection status. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1050 | */ |
| 1051 | private boolean isTemplateRelevant(NetworkTemplate template) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1052 | if (template.isMatchRuleMobile()) { |
| 1053 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 1054 | final SubscriptionManager sub = SubscriptionManager.from(mContext); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1055 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1056 | // Mobile template is relevant when any active subscriber matches |
| 1057 | final int[] subIds = sub.getActiveSubscriptionIdList(); |
| 1058 | for (int subId : subIds) { |
| 1059 | final String subscriberId = tele.getSubscriberId(subId); |
| 1060 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 1061 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1062 | if (template.matches(probeIdent)) { |
| 1063 | return true; |
Jeff Sharkey | 3a66cf3 | 2012-03-20 17:00:01 -0700 | [diff] [blame] | 1064 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1065 | } |
| 1066 | return false; |
| 1067 | } else { |
| 1068 | return true; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1069 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1073 | * Notify that given {@link NetworkTemplate} is over |
| 1074 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 1075 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1076 | private void notifyOverLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1077 | if (!mOverLimitNotified.contains(template)) { |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1078 | mContext.startActivity(buildNetworkOverLimitIntent(mContext.getResources(), template)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1079 | mOverLimitNotified.add(template); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1083 | private void notifyUnderLimitNL(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1084 | mOverLimitNotified.remove(template); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1088 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 1089 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 1090 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1091 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes) { |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1092 | final NotificationId notificationId = new NotificationId(policy, type); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1093 | final Notification.Builder builder = |
| 1094 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1095 | builder.setOnlyAlertOnce(true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1096 | builder.setWhen(0L); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 1097 | builder.setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 1098 | com.android.internal.R.color.system_notification_accent_color)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1099 | |
| 1100 | final Resources res = mContext.getResources(); |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 1101 | CharSequence body = null; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1102 | switch (type) { |
| 1103 | case TYPE_WARNING: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1104 | final CharSequence title = res.getText(R.string.data_usage_warning_title); |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 1105 | body = res.getString(R.string.data_usage_warning_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1106 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1107 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1108 | builder.setTicker(title); |
| 1109 | builder.setContentTitle(title); |
| 1110 | builder.setContentText(body); |
Sanket Padawe | c015e1c | 2016-08-11 16:34:10 -0700 | [diff] [blame] | 1111 | builder.setDefaults(Notification.DEFAULT_ALL); |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1112 | builder.setChannelId(SystemNotificationChannels.NETWORK_ALERTS); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1113 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1114 | final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template); |
| 1115 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1116 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1117 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1118 | final Intent viewIntent = buildViewDataUsageIntent(res, policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1119 | builder.setContentIntent(PendingIntent.getActivity( |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1120 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1121 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | case TYPE_LIMIT: { |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 1125 | body = res.getText(R.string.data_usage_limit_body); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1126 | |
| 1127 | final CharSequence title; |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1128 | int icon = R.drawable.stat_notify_disabled_data; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1129 | switch (policy.template.getMatchRule()) { |
| 1130 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1131 | title = res.getText(R.string.data_usage_3g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1132 | break; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1133 | case MATCH_MOBILE_4G: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1134 | title = res.getText(R.string.data_usage_4g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1135 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1136 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1137 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1138 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1139 | case MATCH_WIFI: |
| 1140 | title = res.getText(R.string.data_usage_wifi_limit_title); |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1141 | icon = R.drawable.stat_notify_error; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1142 | break; |
| 1143 | default: |
| 1144 | title = null; |
| 1145 | break; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1148 | builder.setOngoing(true); |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1149 | builder.setSmallIcon(icon); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1150 | builder.setTicker(title); |
| 1151 | builder.setContentTitle(title); |
| 1152 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1153 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1154 | final Intent intent = buildNetworkOverLimitIntent(res, policy.template); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1155 | builder.setContentIntent(PendingIntent.getActivity( |
| 1156 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1157 | break; |
| 1158 | } |
| 1159 | case TYPE_LIMIT_SNOOZED: { |
| 1160 | final long overBytes = totalBytes - policy.limitBytes; |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 1161 | body = res.getString(R.string.data_usage_limit_snoozed_body, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1162 | Formatter.formatFileSize(mContext, overBytes)); |
| 1163 | |
| 1164 | final CharSequence title; |
| 1165 | switch (policy.template.getMatchRule()) { |
| 1166 | case MATCH_MOBILE_3G_LOWER: |
| 1167 | title = res.getText(R.string.data_usage_3g_limit_snoozed_title); |
| 1168 | break; |
| 1169 | case MATCH_MOBILE_4G: |
| 1170 | title = res.getText(R.string.data_usage_4g_limit_snoozed_title); |
| 1171 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1172 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1173 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 1174 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1175 | case MATCH_WIFI: |
| 1176 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 1177 | break; |
| 1178 | default: |
| 1179 | title = null; |
| 1180 | break; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1183 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1184 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1185 | builder.setTicker(title); |
| 1186 | builder.setContentTitle(title); |
| 1187 | builder.setContentText(body); |
| 1188 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 1189 | final Intent intent = buildViewDataUsageIntent(res, policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1190 | builder.setContentIntent(PendingIntent.getActivity( |
| 1191 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | // TODO: move to NotificationManager once we can mock it |
| 1197 | try { |
| 1198 | final String packageName = mContext.getPackageName(); |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1199 | if (!TextUtils.isEmpty(body)) { |
Chris Wren | 8a3d56c | 2016-08-01 15:52:52 -0400 | [diff] [blame] | 1200 | builder.setStyle(new Notification.BigTextStyle() |
| 1201 | .bigText(body)); |
| 1202 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1203 | mNotifManager.enqueueNotificationWithTag( |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1204 | packageName, packageName, notificationId.getTag(), notificationId.getId(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1205 | builder.build(), UserHandle.USER_ALL); |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1206 | mActiveNotifs.add(notificationId); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1207 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1208 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1212 | private void cancelNotification(NotificationId notificationId) { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1213 | // TODO: move to NotificationManager once we can mock it |
| 1214 | try { |
| 1215 | final String packageName = mContext.getPackageName(); |
| 1216 | mNotifManager.cancelNotificationWithTag( |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 1217 | packageName, notificationId.getTag(), notificationId.getId(), |
| 1218 | UserHandle.USER_ALL); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1219 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1220 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1225 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1226 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1227 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1228 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1229 | @Override |
| 1230 | public void onReceive(Context context, Intent intent) { |
| 1231 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 1232 | // permission above. |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1233 | |
| 1234 | maybeRefreshTrustedTime(); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1235 | synchronized (mUidRulesFirstLock) { |
| 1236 | synchronized (mNetworkPoliciesSecondLock) { |
| 1237 | ensureActiveMobilePolicyAL(); |
| 1238 | normalizePoliciesNL(); |
| 1239 | updateNetworkEnabledNL(); |
| 1240 | updateNetworkRulesNL(); |
| 1241 | updateNotificationsNL(); |
| 1242 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | }; |
| 1246 | |
| 1247 | /** |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1248 | * Update mobile policies with data cycle information from {@link CarrierConfigManager} |
| 1249 | * if necessary. |
| 1250 | * |
| 1251 | * @param subId that has its associated NetworkPolicy updated if necessary |
| 1252 | * @return if any policies were updated |
| 1253 | */ |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1254 | private boolean maybeUpdateMobilePolicyCycleAL(int subId) { |
| 1255 | if (LOGV) Slog.v(TAG, "maybeUpdateMobilePolicyCycleAL()"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1256 | |
| 1257 | boolean policyUpdated = false; |
| 1258 | final String subscriberId = TelephonyManager.from(mContext).getSubscriberId(subId); |
| 1259 | |
| 1260 | // find and update the mobile NetworkPolicy for this subscriber id |
| 1261 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1262 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
| 1263 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1264 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1265 | if (template.matches(probeIdent)) { |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1266 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1267 | policyUpdated |= updateDefaultMobilePolicyAL(subId, policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1270 | return policyUpdated; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * Returns the cycle day that should be used for a mobile NetworkPolicy. |
| 1275 | * |
| 1276 | * It attempts to get an appropriate cycle day from the passed in CarrierConfig. If it's unable |
| 1277 | * to do so, it returns the fallback value. |
| 1278 | * |
| 1279 | * @param config The CarrierConfig to read the value from. |
| 1280 | * @param fallbackCycleDay to return if the CarrierConfig can't be read. |
| 1281 | * @return cycleDay to use in the mobile NetworkPolicy. |
| 1282 | */ |
| 1283 | @VisibleForTesting |
| 1284 | public int getCycleDayFromCarrierConfig(@Nullable PersistableBundle config, |
| 1285 | int fallbackCycleDay) { |
| 1286 | if (config == null) { |
| 1287 | return fallbackCycleDay; |
| 1288 | } |
| 1289 | int cycleDay = |
| 1290 | config.getInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT); |
| 1291 | if (cycleDay == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1292 | return fallbackCycleDay; |
| 1293 | } |
| 1294 | // validate cycleDay value |
| 1295 | final Calendar cal = Calendar.getInstance(); |
| 1296 | if (cycleDay < cal.getMinimum(Calendar.DAY_OF_MONTH) || |
| 1297 | cycleDay > cal.getMaximum(Calendar.DAY_OF_MONTH)) { |
| 1298 | Slog.e(TAG, "Invalid date in " |
| 1299 | + "CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT: " + cycleDay); |
| 1300 | return fallbackCycleDay; |
| 1301 | } |
| 1302 | return cycleDay; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Returns the warning bytes that should be used for a mobile NetworkPolicy. |
| 1307 | * |
| 1308 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1309 | * to do so, it returns the fallback value. |
| 1310 | * |
| 1311 | * @param config The CarrierConfig to read the value from. |
| 1312 | * @param fallbackWarningBytes to return if the CarrierConfig can't be read. |
| 1313 | * @return warningBytes to use in the mobile NetworkPolicy. |
| 1314 | */ |
| 1315 | @VisibleForTesting |
| 1316 | public long getWarningBytesFromCarrierConfig(@Nullable PersistableBundle config, |
| 1317 | long fallbackWarningBytes) { |
| 1318 | if (config == null) { |
| 1319 | return fallbackWarningBytes; |
| 1320 | } |
| 1321 | long warningBytes = |
| 1322 | config.getLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG); |
| 1323 | |
| 1324 | if (warningBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1325 | return WARNING_DISABLED; |
| 1326 | } else if (warningBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1327 | return getPlatformDefaultWarningBytes(); |
| 1328 | } else if (warningBytes < 0) { |
| 1329 | Slog.e(TAG, "Invalid value in " |
| 1330 | + "CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; expected a " |
| 1331 | + "non-negative value but got: " + warningBytes); |
| 1332 | return fallbackWarningBytes; |
| 1333 | } |
| 1334 | |
| 1335 | return warningBytes; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Returns the limit bytes that should be used for a mobile NetworkPolicy. |
| 1340 | * |
| 1341 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1342 | * to do so, it returns the fallback value. |
| 1343 | * |
| 1344 | * @param config The CarrierConfig to read the value from. |
| 1345 | * @param fallbackLimitBytes to return if the CarrierConfig can't be read. |
| 1346 | * @return limitBytes to use in the mobile NetworkPolicy. |
| 1347 | */ |
| 1348 | @VisibleForTesting |
| 1349 | public long getLimitBytesFromCarrierConfig(@Nullable PersistableBundle config, |
| 1350 | long fallbackLimitBytes) { |
| 1351 | if (config == null) { |
| 1352 | return fallbackLimitBytes; |
| 1353 | } |
| 1354 | long limitBytes = |
| 1355 | config.getLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG); |
| 1356 | |
| 1357 | if (limitBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1358 | return LIMIT_DISABLED; |
| 1359 | } else if (limitBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1360 | return getPlatformDefaultLimitBytes(); |
| 1361 | } else if (limitBytes < 0) { |
| 1362 | Slog.e(TAG, "Invalid value in " |
| 1363 | + "CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; expected a " |
| 1364 | + "non-negative value but got: " + limitBytes); |
| 1365 | return fallbackLimitBytes; |
| 1366 | } |
| 1367 | return limitBytes; |
| 1368 | } |
| 1369 | |
| 1370 | /** |
| 1371 | * Receiver that watches for {@link CarrierConfigManager} to be changed. |
| 1372 | */ |
| 1373 | private BroadcastReceiver mCarrierConfigReceiver = new BroadcastReceiver() { |
| 1374 | @Override |
| 1375 | public void onReceive(Context context, Intent intent) { |
| 1376 | // No need to do a permission check, because the ACTION_CARRIER_CONFIG_CHANGED |
| 1377 | // broadcast is protected and can't be spoofed. Runs on a background handler thread. |
| 1378 | |
| 1379 | if (!intent.hasExtra(PhoneConstants.SUBSCRIPTION_KEY)) { |
| 1380 | return; |
| 1381 | } |
| 1382 | final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, -1); |
| 1383 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 1384 | final String subscriberId = tele.getSubscriberId(subId); |
| 1385 | |
| 1386 | maybeRefreshTrustedTime(); |
| 1387 | synchronized (mUidRulesFirstLock) { |
| 1388 | synchronized (mNetworkPoliciesSecondLock) { |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1389 | final boolean added = ensureActiveMobilePolicyAL(subId, subscriberId); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1390 | if (added) return; |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1391 | final boolean updated = maybeUpdateMobilePolicyCycleAL(subId); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1392 | if (!updated) return; |
| 1393 | // update network and notification rules, as the data cycle changed and it's |
| 1394 | // possible that we should be triggering warnings/limits now |
| 1395 | handleNetworkPoliciesUpdateAL(true); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | }; |
| 1400 | |
| 1401 | /** |
| 1402 | * Handles all tasks that need to be run after a new network policy has been set, or an existing |
| 1403 | * one has been updated. |
| 1404 | * |
| 1405 | * @param shouldNormalizePolicies true iff network policies need to be normalized after the |
| 1406 | * update. |
| 1407 | */ |
| 1408 | void handleNetworkPoliciesUpdateAL(boolean shouldNormalizePolicies) { |
| 1409 | if (shouldNormalizePolicies) { |
| 1410 | normalizePoliciesNL(); |
| 1411 | } |
| 1412 | updateNetworkEnabledNL(); |
| 1413 | updateNetworkRulesNL(); |
| 1414 | updateNotificationsNL(); |
| 1415 | writePolicyAL(); |
| 1416 | } |
| 1417 | |
| 1418 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1419 | * Proactively control network data connections when they exceed |
| 1420 | * {@link NetworkPolicy#limitBytes}. |
| 1421 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1422 | void updateNetworkEnabledNL() { |
| 1423 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledNL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1424 | |
| 1425 | // TODO: reset any policy-disabled networks when any policy is removed |
| 1426 | // completely, which is currently rare case. |
| 1427 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1428 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1429 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1430 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1431 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1432 | setNetworkTemplateEnabled(policy.template, true); |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1436 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1437 | .cycleIterator(policy).next(); |
| 1438 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1439 | final long end = cycle.second.toInstant().toEpochMilli(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1440 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1441 | |
| 1442 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1443 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 1444 | && policy.lastLimitSnooze < start; |
| 1445 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1446 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1447 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1452 | * Proactively disable networks that match the given |
| 1453 | * {@link NetworkTemplate}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1454 | */ |
| 1455 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1456 | // TODO: reach into ConnectivityManager to proactively disable bringing |
| 1457 | // up this network, since we know that traffic will be blocked. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1458 | |
| 1459 | if (template.getMatchRule() == MATCH_MOBILE_ALL) { |
| 1460 | // If mobile data usage hits the limit or if the user resumes the data, we need to |
| 1461 | // notify telephony. |
| 1462 | final SubscriptionManager sm = SubscriptionManager.from(mContext); |
| 1463 | final TelephonyManager tm = TelephonyManager.from(mContext); |
| 1464 | |
| 1465 | final int[] subIds = sm.getActiveSubscriptionIdList(); |
| 1466 | for (int subId : subIds) { |
| 1467 | final String subscriberId = tm.getSubscriberId(subId); |
| 1468 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1469 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
| 1470 | // Template is matched when subscriber id matches. |
| 1471 | if (template.matches(probeIdent)) { |
| 1472 | tm.setPolicyDataEnabled(enabled, subId); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /** |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1479 | * Collect all ifaces from a {@link NetworkState} into the given set. |
| 1480 | */ |
| 1481 | private static void collectIfaces(ArraySet<String> ifaces, NetworkState state) { |
| 1482 | final String baseIface = state.linkProperties.getInterfaceName(); |
| 1483 | if (baseIface != null) { |
| 1484 | ifaces.add(baseIface); |
| 1485 | } |
| 1486 | for (LinkProperties stackedLink : state.linkProperties.getStackedLinks()) { |
| 1487 | final String stackedIface = stackedLink.getInterfaceName(); |
| 1488 | if (stackedIface != null) { |
| 1489 | ifaces.add(stackedIface); |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1495 | * Examine all connected {@link NetworkState}, looking for |
| 1496 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1497 | * remaining quota based on usage cycle and historical stats. |
| 1498 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1499 | void updateNetworkRulesNL() { |
| 1500 | if (LOGV) Slog.v(TAG, "updateNetworkRulesNL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1501 | |
| 1502 | final NetworkState[] states; |
| 1503 | try { |
| 1504 | states = mConnManager.getAllNetworkState(); |
| 1505 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1506 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1507 | return; |
| 1508 | } |
| 1509 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1510 | // First, generate identities of all connected networks so we can |
| 1511 | // quickly compare them against all defined policies below. |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1512 | final ArrayMap<NetworkState, NetworkIdentity> identified = new ArrayMap<>(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1513 | for (NetworkState state : states) { |
Wei Liu | b8eaf45 | 2016-01-25 10:32:27 -0800 | [diff] [blame] | 1514 | if (state.networkInfo != null && state.networkInfo.isConnected()) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1515 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1516 | identified.put(state, ident); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1520 | final ArraySet<String> newMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1521 | long lowestRule = Long.MAX_VALUE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1522 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1523 | // For every well-defined policy, compute remaining data based on |
| 1524 | // current cycle and historical stats, and push to kernel. |
| 1525 | final ArraySet<String> matchingIfaces = new ArraySet<>(); |
| 1526 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1527 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1528 | |
| 1529 | // Collect all ifaces that match this policy |
| 1530 | matchingIfaces.clear(); |
| 1531 | for (int j = identified.size() - 1; j >= 0; j--) { |
| 1532 | if (policy.template.matches(identified.valueAt(j))) { |
| 1533 | collectIfaces(matchingIfaces, identified.keyAt(j)); |
| 1534 | } |
| 1535 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1536 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1537 | if (LOGD) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1538 | Slog.d(TAG, "Applying " + policy + " to ifaces " + matchingIfaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1541 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1542 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1543 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1544 | final long quotaBytes; |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1545 | if (hasLimit && policy.hasCycle()) { |
| 1546 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1547 | .cycleIterator(policy).next(); |
| 1548 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1549 | final long end = cycle.second.toInstant().toEpochMilli(); |
| 1550 | final long totalBytes = getTotalBytes(policy.template, start, end); |
| 1551 | |
| 1552 | if (policy.lastLimitSnooze >= start) { |
| 1553 | // snoozing past quota, but we still need to restrict apps, |
| 1554 | // so push really high quota. |
| 1555 | quotaBytes = Long.MAX_VALUE; |
| 1556 | } else { |
| 1557 | // remaining "quota" bytes are based on total usage in |
| 1558 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1559 | // set 1-byte quota and disable the radio later. |
| 1560 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1561 | } |
| 1562 | } else { |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1563 | // metered network, but no policy limit; we still need to |
| 1564 | // restrict apps, so push really high quota. |
| 1565 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1566 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1567 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1568 | if (matchingIfaces.size() > 1) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1569 | // TODO: switch to shared quota once NMS supports |
| 1570 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1573 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1574 | final String iface = matchingIfaces.valueAt(j); |
| 1575 | setInterfaceQuotaAsync(iface, quotaBytes); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1576 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1579 | |
| 1580 | // keep track of lowest warning or limit of active policies |
| 1581 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1582 | lowestRule = policy.warningBytes; |
| 1583 | } |
| 1584 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1585 | lowestRule = policy.limitBytes; |
| 1586 | } |
| 1587 | } |
| 1588 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1589 | // One final pass to catch any metered ifaces that don't have explicitly |
| 1590 | // defined policies; typically Wi-Fi networks. |
| 1591 | for (NetworkState state : states) { |
| 1592 | if (state.networkInfo != null && state.networkInfo.isConnected() |
| 1593 | && !state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 1594 | matchingIfaces.clear(); |
| 1595 | collectIfaces(matchingIfaces, state); |
| 1596 | for (int j = matchingIfaces.size() - 1; j >= 0; j--) { |
| 1597 | final String iface = matchingIfaces.valueAt(j); |
| 1598 | if (!newMeteredIfaces.contains(iface)) { |
| 1599 | setInterfaceQuotaAsync(iface, Long.MAX_VALUE); |
| 1600 | newMeteredIfaces.add(iface); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1606 | // Remove quota from any interfaces that are no longer metered. |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1607 | for (int i = mMeteredIfaces.size() - 1; i >= 0; i--) { |
| 1608 | final String iface = mMeteredIfaces.valueAt(i); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1609 | if (!newMeteredIfaces.contains(iface)) { |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1610 | removeInterfaceQuotaAsync(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | mMeteredIfaces = newMeteredIfaces; |
| 1614 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1615 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1616 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 1617 | |
| 1618 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1623 | * have at least a default mobile policy defined. |
| 1624 | */ |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1625 | private void ensureActiveMobilePolicyAL() { |
| 1626 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyAL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1627 | if (mSuppressDefaultPolicy) return; |
| 1628 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1629 | final TelephonyManager tele = TelephonyManager.from(mContext); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1630 | final SubscriptionManager sub = SubscriptionManager.from(mContext); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1631 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1632 | final int[] subIds = sub.getActiveSubscriptionIdList(); |
| 1633 | for (int subId : subIds) { |
| 1634 | final String subscriberId = tele.getSubscriberId(subId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1635 | ensureActiveMobilePolicyAL(subId, subscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1636 | } |
| 1637 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1638 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1639 | /** |
| 1640 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1641 | * have at least a default mobile policy defined. |
| 1642 | * |
| 1643 | * @param subId to build a default policy for |
| 1644 | * @param subscriberId that we check for an existing policy |
| 1645 | * @return true if a mobile network policy was added, or false one already existed. |
| 1646 | */ |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1647 | private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1648 | // Poke around to see if we already have a policy |
| 1649 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 1650 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1651 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1652 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1653 | if (template.matches(probeIdent)) { |
| 1654 | if (LOGD) { |
| 1655 | Slog.d(TAG, "Found template " + template + " which matches subscriber " |
| 1656 | + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 1657 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1658 | return false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1662 | Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 1663 | + "; generating default policy"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1664 | final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1665 | addNetworkPolicyAL(policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1666 | return true; |
| 1667 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1668 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1669 | private long getPlatformDefaultWarningBytes() { |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1670 | final int dataWarningConfig = mContext.getResources().getInteger( |
| 1671 | com.android.internal.R.integer.config_networkPolicyDefaultWarning); |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1672 | if (dataWarningConfig == WARNING_DISABLED) { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1673 | return WARNING_DISABLED; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1674 | } else { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1675 | return dataWarningConfig * MB_IN_BYTES; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1676 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1677 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1678 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1679 | private long getPlatformDefaultLimitBytes() { |
| 1680 | return LIMIT_DISABLED; |
| 1681 | } |
| 1682 | |
| 1683 | @VisibleForTesting |
| 1684 | public NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1685 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1686 | final RecurrenceRule cycleRule = NetworkPolicy |
| 1687 | .buildRule(ZonedDateTime.now().getDayOfMonth(), ZoneId.systemDefault()); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1688 | final NetworkPolicy policy = new NetworkPolicy(template, cycleRule, |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1689 | getPlatformDefaultWarningBytes(), getPlatformDefaultLimitBytes(), |
| 1690 | SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 1691 | synchronized (mUidRulesFirstLock) { |
| 1692 | synchronized (mNetworkPoliciesSecondLock) { |
| 1693 | updateDefaultMobilePolicyAL(subId, policy); |
| 1694 | } |
| 1695 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1696 | return policy; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 1699 | /** |
| 1700 | * Update the given {@link NetworkPolicy} based on any carrier-provided |
| 1701 | * defaults via {@link SubscriptionPlan} or {@link CarrierConfigManager}. |
| 1702 | * Leaves policy untouched if the user has modified it. |
| 1703 | * |
| 1704 | * @return if the policy was modified |
| 1705 | */ |
| 1706 | private boolean updateDefaultMobilePolicyAL(int subId, NetworkPolicy policy) { |
| 1707 | if (!policy.inferred) { |
| 1708 | if (LOGD) Slog.d(TAG, "Ignoring user-defined policy " + policy); |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | final NetworkPolicy original = new NetworkPolicy(policy.template, policy.cycleRule, |
| 1713 | policy.warningBytes, policy.limitBytes, policy.lastWarningSnooze, |
| 1714 | policy.lastLimitSnooze, policy.metered, policy.inferred); |
| 1715 | |
| 1716 | final SubscriptionPlan[] plans = mSubscriptionPlans.get(subId); |
| 1717 | if (!ArrayUtils.isEmpty(plans)) { |
| 1718 | final SubscriptionPlan plan = plans[0]; |
| 1719 | policy.cycleRule = plan.getCycleRule(); |
| 1720 | final long planLimitBytes = plan.getDataLimitBytes(); |
| 1721 | if (planLimitBytes == SubscriptionPlan.BYTES_UNKNOWN) { |
| 1722 | policy.warningBytes = getPlatformDefaultWarningBytes(); |
| 1723 | policy.limitBytes = getPlatformDefaultLimitBytes(); |
| 1724 | } else if (planLimitBytes == SubscriptionPlan.BYTES_UNLIMITED) { |
| 1725 | policy.warningBytes = NetworkPolicy.WARNING_DISABLED; |
| 1726 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 1727 | } else { |
| 1728 | policy.warningBytes = (planLimitBytes * 9) / 10; |
| 1729 | switch (plan.getDataLimitBehavior()) { |
| 1730 | case SubscriptionPlan.LIMIT_BEHAVIOR_BILLED: |
| 1731 | case SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED: |
| 1732 | policy.limitBytes = planLimitBytes; |
| 1733 | break; |
| 1734 | default: |
| 1735 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 1736 | break; |
| 1737 | } |
| 1738 | } |
| 1739 | } else { |
| 1740 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
| 1741 | final int currentCycleDay; |
| 1742 | if (policy.cycleRule.isMonthly()) { |
| 1743 | currentCycleDay = policy.cycleRule.start.getDayOfMonth(); |
| 1744 | } else { |
| 1745 | currentCycleDay = NetworkPolicy.CYCLE_NONE; |
| 1746 | } |
| 1747 | final int cycleDay = getCycleDayFromCarrierConfig(config, currentCycleDay); |
| 1748 | policy.cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.systemDefault()); |
| 1749 | policy.warningBytes = getWarningBytesFromCarrierConfig(config, policy.warningBytes); |
| 1750 | policy.limitBytes = getLimitBytesFromCarrierConfig(config, policy.limitBytes); |
| 1751 | } |
| 1752 | |
| 1753 | if (policy.equals(original)) { |
| 1754 | return false; |
| 1755 | } else { |
| 1756 | Slog.d(TAG, "Updated " + original + " to " + policy); |
| 1757 | return true; |
| 1758 | } |
| 1759 | } |
| 1760 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1761 | private void readPolicyAL() { |
| 1762 | if (LOGV) Slog.v(TAG, "readPolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1763 | |
| 1764 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1765 | mNetworkPolicy.clear(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1766 | mSubscriptionPlans.clear(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 1767 | mSubscriptionPlansOwner.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1768 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1769 | |
| 1770 | FileInputStream fis = null; |
| 1771 | try { |
| 1772 | fis = mPolicyFile.openRead(); |
| 1773 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1774 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1775 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1776 | // Must save the <restrict-background> tags and convert them to <uid-policy> later, |
| 1777 | // to skip UIDs that were explicitly blacklisted. |
| 1778 | final SparseBooleanArray whitelistedRestrictBackground = new SparseBooleanArray(); |
| 1779 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1780 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1781 | int version = VERSION_INIT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1782 | boolean insideWhitelist = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1783 | while ((type = in.next()) != END_DOCUMENT) { |
| 1784 | final String tag = in.getName(); |
| 1785 | if (type == START_TAG) { |
| 1786 | if (TAG_POLICY_LIST.equals(tag)) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1787 | final boolean oldValue = mRestrictBackground; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1788 | version = readIntAttribute(in, ATTR_VERSION); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 1789 | mLoadedRestrictBackground = (version >= VERSION_ADDED_RESTRICT_BACKGROUND) |
| 1790 | && readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1791 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 1792 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 1793 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1794 | final String networkId; |
| 1795 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 1796 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 1797 | } else { |
| 1798 | networkId = null; |
| 1799 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1800 | final RecurrenceRule cycleRule; |
| 1801 | if (version >= VERSION_ADDED_CYCLE) { |
| 1802 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 1803 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 1804 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 1805 | cycleRule = new RecurrenceRule( |
| 1806 | RecurrenceRule.convertZonedDateTime(start), |
| 1807 | RecurrenceRule.convertZonedDateTime(end), |
| 1808 | RecurrenceRule.convertPeriod(period)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1809 | } else { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1810 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
| 1811 | final String cycleTimezone; |
| 1812 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 1813 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 1814 | } else { |
| 1815 | cycleTimezone = "UTC"; |
| 1816 | } |
| 1817 | cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.of(cycleTimezone)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1818 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1819 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 1820 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1821 | final long lastLimitSnooze; |
| 1822 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1823 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 1824 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 1825 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1826 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1827 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1828 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1829 | final boolean metered; |
| 1830 | if (version >= VERSION_ADDED_METERED) { |
| 1831 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 1832 | } else { |
| 1833 | switch (networkTemplate) { |
| 1834 | case MATCH_MOBILE_3G_LOWER: |
| 1835 | case MATCH_MOBILE_4G: |
| 1836 | case MATCH_MOBILE_ALL: |
| 1837 | metered = true; |
| 1838 | break; |
| 1839 | default: |
| 1840 | metered = false; |
| 1841 | } |
| 1842 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1843 | final long lastWarningSnooze; |
| 1844 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1845 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 1846 | } else { |
| 1847 | lastWarningSnooze = SNOOZE_NEVER; |
| 1848 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1849 | final boolean inferred; |
| 1850 | if (version >= VERSION_ADDED_INFERRED) { |
| 1851 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 1852 | } else { |
| 1853 | inferred = false; |
| 1854 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1855 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1856 | final NetworkTemplate template = new NetworkTemplate(networkTemplate, |
| 1857 | subscriberId, networkId); |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1858 | if (template.isPersistable()) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1859 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleRule, |
| 1860 | warningBytes, limitBytes, lastWarningSnooze, |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1861 | lastLimitSnooze, metered, inferred)); |
| 1862 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1863 | |
| 1864 | } else if (TAG_SUBSCRIPTION_PLAN.equals(tag)) { |
| 1865 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 1866 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 1867 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 1868 | final SubscriptionPlan.Builder builder = new SubscriptionPlan.Builder( |
| 1869 | RecurrenceRule.convertZonedDateTime(start), |
| 1870 | RecurrenceRule.convertZonedDateTime(end), |
| 1871 | RecurrenceRule.convertPeriod(period)); |
| 1872 | builder.setTitle(readStringAttribute(in, ATTR_TITLE)); |
| 1873 | builder.setSummary(readStringAttribute(in, ATTR_SUMMARY)); |
| 1874 | |
| 1875 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES, |
| 1876 | SubscriptionPlan.BYTES_UNKNOWN); |
| 1877 | final int limitBehavior = readIntAttribute(in, ATTR_LIMIT_BEHAVIOR, |
| 1878 | SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN); |
| 1879 | if (limitBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 1880 | && limitBehavior != SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
| 1881 | builder.setDataLimit(limitBytes, limitBehavior); |
| 1882 | } |
| 1883 | |
| 1884 | final long usageBytes = readLongAttribute(in, ATTR_USAGE_BYTES, |
| 1885 | SubscriptionPlan.BYTES_UNKNOWN); |
| 1886 | final long usageTime = readLongAttribute(in, ATTR_USAGE_TIME, |
| 1887 | SubscriptionPlan.TIME_UNKNOWN); |
| 1888 | if (usageBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 1889 | && usageTime != SubscriptionPlan.TIME_UNKNOWN) { |
| 1890 | builder.setDataUsage(usageBytes, usageTime); |
| 1891 | } |
| 1892 | |
| 1893 | final int subId = readIntAttribute(in, ATTR_SUB_ID); |
| 1894 | final SubscriptionPlan plan = builder.build(); |
| 1895 | mSubscriptionPlans.put(subId, ArrayUtils.appendElement( |
| 1896 | SubscriptionPlan.class, mSubscriptionPlans.get(subId), plan)); |
| 1897 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 1898 | final String ownerPackage = readStringAttribute(in, ATTR_OWNER_PACKAGE); |
| 1899 | mSubscriptionPlansOwner.put(subId, ownerPackage); |
| 1900 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1901 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1902 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1903 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1904 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1905 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1906 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1907 | } else { |
| 1908 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 1909 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1910 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1911 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 1912 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1913 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1914 | // TODO: set for other users during upgrade |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 1915 | // app policy is deprecated so this is only used in pre system user split. |
| 1916 | final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1917 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1918 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1919 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1920 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1921 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1922 | } else if (TAG_WHITELIST.equals(tag)) { |
| 1923 | insideWhitelist = true; |
| 1924 | } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1925 | final int uid = readIntAttribute(in, ATTR_UID); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1926 | whitelistedRestrictBackground.append(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 1927 | } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1928 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1929 | mRestrictBackgroundWhitelistRevokedUids.put(uid, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1930 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1931 | } else if (type == END_TAG) { |
| 1932 | if (TAG_WHITELIST.equals(tag)) { |
| 1933 | insideWhitelist = false; |
| 1934 | } |
| 1935 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1939 | final int size = whitelistedRestrictBackground.size(); |
| 1940 | for (int i = 0; i < size; i++) { |
| 1941 | final int uid = whitelistedRestrictBackground.keyAt(i); |
| 1942 | final int policy = mUidPolicy.get(uid, POLICY_NONE); |
| 1943 | if ((policy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
| 1944 | Slog.w(TAG, "ignoring restrict-background-whitelist for " + uid |
| 1945 | + " because its policy is " + uidPoliciesToString(policy)); |
| 1946 | continue; |
| 1947 | } |
| 1948 | if (UserHandle.isApp(uid)) { |
| 1949 | final int newPolicy = policy | POLICY_ALLOW_METERED_BACKGROUND; |
| 1950 | if (LOGV) |
| 1951 | Log.v(TAG, "new policy for " + uid + ": " + uidPoliciesToString(newPolicy)); |
| 1952 | setUidPolicyUncheckedUL(uid, newPolicy, false); |
| 1953 | } else { |
| 1954 | Slog.w(TAG, "unable to update policy on UID " + uid); |
| 1955 | } |
| 1956 | } |
| 1957 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1958 | } catch (FileNotFoundException e) { |
| 1959 | // missing policy is okay, probably first boot |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1960 | upgradeDefaultBackgroundDataUL(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1961 | } catch (Exception e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1962 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1963 | } finally { |
| 1964 | IoUtils.closeQuietly(fis); |
| 1965 | } |
| 1966 | } |
| 1967 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1968 | /** |
| 1969 | * Upgrade legacy background data flags, notifying listeners of one last |
| 1970 | * change to always-true. |
| 1971 | */ |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1972 | private void upgradeDefaultBackgroundDataUL() { |
| 1973 | // This method is only called when we're unable to find the network policy flag, which |
| 1974 | // usually happens on first boot of a new device and not one that has received an OTA. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1975 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1976 | // Seed from the default value configured for this device. |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 1977 | mLoadedRestrictBackground = Settings.Global.getInt( |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1978 | mContext.getContentResolver(), Global.DEFAULT_RESTRICT_BACKGROUND_DATA, 0) == 1; |
| 1979 | |
| 1980 | // NOTE: We used to read the legacy setting here : |
| 1981 | // |
| 1982 | // final int legacyFlagValue = Settings.Secure.getInt( |
| 1983 | // mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, ..); |
| 1984 | // |
| 1985 | // This is no longer necessary because we will never upgrade directly from Gingerbread |
| 1986 | // to O+. Devices upgrading from ICS onwards to O will have a netpolicy.xml file that |
| 1987 | // contains the correct value that we will continue to use. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1990 | /** |
| 1991 | * Perform upgrade step of moving any user-defined meterness overrides over |
| 1992 | * into {@link WifiConfiguration}. |
| 1993 | */ |
| 1994 | private void upgradeWifiMeteredOverrideAL() { |
| 1995 | boolean modified = false; |
| 1996 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 1997 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 1998 | for (int i = 0; i < mNetworkPolicy.size(); ) { |
| 1999 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 2000 | if (policy.template.getMatchRule() == NetworkTemplate.MATCH_WIFI |
| 2001 | && !policy.inferred) { |
| 2002 | mNetworkPolicy.removeAt(i); |
| 2003 | modified = true; |
| 2004 | |
| 2005 | final String networkId = resolveNetworkId(policy.template.getNetworkId()); |
| 2006 | for (WifiConfiguration config : configs) { |
| 2007 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2008 | Slog.d(TAG, "Found network " + networkId + "; upgrading metered hint"); |
| 2009 | config.meteredOverride = policy.metered |
| 2010 | ? WifiConfiguration.METERED_OVERRIDE_METERED |
| 2011 | : WifiConfiguration.METERED_OVERRIDE_NOT_METERED; |
| 2012 | wm.updateNetwork(config); |
| 2013 | } |
| 2014 | } |
| 2015 | } else { |
| 2016 | i++; |
| 2017 | } |
| 2018 | } |
| 2019 | if (modified) { |
| 2020 | writePolicyAL(); |
| 2021 | } |
| 2022 | } |
| 2023 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2024 | void writePolicyAL() { |
| 2025 | if (LOGV) Slog.v(TAG, "writePolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2026 | |
| 2027 | FileOutputStream fos = null; |
| 2028 | try { |
| 2029 | fos = mPolicyFile.startWrite(); |
| 2030 | |
| 2031 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2032 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2033 | out.startDocument(null, true); |
| 2034 | |
| 2035 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2036 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2037 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2038 | |
| 2039 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2040 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2041 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2042 | final NetworkTemplate template = policy.template; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2043 | if (!template.isPersistable()) continue; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2044 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2045 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2046 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 2047 | final String subscriberId = template.getSubscriberId(); |
| 2048 | if (subscriberId != null) { |
| 2049 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2050 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2051 | final String networkId = template.getNetworkId(); |
| 2052 | if (networkId != null) { |
| 2053 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 2054 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2055 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2056 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.start)); |
| 2057 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2058 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.end)); |
| 2059 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2060 | RecurrenceRule.convertPeriod(policy.cycleRule.period)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2061 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 2062 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2063 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 2064 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2065 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2066 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2067 | out.endTag(null, TAG_NETWORK_POLICY); |
| 2068 | } |
| 2069 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2070 | // write all known subscription plans |
| 2071 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 2072 | final int subId = mSubscriptionPlans.keyAt(i); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2073 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2074 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 2075 | if (ArrayUtils.isEmpty(plans)) continue; |
| 2076 | |
| 2077 | for (SubscriptionPlan plan : plans) { |
| 2078 | out.startTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2079 | writeIntAttribute(out, ATTR_SUB_ID, subId); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2080 | writeStringAttribute(out, ATTR_OWNER_PACKAGE, ownerPackage); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2081 | final RecurrenceRule cycleRule = plan.getCycleRule(); |
| 2082 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2083 | RecurrenceRule.convertZonedDateTime(cycleRule.start)); |
| 2084 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2085 | RecurrenceRule.convertZonedDateTime(cycleRule.end)); |
| 2086 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2087 | RecurrenceRule.convertPeriod(cycleRule.period)); |
| 2088 | writeStringAttribute(out, ATTR_TITLE, plan.getTitle()); |
| 2089 | writeStringAttribute(out, ATTR_SUMMARY, plan.getSummary()); |
| 2090 | writeLongAttribute(out, ATTR_LIMIT_BYTES, plan.getDataLimitBytes()); |
| 2091 | writeIntAttribute(out, ATTR_LIMIT_BEHAVIOR, plan.getDataLimitBehavior()); |
| 2092 | writeLongAttribute(out, ATTR_USAGE_BYTES, plan.getDataUsageBytes()); |
| 2093 | writeLongAttribute(out, ATTR_USAGE_TIME, plan.getDataUsageTime()); |
| 2094 | out.endTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2095 | } |
| 2096 | } |
| 2097 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2098 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2099 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2100 | final int uid = mUidPolicy.keyAt(i); |
| 2101 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2102 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2103 | // skip writing empty policies |
| 2104 | if (policy == POLICY_NONE) continue; |
| 2105 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2106 | out.startTag(null, TAG_UID_POLICY); |
| 2107 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2108 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2109 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | out.endTag(null, TAG_POLICY_LIST); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2113 | |
| 2114 | // write all whitelists |
| 2115 | out.startTag(null, TAG_WHITELIST); |
| 2116 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2117 | // revoked restrict background whitelist |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2118 | int size = mRestrictBackgroundWhitelistRevokedUids.size(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2119 | for (int i = 0; i < size; i++) { |
| 2120 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2121 | out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2122 | writeIntAttribute(out, ATTR_UID, uid); |
| 2123 | out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2124 | } |
| 2125 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2126 | out.endTag(null, TAG_WHITELIST); |
| 2127 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2128 | out.endDocument(); |
| 2129 | |
| 2130 | mPolicyFile.finishWrite(fos); |
| 2131 | } catch (IOException e) { |
| 2132 | if (fos != null) { |
| 2133 | mPolicyFile.failWrite(fos); |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2138 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2139 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2140 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2141 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2142 | if (!UserHandle.isApp(uid)) { |
| 2143 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2144 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2145 | synchronized (mUidRulesFirstLock) { |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2146 | final long token = Binder.clearCallingIdentity(); |
| 2147 | try { |
| 2148 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2149 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2150 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2151 | } |
| 2152 | } finally { |
| 2153 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2154 | } |
| 2155 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2158 | @Override |
| 2159 | public void addUidPolicy(int uid, int policy) { |
| 2160 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2161 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2162 | if (!UserHandle.isApp(uid)) { |
| 2163 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2164 | } |
| 2165 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2166 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2167 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2168 | policy |= oldPolicy; |
| 2169 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2170 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2171 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2172 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2176 | public void removeUidPolicy(int uid, int policy) { |
| 2177 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2178 | |
| 2179 | if (!UserHandle.isApp(uid)) { |
| 2180 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2181 | } |
| 2182 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2183 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2184 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2185 | policy = oldPolicy & ~policy; |
| 2186 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2187 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | } |
| 2191 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2192 | private void setUidPolicyUncheckedUL(int uid, int oldPolicy, int policy, boolean persist) { |
| 2193 | setUidPolicyUncheckedUL(uid, policy, persist); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2194 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2195 | final boolean notifyApp; |
| 2196 | if (!isUidValidForWhitelistRules(uid)) { |
| 2197 | notifyApp = false; |
| 2198 | } else { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2199 | final boolean wasBlacklisted = oldPolicy == POLICY_REJECT_METERED_BACKGROUND; |
| 2200 | final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND; |
| 2201 | final boolean wasWhitelisted = oldPolicy == POLICY_ALLOW_METERED_BACKGROUND; |
| 2202 | final boolean isWhitelisted = policy == POLICY_ALLOW_METERED_BACKGROUND; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2203 | final boolean wasBlocked = wasBlacklisted || (mRestrictBackground && !wasWhitelisted); |
| 2204 | final boolean isBlocked = isBlacklisted || (mRestrictBackground && !isWhitelisted); |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2205 | if ((wasWhitelisted && (!isWhitelisted || isBlacklisted)) |
| 2206 | && mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2207 | && !mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2208 | if (LOGD) |
| 2209 | Slog.d(TAG, "Adding uid " + uid + " to revoked restrict background whitelist"); |
| 2210 | mRestrictBackgroundWhitelistRevokedUids.append(uid, true); |
| 2211 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2212 | notifyApp = wasBlocked != isBlocked; |
| 2213 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2214 | mHandler.obtainMessage(MSG_POLICIES_CHANGED, uid, policy, Boolean.valueOf(notifyApp)) |
| 2215 | .sendToTarget(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2216 | } |
| 2217 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2218 | private void setUidPolicyUncheckedUL(int uid, int policy, boolean persist) { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2219 | if (policy == POLICY_NONE) { |
| 2220 | mUidPolicy.delete(uid); |
| 2221 | } else { |
| 2222 | mUidPolicy.put(uid, policy); |
| 2223 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2224 | |
| 2225 | // uid policy changed, recompute rules and persist policy. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2226 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2227 | if (persist) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2228 | synchronized (mNetworkPoliciesSecondLock) { |
| 2229 | writePolicyAL(); |
| 2230 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2235 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2236 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2237 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2238 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2239 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2240 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2243 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2244 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2245 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2246 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2247 | int[] uids = new int[0]; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2248 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2249 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2250 | final int uid = mUidPolicy.keyAt(i); |
| 2251 | final int uidPolicy = mUidPolicy.valueAt(i); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 2252 | if ((policy == POLICY_NONE && uidPolicy == POLICY_NONE) || |
| 2253 | (uidPolicy & policy) != 0) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2254 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2255 | } |
| 2256 | } |
| 2257 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2258 | return uids; |
| 2259 | } |
| 2260 | |
| 2261 | /** |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2262 | * Removes any persistable state associated with given {@link UserHandle}, persisting |
| 2263 | * if any changes that are made. |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2264 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2265 | boolean removeUserStateUL(int userId, boolean writePolicy) { |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2266 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2267 | if (LOGV) Slog.v(TAG, "removeUserStateUL()"); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2268 | boolean changed = false; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2269 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2270 | // Remove entries from revoked default restricted background UID whitelist |
| 2271 | for (int i = mRestrictBackgroundWhitelistRevokedUids.size() - 1; i >= 0; i--) { |
| 2272 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2273 | if (UserHandle.getUserId(uid) == userId) { |
| 2274 | mRestrictBackgroundWhitelistRevokedUids.removeAt(i); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2275 | changed = true; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2279 | // Remove associated UID policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2280 | int[] uids = new int[0]; |
| 2281 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2282 | final int uid = mUidPolicy.keyAt(i); |
| 2283 | if (UserHandle.getUserId(uid) == userId) { |
| 2284 | uids = appendInt(uids, uid); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | if (uids.length > 0) { |
| 2289 | for (int uid : uids) { |
| 2290 | mUidPolicy.delete(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2291 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2292 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2293 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2294 | synchronized (mNetworkPoliciesSecondLock) { |
| 2295 | updateRulesForGlobalChangeAL(true); |
| 2296 | if (writePolicy && changed) { |
| 2297 | writePolicyAL(); |
| 2298 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2299 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2300 | return changed; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2304 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2305 | // TODO: create permission for observing network policy |
| 2306 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2307 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | @Override |
| 2311 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2312 | // TODO: create permission for observing network policy |
| 2313 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2314 | mListeners.unregister(listener); |
| 2315 | } |
| 2316 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2317 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2318 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2319 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2320 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2321 | final long token = Binder.clearCallingIdentity(); |
| 2322 | try { |
| 2323 | maybeRefreshTrustedTime(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2324 | synchronized (mUidRulesFirstLock) { |
| 2325 | synchronized (mNetworkPoliciesSecondLock) { |
| 2326 | normalizePoliciesNL(policies); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2327 | handleNetworkPoliciesUpdateAL(false); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2328 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2329 | } |
| 2330 | } finally { |
| 2331 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2335 | void addNetworkPolicyAL(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2336 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2337 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 2338 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2341 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2342 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2343 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2344 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 2345 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 2346 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 2347 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2348 | } catch (SecurityException e) { |
| 2349 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2350 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2351 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 2352 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 2353 | return new NetworkPolicy[0]; |
| 2354 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2357 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2358 | final int size = mNetworkPolicy.size(); |
| 2359 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 2360 | for (int i = 0; i < size; i++) { |
| 2361 | policies[i] = mNetworkPolicy.valueAt(i); |
| 2362 | } |
| 2363 | return policies; |
| 2364 | } |
| 2365 | } |
| 2366 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2367 | private void normalizePoliciesNL() { |
| 2368 | normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2369 | } |
| 2370 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2371 | private void normalizePoliciesNL(NetworkPolicy[] policies) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2372 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 2373 | final String[] merged = tele.getMergedSubscriberIds(); |
| 2374 | |
| 2375 | mNetworkPolicy.clear(); |
| 2376 | for (NetworkPolicy policy : policies) { |
| 2377 | // When two normalized templates conflict, prefer the most |
| 2378 | // restrictive policy |
| 2379 | policy.template = NetworkTemplate.normalize(policy.template, merged); |
| 2380 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 2381 | if (existing == null || existing.compareTo(policy) > 0) { |
| 2382 | if (existing != null) { |
| 2383 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 2384 | } |
| 2385 | mNetworkPolicy.put(policy.template, policy); |
| 2386 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2391 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2392 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 2393 | |
| 2394 | final long token = Binder.clearCallingIdentity(); |
| 2395 | try { |
| 2396 | performSnooze(template, TYPE_LIMIT); |
| 2397 | } finally { |
| 2398 | Binder.restoreCallingIdentity(token); |
| 2399 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2400 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2401 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2402 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2403 | maybeRefreshTrustedTime(); |
| 2404 | final long currentTime = currentTimeMillis(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2405 | synchronized (mUidRulesFirstLock) { |
| 2406 | synchronized (mNetworkPoliciesSecondLock) { |
| 2407 | // find and snooze local policy that matches |
| 2408 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 2409 | if (policy == null) { |
| 2410 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 2411 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2412 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2413 | switch (type) { |
| 2414 | case TYPE_WARNING: |
| 2415 | policy.lastWarningSnooze = currentTime; |
| 2416 | break; |
| 2417 | case TYPE_LIMIT: |
| 2418 | policy.lastLimitSnooze = currentTime; |
| 2419 | break; |
| 2420 | default: |
| 2421 | throw new IllegalArgumentException("unexpected type"); |
| 2422 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2423 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2424 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2425 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2430 | public void onTetheringChanged(String iface, boolean tethering) { |
| 2431 | // No need to enforce permission because setRestrictBackground() will do it. |
| 2432 | if (LOGD) Log.d(TAG, "onTetherStateChanged(" + iface + ", " + tethering + ")"); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2433 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2434 | if (mRestrictBackground && tethering) { |
| 2435 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 2436 | setRestrictBackground(false); |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2442 | public void setRestrictBackground(boolean restrictBackground) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2443 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackground"); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2444 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2445 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2446 | final long token = Binder.clearCallingIdentity(); |
| 2447 | try { |
| 2448 | maybeRefreshTrustedTime(); |
| 2449 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2450 | setRestrictBackgroundUL(restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2451 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2452 | } finally { |
| 2453 | Binder.restoreCallingIdentity(token); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2454 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2455 | } finally { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2456 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2460 | private void setRestrictBackgroundUL(boolean restrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2461 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackgroundUL"); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2462 | try { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2463 | if (restrictBackground == mRestrictBackground) { |
| 2464 | // Ideally, UI should never allow this scenario... |
| 2465 | Slog.w(TAG, "setRestrictBackgroundUL: already " + restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2466 | return; |
| 2467 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2468 | Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground); |
| 2469 | final boolean oldRestrictBackground = mRestrictBackground; |
| 2470 | mRestrictBackground = restrictBackground; |
| 2471 | // Must whitelist foreground apps before turning data saver mode on. |
| 2472 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 2473 | // so it could call AM to get the UIDs of such apps, and iterate through them instead. |
| 2474 | updateRulesForRestrictBackgroundUL(); |
| 2475 | try { |
| 2476 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2477 | Slog.e(TAG, |
| 2478 | "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2479 | mRestrictBackground = oldRestrictBackground; |
| 2480 | // TODO: if it knew the foreground apps (see TODO above), it could call |
| 2481 | // updateRulesForRestrictBackgroundUL() again to restore state. |
| 2482 | return; |
| 2483 | } |
| 2484 | } catch (RemoteException e) { |
| 2485 | // ignored; service lives in system_server |
| 2486 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2487 | |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2488 | sendRestrictBackgroundChangedMsg(); |
| 2489 | |
| 2490 | if (mRestrictBackgroundPowerState.globalBatterySaverEnabled) { |
| 2491 | mRestrictBackgroundChangedInBsm = true; |
| 2492 | } |
| 2493 | synchronized (mNetworkPoliciesSecondLock) { |
| 2494 | updateNotificationsNL(); |
| 2495 | writePolicyAL(); |
| 2496 | } |
| 2497 | } finally { |
| 2498 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2499 | } |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | private void sendRestrictBackgroundChangedMsg() { |
| 2503 | mHandler.removeMessages(MSG_RESTRICT_BACKGROUND_CHANGED); |
| 2504 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, mRestrictBackground ? 1 : 0, 0) |
| 2505 | .sendToTarget(); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2508 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2509 | public int getRestrictBackgroundByCaller() { |
| 2510 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2511 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2512 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2513 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2514 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2515 | final long token = Binder.clearCallingIdentity(); |
| 2516 | final int policy; |
| 2517 | try { |
| 2518 | policy = getUidPolicy(uid); |
| 2519 | } finally { |
| 2520 | Binder.restoreCallingIdentity(token); |
| 2521 | } |
| 2522 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2523 | // App is blacklisted. |
| 2524 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2525 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2526 | if (!mRestrictBackground) { |
| 2527 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2528 | } |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2529 | return (mUidPolicy.get(uid) & POLICY_ALLOW_METERED_BACKGROUND) != 0 |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2530 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2531 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2536 | public boolean getRestrictBackground() { |
| 2537 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2538 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2539 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2540 | return mRestrictBackground; |
| 2541 | } |
| 2542 | } |
| 2543 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2544 | @Override |
| 2545 | public void setDeviceIdleMode(boolean enabled) { |
| 2546 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2547 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode"); |
| 2548 | try { |
| 2549 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2550 | if (mDeviceIdleMode == enabled) { |
| 2551 | return; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2552 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2553 | mDeviceIdleMode = enabled; |
| 2554 | if (mSystemReady) { |
| 2555 | // Device idle change means we need to rebuild rules for all |
| 2556 | // known apps, so do a global refresh. |
| 2557 | updateRulesForRestrictPowerUL(); |
| 2558 | } |
| 2559 | } |
| 2560 | if (enabled) { |
| 2561 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 2562 | } else { |
| 2563 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2564 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2565 | } finally { |
| 2566 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2570 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2571 | public void setWifiMeteredOverride(String networkId, int meteredOverride) { |
| 2572 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2573 | final long token = Binder.clearCallingIdentity(); |
| 2574 | try { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2575 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2576 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2577 | for (WifiConfiguration config : configs) { |
| 2578 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2579 | config.meteredOverride = meteredOverride; |
| 2580 | wm.updateNetwork(config); |
| 2581 | } |
| 2582 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2583 | } finally { |
| 2584 | Binder.restoreCallingIdentity(token); |
| 2585 | } |
| 2586 | } |
| 2587 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2588 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2589 | @Deprecated |
| 2590 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 2591 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 2592 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 2593 | return new NetworkQuotaInfo(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2596 | private void enforceSubscriptionPlanAccess(int subId, int callingUid, String callingPackage) { |
| 2597 | // Verify they're not lying about package name |
| 2598 | mAppOps.checkPackage(callingUid, callingPackage); |
| 2599 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2600 | final SubscriptionInfo si; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2601 | final PersistableBundle config; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2602 | final long token = Binder.clearCallingIdentity(); |
| 2603 | try { |
| 2604 | si = mContext.getSystemService(SubscriptionManager.class) |
| 2605 | .getActiveSubscriptionInfo(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2606 | config = mCarrierConfigManager.getConfigForSubId(subId); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2607 | } finally { |
| 2608 | Binder.restoreCallingIdentity(token); |
| 2609 | } |
| 2610 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2611 | // First check: is caller the CarrierService? |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2612 | if (si != null) { |
| 2613 | if (si.isEmbedded() && si.canManageSubscription(mContext, callingPackage)) { |
| 2614 | return; |
| 2615 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2616 | } |
| 2617 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2618 | // Second check: has the CarrierService delegated access? |
| 2619 | if (config != null) { |
| 2620 | final String overridePackage = config |
| 2621 | .getString(CarrierConfigManager.KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING, null); |
| 2622 | if (!TextUtils.isEmpty(overridePackage) |
| 2623 | && Objects.equals(overridePackage, callingPackage)) { |
| 2624 | return; |
| 2625 | } |
| 2626 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2627 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2628 | // Third check: is caller the fallback/default CarrierService? |
| 2629 | final String defaultPackage = mCarrierConfigManager.getDefaultCarrierServicePackageName(); |
| 2630 | if (!TextUtils.isEmpty(defaultPackage) |
| 2631 | && Objects.equals(defaultPackage, callingPackage)) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2632 | return; |
| 2633 | } |
| 2634 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2635 | // Final check: does the caller hold a permission? |
| 2636 | mContext.enforceCallingOrSelfPermission(MANAGE_SUBSCRIPTION_PLANS, TAG); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | @Override |
| 2640 | public SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage) { |
| 2641 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 2642 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2643 | final String fake = SystemProperties.get("fw.fake_plan"); |
| 2644 | if (!TextUtils.isEmpty(fake)) { |
| 2645 | final List<SubscriptionPlan> plans = new ArrayList<>(); |
| 2646 | if ("month_hard".equals(fake)) { |
| 2647 | plans.add(SubscriptionPlan.Builder |
| 2648 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2649 | .setTitle("G-Mobile") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2650 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2651 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 2652 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 2653 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 2654 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 2655 | plans.add(SubscriptionPlan.Builder |
| 2656 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 2657 | .setTitle("G-Mobile Happy") |
| 2658 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 2659 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 2660 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 2661 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 2662 | .build()); |
| 2663 | plans.add(SubscriptionPlan.Builder |
| 2664 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 2665 | .setTitle("G-Mobile, Charged after limit") |
| 2666 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2667 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 2668 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 2669 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 2670 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2671 | } else if ("month_soft".equals(fake)) { |
| 2672 | plans.add(SubscriptionPlan.Builder |
| 2673 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2674 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 2675 | .setSummary("Crazy unlimited bandwidth plan with incredibly long title " |
| 2676 | + "that should be cut off to prevent UI from looking terrible") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2677 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2678 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2679 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 2680 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2681 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 2682 | plans.add(SubscriptionPlan.Builder |
| 2683 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 2684 | .setTitle("G-Mobile, Throttled after limit") |
| 2685 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2686 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2687 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 2688 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2689 | .build()); |
| 2690 | plans.add(SubscriptionPlan.Builder |
| 2691 | .createRecurringMonthly(ZonedDateTime.parse("2017-03-14T00:00:00.000Z")) |
| 2692 | .setTitle("G-Mobile, No data connection after limit") |
| 2693 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2694 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 2695 | .setDataUsage(5 * TrafficStats.GB_IN_BYTES, |
| 2696 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2697 | .build()); |
| 2698 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2699 | } else if ("month_none".equals(fake)) { |
| 2700 | plans.add(SubscriptionPlan.Builder |
| 2701 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2702 | .setTitle("G-Mobile") |
| 2703 | .build()); |
| 2704 | } else if ("prepaid".equals(fake)) { |
| 2705 | plans.add(SubscriptionPlan.Builder |
| 2706 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 2707 | ZonedDateTime.now().plusDays(10)) |
| 2708 | .setTitle("G-Mobile") |
| 2709 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 2710 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 2711 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 2712 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 2713 | .build()); |
| 2714 | } else if ("prepaid_crazy".equals(fake)) { |
| 2715 | plans.add(SubscriptionPlan.Builder |
| 2716 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 2717 | ZonedDateTime.now().plusDays(10)) |
| 2718 | .setTitle("G-Mobile Anytime") |
| 2719 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 2720 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 2721 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 2722 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 2723 | .build()); |
| 2724 | plans.add(SubscriptionPlan.Builder |
| 2725 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 2726 | ZonedDateTime.now().plusDays(20)) |
| 2727 | .setTitle("G-Mobile Nickel Nights") |
| 2728 | .setSummary("5¢/GB between 1-5AM") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 2729 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2730 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2731 | .setDataUsage(15 * TrafficStats.MB_IN_BYTES, |
| 2732 | ZonedDateTime.now().minusHours(30).toInstant().toEpochMilli()) |
| 2733 | .build()); |
| 2734 | plans.add(SubscriptionPlan.Builder |
| 2735 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 2736 | ZonedDateTime.now().plusDays(20)) |
| 2737 | .setTitle("G-Mobile Bonus 3G") |
| 2738 | .setSummary("Unlimited 3G data") |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 2739 | .setDataLimit(1 * TrafficStats.GB_IN_BYTES, |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2740 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2741 | .setDataUsage(300 * TrafficStats.MB_IN_BYTES, |
| 2742 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2743 | .build()); |
Rajeev Kumar | 4701beb | 2017-07-26 17:03:45 -0700 | [diff] [blame] | 2744 | } else if ("unlimited".equals(fake)) { |
| 2745 | plans.add(SubscriptionPlan.Builder |
| 2746 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 2747 | ZonedDateTime.now().plusDays(10)) |
| 2748 | .setTitle("G-Mobile Awesome") |
| 2749 | .setDataLimit(SubscriptionPlan.BYTES_UNLIMITED, |
| 2750 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2751 | .setDataUsage(50 * TrafficStats.MB_IN_BYTES, |
| 2752 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 2753 | .build()); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2754 | } |
| 2755 | return plans.toArray(new SubscriptionPlan[plans.size()]); |
| 2756 | } |
| 2757 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 2758 | synchronized (mNetworkPoliciesSecondLock) { |
| 2759 | // Only give out plan details to the package that defined them, |
| 2760 | // so that we don't risk leaking plans between apps. We always |
| 2761 | // let in core system components (like the Settings app). |
| 2762 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
| 2763 | if (Objects.equals(ownerPackage, callingPackage) |
| 2764 | || (UserHandle.getCallingAppId() == android.os.Process.SYSTEM_UID)) { |
| 2765 | return mSubscriptionPlans.get(subId); |
| 2766 | } else { |
| 2767 | Log.w(TAG, "Not returning plans because caller " + callingPackage |
| 2768 | + " doesn't match owner " + ownerPackage); |
| 2769 | return null; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2770 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | @Override |
| 2775 | public void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) { |
| 2776 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 2777 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2778 | for (SubscriptionPlan plan : plans) { |
| 2779 | Preconditions.checkNotNull(plan); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | final long token = Binder.clearCallingIdentity(); |
| 2783 | try { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2784 | maybeRefreshTrustedTime(); |
| 2785 | synchronized (mUidRulesFirstLock) { |
| 2786 | synchronized (mNetworkPoliciesSecondLock) { |
| 2787 | mSubscriptionPlans.put(subId, plans); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame] | 2788 | mSubscriptionPlansOwner.put(subId, callingPackage); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2789 | |
| 2790 | final String subscriberId = mContext.getSystemService(TelephonyManager.class) |
| 2791 | .getSubscriberId(subId); |
| 2792 | ensureActiveMobilePolicyAL(subId, subscriberId); |
| 2793 | maybeUpdateMobilePolicyCycleAL(subId); |
| 2794 | handleNetworkPoliciesUpdateAL(true); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2795 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2796 | } |
| 2797 | } finally { |
| 2798 | Binder.restoreCallingIdentity(token); |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2803 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2804 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2805 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2806 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 2807 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2808 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2809 | for (String arg : args) { |
| 2810 | argSet.add(arg); |
| 2811 | } |
| 2812 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2813 | synchronized (mUidRulesFirstLock) { |
| 2814 | synchronized (mNetworkPoliciesSecondLock) { |
| 2815 | if (argSet.contains("--unsnooze")) { |
| 2816 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 2817 | mNetworkPolicy.valueAt(i).clearSnooze(); |
| 2818 | } |
| 2819 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2820 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2821 | |
| 2822 | fout.println("Cleared snooze timestamps"); |
| 2823 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2824 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2825 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2826 | fout.print("System ready: "); fout.println(mSystemReady); |
| 2827 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
| 2828 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
| 2829 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2830 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 2831 | |
| 2832 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2833 | fout.println("Network policies:"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2834 | fout.increaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2835 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2836 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
| 2837 | } |
| 2838 | fout.decreaseIndent(); |
| 2839 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2840 | fout.println(); |
| 2841 | fout.println("Subscription plans:"); |
| 2842 | fout.increaseIndent(); |
| 2843 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 2844 | final int subId = mSubscriptionPlans.keyAt(i); |
| 2845 | fout.println("Subscriber ID " + subId + ":"); |
| 2846 | fout.increaseIndent(); |
| 2847 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 2848 | if (!ArrayUtils.isEmpty(plans)) { |
| 2849 | for (SubscriptionPlan plan : plans) { |
| 2850 | fout.println(plan); |
| 2851 | } |
| 2852 | } |
| 2853 | fout.decreaseIndent(); |
| 2854 | } |
| 2855 | fout.decreaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2856 | |
Jeff Sharkey | 0f2910c | 2017-07-30 16:52:51 -0600 | [diff] [blame] | 2857 | fout.println(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2858 | fout.println("Policy for UIDs:"); |
| 2859 | fout.increaseIndent(); |
| 2860 | int size = mUidPolicy.size(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2861 | for (int i = 0; i < size; i++) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2862 | final int uid = mUidPolicy.keyAt(i); |
| 2863 | final int policy = mUidPolicy.valueAt(i); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2864 | fout.print("UID="); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2865 | fout.print(uid); |
| 2866 | fout.print(" policy="); |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 2867 | fout.print(uidPoliciesToString(policy)); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2868 | fout.println(); |
| 2869 | } |
| 2870 | fout.decreaseIndent(); |
| 2871 | |
| 2872 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 2873 | if (size > 0) { |
| 2874 | fout.println("Power save whitelist (except idle) app ids:"); |
| 2875 | fout.increaseIndent(); |
| 2876 | for (int i = 0; i < size; i++) { |
| 2877 | fout.print("UID="); |
| 2878 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 2879 | fout.print(": "); |
| 2880 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 2881 | fout.println(); |
| 2882 | } |
| 2883 | fout.decreaseIndent(); |
| 2884 | } |
| 2885 | |
| 2886 | size = mPowerSaveWhitelistAppIds.size(); |
| 2887 | if (size > 0) { |
| 2888 | fout.println("Power save whitelist app ids:"); |
| 2889 | fout.increaseIndent(); |
| 2890 | for (int i = 0; i < size; i++) { |
| 2891 | fout.print("UID="); |
| 2892 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 2893 | fout.print(": "); |
| 2894 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 2895 | fout.println(); |
| 2896 | } |
| 2897 | fout.decreaseIndent(); |
| 2898 | } |
| 2899 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2900 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 2901 | if (size > 0) { |
| 2902 | fout.println("Default restrict background whitelist uids:"); |
| 2903 | fout.increaseIndent(); |
| 2904 | for (int i = 0; i < size; i++) { |
| 2905 | fout.print("UID="); |
| 2906 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 2907 | fout.println(); |
| 2908 | } |
| 2909 | fout.decreaseIndent(); |
| 2910 | } |
| 2911 | |
| 2912 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 2913 | if (size > 0) { |
| 2914 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 2915 | fout.increaseIndent(); |
| 2916 | for (int i = 0; i < size; i++) { |
| 2917 | fout.print("UID="); |
| 2918 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 2919 | fout.println(); |
| 2920 | } |
| 2921 | fout.decreaseIndent(); |
| 2922 | } |
| 2923 | |
| 2924 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 2925 | collectKeys(mUidState, knownUids); |
| 2926 | collectKeys(mUidRules, knownUids); |
| 2927 | |
| 2928 | fout.println("Status for all known UIDs:"); |
| 2929 | fout.increaseIndent(); |
| 2930 | size = knownUids.size(); |
| 2931 | for (int i = 0; i < size; i++) { |
| 2932 | final int uid = knownUids.keyAt(i); |
| 2933 | fout.print("UID="); |
| 2934 | fout.print(uid); |
| 2935 | |
| 2936 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2937 | fout.print(" state="); |
| 2938 | fout.print(state); |
| 2939 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 2940 | fout.print(" (fg)"); |
| 2941 | } else { |
| 2942 | fout.print(state <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 2943 | ? " (fg svc)" : " (bg)"); |
| 2944 | } |
| 2945 | |
| 2946 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2947 | fout.print(" rules="); |
| 2948 | fout.print(uidRulesToString(uidRules)); |
| 2949 | fout.println(); |
| 2950 | } |
| 2951 | fout.decreaseIndent(); |
| 2952 | |
| 2953 | fout.println("Status for just UIDs with rules:"); |
| 2954 | fout.increaseIndent(); |
| 2955 | size = mUidRules.size(); |
| 2956 | for (int i = 0; i < size; i++) { |
| 2957 | final int uid = mUidRules.keyAt(i); |
| 2958 | fout.print("UID="); |
| 2959 | fout.print(uid); |
| 2960 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2961 | fout.print(" rules="); |
| 2962 | fout.print(uidRulesToString(uidRules)); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2963 | fout.println(); |
| 2964 | } |
| 2965 | fout.decreaseIndent(); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 2966 | |
| 2967 | fout.println("Observed uid state changes:"); |
| 2968 | fout.increaseIndent(); |
| 2969 | mObservedHistory.dumpUL(fout); |
| 2970 | fout.decreaseIndent(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2971 | } |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2972 | } |
| 2973 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2974 | |
| 2975 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2976 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2977 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 2978 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2979 | this, in, out, err, args, callback, resultReceiver); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | @Override |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2983 | public boolean isUidForeground(int uid) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2984 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2985 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2986 | synchronized (mUidRulesFirstLock) { |
| 2987 | return isUidForegroundUL(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2988 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2991 | private boolean isUidForegroundUL(int uid) { |
| 2992 | return isUidStateForegroundUL( |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2993 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
| 2994 | } |
| 2995 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2996 | private boolean isUidForegroundOnRestrictBackgroundUL(int uid) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2997 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2998 | return isProcStateAllowedWhileOnRestrictBackground(procState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3001 | private boolean isUidForegroundOnRestrictPowerUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3002 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3003 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 3004 | } |
| 3005 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3006 | private boolean isUidStateForegroundUL(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3007 | // only really in foreground when screen is also on |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 3008 | return state <= ActivityManager.PROCESS_STATE_TOP; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3011 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3012 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3013 | * {@link #updateRulesForDataUsageRestrictionsUL(int)} and |
| 3014 | * {@link #updateRulesForPowerRestrictionsUL(int)} |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3015 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3016 | private void updateUidStateUL(int uid, int uidState) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3017 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL"); |
| 3018 | try { |
| 3019 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 3020 | if (oldUidState != uidState) { |
| 3021 | // state changed, push updated rules |
| 3022 | mUidState.put(uid, uidState); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3023 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState); |
| 3024 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 3025 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
Sudheer Shanka | 9e77d23 | 2017-08-14 14:43:11 -0700 | [diff] [blame] | 3026 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3027 | if (mDeviceIdleMode) { |
| 3028 | updateRuleForDeviceIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3029 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3030 | if (mRestrictPower) { |
| 3031 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3032 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3033 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3034 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3035 | updateNetworkStats(uid, isUidStateForegroundUL(uidState)); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3036 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3037 | } finally { |
| 3038 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3042 | private void removeUidStateUL(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3043 | final int index = mUidState.indexOfKey(uid); |
| 3044 | if (index >= 0) { |
| 3045 | final int oldUidState = mUidState.valueAt(index); |
| 3046 | mUidState.removeAt(index); |
| 3047 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3048 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3049 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3050 | if (mDeviceIdleMode) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3051 | updateRuleForDeviceIdleUL(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3052 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3053 | if (mRestrictPower) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3054 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3055 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3056 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3057 | updateNetworkStats(uid, false); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3058 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3059 | } |
| 3060 | } |
| 3061 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3062 | // adjust stats accounting based on foreground status |
| 3063 | private void updateNetworkStats(int uid, boolean uidForeground) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3064 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3065 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3066 | "updateNetworkStats: " + uid + "/" + (uidForeground ? "F" : "B")); |
| 3067 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3068 | try { |
| 3069 | mNetworkStats.setUidForeground(uid, uidForeground); |
| 3070 | } catch (RemoteException e) { |
| 3071 | // ignored; service lives in system_server |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3072 | } finally { |
| 3073 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3077 | private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState, |
| 3078 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3079 | final boolean oldForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3080 | isProcStateAllowedWhileOnRestrictBackground(oldUidState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3081 | final boolean newForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3082 | isProcStateAllowedWhileOnRestrictBackground(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3083 | if (oldForeground != newForeground) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3084 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3085 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3088 | void updateRulesForPowerSaveUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3089 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL"); |
| 3090 | try { |
| 3091 | updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 3092 | mUidFirewallPowerSaveRules); |
| 3093 | } finally { |
| 3094 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3095 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3096 | } |
| 3097 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3098 | void updateRuleForRestrictPowerUL(int uid) { |
| 3099 | updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3102 | void updateRulesForDeviceIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3103 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL"); |
| 3104 | try { |
| 3105 | updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 3106 | mUidFirewallDozableRules); |
| 3107 | } finally { |
| 3108 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3109 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3112 | void updateRuleForDeviceIdleUL(int uid) { |
| 3113 | updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3114 | } |
| 3115 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3116 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3117 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3118 | private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain, |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3119 | SparseIntArray rules) { |
| 3120 | if (enabled) { |
| 3121 | // 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] | 3122 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3123 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3124 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3125 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3126 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3127 | UserInfo user = users.get(ui); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3128 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveTempWhitelistAppIds, user.id); |
| 3129 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveWhitelistAppIds, user.id); |
| 3130 | if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3131 | updateRulesForWhitelistedAppIds(uidRules, |
| 3132 | mPowerSaveWhitelistExceptIdleAppIds, user.id); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3133 | } |
| 3134 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3135 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3136 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3137 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 3138 | } |
| 3139 | } |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3140 | setUidFirewallRulesUL(chain, uidRules, CHAIN_TOGGLE_ENABLE); |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3141 | } else { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3142 | setUidFirewallRulesUL(chain, null, CHAIN_TOGGLE_DISABLE); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3143 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3146 | private void updateRulesForWhitelistedAppIds(final SparseIntArray uidRules, |
| 3147 | final SparseBooleanArray whitelistedAppIds, int userId) { |
| 3148 | for (int i = whitelistedAppIds.size() - 1; i >= 0; --i) { |
| 3149 | if (whitelistedAppIds.valueAt(i)) { |
| 3150 | final int appId = whitelistedAppIds.keyAt(i); |
| 3151 | final int uid = UserHandle.getUid(userId, appId); |
| 3152 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /** |
| 3158 | * @param deviceIdleMode if true then we don't consider |
| 3159 | * {@link #mPowerSaveWhitelistExceptIdleAppIds} for checking if the {@param uid} is |
| 3160 | * whitelisted. |
| 3161 | */ |
| 3162 | private boolean isWhitelistedBatterySaverUL(int uid, boolean deviceIdleMode) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3163 | final int appId = UserHandle.getAppId(uid); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3164 | boolean isWhitelisted = mPowerSaveTempWhitelistAppIds.get(appId) |
| 3165 | || mPowerSaveWhitelistAppIds.get(appId); |
| 3166 | if (!deviceIdleMode) { |
| 3167 | isWhitelisted = isWhitelisted || mPowerSaveWhitelistExceptIdleAppIds.get(appId); |
| 3168 | } |
| 3169 | return isWhitelisted; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3172 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3173 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3174 | private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3175 | if (enabled) { |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3176 | final boolean isWhitelisted = isWhitelistedBatterySaverUL(uid, |
| 3177 | chain == FIREWALL_CHAIN_DOZABLE); |
| 3178 | if (isWhitelisted || isUidForegroundOnRestrictPowerUL(uid)) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3179 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3180 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3181 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3186 | void updateRulesForAppIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3187 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL"); |
| 3188 | try { |
| 3189 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 3190 | uidRules.clear(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3191 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3192 | // Fully update the app idle firewall chain. |
| 3193 | final List<UserInfo> users = mUserManager.getUsers(); |
| 3194 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3195 | UserInfo user = users.get(ui); |
| 3196 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 3197 | for (int uid : idleUids) { |
| 3198 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
| 3199 | // quick check: if this uid doesn't have INTERNET permission, it |
| 3200 | // doesn't have network access anyway, so it is a waste to mess |
| 3201 | // with it here. |
| 3202 | if (hasInternetPermissions(uid)) { |
| 3203 | uidRules.put(uid, FIREWALL_RULE_DENY); |
| 3204 | } |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 3205 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3206 | } |
| 3207 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3208 | |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 3209 | setUidFirewallRulesUL(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3210 | } finally { |
| 3211 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3212 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3215 | void updateRuleForAppIdleUL(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3216 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3217 | |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3218 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3219 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRuleForAppIdleUL: " + uid ); |
| 3220 | } |
| 3221 | try { |
| 3222 | int appId = UserHandle.getAppId(uid); |
| 3223 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 3224 | && !isUidForegroundOnRestrictPowerUL(uid)) { |
| 3225 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
| 3226 | } else { |
| 3227 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 3228 | } |
| 3229 | } finally { |
| 3230 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3234 | /** |
| 3235 | * Toggle the firewall standby chain and inform listeners if the uid rules have effectively |
| 3236 | * changed. |
| 3237 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3238 | void updateRulesForAppIdleParoleUL() { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3239 | boolean paroled = mUsageStats.isAppIdleParoleOn(); |
| 3240 | boolean enableChain = !paroled; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3241 | enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, enableChain); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3242 | |
| 3243 | int ruleCount = mUidFirewallStandbyRules.size(); |
| 3244 | for (int i = 0; i < ruleCount; i++) { |
| 3245 | int uid = mUidFirewallStandbyRules.keyAt(i); |
| 3246 | int oldRules = mUidRules.get(uid); |
| 3247 | if (enableChain) { |
| 3248 | // Chain wasn't enabled before and the other power-related |
| 3249 | // chains are whitelists, so we can clear the |
| 3250 | // MASK_ALL_NETWORKS part of the rules and re-inform listeners if |
| 3251 | // the effective rules result in blocking network access. |
| 3252 | oldRules &= MASK_METERED_NETWORKS; |
| 3253 | } else { |
| 3254 | // Skip if it had no restrictions to begin with |
| 3255 | if ((oldRules & MASK_ALL_NETWORKS) == 0) continue; |
| 3256 | } |
Sudheer Shanka | 3af0294 | 2017-04-12 14:29:14 -0700 | [diff] [blame] | 3257 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldRules, paroled); |
| 3258 | if (newUidRules == RULE_NONE) { |
| 3259 | mUidRules.delete(uid); |
| 3260 | } else { |
| 3261 | mUidRules.put(uid, newUidRules); |
| 3262 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3263 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3266 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3267 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 3268 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3269 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3270 | private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3271 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3272 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3273 | "updateRulesForGlobalChangeAL: " + (restrictedNetworksChanged ? "R" : "-")); |
| 3274 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3275 | try { |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3276 | updateRulesForAppIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3277 | updateRulesForRestrictPowerUL(); |
| 3278 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3279 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3280 | // If the set of restricted networks may have changed, re-evaluate those. |
| 3281 | if (restrictedNetworksChanged) { |
| 3282 | normalizePoliciesNL(); |
| 3283 | updateNetworkRulesNL(); |
| 3284 | } |
| 3285 | } finally { |
| 3286 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3290 | // 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] | 3291 | private void updateRulesForRestrictPowerUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3292 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL"); |
| 3293 | try { |
| 3294 | updateRulesForDeviceIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3295 | updateRulesForPowerSaveUL(); |
| 3296 | updateRulesForAllAppsUL(TYPE_RESTRICT_POWER); |
| 3297 | } finally { |
| 3298 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3299 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3302 | private void updateRulesForRestrictBackgroundUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3303 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL"); |
| 3304 | try { |
| 3305 | updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND); |
| 3306 | } finally { |
| 3307 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3308 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 3312 | private static final int TYPE_RESTRICT_POWER = 2; |
| 3313 | @Retention(RetentionPolicy.SOURCE) |
| 3314 | @IntDef(flag = false, value = { |
| 3315 | TYPE_RESTRICT_BACKGROUND, |
| 3316 | TYPE_RESTRICT_POWER, |
| 3317 | }) |
| 3318 | public @interface RestrictType { |
| 3319 | } |
| 3320 | |
| 3321 | // 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] | 3322 | private void updateRulesForAllAppsUL(@RestrictType int type) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3323 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3324 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type); |
| 3325 | } |
| 3326 | try { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3327 | // update rules for all installed applications |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3328 | |
| 3329 | final PackageManager pm = mContext.getPackageManager(); |
| 3330 | final List<UserInfo> users; |
| 3331 | final List<ApplicationInfo> apps; |
| 3332 | |
| 3333 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-users"); |
| 3334 | try { |
| 3335 | users = mUserManager.getUsers(); |
| 3336 | } finally { |
| 3337 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3338 | } |
| 3339 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-uids"); |
| 3340 | try { |
| 3341 | apps = pm.getInstalledApplications( |
| 3342 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS |
| 3343 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 3344 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 3345 | } finally { |
| 3346 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3347 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3348 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3349 | final int usersSize = users.size(); |
| 3350 | final int appsSize = apps.size(); |
| 3351 | for (int i = 0; i < usersSize; i++) { |
| 3352 | final UserInfo user = users.get(i); |
| 3353 | for (int j = 0; j < appsSize; j++) { |
| 3354 | final ApplicationInfo app = apps.get(j); |
| 3355 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 3356 | switch (type) { |
| 3357 | case TYPE_RESTRICT_BACKGROUND: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3358 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3359 | break; |
| 3360 | case TYPE_RESTRICT_POWER: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3361 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3362 | break; |
| 3363 | default: |
| 3364 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 3365 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3366 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3367 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3368 | } finally { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3369 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3370 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3373 | private void updateRulesForTempWhitelistChangeUL(int appId) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3374 | final List<UserInfo> users = mUserManager.getUsers(); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3375 | final int numUsers = users.size(); |
| 3376 | for (int i = 0; i < numUsers; i++) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3377 | final UserInfo user = users.get(i); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 3378 | int uid = UserHandle.getUid(user.id, appId); |
| 3379 | // Update external firewall rules. |
| 3380 | updateRuleForAppIdleUL(uid); |
| 3381 | updateRuleForDeviceIdleUL(uid); |
| 3382 | updateRuleForRestrictPowerUL(uid); |
| 3383 | // Update internal rules. |
| 3384 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3385 | } |
| 3386 | } |
| 3387 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3388 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 3389 | // methods below could be merged into a isUidValidForRules() method. |
| 3390 | private boolean isUidValidForBlacklistRules(int uid) { |
| 3391 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3392 | 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] | 3393 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3394 | return true; |
| 3395 | } |
| 3396 | |
| 3397 | return false; |
| 3398 | } |
| 3399 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3400 | private boolean isUidValidForWhitelistRules(int uid) { |
| 3401 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 3402 | } |
| 3403 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3404 | private boolean isUidIdle(int uid) { |
| 3405 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 3406 | final int userId = UserHandle.getUserId(uid); |
| 3407 | |
songjinshi | 0655edd | 2016-05-18 19:55:32 +0800 | [diff] [blame] | 3408 | if (packages != null) { |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 3409 | for (String packageName : packages) { |
| 3410 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 3411 | return false; |
| 3412 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | return true; |
| 3416 | } |
| 3417 | |
| 3418 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3419 | * Checks if an uid has INTERNET permissions. |
| 3420 | * <p> |
| 3421 | * 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] | 3422 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3423 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3424 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 3425 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3426 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3427 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3428 | } |
| 3429 | } catch (RemoteException e) { |
| 3430 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3431 | return true; |
| 3432 | } |
| 3433 | |
| 3434 | /** |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3435 | * Clears all state - internal and external - associated with an UID. |
| 3436 | */ |
| 3437 | private void onUidDeletedUL(int uid) { |
| 3438 | // First cleanup in-memory state synchronously... |
| 3439 | mUidRules.delete(uid); |
| 3440 | mUidPolicy.delete(uid); |
| 3441 | mUidFirewallStandbyRules.delete(uid); |
| 3442 | mUidFirewallDozableRules.delete(uid); |
| 3443 | mUidFirewallPowerSaveRules.delete(uid); |
| 3444 | mPowerSaveWhitelistExceptIdleAppIds.delete(uid); |
| 3445 | mPowerSaveWhitelistAppIds.delete(uid); |
| 3446 | mPowerSaveTempWhitelistAppIds.delete(uid); |
| 3447 | |
| 3448 | // ...then update iptables asynchronously. |
| 3449 | mHandler.obtainMessage(MSG_RESET_FIREWALL_RULES_BY_UID, uid, 0).sendToTarget(); |
| 3450 | } |
| 3451 | |
| 3452 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3453 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3454 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3455 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3456 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3457 | * <li>Doze mode |
| 3458 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3459 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3460 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3461 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3462 | * |
| 3463 | * <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] | 3464 | */ |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3465 | private void updateRestrictionRulesForUidUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3466 | // Methods below only changes the firewall rules for the power-related modes. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3467 | updateRuleForDeviceIdleUL(uid); |
| 3468 | updateRuleForAppIdleUL(uid); |
| 3469 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3470 | |
| 3471 | // Update internal state for power-related modes. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3472 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3473 | |
| 3474 | // Update firewall and internal rules for Data Saver Mode. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3475 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3476 | } |
| 3477 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3478 | /** |
| 3479 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 3480 | * restrictions (blacklist / whitelist). |
| 3481 | * |
| 3482 | * <p> |
| 3483 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 3484 | * networks: |
| 3485 | * <ul> |
| 3486 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 3487 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 3488 | * also blacklisted. |
| 3489 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
| 3490 | * no UIDs other those whitelisted will have access. |
| 3491 | * <ul> |
| 3492 | * |
| 3493 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 3494 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 3495 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 3496 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3497 | * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3498 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 3499 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 3500 | * following rules should also be applied: |
| 3501 | * |
| 3502 | * <ul> |
| 3503 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 3504 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 3505 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 3506 | * {@code bw_penalty_box}. |
| 3507 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 3508 | * </ul> |
| 3509 | * |
| 3510 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 3511 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 3512 | * have permission to use the internet. |
| 3513 | * |
| 3514 | * <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] | 3515 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3516 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3517 | private void updateRulesForDataUsageRestrictionsUL(int uid) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3518 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3519 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3520 | "updateRulesForDataUsageRestrictionsUL: " + uid); |
| 3521 | } |
| 3522 | try { |
| 3523 | updateRulesForDataUsageRestrictionsULInner(uid); |
| 3524 | } finally { |
| 3525 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | private void updateRulesForDataUsageRestrictionsULInner(int uid) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3530 | if (!isUidValidForWhitelistRules(uid)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3531 | if (LOGD) Slog.d(TAG, "no need to update restrict data rules for uid " + uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3532 | return; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3533 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3534 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3535 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3536 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3537 | final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3538 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3539 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 3540 | final boolean isWhitelisted = (uidPolicy & POLICY_ALLOW_METERED_BACKGROUND) != 0; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3541 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 3542 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3543 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3544 | // First step: define the new rule based on user restrictions and foreground state. |
| 3545 | if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3546 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 3547 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 3548 | } else if (isWhitelisted) { |
| 3549 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3550 | } |
| 3551 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3552 | if (isBlacklisted) { |
| 3553 | newRule = RULE_REJECT_METERED; |
| 3554 | } else if (mRestrictBackground && isWhitelisted) { |
| 3555 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3556 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3557 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3558 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3559 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3560 | if (LOGV) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3561 | Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3562 | + ": isForeground=" +isForeground |
| 3563 | + ", isBlacklisted=" + isBlacklisted |
| 3564 | + ", isWhitelisted=" + isWhitelisted |
| 3565 | + ", oldRule=" + uidRulesToString(oldRule) |
| 3566 | + ", newRule=" + uidRulesToString(newRule) |
| 3567 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 3568 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3569 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3570 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3571 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 3572 | mUidRules.delete(uid); |
| 3573 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3574 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 3575 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3576 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3577 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3578 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3579 | if (hasRule(newRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3580 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 3581 | // (since bw_penalty_box prevails over bw_happy_box). |
| 3582 | |
| 3583 | setMeteredNetworkWhitelist(uid, true); |
| 3584 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 3585 | // but ideally it should be just: |
| 3586 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3587 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3588 | setMeteredNetworkBlacklist(uid, false); |
| 3589 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3590 | } else if (hasRule(oldRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3591 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 3592 | |
| 3593 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 3594 | // but ideally they should be just: |
| 3595 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 3596 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3597 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3598 | setMeteredNetworkWhitelist(uid, false); |
| 3599 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3600 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3601 | setMeteredNetworkBlacklist(uid, true); |
| 3602 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3603 | } else if (hasRule(newRule, RULE_REJECT_METERED) |
| 3604 | || hasRule(oldRule, RULE_REJECT_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3605 | // Flip state because app was explicitly added or removed to blacklist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3606 | setMeteredNetworkBlacklist(uid, isBlacklisted); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3607 | if (hasRule(oldRule, RULE_REJECT_METERED) && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3608 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 3609 | // where app is whitelisted and blacklisted at the same time (although such |
| 3610 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3611 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3612 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3613 | } else if (hasRule(newRule, RULE_ALLOW_METERED) |
| 3614 | || hasRule(oldRule, RULE_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3615 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3616 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3617 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3618 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3619 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 3620 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3621 | + ", whitelisted=" + isWhitelisted |
| 3622 | + ", blacklisted=" + isBlacklisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3623 | + ", newRule=" + uidRulesToString(newUidRules) |
| 3624 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3625 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3626 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3627 | // Dispatch changed rule to existing listeners. |
| 3628 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | /** |
| 3633 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 3634 | * listeners in case of change. |
| 3635 | * <p> |
| 3636 | * There are 3 power-related rules that affects whether an app has background access on |
| 3637 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 3638 | * restriction, it's added to the equivalent firewall chain: |
| 3639 | * <ul> |
| 3640 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 3641 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 3642 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 3643 | * </ul> |
| 3644 | * <p> |
| 3645 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 3646 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 3647 | * <p> |
| 3648 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 3649 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3650 | private void updateRulesForPowerRestrictionsUL(int uid) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3651 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 3652 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3653 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules, false); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3654 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3655 | if (newUidRules == RULE_NONE) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3656 | mUidRules.delete(uid); |
| 3657 | } else { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3658 | mUidRules.put(uid, newUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * Similar to above but ignores idle state if app standby is currently disabled by parole. |
| 3664 | * |
| 3665 | * @param uid the uid of the app to update rules for |
| 3666 | * @param oldUidRules the current rules for the uid, in order to determine if there's a change |
| 3667 | * @param paroled whether to ignore idle state of apps and only look at other restrictions. |
| 3668 | * |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3669 | * @return the new computed rules for the uid |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3670 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3671 | private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules, boolean paroled) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3672 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3673 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3674 | "updateRulesForPowerRestrictionsUL: " + uid + "/" + oldUidRules + "/" |
| 3675 | + (paroled ? "P" : "-")); |
| 3676 | } |
| 3677 | try { |
| 3678 | return updateRulesForPowerRestrictionsULInner(uid, oldUidRules, paroled); |
| 3679 | } finally { |
| 3680 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | private int updateRulesForPowerRestrictionsULInner(int uid, int oldUidRules, boolean paroled) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3685 | if (!isUidValidForBlacklistRules(uid)) { |
| 3686 | if (LOGD) Slog.d(TAG, "no need to update restrict power rules for uid " + uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3687 | return RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3688 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3689 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3690 | final boolean isIdle = !paroled && isUidIdle(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3691 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3692 | final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3693 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3694 | final boolean isWhitelisted = isWhitelistedBatterySaverUL(uid, mDeviceIdleMode); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3695 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 3696 | int newRule = RULE_NONE; |
| 3697 | |
| 3698 | // First step: define the new rule based on user restrictions and foreground state. |
| 3699 | |
| 3700 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 3701 | // by considering the foreground and non-foreground states. |
| 3702 | if (isForeground) { |
| 3703 | if (restrictMode) { |
| 3704 | newRule = RULE_ALLOW_ALL; |
| 3705 | } |
| 3706 | } else if (restrictMode) { |
| 3707 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 3708 | } |
| 3709 | |
| 3710 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 3711 | |
| 3712 | if (LOGV) { |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 3713 | Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3714 | + ", isIdle: " + isIdle |
| 3715 | + ", mRestrictPower: " + mRestrictPower |
| 3716 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 3717 | + ", isForeground=" + isForeground |
| 3718 | + ", isWhitelisted=" + isWhitelisted |
| 3719 | + ", oldRule=" + uidRulesToString(oldRule) |
| 3720 | + ", newRule=" + uidRulesToString(newRule) |
| 3721 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 3722 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 3723 | } |
| 3724 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3725 | // Second step: notify listeners if state changed. |
| 3726 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3727 | if (newRule == RULE_NONE || hasRule(newRule, RULE_ALLOW_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3728 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3729 | } else if (hasRule(newRule, RULE_REJECT_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3730 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 3731 | } else { |
| 3732 | // All scenarios should have been covered above |
| 3733 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 3734 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3735 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3736 | + ", newRule=" + uidRulesToString(newUidRules) |
| 3737 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 3738 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3739 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3740 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3741 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3742 | return newUidRules; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3745 | private class AppIdleStateChangeListener |
| 3746 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 3747 | |
| 3748 | @Override |
| 3749 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle) { |
| 3750 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 3751 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 3752 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3753 | if (LOGV) Log.v(TAG, "onAppIdleStateChanged(): uid=" + uid + ", idle=" + idle); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3754 | synchronized (mUidRulesFirstLock) { |
| 3755 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3756 | updateRulesForPowerRestrictionsUL(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3757 | } |
| 3758 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3759 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | @Override |
| 3763 | public void onParoleStateChanged(boolean isParoleOn) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3764 | synchronized (mUidRulesFirstLock) { |
| 3765 | updateRulesForAppIdleParoleUL(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3766 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3770 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 3771 | if (listener != null) { |
| 3772 | try { |
| 3773 | listener.onUidRulesChanged(uid, uidRules); |
| 3774 | } catch (RemoteException ignored) { |
| 3775 | } |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 3780 | String[] meteredIfaces) { |
| 3781 | if (listener != null) { |
| 3782 | try { |
| 3783 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 3784 | } catch (RemoteException ignored) { |
| 3785 | } |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 3790 | boolean restrictBackground) { |
| 3791 | if (listener != null) { |
| 3792 | try { |
| 3793 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 3794 | } catch (RemoteException ignored) { |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3799 | private void dispatchUidPoliciesChanged(INetworkPolicyListener listener, int uid, |
| 3800 | int uidPolicies) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3801 | if (listener != null) { |
| 3802 | try { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3803 | listener.onUidPoliciesChanged(uid, uidPolicies); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3804 | } catch (RemoteException ignored) { |
| 3805 | } |
| 3806 | } |
| 3807 | } |
| 3808 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3809 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 3810 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3811 | public boolean handleMessage(Message msg) { |
| 3812 | switch (msg.what) { |
| 3813 | case MSG_RULES_CHANGED: { |
| 3814 | final int uid = msg.arg1; |
| 3815 | final int uidRules = msg.arg2; |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3816 | final int length = mListeners.beginBroadcast(); |
| 3817 | for (int i = 0; i < length; i++) { |
| 3818 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 3819 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3820 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3821 | mListeners.finishBroadcast(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3822 | return true; |
| 3823 | } |
| 3824 | case MSG_METERED_IFACES_CHANGED: { |
| 3825 | final String[] meteredIfaces = (String[]) msg.obj; |
| 3826 | final int length = mListeners.beginBroadcast(); |
| 3827 | for (int i = 0; i < length; i++) { |
| 3828 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3829 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3830 | } |
| 3831 | mListeners.finishBroadcast(); |
| 3832 | return true; |
| 3833 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3834 | case MSG_LIMIT_REACHED: { |
| 3835 | final String iface = (String) msg.obj; |
| 3836 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3837 | maybeRefreshTrustedTime(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3838 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3839 | if (mMeteredIfaces.contains(iface)) { |
| 3840 | try { |
| 3841 | // force stats update to make sure we have |
| 3842 | // numbers that caused alert to trigger. |
| 3843 | mNetworkStats.forceUpdate(); |
| 3844 | } catch (RemoteException e) { |
| 3845 | // ignored; service lives in system_server |
| 3846 | } |
| 3847 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3848 | updateNetworkEnabledNL(); |
| 3849 | updateNotificationsNL(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3850 | } |
| 3851 | } |
| 3852 | return true; |
| 3853 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3854 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 3855 | final boolean restrictBackground = msg.arg1 != 0; |
| 3856 | final int length = mListeners.beginBroadcast(); |
| 3857 | for (int i = 0; i < length; i++) { |
| 3858 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3859 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3860 | } |
| 3861 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3862 | final Intent intent = |
| 3863 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3864 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3865 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 3866 | return true; |
| 3867 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3868 | case MSG_POLICIES_CHANGED: { |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3869 | final int uid = msg.arg1; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3870 | final int policy = msg.arg2; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3871 | final Boolean notifyApp = (Boolean) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3872 | // First notify internal listeners... |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3873 | final int length = mListeners.beginBroadcast(); |
| 3874 | for (int i = 0; i < length; i++) { |
| 3875 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3876 | dispatchUidPoliciesChanged(listener, uid, policy); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3877 | } |
| 3878 | mListeners.finishBroadcast(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3879 | // ...then apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
| 3880 | if (notifyApp.booleanValue()) { |
| 3881 | broadcastRestrictBackgroundChanged(uid, notifyApp); |
| 3882 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3883 | return true; |
| 3884 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 3885 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 3886 | final long lowestRule = (Long) msg.obj; |
| 3887 | try { |
| 3888 | // make sure stats are recorded frequently enough; we aim |
| 3889 | // for 2MB threshold for 2GB/month rules. |
| 3890 | final long persistThreshold = lowestRule / 1000; |
| 3891 | mNetworkStats.advisePersistThreshold(persistThreshold); |
| 3892 | } catch (RemoteException e) { |
| 3893 | // ignored; service lives in system_server |
| 3894 | } |
| 3895 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3896 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 3897 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 3898 | removeInterfaceQuota((String) msg.obj); |
| 3899 | // int params need to be stitched back into a long |
| 3900 | setInterfaceQuota((String) msg.obj, |
| 3901 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 3902 | return true; |
| 3903 | } |
| 3904 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 3905 | removeInterfaceQuota((String) msg.obj); |
| 3906 | return true; |
| 3907 | } |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3908 | case MSG_RESET_FIREWALL_RULES_BY_UID: { |
| 3909 | resetUidFirewallRules(msg.arg1); |
| 3910 | return true; |
| 3911 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3912 | default: { |
| 3913 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3917 | }; |
| 3918 | |
| 3919 | private final Handler.Callback mUidEventHandlerCallback = new Handler.Callback() { |
| 3920 | @Override |
| 3921 | public boolean handleMessage(Message msg) { |
| 3922 | switch (msg.what) { |
| 3923 | case UID_MSG_STATE_CHANGED: { |
| 3924 | final int uid = msg.arg1; |
| 3925 | final int procState = msg.arg2; |
| 3926 | final long procStateSeq = (Long) msg.obj; |
| 3927 | |
| 3928 | handleUidChanged(uid, procState, procStateSeq); |
| 3929 | return true; |
| 3930 | } |
| 3931 | case UID_MSG_GONE: { |
| 3932 | final int uid = msg.arg1; |
| 3933 | handleUidGone(uid); |
| 3934 | return true; |
| 3935 | } |
| 3936 | default: { |
| 3937 | return false; |
| 3938 | } |
| 3939 | } |
| 3940 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3941 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3942 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 3943 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3944 | void handleUidChanged(int uid, int procState, long procStateSeq) { |
| 3945 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged"); |
| 3946 | try { |
| 3947 | synchronized (mUidRulesFirstLock) { |
| 3948 | // We received a uid state change callback, add it to the history so that it |
| 3949 | // will be useful for debugging. |
| 3950 | mObservedHistory.addProcStateSeqUL(uid, procStateSeq); |
| 3951 | // Now update the network policy rules as per the updated uid state. |
| 3952 | updateUidStateUL(uid, procState); |
| 3953 | // Updating the network rules is done, so notify AMS about this. |
| 3954 | mActivityManagerInternal.notifyNetworkPolicyRulesUpdated(uid, procStateSeq); |
| 3955 | } |
| 3956 | } finally { |
| 3957 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | void handleUidGone(int uid) { |
| 3962 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidGone"); |
| 3963 | try { |
| 3964 | synchronized (mUidRulesFirstLock) { |
| 3965 | removeUidStateUL(uid); |
| 3966 | } |
| 3967 | } finally { |
| 3968 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3969 | } |
| 3970 | } |
| 3971 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3972 | private void broadcastRestrictBackgroundChanged(int uid, Boolean changed) { |
| 3973 | final PackageManager pm = mContext.getPackageManager(); |
| 3974 | final String[] packages = pm.getPackagesForUid(uid); |
| 3975 | if (packages != null) { |
| 3976 | final int userId = UserHandle.getUserId(uid); |
| 3977 | for (String packageName : packages) { |
| 3978 | final Intent intent = |
| 3979 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3980 | intent.setPackage(packageName); |
| 3981 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3982 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 3983 | } |
| 3984 | } |
| 3985 | } |
| 3986 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 3987 | private void setInterfaceQuotaAsync(String iface, long quotaBytes) { |
| 3988 | // long quotaBytes split up into two ints to fit in message |
| 3989 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, (int) (quotaBytes >> 32), |
| 3990 | (int) (quotaBytes & 0xFFFFFFFF), iface).sendToTarget(); |
| 3991 | } |
| 3992 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3993 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3994 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3995 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3996 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3997 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3998 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3999 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4000 | } |
| 4001 | } |
| 4002 | |
Jeff Sharkey | 64c96ec | 2017-08-30 16:28:26 -0600 | [diff] [blame] | 4003 | private void removeInterfaceQuotaAsync(String iface) { |
| 4004 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface).sendToTarget(); |
| 4005 | } |
| 4006 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4007 | private void removeInterfaceQuota(String iface) { |
| 4008 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4009 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4010 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4011 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4012 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4013 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4017 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 4018 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4019 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4020 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4021 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 4022 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 4023 | } catch (RemoteException e) { |
| 4024 | // ignored; service lives in system_server |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 4029 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 4030 | try { |
| 4031 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 4032 | } catch (IllegalStateException e) { |
| 4033 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4034 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4035 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 4036 | } |
| 4037 | } |
| 4038 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4039 | private static final int CHAIN_TOGGLE_NONE = 0; |
| 4040 | private static final int CHAIN_TOGGLE_ENABLE = 1; |
| 4041 | private static final int CHAIN_TOGGLE_DISABLE = 2; |
| 4042 | @Retention(RetentionPolicy.SOURCE) |
| 4043 | @IntDef(flag = false, value = { |
| 4044 | CHAIN_TOGGLE_NONE, |
| 4045 | CHAIN_TOGGLE_ENABLE, |
| 4046 | CHAIN_TOGGLE_DISABLE |
| 4047 | }) |
| 4048 | public @interface ChainToggleType { |
| 4049 | } |
| 4050 | |
| 4051 | /** |
| 4052 | * Calls {@link #setUidFirewallRules(int, SparseIntArray)} and |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4053 | * {@link #enableFirewallChainUL(int, boolean)} synchronously. |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4054 | * |
| 4055 | * @param chain firewall chain. |
| 4056 | * @param uidRules new UID rules; if {@code null}, only toggles chain state. |
| 4057 | * @param toggle whether the chain should be enabled, disabled, or not changed. |
| 4058 | */ |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4059 | private void setUidFirewallRulesUL(int chain, @Nullable SparseIntArray uidRules, |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4060 | @ChainToggleType int toggle) { |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4061 | if (uidRules != null) { |
| 4062 | setUidFirewallRulesUL(chain, uidRules); |
| 4063 | } |
| 4064 | if (toggle != CHAIN_TOGGLE_NONE) { |
| 4065 | enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE); |
| 4066 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4069 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4070 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 4071 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 4072 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4073 | */ |
Sudheer Shanka | addebcc | 2017-10-03 09:43:20 -0700 | [diff] [blame] | 4074 | private void setUidFirewallRulesUL(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4075 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4076 | int size = uidRules.size(); |
| 4077 | int[] uids = new int[size]; |
| 4078 | int[] rules = new int[size]; |
| 4079 | for(int index = size - 1; index >= 0; --index) { |
| 4080 | uids[index] = uidRules.keyAt(index); |
| 4081 | rules[index] = uidRules.valueAt(index); |
| 4082 | } |
| 4083 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4084 | } catch (IllegalStateException e) { |
| 4085 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4086 | } catch (RemoteException e) { |
| 4087 | // ignored; service lives in system_server |
| 4088 | } |
| 4089 | } |
| 4090 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4091 | /** |
| 4092 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4093 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4094 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4095 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4096 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4097 | "setUidFirewallRule: " + chain + "/" + uid + "/" + rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 4098 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4099 | try { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4100 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 4101 | mUidFirewallDozableRules.put(uid, rule); |
| 4102 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 4103 | mUidFirewallStandbyRules.put(uid, rule); |
| 4104 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 4105 | mUidFirewallPowerSaveRules.put(uid, rule); |
| 4106 | } |
| 4107 | |
| 4108 | try { |
| 4109 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
| 4110 | } catch (IllegalStateException e) { |
| 4111 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4112 | } catch (RemoteException e) { |
| 4113 | // ignored; service lives in system_server |
| 4114 | } |
| 4115 | } finally { |
| 4116 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
| 4120 | /** |
| 4121 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4122 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4123 | private void enableFirewallChainUL(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4124 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 4125 | mFirewallChainStates.get(chain) == enable) { |
| 4126 | // All is the same, nothing to do. |
| 4127 | return; |
| 4128 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4129 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4130 | try { |
| 4131 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
| 4132 | } catch (IllegalStateException e) { |
| 4133 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 4134 | } catch (RemoteException e) { |
| 4135 | // ignored; service lives in system_server |
| 4136 | } |
| 4137 | } |
| 4138 | |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4139 | /** |
| 4140 | * Resets all firewall rules associated with an UID. |
| 4141 | */ |
| 4142 | private void resetUidFirewallRules(int uid) { |
| 4143 | try { |
| 4144 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_DOZABLE, uid, FIREWALL_RULE_DEFAULT); |
| 4145 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 4146 | mNetworkManager |
| 4147 | .setFirewallUidRule(FIREWALL_CHAIN_POWERSAVE, uid, FIREWALL_RULE_DEFAULT); |
| 4148 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, false); |
| 4149 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, false); |
| 4150 | } catch (IllegalStateException e) { |
| 4151 | Log.wtf(TAG, "problem resetting firewall uid rules for " + uid, e); |
| 4152 | } catch (RemoteException e) { |
| 4153 | // ignored; service lives in system_server |
| 4154 | } |
| 4155 | } |
| 4156 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4157 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
| 4158 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 4159 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4160 | } catch (RuntimeException e) { |
| 4161 | Slog.w(TAG, "problem reading network stats: " + e); |
| 4162 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4163 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4164 | // ignored; service lives in system_server |
| 4165 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4166 | } |
| 4167 | } |
| 4168 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 4169 | private boolean isBandwidthControlEnabled() { |
| 4170 | final long token = Binder.clearCallingIdentity(); |
| 4171 | try { |
| 4172 | return mNetworkManager.isBandwidthControlEnabled(); |
| 4173 | } catch (RemoteException e) { |
| 4174 | // ignored; service lives in system_server |
| 4175 | return false; |
| 4176 | } finally { |
| 4177 | Binder.restoreCallingIdentity(token); |
| 4178 | } |
| 4179 | } |
| 4180 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4181 | /** |
| 4182 | * Try refreshing {@link #mTime} when stale. |
| 4183 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 4184 | void maybeRefreshTrustedTime() { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4185 | if (mTime.getCacheAge() > TIME_CACHE_MAX_AGE) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4186 | mTime.forceRefresh(); |
| 4187 | } |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4188 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4189 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4190 | private long currentTimeMillis() { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4191 | return mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); |
| 4192 | } |
| 4193 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 4194 | private static Intent buildAllowBackgroundDataIntent() { |
| 4195 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 4196 | } |
| 4197 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4198 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 4199 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
| 4200 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4201 | return intent; |
| 4202 | } |
| 4203 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4204 | private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4205 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4206 | intent.setComponent(ComponentName.unflattenFromString( |
| 4207 | res.getString(R.string.config_networkOverLimitComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4208 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4209 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4210 | return intent; |
| 4211 | } |
| 4212 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4213 | private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4214 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4215 | intent.setComponent(ComponentName.unflattenFromString( |
| 4216 | res.getString(R.string.config_dataUsageSummaryComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4217 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4218 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4219 | return intent; |
| 4220 | } |
| 4221 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 4222 | @VisibleForTesting |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 4223 | public void addIdleHandler(IdleHandler handler) { |
| 4224 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 4225 | } |
| 4226 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4227 | @VisibleForTesting |
| 4228 | public void updateRestrictBackgroundByLowPowerModeUL(final PowerSaveState result) { |
| 4229 | mRestrictBackgroundPowerState = result; |
| 4230 | |
| 4231 | boolean restrictBackground = result.batterySaverEnabled; |
| 4232 | boolean shouldInvokeRestrictBackground; |
| 4233 | // store the temporary mRestrictBackgroundChangedInBsm and update it at last |
| 4234 | boolean localRestrictBgChangedInBsm = mRestrictBackgroundChangedInBsm; |
| 4235 | |
| 4236 | if (result.globalBatterySaverEnabled) { |
| 4237 | // Try to turn on restrictBackground if (1) it is off and (2) batter saver need to |
| 4238 | // turn it on. |
| 4239 | shouldInvokeRestrictBackground = !mRestrictBackground && result.batterySaverEnabled; |
| 4240 | mRestrictBackgroundBeforeBsm = mRestrictBackground; |
| 4241 | localRestrictBgChangedInBsm = false; |
| 4242 | } else { |
| 4243 | // Try to restore the restrictBackground if it doesn't change in bsm |
| 4244 | shouldInvokeRestrictBackground = !mRestrictBackgroundChangedInBsm; |
| 4245 | restrictBackground = mRestrictBackgroundBeforeBsm; |
| 4246 | } |
| 4247 | |
| 4248 | if (shouldInvokeRestrictBackground) { |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 4249 | setRestrictBackgroundUL(restrictBackground); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | // Change it at last so setRestrictBackground() won't affect this variable |
| 4253 | mRestrictBackgroundChangedInBsm = localRestrictBgChangedInBsm; |
| 4254 | } |
| 4255 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 4256 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 4257 | final int size = source.size(); |
| 4258 | for (int i = 0; i < size; i++) { |
| 4259 | target.put(source.keyAt(i), true); |
| 4260 | } |
| 4261 | } |
| 4262 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4263 | @Override |
| 4264 | public void factoryReset(String subscriber) { |
| 4265 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 4266 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4267 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 4268 | return; |
| 4269 | } |
| 4270 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4271 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 4272 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4273 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 4274 | for (NetworkPolicy policy : policies) { |
| 4275 | if (policy.template.equals(template)) { |
| 4276 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 4277 | policy.inferred = false; |
| 4278 | policy.clearSnooze(); |
| 4279 | } |
| 4280 | } |
| 4281 | setNetworkPolicies(policies); |
| 4282 | |
| 4283 | // Turn restrict background data off |
| 4284 | setRestrictBackground(false); |
| 4285 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4286 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 4287 | // Remove app's "restrict background data" flag |
| 4288 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 4289 | setUidPolicy(uid, POLICY_NONE); |
| 4290 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4291 | } |
| 4292 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 4293 | |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4294 | @Override |
| 4295 | public boolean isUidNetworkingBlocked(int uid, boolean isNetworkMetered) { |
| 4296 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 4297 | return isUidNetworkingBlockedInternal(uid, isNetworkMetered); |
| 4298 | } |
| 4299 | |
| 4300 | private boolean isUidNetworkingBlockedInternal(int uid, boolean isNetworkMetered) { |
| 4301 | final int uidRules; |
| 4302 | final boolean isBackgroundRestricted; |
| 4303 | synchronized (mUidRulesFirstLock) { |
| 4304 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 4305 | isBackgroundRestricted = mRestrictBackground; |
| 4306 | } |
| 4307 | if (hasRule(uidRules, RULE_REJECT_ALL)) { |
| 4308 | if (LOGV) logUidStatus(uid, "blocked by power restrictions"); |
| 4309 | return true; |
| 4310 | } |
| 4311 | if (!isNetworkMetered) { |
| 4312 | if (LOGV) logUidStatus(uid, "allowed on unmetered network"); |
| 4313 | return false; |
| 4314 | } |
| 4315 | if (hasRule(uidRules, RULE_REJECT_METERED)) { |
| 4316 | if (LOGV) logUidStatus(uid, "blacklisted on metered network"); |
| 4317 | return true; |
| 4318 | } |
| 4319 | if (hasRule(uidRules, RULE_ALLOW_METERED)) { |
| 4320 | if (LOGV) logUidStatus(uid, "whitelisted on metered network"); |
| 4321 | return false; |
| 4322 | } |
| 4323 | if (hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED)) { |
| 4324 | if (LOGV) logUidStatus(uid, "temporary whitelisted on metered network"); |
| 4325 | return false; |
| 4326 | } |
| 4327 | if (isBackgroundRestricted) { |
| 4328 | if (LOGV) logUidStatus(uid, "blocked when background is restricted"); |
| 4329 | return true; |
| 4330 | } |
| 4331 | if (LOGV) logUidStatus(uid, "allowed by default"); |
| 4332 | return false; |
| 4333 | } |
| 4334 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4335 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 4336 | |
| 4337 | @Override |
| 4338 | public void resetUserState(int userId) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4339 | synchronized (mUidRulesFirstLock) { |
| 4340 | boolean changed = removeUserStateUL(userId, false); |
| 4341 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed; |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4342 | if (changed) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4343 | synchronized (mNetworkPoliciesSecondLock) { |
| 4344 | writePolicyAL(); |
| 4345 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4349 | |
| 4350 | /** |
| 4351 | * @return true if the given uid is restricted from doing networking on metered networks. |
| 4352 | */ |
| 4353 | @Override |
| 4354 | public boolean isUidRestrictedOnMeteredNetworks(int uid) { |
| 4355 | final int uidRules; |
| 4356 | final boolean isBackgroundRestricted; |
| 4357 | synchronized (mUidRulesFirstLock) { |
| 4358 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 4359 | isBackgroundRestricted = mRestrictBackground; |
| 4360 | } |
| 4361 | return isBackgroundRestricted |
| 4362 | && !hasRule(uidRules, RULE_ALLOW_METERED) |
| 4363 | && !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * @return true if networking is blocked on the given interface for the given uid according |
| 4368 | * to current networking policies. |
| 4369 | */ |
| 4370 | @Override |
| 4371 | public boolean isUidNetworkingBlocked(int uid, String ifname) { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4372 | final boolean isNetworkMetered; |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4373 | synchronized (mNetworkPoliciesSecondLock) { |
| 4374 | isNetworkMetered = mMeteredIfaces.contains(ifname); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4375 | } |
Sudheer Shanka | b8f2316 | 2017-08-04 13:30:10 -0700 | [diff] [blame] | 4376 | return isUidNetworkingBlockedInternal(uid, isNetworkMetered); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4377 | } |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 4378 | |
| 4379 | @Override |
| 4380 | public void onTempPowerSaveWhitelistChange(int appId, boolean added) { |
| 4381 | synchronized (mUidRulesFirstLock) { |
| 4382 | if (added) { |
| 4383 | mPowerSaveTempWhitelistAppIds.put(appId, true); |
| 4384 | } else { |
| 4385 | mPowerSaveTempWhitelistAppIds.delete(appId); |
| 4386 | } |
| 4387 | updateRulesForTempWhitelistChangeUL(appId); |
| 4388 | } |
| 4389 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | private static boolean hasRule(int uidRules, int rule) { |
| 4393 | return (uidRules & rule) != 0; |
| 4394 | } |
| 4395 | |
| 4396 | private static void logUidStatus(int uid, String descr) { |
| 4397 | Slog.d(TAG, String.format("uid %d is %s", uid, descr)); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4398 | } |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 4399 | |
| 4400 | /** |
| 4401 | * This class is used for storing and dumping the last {@link #MAX_PROC_STATE_SEQ_HISTORY} |
| 4402 | * (uid, procStateSeq) pairs. |
| 4403 | */ |
| 4404 | @VisibleForTesting |
| 4405 | public static final class ProcStateSeqHistory { |
| 4406 | private static final int INVALID_UID = -1; |
| 4407 | |
| 4408 | /** |
| 4409 | * Denotes maximum number of items this history can hold. |
| 4410 | */ |
| 4411 | private final int mMaxCapacity; |
| 4412 | /** |
| 4413 | * Used for storing the uid information. |
| 4414 | */ |
| 4415 | private final int[] mUids; |
| 4416 | /** |
| 4417 | * Used for storing the sequence numbers associated with {@link #mUids}. |
| 4418 | */ |
| 4419 | private final long[] mProcStateSeqs; |
| 4420 | /** |
| 4421 | * Points to the next available slot for writing (uid, procStateSeq) pair. |
| 4422 | */ |
| 4423 | private int mHistoryNext; |
| 4424 | |
| 4425 | public ProcStateSeqHistory(int maxCapacity) { |
| 4426 | mMaxCapacity = maxCapacity; |
| 4427 | mUids = new int[mMaxCapacity]; |
| 4428 | Arrays.fill(mUids, INVALID_UID); |
| 4429 | mProcStateSeqs = new long[mMaxCapacity]; |
| 4430 | } |
| 4431 | |
| 4432 | @GuardedBy("mUidRulesFirstLock") |
| 4433 | public void addProcStateSeqUL(int uid, long procStateSeq) { |
| 4434 | mUids[mHistoryNext] = uid; |
| 4435 | mProcStateSeqs[mHistoryNext] = procStateSeq; |
| 4436 | mHistoryNext = increaseNext(mHistoryNext, 1); |
| 4437 | } |
| 4438 | |
| 4439 | @GuardedBy("mUidRulesFirstLock") |
| 4440 | public void dumpUL(IndentingPrintWriter fout) { |
| 4441 | if (mUids[0] == INVALID_UID) { |
| 4442 | fout.println("NONE"); |
| 4443 | return; |
| 4444 | } |
| 4445 | int index = mHistoryNext; |
| 4446 | do { |
| 4447 | index = increaseNext(index, -1); |
| 4448 | if (mUids[index] == INVALID_UID) { |
| 4449 | break; |
| 4450 | } |
| 4451 | fout.println(getString(mUids[index], mProcStateSeqs[index])); |
| 4452 | } while (index != mHistoryNext); |
| 4453 | } |
| 4454 | |
| 4455 | public static String getString(int uid, long procStateSeq) { |
Sudheer Shanka | db9aaf03 | 2017-06-19 19:05:31 -0700 | [diff] [blame] | 4456 | return "UID=" + uid + " Seq=" + procStateSeq; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | private int increaseNext(int next, int increment) { |
| 4460 | next += increment; |
| 4461 | if (next >= mMaxCapacity) { |
| 4462 | next = 0; |
| 4463 | } else if (next < 0) { |
| 4464 | next = mMaxCapacity - 1; |
| 4465 | } |
| 4466 | return next; |
| 4467 | } |
| 4468 | } |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 4469 | |
| 4470 | private class NotificationId { |
| 4471 | private final String mTag; |
| 4472 | private final int mId; |
| 4473 | |
| 4474 | NotificationId(NetworkPolicy policy, int type) { |
| 4475 | mTag = buildNotificationTag(policy, type); |
| 4476 | mId = type; |
| 4477 | } |
| 4478 | |
| 4479 | @Override |
| 4480 | public boolean equals(Object o) { |
| 4481 | if (this == o) return true; |
| 4482 | if (!(o instanceof NotificationId)) return false; |
| 4483 | NotificationId that = (NotificationId) o; |
| 4484 | return Objects.equals(mTag, that.mTag); |
| 4485 | } |
| 4486 | |
| 4487 | @Override |
| 4488 | public int hashCode() { |
| 4489 | return Objects.hash(mTag); |
| 4490 | } |
| 4491 | |
| 4492 | /** |
| 4493 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 4494 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 4495 | */ |
| 4496 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
| 4497 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
| 4498 | } |
| 4499 | |
| 4500 | public String getTag() { |
| 4501 | return mTag; |
| 4502 | } |
| 4503 | |
| 4504 | public int getId() { |
| 4505 | return mId; |
| 4506 | } |
| 4507 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 4508 | } |