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