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