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