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 | */ |
| 1268 | private boolean maybeUpdateMobilePolicyCycleNL(int subId) { |
| 1269 | if (LOGV) Slog.v(TAG, "maybeUpdateMobilePolicyCycleNL()"); |
| 1270 | final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
| 1271 | |
| 1272 | if (config == null) { |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | boolean policyUpdated = false; |
| 1277 | final String subscriberId = TelephonyManager.from(mContext).getSubscriberId(subId); |
| 1278 | |
| 1279 | // find and update the mobile NetworkPolicy for this subscriber id |
| 1280 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1281 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
| 1282 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1283 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1284 | if (template.matches(probeIdent)) { |
| 1285 | NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 1286 | |
| 1287 | // only update the policy if the user didn't change any of the defaults. |
| 1288 | if (!policy.inferred) { |
| 1289 | // TODO: inferred could be split, so that if a user changes their data limit or |
| 1290 | // warning, it doesn't prevent their cycle date from being updated. |
| 1291 | if (LOGD) Slog.v(TAG, "Didn't update NetworkPolicy because policy.inferred"); |
| 1292 | continue; |
| 1293 | } |
| 1294 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1295 | final int currentCycleDay; |
| 1296 | if (policy.cycleRule.isMonthly()) { |
| 1297 | currentCycleDay = policy.cycleRule.start.getDayOfMonth(); |
| 1298 | } else { |
| 1299 | currentCycleDay = NetworkPolicy.CYCLE_NONE; |
| 1300 | } |
| 1301 | |
| 1302 | final int cycleDay = getCycleDayFromCarrierConfig(config, currentCycleDay); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1303 | final long warningBytes = getWarningBytesFromCarrierConfig(config, |
| 1304 | policy.warningBytes); |
| 1305 | final long limitBytes = getLimitBytesFromCarrierConfig(config, |
| 1306 | policy.limitBytes); |
| 1307 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1308 | if (currentCycleDay == cycleDay && |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1309 | policy.warningBytes == warningBytes && |
| 1310 | policy.limitBytes == limitBytes) { |
| 1311 | continue; |
| 1312 | } |
| 1313 | |
| 1314 | policyUpdated = true; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1315 | policy.cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.systemDefault()); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1316 | policy.warningBytes = warningBytes; |
| 1317 | policy.limitBytes = limitBytes; |
| 1318 | |
| 1319 | if (LOGD) { |
| 1320 | Slog.d(TAG, "Updated NetworkPolicy " + policy + " which matches subscriber " |
| 1321 | + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 1322 | + " from CarrierConfigManager"); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | return policyUpdated; |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Returns the cycle day that should be used for a mobile NetworkPolicy. |
| 1332 | * |
| 1333 | * It attempts to get an appropriate cycle day from the passed in CarrierConfig. If it's unable |
| 1334 | * to do so, it returns the fallback value. |
| 1335 | * |
| 1336 | * @param config The CarrierConfig to read the value from. |
| 1337 | * @param fallbackCycleDay to return if the CarrierConfig can't be read. |
| 1338 | * @return cycleDay to use in the mobile NetworkPolicy. |
| 1339 | */ |
| 1340 | @VisibleForTesting |
| 1341 | public int getCycleDayFromCarrierConfig(@Nullable PersistableBundle config, |
| 1342 | int fallbackCycleDay) { |
| 1343 | if (config == null) { |
| 1344 | return fallbackCycleDay; |
| 1345 | } |
| 1346 | int cycleDay = |
| 1347 | config.getInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT); |
| 1348 | if (cycleDay == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1349 | return fallbackCycleDay; |
| 1350 | } |
| 1351 | // validate cycleDay value |
| 1352 | final Calendar cal = Calendar.getInstance(); |
| 1353 | if (cycleDay < cal.getMinimum(Calendar.DAY_OF_MONTH) || |
| 1354 | cycleDay > cal.getMaximum(Calendar.DAY_OF_MONTH)) { |
| 1355 | Slog.e(TAG, "Invalid date in " |
| 1356 | + "CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT: " + cycleDay); |
| 1357 | return fallbackCycleDay; |
| 1358 | } |
| 1359 | return cycleDay; |
| 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * Returns the warning bytes that should be used for a mobile NetworkPolicy. |
| 1364 | * |
| 1365 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1366 | * to do so, it returns the fallback value. |
| 1367 | * |
| 1368 | * @param config The CarrierConfig to read the value from. |
| 1369 | * @param fallbackWarningBytes to return if the CarrierConfig can't be read. |
| 1370 | * @return warningBytes to use in the mobile NetworkPolicy. |
| 1371 | */ |
| 1372 | @VisibleForTesting |
| 1373 | public long getWarningBytesFromCarrierConfig(@Nullable PersistableBundle config, |
| 1374 | long fallbackWarningBytes) { |
| 1375 | if (config == null) { |
| 1376 | return fallbackWarningBytes; |
| 1377 | } |
| 1378 | long warningBytes = |
| 1379 | config.getLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG); |
| 1380 | |
| 1381 | if (warningBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1382 | return WARNING_DISABLED; |
| 1383 | } else if (warningBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1384 | return getPlatformDefaultWarningBytes(); |
| 1385 | } else if (warningBytes < 0) { |
| 1386 | Slog.e(TAG, "Invalid value in " |
| 1387 | + "CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; expected a " |
| 1388 | + "non-negative value but got: " + warningBytes); |
| 1389 | return fallbackWarningBytes; |
| 1390 | } |
| 1391 | |
| 1392 | return warningBytes; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * Returns the limit bytes that should be used for a mobile NetworkPolicy. |
| 1397 | * |
| 1398 | * It attempts to get an appropriate value from the passed in CarrierConfig. If it's unable |
| 1399 | * to do so, it returns the fallback value. |
| 1400 | * |
| 1401 | * @param config The CarrierConfig to read the value from. |
| 1402 | * @param fallbackLimitBytes to return if the CarrierConfig can't be read. |
| 1403 | * @return limitBytes to use in the mobile NetworkPolicy. |
| 1404 | */ |
| 1405 | @VisibleForTesting |
| 1406 | public long getLimitBytesFromCarrierConfig(@Nullable PersistableBundle config, |
| 1407 | long fallbackLimitBytes) { |
| 1408 | if (config == null) { |
| 1409 | return fallbackLimitBytes; |
| 1410 | } |
| 1411 | long limitBytes = |
| 1412 | config.getLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG); |
| 1413 | |
| 1414 | if (limitBytes == DATA_CYCLE_THRESHOLD_DISABLED) { |
| 1415 | return LIMIT_DISABLED; |
| 1416 | } else if (limitBytes == DATA_CYCLE_USE_PLATFORM_DEFAULT) { |
| 1417 | return getPlatformDefaultLimitBytes(); |
| 1418 | } else if (limitBytes < 0) { |
| 1419 | Slog.e(TAG, "Invalid value in " |
| 1420 | + "CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; expected a " |
| 1421 | + "non-negative value but got: " + limitBytes); |
| 1422 | return fallbackLimitBytes; |
| 1423 | } |
| 1424 | return limitBytes; |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * Receiver that watches for {@link CarrierConfigManager} to be changed. |
| 1429 | */ |
| 1430 | private BroadcastReceiver mCarrierConfigReceiver = new BroadcastReceiver() { |
| 1431 | @Override |
| 1432 | public void onReceive(Context context, Intent intent) { |
| 1433 | // No need to do a permission check, because the ACTION_CARRIER_CONFIG_CHANGED |
| 1434 | // broadcast is protected and can't be spoofed. Runs on a background handler thread. |
| 1435 | |
| 1436 | if (!intent.hasExtra(PhoneConstants.SUBSCRIPTION_KEY)) { |
| 1437 | return; |
| 1438 | } |
| 1439 | final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, -1); |
| 1440 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 1441 | final String subscriberId = tele.getSubscriberId(subId); |
| 1442 | |
| 1443 | maybeRefreshTrustedTime(); |
| 1444 | synchronized (mUidRulesFirstLock) { |
| 1445 | synchronized (mNetworkPoliciesSecondLock) { |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1446 | final boolean added = ensureActiveMobilePolicyAL(subId, subscriberId); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1447 | if (added) return; |
| 1448 | final boolean updated = maybeUpdateMobilePolicyCycleNL(subId); |
| 1449 | if (!updated) return; |
| 1450 | // update network and notification rules, as the data cycle changed and it's |
| 1451 | // possible that we should be triggering warnings/limits now |
| 1452 | handleNetworkPoliciesUpdateAL(true); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | }; |
| 1457 | |
| 1458 | /** |
| 1459 | * Handles all tasks that need to be run after a new network policy has been set, or an existing |
| 1460 | * one has been updated. |
| 1461 | * |
| 1462 | * @param shouldNormalizePolicies true iff network policies need to be normalized after the |
| 1463 | * update. |
| 1464 | */ |
| 1465 | void handleNetworkPoliciesUpdateAL(boolean shouldNormalizePolicies) { |
| 1466 | if (shouldNormalizePolicies) { |
| 1467 | normalizePoliciesNL(); |
| 1468 | } |
| 1469 | updateNetworkEnabledNL(); |
| 1470 | updateNetworkRulesNL(); |
| 1471 | updateNotificationsNL(); |
| 1472 | writePolicyAL(); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1476 | * Proactively control network data connections when they exceed |
| 1477 | * {@link NetworkPolicy#limitBytes}. |
| 1478 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1479 | void updateNetworkEnabledNL() { |
| 1480 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledNL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1481 | |
| 1482 | // TODO: reset any policy-disabled networks when any policy is removed |
| 1483 | // completely, which is currently rare case. |
| 1484 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1485 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1486 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1487 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1488 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1489 | setNetworkTemplateEnabled(policy.template, true); |
| 1490 | continue; |
| 1491 | } |
| 1492 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1493 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1494 | .cycleIterator(policy).next(); |
| 1495 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1496 | final long end = cycle.second.toInstant().toEpochMilli(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1497 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1498 | |
| 1499 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1500 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 1501 | && policy.lastLimitSnooze < start; |
| 1502 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1503 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1504 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1509 | * Proactively disable networks that match the given |
| 1510 | * {@link NetworkTemplate}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1511 | */ |
| 1512 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1513 | // TODO: reach into ConnectivityManager to proactively disable bringing |
| 1514 | // up this network, since we know that traffic will be blocked. |
Jack Yu | 8781b68 | 2016-07-08 14:28:51 -0700 | [diff] [blame] | 1515 | |
| 1516 | if (template.getMatchRule() == MATCH_MOBILE_ALL) { |
| 1517 | // If mobile data usage hits the limit or if the user resumes the data, we need to |
| 1518 | // notify telephony. |
| 1519 | final SubscriptionManager sm = SubscriptionManager.from(mContext); |
| 1520 | final TelephonyManager tm = TelephonyManager.from(mContext); |
| 1521 | |
| 1522 | final int[] subIds = sm.getActiveSubscriptionIdList(); |
| 1523 | for (int subId : subIds) { |
| 1524 | final String subscriberId = tm.getSubscriberId(subId); |
| 1525 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
| 1526 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
| 1527 | // Template is matched when subscriber id matches. |
| 1528 | if (template.matches(probeIdent)) { |
| 1529 | tm.setPolicyDataEnabled(enabled, subId); |
| 1530 | } |
| 1531 | } |
| 1532 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1536 | * Examine all connected {@link NetworkState}, looking for |
| 1537 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1538 | * remaining quota based on usage cycle and historical stats. |
| 1539 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1540 | void updateNetworkRulesNL() { |
| 1541 | if (LOGV) Slog.v(TAG, "updateNetworkRulesNL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1542 | |
| 1543 | final NetworkState[] states; |
| 1544 | try { |
| 1545 | states = mConnManager.getAllNetworkState(); |
| 1546 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1547 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1548 | return; |
| 1549 | } |
| 1550 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1551 | // First, generate identities of all connected networks so we can |
| 1552 | // quickly compare them against all defined policies below. |
| 1553 | final ArrayList<Pair<String, NetworkIdentity>> connIdents = new ArrayList<>(states.length); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1554 | final ArraySet<String> connIfaces = new ArraySet<String>(states.length); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1555 | for (NetworkState state : states) { |
Wei Liu | b8eaf45 | 2016-01-25 10:32:27 -0800 | [diff] [blame] | 1556 | if (state.networkInfo != null && state.networkInfo.isConnected()) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1557 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1558 | |
| 1559 | final String baseIface = state.linkProperties.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1560 | if (baseIface != null) { |
| 1561 | connIdents.add(Pair.create(baseIface, ident)); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | // Stacked interfaces are considered to have same identity as |
| 1565 | // their parent network. |
| 1566 | final List<LinkProperties> stackedLinks = state.linkProperties.getStackedLinks(); |
| 1567 | for (LinkProperties stackedLink : stackedLinks) { |
| 1568 | final String stackedIface = stackedLink.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1569 | if (stackedIface != null) { |
| 1570 | connIdents.add(Pair.create(stackedIface, ident)); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1571 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1572 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1576 | // Apply policies against all connected interfaces found above |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1577 | mNetworkRules.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1578 | final ArrayList<String> ifaceList = Lists.newArrayList(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1579 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1580 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1581 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1582 | ifaceList.clear(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1583 | for (int j = connIdents.size() - 1; j >= 0; j--) { |
| 1584 | final Pair<String, NetworkIdentity> ident = connIdents.get(j); |
| 1585 | if (policy.template.matches(ident.second)) { |
| 1586 | ifaceList.add(ident.first); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | if (ifaceList.size() > 0) { |
| 1591 | final String[] ifaces = ifaceList.toArray(new String[ifaceList.size()]); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1592 | mNetworkRules.put(policy, ifaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1596 | long lowestRule = Long.MAX_VALUE; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1597 | final ArraySet<String> newMeteredIfaces = new ArraySet<String>(states.length); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1598 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1599 | // apply each policy that we found ifaces for; compute remaining data |
| 1600 | // based on current cycle and historical stats, and push to kernel. |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1601 | for (int i = mNetworkRules.size()-1; i >= 0; i--) { |
| 1602 | final NetworkPolicy policy = mNetworkRules.keyAt(i); |
| 1603 | final String[] ifaces = mNetworkRules.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1604 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1605 | final Pair<ZonedDateTime, ZonedDateTime> cycle = NetworkPolicyManager |
| 1606 | .cycleIterator(policy).next(); |
| 1607 | final long start = cycle.first.toInstant().toEpochMilli(); |
| 1608 | final long end = cycle.second.toInstant().toEpochMilli(); |
| 1609 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1610 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1611 | if (LOGD) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 1612 | Slog.d(TAG, "applying policy " + policy + " to ifaces " + Arrays.toString(ifaces)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1615 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1616 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1617 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1618 | final long quotaBytes; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1619 | if (!hasLimit) { |
| 1620 | // metered network, but no policy limit; we still need to |
| 1621 | // restrict apps, so push really high quota. |
| 1622 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1623 | } else if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1624 | // snoozing past quota, but we still need to restrict apps, |
| 1625 | // so push really high quota. |
| 1626 | quotaBytes = Long.MAX_VALUE; |
| 1627 | } else { |
| 1628 | // remaining "quota" bytes are based on total usage in |
| 1629 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1630 | // set 1-byte quota and disable the radio later. |
| 1631 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1632 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1633 | |
| 1634 | if (ifaces.length > 1) { |
| 1635 | // TODO: switch to shared quota once NMS supports |
| 1636 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1639 | for (String iface : ifaces) { |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1640 | // long quotaBytes split up into two ints to fit in message |
| 1641 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, |
| 1642 | (int) (quotaBytes >> 32), (int) (quotaBytes & 0xFFFFFFFF), iface) |
| 1643 | .sendToTarget(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1644 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1645 | } |
| 1646 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1647 | |
| 1648 | // keep track of lowest warning or limit of active policies |
| 1649 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1650 | lowestRule = policy.warningBytes; |
| 1651 | } |
| 1652 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1653 | lowestRule = policy.limitBytes; |
| 1654 | } |
| 1655 | } |
| 1656 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1657 | for (int i = connIfaces.size()-1; i >= 0; i--) { |
| 1658 | String iface = connIfaces.valueAt(i); |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1659 | // long quotaBytes split up into two ints to fit in message |
| 1660 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, |
| 1661 | (int) (Long.MAX_VALUE >> 32), (int) (Long.MAX_VALUE & 0xFFFFFFFF), iface) |
| 1662 | .sendToTarget(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1663 | newMeteredIfaces.add(iface); |
| 1664 | } |
| 1665 | |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 1666 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1667 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1668 | // remove quota on any trailing interfaces |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1669 | for (int i = mMeteredIfaces.size() - 1; i >= 0; i--) { |
| 1670 | final String iface = mMeteredIfaces.valueAt(i); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1671 | if (!newMeteredIfaces.contains(iface)) { |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1672 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface) |
| 1673 | .sendToTarget(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | mMeteredIfaces = newMeteredIfaces; |
| 1677 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1678 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1679 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1684 | * have at least a default mobile policy defined. |
| 1685 | */ |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1686 | private void ensureActiveMobilePolicyAL() { |
| 1687 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyAL()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1688 | if (mSuppressDefaultPolicy) return; |
| 1689 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1690 | final TelephonyManager tele = TelephonyManager.from(mContext); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1691 | final SubscriptionManager sub = SubscriptionManager.from(mContext); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1692 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1693 | final int[] subIds = sub.getActiveSubscriptionIdList(); |
| 1694 | for (int subId : subIds) { |
| 1695 | final String subscriberId = tele.getSubscriberId(subId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1696 | ensureActiveMobilePolicyAL(subId, subscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1699 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1700 | /** |
| 1701 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1702 | * have at least a default mobile policy defined. |
| 1703 | * |
| 1704 | * @param subId to build a default policy for |
| 1705 | * @param subscriberId that we check for an existing policy |
| 1706 | * @return true if a mobile network policy was added, or false one already existed. |
| 1707 | */ |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1708 | private boolean ensureActiveMobilePolicyAL(int subId, String subscriberId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1709 | // Poke around to see if we already have a policy |
| 1710 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 1711 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1712 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1713 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1714 | if (template.matches(probeIdent)) { |
| 1715 | if (LOGD) { |
| 1716 | Slog.d(TAG, "Found template " + template + " which matches subscriber " |
| 1717 | + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 1718 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1719 | return false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1723 | Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 1724 | + "; generating default policy"); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1725 | final NetworkPolicy policy = buildDefaultMobilePolicy(subId, subscriberId); |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 1726 | addNetworkPolicyAL(policy); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1727 | return true; |
| 1728 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1729 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1730 | private long getPlatformDefaultWarningBytes() { |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1731 | final int dataWarningConfig = mContext.getResources().getInteger( |
| 1732 | com.android.internal.R.integer.config_networkPolicyDefaultWarning); |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1733 | if (dataWarningConfig == WARNING_DISABLED) { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1734 | return WARNING_DISABLED; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1735 | } else { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1736 | return dataWarningConfig * MB_IN_BYTES; |
Fan Zhang | da71ca0 | 2016-09-12 17:36:22 -0700 | [diff] [blame] | 1737 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1738 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1739 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1740 | private long getPlatformDefaultLimitBytes() { |
| 1741 | return LIMIT_DISABLED; |
| 1742 | } |
| 1743 | |
| 1744 | @VisibleForTesting |
| 1745 | public NetworkPolicy buildDefaultMobilePolicy(int subId, String subscriberId) { |
| 1746 | PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId); |
| 1747 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1748 | final int cycleDay = getCycleDayFromCarrierConfig(config, |
| 1749 | ZonedDateTime.now().getDayOfMonth()); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1750 | final long warningBytes = getWarningBytesFromCarrierConfig(config, |
| 1751 | getPlatformDefaultWarningBytes()); |
| 1752 | final long limitBytes = getLimitBytesFromCarrierConfig(config, |
| 1753 | getPlatformDefaultLimitBytes()); |
| 1754 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1755 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1756 | final RecurrenceRule cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.systemDefault()); |
| 1757 | final NetworkPolicy policy = new NetworkPolicy(template, cycleRule, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1758 | warningBytes, limitBytes, SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 1759 | return policy; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1762 | private void readPolicyAL() { |
| 1763 | if (LOGV) Slog.v(TAG, "readPolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1764 | |
| 1765 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1766 | mNetworkPolicy.clear(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1767 | mSubscriptionPlans.clear(); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 1768 | mSubscriptionPlansOwner.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1769 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1770 | |
| 1771 | FileInputStream fis = null; |
| 1772 | try { |
| 1773 | fis = mPolicyFile.openRead(); |
| 1774 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1775 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1776 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1777 | // Must save the <restrict-background> tags and convert them to <uid-policy> later, |
| 1778 | // to skip UIDs that were explicitly blacklisted. |
| 1779 | final SparseBooleanArray whitelistedRestrictBackground = new SparseBooleanArray(); |
| 1780 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1781 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1782 | int version = VERSION_INIT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1783 | boolean insideWhitelist = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1784 | while ((type = in.next()) != END_DOCUMENT) { |
| 1785 | final String tag = in.getName(); |
| 1786 | if (type == START_TAG) { |
| 1787 | if (TAG_POLICY_LIST.equals(tag)) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1788 | final boolean oldValue = mRestrictBackground; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1789 | version = readIntAttribute(in, ATTR_VERSION); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1790 | if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) { |
| 1791 | mRestrictBackground = readBooleanAttribute( |
| 1792 | in, ATTR_RESTRICT_BACKGROUND); |
| 1793 | } else { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1794 | mRestrictBackground = false; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1795 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1796 | if (mRestrictBackground != oldValue) { |
| 1797 | // Some early services may have read the default value, |
| 1798 | // so notify them that it's changed |
| 1799 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, |
| 1800 | mRestrictBackground ? 1 : 0, 0).sendToTarget(); |
| 1801 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1802 | |
| 1803 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 1804 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 1805 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1806 | final String networkId; |
| 1807 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 1808 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 1809 | } else { |
| 1810 | networkId = null; |
| 1811 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1812 | final RecurrenceRule cycleRule; |
| 1813 | if (version >= VERSION_ADDED_CYCLE) { |
| 1814 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 1815 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 1816 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 1817 | cycleRule = new RecurrenceRule( |
| 1818 | RecurrenceRule.convertZonedDateTime(start), |
| 1819 | RecurrenceRule.convertZonedDateTime(end), |
| 1820 | RecurrenceRule.convertPeriod(period)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1821 | } else { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1822 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
| 1823 | final String cycleTimezone; |
| 1824 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 1825 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 1826 | } else { |
| 1827 | cycleTimezone = "UTC"; |
| 1828 | } |
| 1829 | cycleRule = NetworkPolicy.buildRule(cycleDay, ZoneId.of(cycleTimezone)); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1830 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1831 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 1832 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1833 | final long lastLimitSnooze; |
| 1834 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1835 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 1836 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 1837 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1838 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1839 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1840 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1841 | final boolean metered; |
| 1842 | if (version >= VERSION_ADDED_METERED) { |
| 1843 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 1844 | } else { |
| 1845 | switch (networkTemplate) { |
| 1846 | case MATCH_MOBILE_3G_LOWER: |
| 1847 | case MATCH_MOBILE_4G: |
| 1848 | case MATCH_MOBILE_ALL: |
| 1849 | metered = true; |
| 1850 | break; |
| 1851 | default: |
| 1852 | metered = false; |
| 1853 | } |
| 1854 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1855 | final long lastWarningSnooze; |
| 1856 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1857 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 1858 | } else { |
| 1859 | lastWarningSnooze = SNOOZE_NEVER; |
| 1860 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1861 | final boolean inferred; |
| 1862 | if (version >= VERSION_ADDED_INFERRED) { |
| 1863 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 1864 | } else { |
| 1865 | inferred = false; |
| 1866 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1867 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1868 | final NetworkTemplate template = new NetworkTemplate(networkTemplate, |
| 1869 | subscriberId, networkId); |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1870 | if (template.isPersistable()) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1871 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleRule, |
| 1872 | warningBytes, limitBytes, lastWarningSnooze, |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1873 | lastLimitSnooze, metered, inferred)); |
| 1874 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1875 | |
| 1876 | } else if (TAG_SUBSCRIPTION_PLAN.equals(tag)) { |
| 1877 | final String start = readStringAttribute(in, ATTR_CYCLE_START); |
| 1878 | final String end = readStringAttribute(in, ATTR_CYCLE_END); |
| 1879 | final String period = readStringAttribute(in, ATTR_CYCLE_PERIOD); |
| 1880 | final SubscriptionPlan.Builder builder = new SubscriptionPlan.Builder( |
| 1881 | RecurrenceRule.convertZonedDateTime(start), |
| 1882 | RecurrenceRule.convertZonedDateTime(end), |
| 1883 | RecurrenceRule.convertPeriod(period)); |
| 1884 | builder.setTitle(readStringAttribute(in, ATTR_TITLE)); |
| 1885 | builder.setSummary(readStringAttribute(in, ATTR_SUMMARY)); |
| 1886 | |
| 1887 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES, |
| 1888 | SubscriptionPlan.BYTES_UNKNOWN); |
| 1889 | final int limitBehavior = readIntAttribute(in, ATTR_LIMIT_BEHAVIOR, |
| 1890 | SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN); |
| 1891 | if (limitBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 1892 | && limitBehavior != SubscriptionPlan.LIMIT_BEHAVIOR_UNKNOWN) { |
| 1893 | builder.setDataLimit(limitBytes, limitBehavior); |
| 1894 | } |
| 1895 | |
| 1896 | final long usageBytes = readLongAttribute(in, ATTR_USAGE_BYTES, |
| 1897 | SubscriptionPlan.BYTES_UNKNOWN); |
| 1898 | final long usageTime = readLongAttribute(in, ATTR_USAGE_TIME, |
| 1899 | SubscriptionPlan.TIME_UNKNOWN); |
| 1900 | if (usageBytes != SubscriptionPlan.BYTES_UNKNOWN |
| 1901 | && usageTime != SubscriptionPlan.TIME_UNKNOWN) { |
| 1902 | builder.setDataUsage(usageBytes, usageTime); |
| 1903 | } |
| 1904 | |
| 1905 | final int subId = readIntAttribute(in, ATTR_SUB_ID); |
| 1906 | final SubscriptionPlan plan = builder.build(); |
| 1907 | mSubscriptionPlans.put(subId, ArrayUtils.appendElement( |
| 1908 | SubscriptionPlan.class, mSubscriptionPlans.get(subId), plan)); |
| 1909 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 1910 | final String ownerPackage = readStringAttribute(in, ATTR_OWNER_PACKAGE); |
| 1911 | mSubscriptionPlansOwner.put(subId, ownerPackage); |
| 1912 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1913 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1914 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1915 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1916 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1917 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1918 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1919 | } else { |
| 1920 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 1921 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1922 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1923 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 1924 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1925 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1926 | // TODO: set for other users during upgrade |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 1927 | // app policy is deprecated so this is only used in pre system user split. |
| 1928 | final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1929 | if (UserHandle.isApp(uid)) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 1930 | setUidPolicyUncheckedUL(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1931 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1932 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1933 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1934 | } else if (TAG_WHITELIST.equals(tag)) { |
| 1935 | insideWhitelist = true; |
| 1936 | } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1937 | final int uid = readIntAttribute(in, ATTR_UID); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1938 | whitelistedRestrictBackground.append(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 1939 | } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1940 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1941 | mRestrictBackgroundWhitelistRevokedUids.put(uid, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1942 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1943 | } else if (type == END_TAG) { |
| 1944 | if (TAG_WHITELIST.equals(tag)) { |
| 1945 | insideWhitelist = false; |
| 1946 | } |
| 1947 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1951 | final int size = whitelistedRestrictBackground.size(); |
| 1952 | for (int i = 0; i < size; i++) { |
| 1953 | final int uid = whitelistedRestrictBackground.keyAt(i); |
| 1954 | final int policy = mUidPolicy.get(uid, POLICY_NONE); |
| 1955 | if ((policy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
| 1956 | Slog.w(TAG, "ignoring restrict-background-whitelist for " + uid |
| 1957 | + " because its policy is " + uidPoliciesToString(policy)); |
| 1958 | continue; |
| 1959 | } |
| 1960 | if (UserHandle.isApp(uid)) { |
| 1961 | final int newPolicy = policy | POLICY_ALLOW_METERED_BACKGROUND; |
| 1962 | if (LOGV) |
| 1963 | Log.v(TAG, "new policy for " + uid + ": " + uidPoliciesToString(newPolicy)); |
| 1964 | setUidPolicyUncheckedUL(uid, newPolicy, false); |
| 1965 | } else { |
| 1966 | Slog.w(TAG, "unable to update policy on UID " + uid); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1970 | } catch (FileNotFoundException e) { |
| 1971 | // missing policy is okay, probably first boot |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1972 | upgradeDefaultBackgroundDataUL(); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1973 | } catch (Exception e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1974 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1975 | } finally { |
| 1976 | IoUtils.closeQuietly(fis); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1980 | /** |
| 1981 | * Upgrade legacy background data flags, notifying listeners of one last |
| 1982 | * change to always-true. |
| 1983 | */ |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1984 | private void upgradeDefaultBackgroundDataUL() { |
| 1985 | // This method is only called when we're unable to find the network policy flag, which |
| 1986 | // 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] | 1987 | |
Narayan Kamath | 94bcdbc | 2017-07-17 15:32:53 +0100 | [diff] [blame] | 1988 | // Seed from the default value configured for this device. |
| 1989 | mRestrictBackground = Settings.Global.getInt( |
| 1990 | mContext.getContentResolver(), Global.DEFAULT_RESTRICT_BACKGROUND_DATA, 0) == 1; |
| 1991 | |
| 1992 | // NOTE: We used to read the legacy setting here : |
| 1993 | // |
| 1994 | // final int legacyFlagValue = Settings.Secure.getInt( |
| 1995 | // mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, ..); |
| 1996 | // |
| 1997 | // This is no longer necessary because we will never upgrade directly from Gingerbread |
| 1998 | // to O+. Devices upgrading from ICS onwards to O will have a netpolicy.xml file that |
| 1999 | // contains the correct value that we will continue to use. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2002 | /** |
| 2003 | * Perform upgrade step of moving any user-defined meterness overrides over |
| 2004 | * into {@link WifiConfiguration}. |
| 2005 | */ |
| 2006 | private void upgradeWifiMeteredOverrideAL() { |
| 2007 | boolean modified = false; |
| 2008 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2009 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2010 | for (int i = 0; i < mNetworkPolicy.size(); ) { |
| 2011 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
| 2012 | if (policy.template.getMatchRule() == NetworkTemplate.MATCH_WIFI |
| 2013 | && !policy.inferred) { |
| 2014 | mNetworkPolicy.removeAt(i); |
| 2015 | modified = true; |
| 2016 | |
| 2017 | final String networkId = resolveNetworkId(policy.template.getNetworkId()); |
| 2018 | for (WifiConfiguration config : configs) { |
| 2019 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2020 | Slog.d(TAG, "Found network " + networkId + "; upgrading metered hint"); |
| 2021 | config.meteredOverride = policy.metered |
| 2022 | ? WifiConfiguration.METERED_OVERRIDE_METERED |
| 2023 | : WifiConfiguration.METERED_OVERRIDE_NOT_METERED; |
| 2024 | wm.updateNetwork(config); |
| 2025 | } |
| 2026 | } |
| 2027 | } else { |
| 2028 | i++; |
| 2029 | } |
| 2030 | } |
| 2031 | if (modified) { |
| 2032 | writePolicyAL(); |
| 2033 | } |
| 2034 | } |
| 2035 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2036 | void writePolicyAL() { |
| 2037 | if (LOGV) Slog.v(TAG, "writePolicyAL()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2038 | |
| 2039 | FileOutputStream fos = null; |
| 2040 | try { |
| 2041 | fos = mPolicyFile.startWrite(); |
| 2042 | |
| 2043 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2044 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2045 | out.startDocument(null, true); |
| 2046 | |
| 2047 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2048 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2049 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2050 | |
| 2051 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2052 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2053 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2054 | final NetworkTemplate template = policy.template; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 2055 | if (!template.isPersistable()) continue; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2056 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2057 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 2058 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 2059 | final String subscriberId = template.getSubscriberId(); |
| 2060 | if (subscriberId != null) { |
| 2061 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2062 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2063 | final String networkId = template.getNetworkId(); |
| 2064 | if (networkId != null) { |
| 2065 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 2066 | } |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2067 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2068 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.start)); |
| 2069 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2070 | RecurrenceRule.convertZonedDateTime(policy.cycleRule.end)); |
| 2071 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2072 | RecurrenceRule.convertPeriod(policy.cycleRule.period)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2073 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 2074 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2075 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 2076 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 2077 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 2078 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2079 | out.endTag(null, TAG_NETWORK_POLICY); |
| 2080 | } |
| 2081 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2082 | // write all known subscription plans |
| 2083 | for (int i = 0; i < mSubscriptionPlans.size(); i++) { |
| 2084 | final int subId = mSubscriptionPlans.keyAt(i); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2085 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2086 | final SubscriptionPlan[] plans = mSubscriptionPlans.valueAt(i); |
| 2087 | if (ArrayUtils.isEmpty(plans)) continue; |
| 2088 | |
| 2089 | for (SubscriptionPlan plan : plans) { |
| 2090 | out.startTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2091 | writeIntAttribute(out, ATTR_SUB_ID, subId); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2092 | writeStringAttribute(out, ATTR_OWNER_PACKAGE, ownerPackage); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2093 | final RecurrenceRule cycleRule = plan.getCycleRule(); |
| 2094 | writeStringAttribute(out, ATTR_CYCLE_START, |
| 2095 | RecurrenceRule.convertZonedDateTime(cycleRule.start)); |
| 2096 | writeStringAttribute(out, ATTR_CYCLE_END, |
| 2097 | RecurrenceRule.convertZonedDateTime(cycleRule.end)); |
| 2098 | writeStringAttribute(out, ATTR_CYCLE_PERIOD, |
| 2099 | RecurrenceRule.convertPeriod(cycleRule.period)); |
| 2100 | writeStringAttribute(out, ATTR_TITLE, plan.getTitle()); |
| 2101 | writeStringAttribute(out, ATTR_SUMMARY, plan.getSummary()); |
| 2102 | writeLongAttribute(out, ATTR_LIMIT_BYTES, plan.getDataLimitBytes()); |
| 2103 | writeIntAttribute(out, ATTR_LIMIT_BEHAVIOR, plan.getDataLimitBehavior()); |
| 2104 | writeLongAttribute(out, ATTR_USAGE_BYTES, plan.getDataUsageBytes()); |
| 2105 | writeLongAttribute(out, ATTR_USAGE_TIME, plan.getDataUsageTime()); |
| 2106 | out.endTag(null, TAG_SUBSCRIPTION_PLAN); |
| 2107 | } |
| 2108 | } |
| 2109 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2110 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2111 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2112 | final int uid = mUidPolicy.keyAt(i); |
| 2113 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2114 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2115 | // skip writing empty policies |
| 2116 | if (policy == POLICY_NONE) continue; |
| 2117 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2118 | out.startTag(null, TAG_UID_POLICY); |
| 2119 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2120 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2121 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | out.endTag(null, TAG_POLICY_LIST); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2125 | |
| 2126 | // write all whitelists |
| 2127 | out.startTag(null, TAG_WHITELIST); |
| 2128 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2129 | // revoked restrict background whitelist |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2130 | int size = mRestrictBackgroundWhitelistRevokedUids.size(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2131 | for (int i = 0; i < size; i++) { |
| 2132 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2133 | out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2134 | writeIntAttribute(out, ATTR_UID, uid); |
| 2135 | out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 2136 | } |
| 2137 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2138 | out.endTag(null, TAG_WHITELIST); |
| 2139 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2140 | out.endDocument(); |
| 2141 | |
| 2142 | mPolicyFile.finishWrite(fos); |
| 2143 | } catch (IOException e) { |
| 2144 | if (fos != null) { |
| 2145 | mPolicyFile.failWrite(fos); |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2150 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2151 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2152 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2153 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2154 | if (!UserHandle.isApp(uid)) { |
| 2155 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2156 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2157 | synchronized (mUidRulesFirstLock) { |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2158 | final long token = Binder.clearCallingIdentity(); |
| 2159 | try { |
| 2160 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2161 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2162 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 2163 | } |
| 2164 | } finally { |
| 2165 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2166 | } |
| 2167 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2170 | @Override |
| 2171 | public void addUidPolicy(int uid, int policy) { |
| 2172 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2173 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2174 | if (!UserHandle.isApp(uid)) { |
| 2175 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2176 | } |
| 2177 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2178 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2179 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2180 | policy |= oldPolicy; |
| 2181 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2182 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2183 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2184 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2188 | public void removeUidPolicy(int uid, int policy) { |
| 2189 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2190 | |
| 2191 | if (!UserHandle.isApp(uid)) { |
| 2192 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 2193 | } |
| 2194 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2195 | synchronized (mUidRulesFirstLock) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2196 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 2197 | policy = oldPolicy & ~policy; |
| 2198 | if (oldPolicy != policy) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2199 | setUidPolicyUncheckedUL(uid, oldPolicy, policy, true); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2204 | private void setUidPolicyUncheckedUL(int uid, int oldPolicy, int policy, boolean persist) { |
| 2205 | setUidPolicyUncheckedUL(uid, policy, persist); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2206 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2207 | final boolean notifyApp; |
| 2208 | if (!isUidValidForWhitelistRules(uid)) { |
| 2209 | notifyApp = false; |
| 2210 | } else { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2211 | final boolean wasBlacklisted = oldPolicy == POLICY_REJECT_METERED_BACKGROUND; |
| 2212 | final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND; |
| 2213 | final boolean wasWhitelisted = oldPolicy == POLICY_ALLOW_METERED_BACKGROUND; |
| 2214 | final boolean isWhitelisted = policy == POLICY_ALLOW_METERED_BACKGROUND; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2215 | final boolean wasBlocked = wasBlacklisted || (mRestrictBackground && !wasWhitelisted); |
| 2216 | final boolean isBlocked = isBlacklisted || (mRestrictBackground && !isWhitelisted); |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2217 | if ((wasWhitelisted && (!isWhitelisted || isBlacklisted)) |
| 2218 | && mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2219 | && !mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2220 | if (LOGD) |
| 2221 | Slog.d(TAG, "Adding uid " + uid + " to revoked restrict background whitelist"); |
| 2222 | mRestrictBackgroundWhitelistRevokedUids.append(uid, true); |
| 2223 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 2224 | notifyApp = wasBlocked != isBlocked; |
| 2225 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 2226 | mHandler.obtainMessage(MSG_POLICIES_CHANGED, uid, policy, Boolean.valueOf(notifyApp)) |
| 2227 | .sendToTarget(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2230 | private void setUidPolicyUncheckedUL(int uid, int policy, boolean persist) { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 2231 | if (policy == POLICY_NONE) { |
| 2232 | mUidPolicy.delete(uid); |
| 2233 | } else { |
| 2234 | mUidPolicy.put(uid, policy); |
| 2235 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2236 | |
| 2237 | // uid policy changed, recompute rules and persist policy. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2238 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2239 | if (persist) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2240 | synchronized (mNetworkPoliciesSecondLock) { |
| 2241 | writePolicyAL(); |
| 2242 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2247 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2248 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2249 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2250 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2251 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2252 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2255 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2256 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2257 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2258 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2259 | int[] uids = new int[0]; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2260 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2261 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2262 | final int uid = mUidPolicy.keyAt(i); |
| 2263 | final int uidPolicy = mUidPolicy.valueAt(i); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 2264 | if ((policy == POLICY_NONE && uidPolicy == POLICY_NONE) || |
| 2265 | (uidPolicy & policy) != 0) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2266 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2270 | return uids; |
| 2271 | } |
| 2272 | |
| 2273 | /** |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2274 | * Removes any persistable state associated with given {@link UserHandle}, persisting |
| 2275 | * if any changes that are made. |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2276 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2277 | boolean removeUserStateUL(int userId, boolean writePolicy) { |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2278 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2279 | if (LOGV) Slog.v(TAG, "removeUserStateUL()"); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2280 | boolean changed = false; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2281 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2282 | // Remove entries from revoked default restricted background UID whitelist |
| 2283 | for (int i = mRestrictBackgroundWhitelistRevokedUids.size() - 1; i >= 0; i--) { |
| 2284 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 2285 | if (UserHandle.getUserId(uid) == userId) { |
| 2286 | mRestrictBackgroundWhitelistRevokedUids.removeAt(i); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2287 | changed = true; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2291 | // Remove associated UID policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2292 | int[] uids = new int[0]; |
| 2293 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 2294 | final int uid = mUidPolicy.keyAt(i); |
| 2295 | if (UserHandle.getUserId(uid) == userId) { |
| 2296 | uids = appendInt(uids, uid); |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | if (uids.length > 0) { |
| 2301 | for (int uid : uids) { |
| 2302 | mUidPolicy.delete(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2303 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2304 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2305 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2306 | synchronized (mNetworkPoliciesSecondLock) { |
| 2307 | updateRulesForGlobalChangeAL(true); |
| 2308 | if (writePolicy && changed) { |
| 2309 | writePolicyAL(); |
| 2310 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2311 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 2312 | return changed; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2316 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2317 | // TODO: create permission for observing network policy |
| 2318 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2319 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | @Override |
| 2323 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 2324 | // TODO: create permission for observing network policy |
| 2325 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2326 | mListeners.unregister(listener); |
| 2327 | } |
| 2328 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2329 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 2330 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2331 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2332 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2333 | final long token = Binder.clearCallingIdentity(); |
| 2334 | try { |
| 2335 | maybeRefreshTrustedTime(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2336 | synchronized (mUidRulesFirstLock) { |
| 2337 | synchronized (mNetworkPoliciesSecondLock) { |
| 2338 | normalizePoliciesNL(policies); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2339 | handleNetworkPoliciesUpdateAL(false); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2340 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2341 | } |
| 2342 | } finally { |
| 2343 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | |
Hugo Benichi | 446c9c9 | 2017-04-10 09:41:10 +0900 | [diff] [blame] | 2347 | void addNetworkPolicyAL(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2348 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2349 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 2350 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2353 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2354 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2355 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2356 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 2357 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 2358 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 2359 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2360 | } catch (SecurityException e) { |
| 2361 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2362 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 2363 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 2364 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 2365 | return new NetworkPolicy[0]; |
| 2366 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2369 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2370 | final int size = mNetworkPolicy.size(); |
| 2371 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 2372 | for (int i = 0; i < size; i++) { |
| 2373 | policies[i] = mNetworkPolicy.valueAt(i); |
| 2374 | } |
| 2375 | return policies; |
| 2376 | } |
| 2377 | } |
| 2378 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2379 | private void normalizePoliciesNL() { |
| 2380 | normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2383 | private void normalizePoliciesNL(NetworkPolicy[] policies) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2384 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 2385 | final String[] merged = tele.getMergedSubscriberIds(); |
| 2386 | |
| 2387 | mNetworkPolicy.clear(); |
| 2388 | for (NetworkPolicy policy : policies) { |
| 2389 | // When two normalized templates conflict, prefer the most |
| 2390 | // restrictive policy |
| 2391 | policy.template = NetworkTemplate.normalize(policy.template, merged); |
| 2392 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 2393 | if (existing == null || existing.compareTo(policy) > 0) { |
| 2394 | if (existing != null) { |
| 2395 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 2396 | } |
| 2397 | mNetworkPolicy.put(policy.template, policy); |
| 2398 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2403 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2404 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 2405 | |
| 2406 | final long token = Binder.clearCallingIdentity(); |
| 2407 | try { |
| 2408 | performSnooze(template, TYPE_LIMIT); |
| 2409 | } finally { |
| 2410 | Binder.restoreCallingIdentity(token); |
| 2411 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2412 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2413 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2414 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2415 | maybeRefreshTrustedTime(); |
| 2416 | final long currentTime = currentTimeMillis(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2417 | synchronized (mUidRulesFirstLock) { |
| 2418 | synchronized (mNetworkPoliciesSecondLock) { |
| 2419 | // find and snooze local policy that matches |
| 2420 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 2421 | if (policy == null) { |
| 2422 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 2423 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2424 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2425 | switch (type) { |
| 2426 | case TYPE_WARNING: |
| 2427 | policy.lastWarningSnooze = currentTime; |
| 2428 | break; |
| 2429 | case TYPE_LIMIT: |
| 2430 | policy.lastLimitSnooze = currentTime; |
| 2431 | break; |
| 2432 | default: |
| 2433 | throw new IllegalArgumentException("unexpected type"); |
| 2434 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2435 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2436 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2437 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2442 | public void onTetheringChanged(String iface, boolean tethering) { |
| 2443 | // No need to enforce permission because setRestrictBackground() will do it. |
| 2444 | if (LOGD) Log.d(TAG, "onTetherStateChanged(" + iface + ", " + tethering + ")"); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2445 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2446 | if (mRestrictBackground && tethering) { |
| 2447 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 2448 | setRestrictBackground(false); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2454 | public void setRestrictBackground(boolean restrictBackground) { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2455 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setRestrictBackground"); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2456 | try { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2457 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2458 | final long token = Binder.clearCallingIdentity(); |
| 2459 | try { |
| 2460 | maybeRefreshTrustedTime(); |
| 2461 | synchronized (mUidRulesFirstLock) { |
| 2462 | if (restrictBackground == mRestrictBackground) { |
| 2463 | // Ideally, UI should never allow this scenario... |
| 2464 | Slog.w(TAG, "setRestrictBackground: already " + restrictBackground); |
| 2465 | return; |
| 2466 | } |
| 2467 | setRestrictBackgroundUL(restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2468 | } |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2469 | |
| 2470 | } finally { |
| 2471 | Binder.restoreCallingIdentity(token); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2472 | } |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2473 | |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2474 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, restrictBackground ? 1 : 0, 0) |
| 2475 | .sendToTarget(); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 2476 | } finally { |
Felipe Leme | 29e72ea | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 2477 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2481 | private void setRestrictBackgroundUL(boolean restrictBackground) { |
Felipe Leme | 80f0d0f2 | 2016-06-21 13:41:22 -0700 | [diff] [blame] | 2482 | Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2483 | final boolean oldRestrictBackground = mRestrictBackground; |
| 2484 | mRestrictBackground = restrictBackground; |
| 2485 | // Must whitelist foreground apps before turning data saver mode on. |
| 2486 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 2487 | // 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] | 2488 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2489 | try { |
| 2490 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2491 | Slog.e(TAG, "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2492 | mRestrictBackground = oldRestrictBackground; |
| 2493 | // 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] | 2494 | // updateRulesForRestrictBackgroundUL() again to restore state. |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2495 | return; |
| 2496 | } |
| 2497 | } catch (RemoteException e) { |
| 2498 | // ignored; service lives in system_server |
| 2499 | } |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 2500 | |
| 2501 | if (mRestrictBackgroundPowerState.globalBatterySaverEnabled) { |
| 2502 | mRestrictBackgroundChangedInBsm = true; |
| 2503 | } |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2504 | synchronized (mNetworkPoliciesSecondLock) { |
| 2505 | updateNotificationsNL(); |
| 2506 | writePolicyAL(); |
| 2507 | } |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2510 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2511 | public int getRestrictBackgroundByCaller() { |
| 2512 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2513 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2514 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2515 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2516 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2517 | final long token = Binder.clearCallingIdentity(); |
| 2518 | final int policy; |
| 2519 | try { |
| 2520 | policy = getUidPolicy(uid); |
| 2521 | } finally { |
| 2522 | Binder.restoreCallingIdentity(token); |
| 2523 | } |
| 2524 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2525 | // App is blacklisted. |
| 2526 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2527 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2528 | if (!mRestrictBackground) { |
| 2529 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2530 | } |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 2531 | return (mUidPolicy.get(uid) & POLICY_ALLOW_METERED_BACKGROUND) != 0 |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2532 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2533 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2538 | public boolean getRestrictBackground() { |
| 2539 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2540 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2541 | synchronized (mUidRulesFirstLock) { |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2542 | return mRestrictBackground; |
| 2543 | } |
| 2544 | } |
| 2545 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2546 | @Override |
| 2547 | public void setDeviceIdleMode(boolean enabled) { |
| 2548 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2549 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode"); |
| 2550 | try { |
| 2551 | synchronized (mUidRulesFirstLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2552 | if (mDeviceIdleMode == enabled) { |
| 2553 | return; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2554 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 2555 | mDeviceIdleMode = enabled; |
| 2556 | if (mSystemReady) { |
| 2557 | // Device idle change means we need to rebuild rules for all |
| 2558 | // known apps, so do a global refresh. |
| 2559 | updateRulesForRestrictPowerUL(); |
| 2560 | } |
| 2561 | } |
| 2562 | if (enabled) { |
| 2563 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 2564 | } else { |
| 2565 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2566 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2567 | } finally { |
| 2568 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2572 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2573 | public void setWifiMeteredOverride(String networkId, int meteredOverride) { |
| 2574 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2575 | final long token = Binder.clearCallingIdentity(); |
| 2576 | try { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2577 | final WifiManager wm = mContext.getSystemService(WifiManager.class); |
| 2578 | final List<WifiConfiguration> configs = wm.getConfiguredNetworks(); |
| 2579 | for (WifiConfiguration config : configs) { |
| 2580 | if (Objects.equals(resolveNetworkId(config), networkId)) { |
| 2581 | config.meteredOverride = meteredOverride; |
| 2582 | wm.updateNetwork(config); |
| 2583 | } |
| 2584 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2585 | } finally { |
| 2586 | Binder.restoreCallingIdentity(token); |
| 2587 | } |
| 2588 | } |
| 2589 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2590 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2591 | @Deprecated |
| 2592 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 2593 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 2594 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 2595 | return new NetworkQuotaInfo(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2598 | private void enforceSubscriptionPlanAccess(int subId, int callingUid, String callingPackage) { |
| 2599 | // Verify they're not lying about package name |
| 2600 | mAppOps.checkPackage(callingUid, callingPackage); |
| 2601 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2602 | final SubscriptionInfo si; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2603 | final PersistableBundle config; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2604 | final long token = Binder.clearCallingIdentity(); |
| 2605 | try { |
| 2606 | si = mContext.getSystemService(SubscriptionManager.class) |
| 2607 | .getActiveSubscriptionInfo(subId); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2608 | config = mCarrierConfigManager.getConfigForSubId(subId); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2609 | } finally { |
| 2610 | Binder.restoreCallingIdentity(token); |
| 2611 | } |
| 2612 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2613 | // First check: is caller the CarrierService? |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2614 | if (si != null) { |
| 2615 | if (si.isEmbedded() && si.canManageSubscription(mContext, callingPackage)) { |
| 2616 | return; |
| 2617 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2618 | } |
| 2619 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2620 | // Second check: has the CarrierService delegated access? |
| 2621 | if (config != null) { |
| 2622 | final String overridePackage = config |
| 2623 | .getString(CarrierConfigManager.KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING, null); |
| 2624 | if (!TextUtils.isEmpty(overridePackage) |
| 2625 | && Objects.equals(overridePackage, callingPackage)) { |
| 2626 | return; |
| 2627 | } |
| 2628 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2629 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2630 | // Third check: is caller the fallback/default CarrierService? |
| 2631 | final String defaultPackage = mCarrierConfigManager.getDefaultCarrierServicePackageName(); |
| 2632 | if (!TextUtils.isEmpty(defaultPackage) |
| 2633 | && Objects.equals(defaultPackage, callingPackage)) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2634 | return; |
| 2635 | } |
| 2636 | |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2637 | // Final check: does the caller hold a permission? |
| 2638 | mContext.enforceCallingOrSelfPermission(MANAGE_SUBSCRIPTION_PLANS, TAG); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | @Override |
| 2642 | public SubscriptionPlan[] getSubscriptionPlans(int subId, String callingPackage) { |
| 2643 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 2644 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2645 | final String fake = SystemProperties.get("fw.fake_plan"); |
| 2646 | if (!TextUtils.isEmpty(fake)) { |
| 2647 | final List<SubscriptionPlan> plans = new ArrayList<>(); |
| 2648 | if ("month_hard".equals(fake)) { |
| 2649 | plans.add(SubscriptionPlan.Builder |
| 2650 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2651 | .setTitle("G-Mobile") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2652 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2653 | SubscriptionPlan.LIMIT_BEHAVIOR_BILLED) |
| 2654 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 2655 | ZonedDateTime.now().minusHours(36).toInstant().toEpochMilli()) |
| 2656 | .build()); |
| 2657 | } else if ("month_soft".equals(fake)) { |
| 2658 | plans.add(SubscriptionPlan.Builder |
| 2659 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2660 | .setTitle("G-Mobile is the carriers name who this plan belongs to") |
| 2661 | .setSummary("Crazy unlimited bandwidth plan with incredibly long title " |
| 2662 | + "that should be cut off to prevent UI from looking terrible") |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2663 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2664 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2665 | .setDataUsage(1 * TrafficStats.GB_IN_BYTES, |
| 2666 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2667 | .build()); |
| 2668 | } else if ("month_none".equals(fake)) { |
| 2669 | plans.add(SubscriptionPlan.Builder |
| 2670 | .createRecurringMonthly(ZonedDateTime.parse("2007-03-14T00:00:00.000Z")) |
| 2671 | .setTitle("G-Mobile") |
| 2672 | .build()); |
| 2673 | } else if ("prepaid".equals(fake)) { |
| 2674 | plans.add(SubscriptionPlan.Builder |
| 2675 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 2676 | ZonedDateTime.now().plusDays(10)) |
| 2677 | .setTitle("G-Mobile") |
| 2678 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 2679 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 2680 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 2681 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 2682 | .build()); |
| 2683 | } else if ("prepaid_crazy".equals(fake)) { |
| 2684 | plans.add(SubscriptionPlan.Builder |
| 2685 | .createNonrecurring(ZonedDateTime.now().minusDays(20), |
| 2686 | ZonedDateTime.now().plusDays(10)) |
| 2687 | .setTitle("G-Mobile Anytime") |
| 2688 | .setDataLimit(512 * TrafficStats.MB_IN_BYTES, |
| 2689 | SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED) |
| 2690 | .setDataUsage(100 * TrafficStats.MB_IN_BYTES, |
| 2691 | ZonedDateTime.now().minusHours(3).toInstant().toEpochMilli()) |
| 2692 | .build()); |
| 2693 | plans.add(SubscriptionPlan.Builder |
| 2694 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 2695 | ZonedDateTime.now().plusDays(20)) |
| 2696 | .setTitle("G-Mobile Nickel Nights") |
| 2697 | .setSummary("5¢/GB between 1-5AM") |
| 2698 | .setDataUsage(15 * TrafficStats.MB_IN_BYTES, |
| 2699 | ZonedDateTime.now().minusHours(30).toInstant().toEpochMilli()) |
| 2700 | .build()); |
| 2701 | plans.add(SubscriptionPlan.Builder |
| 2702 | .createNonrecurring(ZonedDateTime.now().minusDays(10), |
| 2703 | ZonedDateTime.now().plusDays(20)) |
| 2704 | .setTitle("G-Mobile Bonus 3G") |
| 2705 | .setSummary("Unlimited 3G data") |
| 2706 | .setDataLimit(5 * TrafficStats.GB_IN_BYTES, |
| 2707 | SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) |
| 2708 | .setDataUsage(300 * TrafficStats.MB_IN_BYTES, |
| 2709 | ZonedDateTime.now().minusHours(1).toInstant().toEpochMilli()) |
| 2710 | .build()); |
| 2711 | } |
| 2712 | return plans.toArray(new SubscriptionPlan[plans.size()]); |
| 2713 | } |
| 2714 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2715 | synchronized (mUidRulesFirstLock) { |
| 2716 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2717 | // Only give out plan details to the package that defined them, |
| 2718 | // so that we don't risk leaking plans between apps. We always |
| 2719 | // let in core system components (like the Settings app). |
| 2720 | final String ownerPackage = mSubscriptionPlansOwner.get(subId); |
| 2721 | if (Objects.equals(ownerPackage, callingPackage) |
| 2722 | || (UserHandle.getCallingAppId() == android.os.Process.SYSTEM_UID)) { |
| 2723 | return mSubscriptionPlans.get(subId); |
| 2724 | } else { |
| 2725 | Log.w(TAG, "Not returning plans because caller " + callingPackage |
| 2726 | + " doesn't match owner " + ownerPackage); |
| 2727 | return null; |
| 2728 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2729 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | @Override |
| 2734 | public void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) { |
| 2735 | enforceSubscriptionPlanAccess(subId, Binder.getCallingUid(), callingPackage); |
| 2736 | |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2737 | for (SubscriptionPlan plan : plans) { |
| 2738 | Preconditions.checkNotNull(plan); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | final long token = Binder.clearCallingIdentity(); |
| 2742 | try { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2743 | maybeRefreshTrustedTime(); |
| 2744 | synchronized (mUidRulesFirstLock) { |
| 2745 | synchronized (mNetworkPoliciesSecondLock) { |
| 2746 | mSubscriptionPlans.put(subId, plans); |
Jeff Sharkey | b7479988 | 2017-07-28 16:55:41 -0600 | [diff] [blame^] | 2747 | mSubscriptionPlansOwner.put(subId, callingPackage); |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 2748 | // TODO: update any implicit details from newly defined plans |
| 2749 | handleNetworkPoliciesUpdateAL(false); |
| 2750 | } |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 2751 | } |
| 2752 | } finally { |
| 2753 | Binder.restoreCallingIdentity(token); |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2758 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2759 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2760 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2761 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 2762 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2763 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2764 | for (String arg : args) { |
| 2765 | argSet.add(arg); |
| 2766 | } |
| 2767 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2768 | synchronized (mUidRulesFirstLock) { |
| 2769 | synchronized (mNetworkPoliciesSecondLock) { |
| 2770 | if (argSet.contains("--unsnooze")) { |
| 2771 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 2772 | mNetworkPolicy.valueAt(i).clearSnooze(); |
| 2773 | } |
| 2774 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 2775 | handleNetworkPoliciesUpdateAL(true); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2776 | |
| 2777 | fout.println("Cleared snooze timestamps"); |
| 2778 | return; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2779 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2780 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2781 | fout.print("System ready: "); fout.println(mSystemReady); |
| 2782 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
| 2783 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
| 2784 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
| 2785 | fout.println("Network policies:"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2786 | fout.increaseIndent(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2787 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2788 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
| 2789 | } |
| 2790 | fout.decreaseIndent(); |
| 2791 | |
| 2792 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 2793 | |
| 2794 | fout.println("Policy for UIDs:"); |
| 2795 | fout.increaseIndent(); |
| 2796 | int size = mUidPolicy.size(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2797 | for (int i = 0; i < size; i++) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2798 | final int uid = mUidPolicy.keyAt(i); |
| 2799 | final int policy = mUidPolicy.valueAt(i); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2800 | fout.print("UID="); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2801 | fout.print(uid); |
| 2802 | fout.print(" policy="); |
Felipe Leme | b146f76 | 2016-08-19 09:52:16 -0700 | [diff] [blame] | 2803 | fout.print(uidPoliciesToString(policy)); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2804 | fout.println(); |
| 2805 | } |
| 2806 | fout.decreaseIndent(); |
| 2807 | |
| 2808 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 2809 | if (size > 0) { |
| 2810 | fout.println("Power save whitelist (except idle) app ids:"); |
| 2811 | fout.increaseIndent(); |
| 2812 | for (int i = 0; i < size; i++) { |
| 2813 | fout.print("UID="); |
| 2814 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 2815 | fout.print(": "); |
| 2816 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 2817 | fout.println(); |
| 2818 | } |
| 2819 | fout.decreaseIndent(); |
| 2820 | } |
| 2821 | |
| 2822 | size = mPowerSaveWhitelistAppIds.size(); |
| 2823 | if (size > 0) { |
| 2824 | fout.println("Power save whitelist app ids:"); |
| 2825 | fout.increaseIndent(); |
| 2826 | for (int i = 0; i < size; i++) { |
| 2827 | fout.print("UID="); |
| 2828 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 2829 | fout.print(": "); |
| 2830 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 2831 | fout.println(); |
| 2832 | } |
| 2833 | fout.decreaseIndent(); |
| 2834 | } |
| 2835 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2836 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 2837 | if (size > 0) { |
| 2838 | fout.println("Default restrict background whitelist uids:"); |
| 2839 | fout.increaseIndent(); |
| 2840 | for (int i = 0; i < size; i++) { |
| 2841 | fout.print("UID="); |
| 2842 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 2843 | fout.println(); |
| 2844 | } |
| 2845 | fout.decreaseIndent(); |
| 2846 | } |
| 2847 | |
| 2848 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 2849 | if (size > 0) { |
| 2850 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 2851 | fout.increaseIndent(); |
| 2852 | for (int i = 0; i < size; i++) { |
| 2853 | fout.print("UID="); |
| 2854 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 2855 | fout.println(); |
| 2856 | } |
| 2857 | fout.decreaseIndent(); |
| 2858 | } |
| 2859 | |
| 2860 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 2861 | collectKeys(mUidState, knownUids); |
| 2862 | collectKeys(mUidRules, knownUids); |
| 2863 | |
| 2864 | fout.println("Status for all known UIDs:"); |
| 2865 | fout.increaseIndent(); |
| 2866 | size = knownUids.size(); |
| 2867 | for (int i = 0; i < size; i++) { |
| 2868 | final int uid = knownUids.keyAt(i); |
| 2869 | fout.print("UID="); |
| 2870 | fout.print(uid); |
| 2871 | |
| 2872 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2873 | fout.print(" state="); |
| 2874 | fout.print(state); |
| 2875 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 2876 | fout.print(" (fg)"); |
| 2877 | } else { |
| 2878 | fout.print(state <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 2879 | ? " (fg svc)" : " (bg)"); |
| 2880 | } |
| 2881 | |
| 2882 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2883 | fout.print(" rules="); |
| 2884 | fout.print(uidRulesToString(uidRules)); |
| 2885 | fout.println(); |
| 2886 | } |
| 2887 | fout.decreaseIndent(); |
| 2888 | |
| 2889 | fout.println("Status for just UIDs with rules:"); |
| 2890 | fout.increaseIndent(); |
| 2891 | size = mUidRules.size(); |
| 2892 | for (int i = 0; i < size; i++) { |
| 2893 | final int uid = mUidRules.keyAt(i); |
| 2894 | fout.print("UID="); |
| 2895 | fout.print(uid); |
| 2896 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2897 | fout.print(" rules="); |
| 2898 | fout.print(uidRulesToString(uidRules)); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2899 | fout.println(); |
| 2900 | } |
| 2901 | fout.decreaseIndent(); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 2902 | |
| 2903 | fout.println("Observed uid state changes:"); |
| 2904 | fout.increaseIndent(); |
| 2905 | mObservedHistory.dumpUL(fout); |
| 2906 | fout.decreaseIndent(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2907 | } |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2908 | } |
| 2909 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2910 | |
| 2911 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2912 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2913 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 2914 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2915 | this, in, out, err, args, callback, resultReceiver); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | @Override |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2919 | public boolean isUidForeground(int uid) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2920 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2921 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2922 | synchronized (mUidRulesFirstLock) { |
| 2923 | return isUidForegroundUL(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2924 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2925 | } |
| 2926 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2927 | private boolean isUidForegroundUL(int uid) { |
| 2928 | return isUidStateForegroundUL( |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2929 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
| 2930 | } |
| 2931 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2932 | private boolean isUidForegroundOnRestrictBackgroundUL(int uid) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2933 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2934 | return isProcStateAllowedWhileOnRestrictBackground(procState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2937 | private boolean isUidForegroundOnRestrictPowerUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2938 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2939 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 2940 | } |
| 2941 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2942 | private boolean isUidStateForegroundUL(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2943 | // only really in foreground when screen is also on |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 2944 | return state <= ActivityManager.PROCESS_STATE_TOP; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2945 | } |
| 2946 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2947 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2948 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2949 | * {@link #updateRulesForDataUsageRestrictionsUL(int)} and |
| 2950 | * {@link #updateRulesForPowerRestrictionsUL(int)} |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2951 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2952 | private void updateUidStateUL(int uid, int uidState) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2953 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL"); |
| 2954 | try { |
| 2955 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2956 | if (oldUidState != uidState) { |
| 2957 | // state changed, push updated rules |
| 2958 | mUidState.put(uid, uidState); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2959 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState); |
| 2960 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 2961 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
| 2962 | if (isUidIdle(uid)) { |
| 2963 | updateRuleForAppIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2964 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2965 | if (mDeviceIdleMode) { |
| 2966 | updateRuleForDeviceIdleUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2967 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2968 | if (mRestrictPower) { |
| 2969 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2970 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2971 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2972 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2973 | updateNetworkStats(uid, isUidStateForegroundUL(uidState)); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2974 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 2975 | } finally { |
| 2976 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2977 | } |
| 2978 | } |
| 2979 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2980 | private void removeUidStateUL(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2981 | final int index = mUidState.indexOfKey(uid); |
| 2982 | if (index >= 0) { |
| 2983 | final int oldUidState = mUidState.valueAt(index); |
| 2984 | mUidState.removeAt(index); |
| 2985 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2986 | updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2987 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2988 | if (mDeviceIdleMode) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2989 | updateRuleForDeviceIdleUL(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2990 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2991 | if (mRestrictPower) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 2992 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2993 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 2994 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2995 | updateNetworkStats(uid, false); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2996 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2997 | } |
| 2998 | } |
| 2999 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3000 | // adjust stats accounting based on foreground status |
| 3001 | private void updateNetworkStats(int uid, boolean uidForeground) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3002 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3003 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3004 | "updateNetworkStats: " + uid + "/" + (uidForeground ? "F" : "B")); |
| 3005 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3006 | try { |
| 3007 | mNetworkStats.setUidForeground(uid, uidForeground); |
| 3008 | } catch (RemoteException e) { |
| 3009 | // ignored; service lives in system_server |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3010 | } finally { |
| 3011 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3015 | private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState, |
| 3016 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3017 | final boolean oldForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3018 | isProcStateAllowedWhileOnRestrictBackground(oldUidState); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 3019 | final boolean newForeground = |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3020 | isProcStateAllowedWhileOnRestrictBackground(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3021 | if (oldForeground != newForeground) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3022 | updateRulesForDataUsageRestrictionsUL(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3023 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3026 | void updateRulesForPowerSaveUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3027 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL"); |
| 3028 | try { |
| 3029 | updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 3030 | mUidFirewallPowerSaveRules); |
| 3031 | } finally { |
| 3032 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3033 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3034 | } |
| 3035 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3036 | void updateRuleForRestrictPowerUL(int uid) { |
| 3037 | updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3038 | } |
| 3039 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3040 | void updateRulesForDeviceIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3041 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL"); |
| 3042 | try { |
| 3043 | updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 3044 | mUidFirewallDozableRules); |
| 3045 | } finally { |
| 3046 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3047 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3050 | void updateRuleForDeviceIdleUL(int uid) { |
| 3051 | updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3052 | } |
| 3053 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3054 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3055 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3056 | private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain, |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3057 | SparseIntArray rules) { |
| 3058 | if (enabled) { |
| 3059 | // 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] | 3060 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3061 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3062 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3063 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3064 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3065 | UserInfo user = users.get(ui); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3066 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveTempWhitelistAppIds, user.id); |
| 3067 | updateRulesForWhitelistedAppIds(uidRules, mPowerSaveWhitelistAppIds, user.id); |
| 3068 | if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3069 | updateRulesForWhitelistedAppIds(uidRules, |
| 3070 | mPowerSaveWhitelistExceptIdleAppIds, user.id); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3071 | } |
| 3072 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3073 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3074 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3075 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 3076 | } |
| 3077 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3078 | setUidFirewallRulesAsync(chain, uidRules, CHAIN_TOGGLE_ENABLE); |
| 3079 | } else { |
| 3080 | setUidFirewallRulesAsync(chain, null, CHAIN_TOGGLE_DISABLE); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3081 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3084 | private void updateRulesForWhitelistedAppIds(final SparseIntArray uidRules, |
| 3085 | final SparseBooleanArray whitelistedAppIds, int userId) { |
| 3086 | for (int i = whitelistedAppIds.size() - 1; i >= 0; --i) { |
| 3087 | if (whitelistedAppIds.valueAt(i)) { |
| 3088 | final int appId = whitelistedAppIds.keyAt(i); |
| 3089 | final int uid = UserHandle.getUid(userId, appId); |
| 3090 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | /** |
| 3096 | * @param deviceIdleMode if true then we don't consider |
| 3097 | * {@link #mPowerSaveWhitelistExceptIdleAppIds} for checking if the {@param uid} is |
| 3098 | * whitelisted. |
| 3099 | */ |
| 3100 | private boolean isWhitelistedBatterySaverUL(int uid, boolean deviceIdleMode) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3101 | final int appId = UserHandle.getAppId(uid); |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3102 | boolean isWhitelisted = mPowerSaveTempWhitelistAppIds.get(appId) |
| 3103 | || mPowerSaveWhitelistAppIds.get(appId); |
| 3104 | if (!deviceIdleMode) { |
| 3105 | isWhitelisted = isWhitelisted || mPowerSaveWhitelistExceptIdleAppIds.get(appId); |
| 3106 | } |
| 3107 | return isWhitelisted; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3110 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 3111 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3112 | private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3113 | if (enabled) { |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3114 | final boolean isWhitelisted = isWhitelistedBatterySaverUL(uid, |
| 3115 | chain == FIREWALL_CHAIN_DOZABLE); |
| 3116 | if (isWhitelisted || isUidForegroundOnRestrictPowerUL(uid)) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3117 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3118 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3119 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3120 | } |
| 3121 | } |
| 3122 | } |
| 3123 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3124 | void updateRulesForAppIdleUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3125 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL"); |
| 3126 | try { |
| 3127 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 3128 | uidRules.clear(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3129 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3130 | // Fully update the app idle firewall chain. |
| 3131 | final List<UserInfo> users = mUserManager.getUsers(); |
| 3132 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 3133 | UserInfo user = users.get(ui); |
| 3134 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 3135 | for (int uid : idleUids) { |
| 3136 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
| 3137 | // quick check: if this uid doesn't have INTERNET permission, it |
| 3138 | // doesn't have network access anyway, so it is a waste to mess |
| 3139 | // with it here. |
| 3140 | if (hasInternetPermissions(uid)) { |
| 3141 | uidRules.put(uid, FIREWALL_RULE_DENY); |
| 3142 | } |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 3143 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3144 | } |
| 3145 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3146 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3147 | setUidFirewallRulesAsync(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3148 | } finally { |
| 3149 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3150 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3153 | void updateRuleForAppIdleUL(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3154 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3155 | |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3156 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3157 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRuleForAppIdleUL: " + uid ); |
| 3158 | } |
| 3159 | try { |
| 3160 | int appId = UserHandle.getAppId(uid); |
| 3161 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 3162 | && !isUidForegroundOnRestrictPowerUL(uid)) { |
| 3163 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
| 3164 | } else { |
| 3165 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 3166 | } |
| 3167 | } finally { |
| 3168 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3172 | /** |
| 3173 | * Toggle the firewall standby chain and inform listeners if the uid rules have effectively |
| 3174 | * changed. |
| 3175 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3176 | void updateRulesForAppIdleParoleUL() { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3177 | boolean paroled = mUsageStats.isAppIdleParoleOn(); |
| 3178 | boolean enableChain = !paroled; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3179 | enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, enableChain); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3180 | |
| 3181 | int ruleCount = mUidFirewallStandbyRules.size(); |
| 3182 | for (int i = 0; i < ruleCount; i++) { |
| 3183 | int uid = mUidFirewallStandbyRules.keyAt(i); |
| 3184 | int oldRules = mUidRules.get(uid); |
| 3185 | if (enableChain) { |
| 3186 | // Chain wasn't enabled before and the other power-related |
| 3187 | // chains are whitelists, so we can clear the |
| 3188 | // MASK_ALL_NETWORKS part of the rules and re-inform listeners if |
| 3189 | // the effective rules result in blocking network access. |
| 3190 | oldRules &= MASK_METERED_NETWORKS; |
| 3191 | } else { |
| 3192 | // Skip if it had no restrictions to begin with |
| 3193 | if ((oldRules & MASK_ALL_NETWORKS) == 0) continue; |
| 3194 | } |
Sudheer Shanka | 3af0294 | 2017-04-12 14:29:14 -0700 | [diff] [blame] | 3195 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldRules, paroled); |
| 3196 | if (newUidRules == RULE_NONE) { |
| 3197 | mUidRules.delete(uid); |
| 3198 | } else { |
| 3199 | mUidRules.put(uid, newUidRules); |
| 3200 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3201 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3204 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 3205 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 3206 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3207 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3208 | private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3209 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3210 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3211 | "updateRulesForGlobalChangeAL: " + (restrictedNetworksChanged ? "R" : "-")); |
| 3212 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3213 | try { |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3214 | updateRulesForAppIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3215 | updateRulesForRestrictPowerUL(); |
| 3216 | updateRulesForRestrictBackgroundUL(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3217 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3218 | // If the set of restricted networks may have changed, re-evaluate those. |
| 3219 | if (restrictedNetworksChanged) { |
| 3220 | normalizePoliciesNL(); |
| 3221 | updateNetworkRulesNL(); |
| 3222 | } |
| 3223 | } finally { |
| 3224 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | |
Felipe Leme | 0970046 | 2016-09-08 09:33:48 -0700 | [diff] [blame] | 3228 | // 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] | 3229 | private void updateRulesForRestrictPowerUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3230 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL"); |
| 3231 | try { |
| 3232 | updateRulesForDeviceIdleUL(); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3233 | updateRulesForPowerSaveUL(); |
| 3234 | updateRulesForAllAppsUL(TYPE_RESTRICT_POWER); |
| 3235 | } finally { |
| 3236 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3237 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3240 | private void updateRulesForRestrictBackgroundUL() { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3241 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL"); |
| 3242 | try { |
| 3243 | updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND); |
| 3244 | } finally { |
| 3245 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3246 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 3250 | private static final int TYPE_RESTRICT_POWER = 2; |
| 3251 | @Retention(RetentionPolicy.SOURCE) |
| 3252 | @IntDef(flag = false, value = { |
| 3253 | TYPE_RESTRICT_BACKGROUND, |
| 3254 | TYPE_RESTRICT_POWER, |
| 3255 | }) |
| 3256 | public @interface RestrictType { |
| 3257 | } |
| 3258 | |
| 3259 | // 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] | 3260 | private void updateRulesForAllAppsUL(@RestrictType int type) { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3261 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3262 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type); |
| 3263 | } |
| 3264 | try { |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3265 | // update rules for all installed applications |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3266 | |
| 3267 | final PackageManager pm = mContext.getPackageManager(); |
| 3268 | final List<UserInfo> users; |
| 3269 | final List<ApplicationInfo> apps; |
| 3270 | |
| 3271 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-users"); |
| 3272 | try { |
| 3273 | users = mUserManager.getUsers(); |
| 3274 | } finally { |
| 3275 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3276 | } |
| 3277 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "list-uids"); |
| 3278 | try { |
| 3279 | apps = pm.getInstalledApplications( |
| 3280 | PackageManager.MATCH_ANY_USER | PackageManager.MATCH_DISABLED_COMPONENTS |
| 3281 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 3282 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
| 3283 | } finally { |
| 3284 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3285 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3286 | |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3287 | final int usersSize = users.size(); |
| 3288 | final int appsSize = apps.size(); |
| 3289 | for (int i = 0; i < usersSize; i++) { |
| 3290 | final UserInfo user = users.get(i); |
| 3291 | for (int j = 0; j < appsSize; j++) { |
| 3292 | final ApplicationInfo app = apps.get(j); |
| 3293 | final int uid = UserHandle.getUid(user.id, app.uid); |
| 3294 | switch (type) { |
| 3295 | case TYPE_RESTRICT_BACKGROUND: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3296 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3297 | break; |
| 3298 | case TYPE_RESTRICT_POWER: |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3299 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3300 | break; |
| 3301 | default: |
| 3302 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 3303 | } |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame] | 3304 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 3305 | } |
Felipe Leme | 873a83a | 2016-09-07 11:34:10 -0700 | [diff] [blame] | 3306 | } finally { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3307 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3308 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3311 | private void updateRulesForTempWhitelistChangeUL() { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3312 | final List<UserInfo> users = mUserManager.getUsers(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3313 | for (int i = 0; i < users.size(); i++) { |
| 3314 | final UserInfo user = users.get(i); |
Rakesh Iyer | 4f3fc21 | 2016-03-03 20:16:41 -0800 | [diff] [blame] | 3315 | for (int j = mPowerSaveTempWhitelistAppIds.size() - 1; j >= 0; j--) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 3316 | int appId = mPowerSaveTempWhitelistAppIds.keyAt(j); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3317 | int uid = UserHandle.getUid(user.id, appId); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3318 | // Update external firewall rules. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3319 | updateRuleForAppIdleUL(uid); |
| 3320 | updateRuleForDeviceIdleUL(uid); |
| 3321 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3322 | // Update internal rules. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3323 | updateRulesForPowerRestrictionsUL(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | } |
| 3327 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3328 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 3329 | // methods below could be merged into a isUidValidForRules() method. |
| 3330 | private boolean isUidValidForBlacklistRules(int uid) { |
| 3331 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3332 | 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] | 3333 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 3334 | return true; |
| 3335 | } |
| 3336 | |
| 3337 | return false; |
| 3338 | } |
| 3339 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3340 | private boolean isUidValidForWhitelistRules(int uid) { |
| 3341 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 3342 | } |
| 3343 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3344 | private boolean isUidIdle(int uid) { |
| 3345 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 3346 | final int userId = UserHandle.getUserId(uid); |
| 3347 | |
songjinshi | 0655edd | 2016-05-18 19:55:32 +0800 | [diff] [blame] | 3348 | if (packages != null) { |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 3349 | for (String packageName : packages) { |
| 3350 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 3351 | return false; |
| 3352 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | return true; |
| 3356 | } |
| 3357 | |
| 3358 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3359 | * Checks if an uid has INTERNET permissions. |
| 3360 | * <p> |
| 3361 | * 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] | 3362 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3363 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3364 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 3365 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3366 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3367 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3368 | } |
| 3369 | } catch (RemoteException e) { |
| 3370 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 3371 | return true; |
| 3372 | } |
| 3373 | |
| 3374 | /** |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3375 | * Clears all state - internal and external - associated with an UID. |
| 3376 | */ |
| 3377 | private void onUidDeletedUL(int uid) { |
| 3378 | // First cleanup in-memory state synchronously... |
| 3379 | mUidRules.delete(uid); |
| 3380 | mUidPolicy.delete(uid); |
| 3381 | mUidFirewallStandbyRules.delete(uid); |
| 3382 | mUidFirewallDozableRules.delete(uid); |
| 3383 | mUidFirewallPowerSaveRules.delete(uid); |
| 3384 | mPowerSaveWhitelistExceptIdleAppIds.delete(uid); |
| 3385 | mPowerSaveWhitelistAppIds.delete(uid); |
| 3386 | mPowerSaveTempWhitelistAppIds.delete(uid); |
| 3387 | |
| 3388 | // ...then update iptables asynchronously. |
| 3389 | mHandler.obtainMessage(MSG_RESET_FIREWALL_RULES_BY_UID, uid, 0).sendToTarget(); |
| 3390 | } |
| 3391 | |
| 3392 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3393 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3394 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3395 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3396 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3397 | * <li>Doze mode |
| 3398 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3399 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3400 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3401 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3402 | * |
| 3403 | * <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] | 3404 | */ |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3405 | private void updateRestrictionRulesForUidUL(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3406 | // Methods below only changes the firewall rules for the power-related modes. |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3407 | updateRuleForDeviceIdleUL(uid); |
| 3408 | updateRuleForAppIdleUL(uid); |
| 3409 | updateRuleForRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3410 | |
| 3411 | // Update internal state for power-related modes. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3412 | updateRulesForPowerRestrictionsUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3413 | |
| 3414 | // Update firewall and internal rules for Data Saver Mode. |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3415 | updateRulesForDataUsageRestrictionsUL(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3418 | /** |
| 3419 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 3420 | * restrictions (blacklist / whitelist). |
| 3421 | * |
| 3422 | * <p> |
| 3423 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 3424 | * networks: |
| 3425 | * <ul> |
| 3426 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 3427 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 3428 | * also blacklisted. |
| 3429 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
| 3430 | * no UIDs other those whitelisted will have access. |
| 3431 | * <ul> |
| 3432 | * |
| 3433 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 3434 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 3435 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 3436 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3437 | * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3438 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 3439 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 3440 | * following rules should also be applied: |
| 3441 | * |
| 3442 | * <ul> |
| 3443 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 3444 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 3445 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 3446 | * {@code bw_penalty_box}. |
| 3447 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 3448 | * </ul> |
| 3449 | * |
| 3450 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 3451 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 3452 | * have permission to use the internet. |
| 3453 | * |
| 3454 | * <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] | 3455 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3456 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3457 | private void updateRulesForDataUsageRestrictionsUL(int uid) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3458 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3459 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3460 | "updateRulesForDataUsageRestrictionsUL: " + uid); |
| 3461 | } |
| 3462 | try { |
| 3463 | updateRulesForDataUsageRestrictionsULInner(uid); |
| 3464 | } finally { |
| 3465 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | private void updateRulesForDataUsageRestrictionsULInner(int uid) { |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3470 | if (!isUidValidForWhitelistRules(uid)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3471 | 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] | 3472 | return; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3473 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 3474 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3475 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3476 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3477 | final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3478 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3479 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 3480 | final boolean isWhitelisted = (uidPolicy & POLICY_ALLOW_METERED_BACKGROUND) != 0; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3481 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 3482 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 3483 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3484 | // First step: define the new rule based on user restrictions and foreground state. |
| 3485 | if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3486 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 3487 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 3488 | } else if (isWhitelisted) { |
| 3489 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3490 | } |
| 3491 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3492 | if (isBlacklisted) { |
| 3493 | newRule = RULE_REJECT_METERED; |
| 3494 | } else if (mRestrictBackground && isWhitelisted) { |
| 3495 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3496 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3497 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3498 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3499 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3500 | if (LOGV) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3501 | Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3502 | + ": isForeground=" +isForeground |
| 3503 | + ", isBlacklisted=" + isBlacklisted |
| 3504 | + ", isWhitelisted=" + isWhitelisted |
| 3505 | + ", oldRule=" + uidRulesToString(oldRule) |
| 3506 | + ", newRule=" + uidRulesToString(newRule) |
| 3507 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 3508 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3509 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3510 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3511 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 3512 | mUidRules.delete(uid); |
| 3513 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3514 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 3515 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3516 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3517 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3518 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3519 | if (hasRule(newRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3520 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 3521 | // (since bw_penalty_box prevails over bw_happy_box). |
| 3522 | |
| 3523 | setMeteredNetworkWhitelist(uid, true); |
| 3524 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 3525 | // but ideally it should be just: |
| 3526 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3527 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3528 | setMeteredNetworkBlacklist(uid, false); |
| 3529 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3530 | } else if (hasRule(oldRule, RULE_TEMPORARY_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3531 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 3532 | |
| 3533 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 3534 | // but ideally they should be just: |
| 3535 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 3536 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3537 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3538 | setMeteredNetworkWhitelist(uid, false); |
| 3539 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3540 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3541 | setMeteredNetworkBlacklist(uid, true); |
| 3542 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3543 | } else if (hasRule(newRule, RULE_REJECT_METERED) |
| 3544 | || hasRule(oldRule, RULE_REJECT_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3545 | // Flip state because app was explicitly added or removed to blacklist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3546 | setMeteredNetworkBlacklist(uid, isBlacklisted); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3547 | if (hasRule(oldRule, RULE_REJECT_METERED) && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3548 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 3549 | // where app is whitelisted and blacklisted at the same time (although such |
| 3550 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3551 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3552 | } |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3553 | } else if (hasRule(newRule, RULE_ALLOW_METERED) |
| 3554 | || hasRule(oldRule, RULE_ALLOW_METERED)) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3555 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3556 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3557 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3558 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3559 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 3560 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3561 | + ", whitelisted=" + isWhitelisted |
| 3562 | + ", blacklisted=" + isBlacklisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3563 | + ", newRule=" + uidRulesToString(newUidRules) |
| 3564 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3565 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3566 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3567 | // Dispatch changed rule to existing listeners. |
| 3568 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | /** |
| 3573 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 3574 | * listeners in case of change. |
| 3575 | * <p> |
| 3576 | * There are 3 power-related rules that affects whether an app has background access on |
| 3577 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 3578 | * restriction, it's added to the equivalent firewall chain: |
| 3579 | * <ul> |
| 3580 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 3581 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 3582 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 3583 | * </ul> |
| 3584 | * <p> |
| 3585 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 3586 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 3587 | * <p> |
| 3588 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 3589 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3590 | private void updateRulesForPowerRestrictionsUL(int uid) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3591 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 3592 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3593 | final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules, false); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3594 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3595 | if (newUidRules == RULE_NONE) { |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3596 | mUidRules.delete(uid); |
| 3597 | } else { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3598 | mUidRules.put(uid, newUidRules); |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | /** |
| 3603 | * Similar to above but ignores idle state if app standby is currently disabled by parole. |
| 3604 | * |
| 3605 | * @param uid the uid of the app to update rules for |
| 3606 | * @param oldUidRules the current rules for the uid, in order to determine if there's a change |
| 3607 | * @param paroled whether to ignore idle state of apps and only look at other restrictions. |
| 3608 | * |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3609 | * @return the new computed rules for the uid |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3610 | */ |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3611 | private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules, boolean paroled) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 3612 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 3613 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 3614 | "updateRulesForPowerRestrictionsUL: " + uid + "/" + oldUidRules + "/" |
| 3615 | + (paroled ? "P" : "-")); |
| 3616 | } |
| 3617 | try { |
| 3618 | return updateRulesForPowerRestrictionsULInner(uid, oldUidRules, paroled); |
| 3619 | } finally { |
| 3620 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | private int updateRulesForPowerRestrictionsULInner(int uid, int oldUidRules, boolean paroled) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3625 | if (!isUidValidForBlacklistRules(uid)) { |
| 3626 | 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] | 3627 | return RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3628 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3629 | |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3630 | final boolean isIdle = !paroled && isUidIdle(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3631 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3632 | final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3633 | |
Sudheer Shanka | 54a92fd | 2017-04-26 10:43:23 -0700 | [diff] [blame] | 3634 | final boolean isWhitelisted = isWhitelistedBatterySaverUL(uid, mDeviceIdleMode); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3635 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 3636 | int newRule = RULE_NONE; |
| 3637 | |
| 3638 | // First step: define the new rule based on user restrictions and foreground state. |
| 3639 | |
| 3640 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 3641 | // by considering the foreground and non-foreground states. |
| 3642 | if (isForeground) { |
| 3643 | if (restrictMode) { |
| 3644 | newRule = RULE_ALLOW_ALL; |
| 3645 | } |
| 3646 | } else if (restrictMode) { |
| 3647 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 3648 | } |
| 3649 | |
| 3650 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 3651 | |
| 3652 | if (LOGV) { |
Felipe Leme | 88f40ad | 2016-08-10 13:00:32 -0700 | [diff] [blame] | 3653 | Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3654 | + ", isIdle: " + isIdle |
| 3655 | + ", mRestrictPower: " + mRestrictPower |
| 3656 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 3657 | + ", isForeground=" + isForeground |
| 3658 | + ", isWhitelisted=" + isWhitelisted |
| 3659 | + ", oldRule=" + uidRulesToString(oldRule) |
| 3660 | + ", newRule=" + uidRulesToString(newRule) |
| 3661 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 3662 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 3663 | } |
| 3664 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3665 | // Second step: notify listeners if state changed. |
| 3666 | if (newRule != oldRule) { |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3667 | if (newRule == RULE_NONE || hasRule(newRule, RULE_ALLOW_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3668 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Hugo Benichi | 2966c18 | 2017-03-28 17:17:13 +0900 | [diff] [blame] | 3669 | } else if (hasRule(newRule, RULE_REJECT_ALL)) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3670 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 3671 | } else { |
| 3672 | // All scenarios should have been covered above |
| 3673 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 3674 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3675 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3676 | + ", newRule=" + uidRulesToString(newUidRules) |
| 3677 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 3678 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3679 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3680 | } |
Amith Yamasani | 0938f2f | 2016-09-16 12:46:31 -0700 | [diff] [blame] | 3681 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3682 | return newUidRules; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3683 | } |
| 3684 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3685 | private class AppIdleStateChangeListener |
| 3686 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 3687 | |
| 3688 | @Override |
| 3689 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle) { |
| 3690 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 3691 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 3692 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3693 | if (LOGV) Log.v(TAG, "onAppIdleStateChanged(): uid=" + uid + ", idle=" + idle); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3694 | synchronized (mUidRulesFirstLock) { |
| 3695 | updateRuleForAppIdleUL(uid); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3696 | updateRulesForPowerRestrictionsUL(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3697 | } |
| 3698 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3699 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | @Override |
| 3703 | public void onParoleStateChanged(boolean isParoleOn) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3704 | synchronized (mUidRulesFirstLock) { |
| 3705 | updateRulesForAppIdleParoleUL(); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3706 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3710 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 3711 | if (listener != null) { |
| 3712 | try { |
| 3713 | listener.onUidRulesChanged(uid, uidRules); |
| 3714 | } catch (RemoteException ignored) { |
| 3715 | } |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 3720 | String[] meteredIfaces) { |
| 3721 | if (listener != null) { |
| 3722 | try { |
| 3723 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 3724 | } catch (RemoteException ignored) { |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 3730 | boolean restrictBackground) { |
| 3731 | if (listener != null) { |
| 3732 | try { |
| 3733 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 3734 | } catch (RemoteException ignored) { |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3739 | private void dispatchUidPoliciesChanged(INetworkPolicyListener listener, int uid, |
| 3740 | int uidPolicies) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3741 | if (listener != null) { |
| 3742 | try { |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3743 | listener.onUidPoliciesChanged(uid, uidPolicies); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3744 | } catch (RemoteException ignored) { |
| 3745 | } |
| 3746 | } |
| 3747 | } |
| 3748 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3749 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 3750 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3751 | public boolean handleMessage(Message msg) { |
| 3752 | switch (msg.what) { |
| 3753 | case MSG_RULES_CHANGED: { |
| 3754 | final int uid = msg.arg1; |
| 3755 | final int uidRules = msg.arg2; |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3756 | final int length = mListeners.beginBroadcast(); |
| 3757 | for (int i = 0; i < length; i++) { |
| 3758 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 3759 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3760 | } |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 3761 | mListeners.finishBroadcast(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3762 | return true; |
| 3763 | } |
| 3764 | case MSG_METERED_IFACES_CHANGED: { |
| 3765 | final String[] meteredIfaces = (String[]) msg.obj; |
| 3766 | final int length = mListeners.beginBroadcast(); |
| 3767 | for (int i = 0; i < length; i++) { |
| 3768 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3769 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3770 | } |
| 3771 | mListeners.finishBroadcast(); |
| 3772 | return true; |
| 3773 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3774 | case MSG_LIMIT_REACHED: { |
| 3775 | final String iface = (String) msg.obj; |
| 3776 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3777 | maybeRefreshTrustedTime(); |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3778 | synchronized (mNetworkPoliciesSecondLock) { |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3779 | if (mMeteredIfaces.contains(iface)) { |
| 3780 | try { |
| 3781 | // force stats update to make sure we have |
| 3782 | // numbers that caused alert to trigger. |
| 3783 | mNetworkStats.forceUpdate(); |
| 3784 | } catch (RemoteException e) { |
| 3785 | // ignored; service lives in system_server |
| 3786 | } |
| 3787 | |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 3788 | updateNetworkEnabledNL(); |
| 3789 | updateNotificationsNL(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | return true; |
| 3793 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3794 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 3795 | final boolean restrictBackground = msg.arg1 != 0; |
| 3796 | final int length = mListeners.beginBroadcast(); |
| 3797 | for (int i = 0; i < length; i++) { |
| 3798 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3799 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3800 | } |
| 3801 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3802 | final Intent intent = |
| 3803 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3804 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3805 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 3806 | return true; |
| 3807 | } |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3808 | case MSG_POLICIES_CHANGED: { |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3809 | final int uid = msg.arg1; |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3810 | final int policy = msg.arg2; |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3811 | final Boolean notifyApp = (Boolean) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3812 | // First notify internal listeners... |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3813 | final int length = mListeners.beginBroadcast(); |
| 3814 | for (int i = 0; i < length; i++) { |
| 3815 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 3816 | dispatchUidPoliciesChanged(listener, uid, policy); |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3817 | } |
| 3818 | mListeners.finishBroadcast(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3819 | // ...then apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
| 3820 | if (notifyApp.booleanValue()) { |
| 3821 | broadcastRestrictBackgroundChanged(uid, notifyApp); |
| 3822 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3823 | return true; |
| 3824 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 3825 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 3826 | final long lowestRule = (Long) msg.obj; |
| 3827 | try { |
| 3828 | // make sure stats are recorded frequently enough; we aim |
| 3829 | // for 2MB threshold for 2GB/month rules. |
| 3830 | final long persistThreshold = lowestRule / 1000; |
| 3831 | mNetworkStats.advisePersistThreshold(persistThreshold); |
| 3832 | } catch (RemoteException e) { |
| 3833 | // ignored; service lives in system_server |
| 3834 | } |
| 3835 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3836 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 3837 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 3838 | removeInterfaceQuota((String) msg.obj); |
| 3839 | // int params need to be stitched back into a long |
| 3840 | setInterfaceQuota((String) msg.obj, |
| 3841 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 3842 | return true; |
| 3843 | } |
| 3844 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 3845 | removeInterfaceQuota((String) msg.obj); |
| 3846 | return true; |
| 3847 | } |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3848 | case MSG_SET_FIREWALL_RULES: { |
| 3849 | final int chain = msg.arg1; |
| 3850 | final int toggle = msg.arg2; |
| 3851 | final SparseIntArray uidRules = (SparseIntArray) msg.obj; |
| 3852 | if (uidRules != null) { |
| 3853 | setUidFirewallRules(chain, uidRules); |
| 3854 | } |
| 3855 | if (toggle != CHAIN_TOGGLE_NONE) { |
| 3856 | enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE); |
| 3857 | } |
| 3858 | return true; |
| 3859 | } |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 3860 | case MSG_RESET_FIREWALL_RULES_BY_UID: { |
| 3861 | resetUidFirewallRules(msg.arg1); |
| 3862 | return true; |
| 3863 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3864 | default: { |
| 3865 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | } |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3869 | }; |
| 3870 | |
| 3871 | private final Handler.Callback mUidEventHandlerCallback = new Handler.Callback() { |
| 3872 | @Override |
| 3873 | public boolean handleMessage(Message msg) { |
| 3874 | switch (msg.what) { |
| 3875 | case UID_MSG_STATE_CHANGED: { |
| 3876 | final int uid = msg.arg1; |
| 3877 | final int procState = msg.arg2; |
| 3878 | final long procStateSeq = (Long) msg.obj; |
| 3879 | |
| 3880 | handleUidChanged(uid, procState, procStateSeq); |
| 3881 | return true; |
| 3882 | } |
| 3883 | case UID_MSG_GONE: { |
| 3884 | final int uid = msg.arg1; |
| 3885 | handleUidGone(uid); |
| 3886 | return true; |
| 3887 | } |
| 3888 | default: { |
| 3889 | return false; |
| 3890 | } |
| 3891 | } |
| 3892 | } |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3893 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3894 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 3895 | |
Makoto Onuki | 8e77733 | 2017-03-28 11:25:47 -0700 | [diff] [blame] | 3896 | void handleUidChanged(int uid, int procState, long procStateSeq) { |
| 3897 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged"); |
| 3898 | try { |
| 3899 | synchronized (mUidRulesFirstLock) { |
| 3900 | // We received a uid state change callback, add it to the history so that it |
| 3901 | // will be useful for debugging. |
| 3902 | mObservedHistory.addProcStateSeqUL(uid, procStateSeq); |
| 3903 | // Now update the network policy rules as per the updated uid state. |
| 3904 | updateUidStateUL(uid, procState); |
| 3905 | // Updating the network rules is done, so notify AMS about this. |
| 3906 | mActivityManagerInternal.notifyNetworkPolicyRulesUpdated(uid, procStateSeq); |
| 3907 | } |
| 3908 | } finally { |
| 3909 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | void handleUidGone(int uid) { |
| 3914 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidGone"); |
| 3915 | try { |
| 3916 | synchronized (mUidRulesFirstLock) { |
| 3917 | removeUidStateUL(uid); |
| 3918 | } |
| 3919 | } finally { |
| 3920 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
| 3921 | } |
| 3922 | } |
| 3923 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 3924 | private void broadcastRestrictBackgroundChanged(int uid, Boolean changed) { |
| 3925 | final PackageManager pm = mContext.getPackageManager(); |
| 3926 | final String[] packages = pm.getPackagesForUid(uid); |
| 3927 | if (packages != null) { |
| 3928 | final int userId = UserHandle.getUserId(uid); |
| 3929 | for (String packageName : packages) { |
| 3930 | final Intent intent = |
| 3931 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3932 | intent.setPackage(packageName); |
| 3933 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3934 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3939 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3940 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3941 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3942 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3943 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3944 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3945 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | private void removeInterfaceQuota(String iface) { |
| 3950 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3951 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3952 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3953 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3954 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3955 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3956 | } |
| 3957 | } |
| 3958 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3959 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 3960 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3961 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3962 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3963 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3964 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 3965 | } catch (RemoteException e) { |
| 3966 | // ignored; service lives in system_server |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 3971 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 3972 | try { |
| 3973 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 3974 | } catch (IllegalStateException e) { |
| 3975 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3976 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3977 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | |
Felipe Leme | bc853dd | 2016-09-08 13:26:55 -0700 | [diff] [blame] | 3981 | private static final int CHAIN_TOGGLE_NONE = 0; |
| 3982 | private static final int CHAIN_TOGGLE_ENABLE = 1; |
| 3983 | private static final int CHAIN_TOGGLE_DISABLE = 2; |
| 3984 | @Retention(RetentionPolicy.SOURCE) |
| 3985 | @IntDef(flag = false, value = { |
| 3986 | CHAIN_TOGGLE_NONE, |
| 3987 | CHAIN_TOGGLE_ENABLE, |
| 3988 | CHAIN_TOGGLE_DISABLE |
| 3989 | }) |
| 3990 | public @interface ChainToggleType { |
| 3991 | } |
| 3992 | |
| 3993 | /** |
| 3994 | * Calls {@link #setUidFirewallRules(int, SparseIntArray)} and |
| 3995 | * {@link #enableFirewallChainUL(int, boolean)} asynchronously. |
| 3996 | * |
| 3997 | * @param chain firewall chain. |
| 3998 | * @param uidRules new UID rules; if {@code null}, only toggles chain state. |
| 3999 | * @param toggle whether the chain should be enabled, disabled, or not changed. |
| 4000 | */ |
| 4001 | private void setUidFirewallRulesAsync(int chain, @Nullable SparseIntArray uidRules, |
| 4002 | @ChainToggleType int toggle) { |
| 4003 | mHandler.obtainMessage(MSG_SET_FIREWALL_RULES, chain, toggle, uidRules).sendToTarget(); |
| 4004 | } |
| 4005 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4006 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4007 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 4008 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 4009 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4010 | */ |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4011 | private void setUidFirewallRules(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4012 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4013 | int size = uidRules.size(); |
| 4014 | int[] uids = new int[size]; |
| 4015 | int[] rules = new int[size]; |
| 4016 | for(int index = size - 1; index >= 0; --index) { |
| 4017 | uids[index] = uidRules.keyAt(index); |
| 4018 | rules[index] = uidRules.valueAt(index); |
| 4019 | } |
| 4020 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 4021 | } catch (IllegalStateException e) { |
| 4022 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4023 | } catch (RemoteException e) { |
| 4024 | // ignored; service lives in system_server |
| 4025 | } |
| 4026 | } |
| 4027 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4028 | /** |
| 4029 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4030 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4031 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4032 | if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) { |
| 4033 | Trace.traceBegin(Trace.TRACE_TAG_NETWORK, |
| 4034 | "setUidFirewallRule: " + chain + "/" + uid + "/" + rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 4035 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4036 | try { |
Makoto Onuki | 0e6e3bd | 2017-03-20 14:53:00 -0700 | [diff] [blame] | 4037 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 4038 | mUidFirewallDozableRules.put(uid, rule); |
| 4039 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 4040 | mUidFirewallStandbyRules.put(uid, rule); |
| 4041 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 4042 | mUidFirewallPowerSaveRules.put(uid, rule); |
| 4043 | } |
| 4044 | |
| 4045 | try { |
| 4046 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
| 4047 | } catch (IllegalStateException e) { |
| 4048 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 4049 | } catch (RemoteException e) { |
| 4050 | // ignored; service lives in system_server |
| 4051 | } |
| 4052 | } finally { |
| 4053 | Trace.traceEnd(Trace.TRACE_TAG_NETWORK); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | /** |
| 4058 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 4059 | */ |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4060 | private void enableFirewallChainUL(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4061 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 4062 | mFirewallChainStates.get(chain) == enable) { |
| 4063 | // All is the same, nothing to do. |
| 4064 | return; |
| 4065 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 4066 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 4067 | try { |
| 4068 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
| 4069 | } catch (IllegalStateException e) { |
| 4070 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 4071 | } catch (RemoteException e) { |
| 4072 | // ignored; service lives in system_server |
| 4073 | } |
| 4074 | } |
| 4075 | |
Felipe Leme | 03e95e2 | 2016-09-09 09:25:31 -0700 | [diff] [blame] | 4076 | /** |
| 4077 | * Resets all firewall rules associated with an UID. |
| 4078 | */ |
| 4079 | private void resetUidFirewallRules(int uid) { |
| 4080 | try { |
| 4081 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_DOZABLE, uid, FIREWALL_RULE_DEFAULT); |
| 4082 | mNetworkManager.setFirewallUidRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 4083 | mNetworkManager |
| 4084 | .setFirewallUidRule(FIREWALL_CHAIN_POWERSAVE, uid, FIREWALL_RULE_DEFAULT); |
| 4085 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, false); |
| 4086 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, false); |
| 4087 | } catch (IllegalStateException e) { |
| 4088 | Log.wtf(TAG, "problem resetting firewall uid rules for " + uid, e); |
| 4089 | } catch (RemoteException e) { |
| 4090 | // ignored; service lives in system_server |
| 4091 | } |
| 4092 | } |
| 4093 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4094 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
| 4095 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 4096 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 4097 | } catch (RuntimeException e) { |
| 4098 | Slog.w(TAG, "problem reading network stats: " + e); |
| 4099 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4100 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 4101 | // ignored; service lives in system_server |
| 4102 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 4106 | private boolean isBandwidthControlEnabled() { |
| 4107 | final long token = Binder.clearCallingIdentity(); |
| 4108 | try { |
| 4109 | return mNetworkManager.isBandwidthControlEnabled(); |
| 4110 | } catch (RemoteException e) { |
| 4111 | // ignored; service lives in system_server |
| 4112 | return false; |
| 4113 | } finally { |
| 4114 | Binder.restoreCallingIdentity(token); |
| 4115 | } |
| 4116 | } |
| 4117 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4118 | /** |
| 4119 | * Try refreshing {@link #mTime} when stale. |
| 4120 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 4121 | void maybeRefreshTrustedTime() { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4122 | if (mTime.getCacheAge() > TIME_CACHE_MAX_AGE) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4123 | mTime.forceRefresh(); |
| 4124 | } |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4125 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4126 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 4127 | private long currentTimeMillis() { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 4128 | return mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); |
| 4129 | } |
| 4130 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 4131 | private static Intent buildAllowBackgroundDataIntent() { |
| 4132 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 4133 | } |
| 4134 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 4135 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 4136 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
| 4137 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4138 | return intent; |
| 4139 | } |
| 4140 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4141 | private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4142 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4143 | intent.setComponent(ComponentName.unflattenFromString( |
| 4144 | res.getString(R.string.config_networkOverLimitComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4145 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4146 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4147 | return intent; |
| 4148 | } |
| 4149 | |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4150 | private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4151 | final Intent intent = new Intent(); |
Wei Liu | 546cb77 | 2016-07-21 16:19:01 -0700 | [diff] [blame] | 4152 | intent.setComponent(ComponentName.unflattenFromString( |
| 4153 | res.getString(R.string.config_dataUsageSummaryComponent))); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 4154 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4155 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 4156 | return intent; |
| 4157 | } |
| 4158 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 4159 | @VisibleForTesting |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 4160 | public void addIdleHandler(IdleHandler handler) { |
| 4161 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 4162 | } |
| 4163 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 4164 | @VisibleForTesting |
| 4165 | public void updateRestrictBackgroundByLowPowerModeUL(final PowerSaveState result) { |
| 4166 | mRestrictBackgroundPowerState = result; |
| 4167 | |
| 4168 | boolean restrictBackground = result.batterySaverEnabled; |
| 4169 | boolean shouldInvokeRestrictBackground; |
| 4170 | // store the temporary mRestrictBackgroundChangedInBsm and update it at last |
| 4171 | boolean localRestrictBgChangedInBsm = mRestrictBackgroundChangedInBsm; |
| 4172 | |
| 4173 | if (result.globalBatterySaverEnabled) { |
| 4174 | // Try to turn on restrictBackground if (1) it is off and (2) batter saver need to |
| 4175 | // turn it on. |
| 4176 | shouldInvokeRestrictBackground = !mRestrictBackground && result.batterySaverEnabled; |
| 4177 | mRestrictBackgroundBeforeBsm = mRestrictBackground; |
| 4178 | localRestrictBgChangedInBsm = false; |
| 4179 | } else { |
| 4180 | // Try to restore the restrictBackground if it doesn't change in bsm |
| 4181 | shouldInvokeRestrictBackground = !mRestrictBackgroundChangedInBsm; |
| 4182 | restrictBackground = mRestrictBackgroundBeforeBsm; |
| 4183 | } |
| 4184 | |
| 4185 | if (shouldInvokeRestrictBackground) { |
| 4186 | setRestrictBackground(restrictBackground); |
| 4187 | } |
| 4188 | |
| 4189 | // Change it at last so setRestrictBackground() won't affect this variable |
| 4190 | mRestrictBackgroundChangedInBsm = localRestrictBgChangedInBsm; |
| 4191 | } |
| 4192 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 4193 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 4194 | final int size = source.size(); |
| 4195 | for (int i = 0; i < size; i++) { |
| 4196 | target.put(source.keyAt(i), true); |
| 4197 | } |
| 4198 | } |
| 4199 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4200 | @Override |
| 4201 | public void factoryReset(String subscriber) { |
| 4202 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 4203 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4204 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 4205 | return; |
| 4206 | } |
| 4207 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4208 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 4209 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4210 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 4211 | for (NetworkPolicy policy : policies) { |
| 4212 | if (policy.template.equals(template)) { |
| 4213 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 4214 | policy.inferred = false; |
| 4215 | policy.clearSnooze(); |
| 4216 | } |
| 4217 | } |
| 4218 | setNetworkPolicies(policies); |
| 4219 | |
| 4220 | // Turn restrict background data off |
| 4221 | setRestrictBackground(false); |
| 4222 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 4223 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 4224 | // Remove app's "restrict background data" flag |
| 4225 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 4226 | setUidPolicy(uid, POLICY_NONE); |
| 4227 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4228 | } |
| 4229 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 4230 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4231 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 4232 | |
| 4233 | @Override |
| 4234 | public void resetUserState(int userId) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4235 | synchronized (mUidRulesFirstLock) { |
| 4236 | boolean changed = removeUserStateUL(userId, false); |
| 4237 | changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed; |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4238 | if (changed) { |
Felipe Leme | f082385 | 2016-06-08 13:43:08 -0700 | [diff] [blame] | 4239 | synchronized (mNetworkPoliciesSecondLock) { |
| 4240 | writePolicyAL(); |
| 4241 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4242 | } |
| 4243 | } |
| 4244 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4245 | |
| 4246 | /** |
| 4247 | * @return true if the given uid is restricted from doing networking on metered networks. |
| 4248 | */ |
| 4249 | @Override |
| 4250 | public boolean isUidRestrictedOnMeteredNetworks(int uid) { |
| 4251 | final int uidRules; |
| 4252 | final boolean isBackgroundRestricted; |
| 4253 | synchronized (mUidRulesFirstLock) { |
| 4254 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 4255 | isBackgroundRestricted = mRestrictBackground; |
| 4256 | } |
| 4257 | return isBackgroundRestricted |
| 4258 | && !hasRule(uidRules, RULE_ALLOW_METERED) |
| 4259 | && !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED); |
| 4260 | } |
| 4261 | |
| 4262 | /** |
| 4263 | * @return true if networking is blocked on the given interface for the given uid according |
| 4264 | * to current networking policies. |
| 4265 | */ |
| 4266 | @Override |
| 4267 | public boolean isUidNetworkingBlocked(int uid, String ifname) { |
| 4268 | final int uidRules; |
| 4269 | final boolean isBackgroundRestricted; |
| 4270 | final boolean isNetworkMetered; |
| 4271 | synchronized (mUidRulesFirstLock) { |
| 4272 | uidRules = mUidRules.get(uid, RULE_NONE); |
| 4273 | isBackgroundRestricted = mRestrictBackground; |
| 4274 | synchronized (mNetworkPoliciesSecondLock) { |
| 4275 | isNetworkMetered = mMeteredIfaces.contains(ifname); |
| 4276 | } |
| 4277 | } |
| 4278 | if (hasRule(uidRules, RULE_REJECT_ALL)) { |
| 4279 | if (LOGV) logUidStatus(uid, "blocked by power restrictions"); |
| 4280 | return true; |
| 4281 | } |
| 4282 | if (!isNetworkMetered) { |
| 4283 | if (LOGV) logUidStatus(uid, "allowed on unmetered network"); |
| 4284 | return false; |
| 4285 | } |
| 4286 | if (hasRule(uidRules, RULE_REJECT_METERED)) { |
| 4287 | if (LOGV) logUidStatus(uid, "blacklisted on metered network"); |
| 4288 | return true; |
| 4289 | } |
| 4290 | if (hasRule(uidRules, RULE_ALLOW_METERED)) { |
| 4291 | if (LOGV) logUidStatus(uid, "whitelisted on metered network"); |
| 4292 | return false; |
| 4293 | } |
| 4294 | if (hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED)) { |
| 4295 | if (LOGV) logUidStatus(uid, "temporary whitelisted on metered network"); |
| 4296 | return false; |
| 4297 | } |
| 4298 | if (isBackgroundRestricted) { |
| 4299 | if (LOGV) logUidStatus(uid, "blocked when background is restricted"); |
| 4300 | return true; |
| 4301 | } |
| 4302 | if (LOGV) logUidStatus(uid, "allowed by default"); |
| 4303 | return false; |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | private static boolean hasRule(int uidRules, int rule) { |
| 4308 | return (uidRules & rule) != 0; |
| 4309 | } |
| 4310 | |
| 4311 | private static void logUidStatus(int uid, String descr) { |
| 4312 | Slog.d(TAG, String.format("uid %d is %s", uid, descr)); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 4313 | } |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 4314 | |
| 4315 | /** |
| 4316 | * This class is used for storing and dumping the last {@link #MAX_PROC_STATE_SEQ_HISTORY} |
| 4317 | * (uid, procStateSeq) pairs. |
| 4318 | */ |
| 4319 | @VisibleForTesting |
| 4320 | public static final class ProcStateSeqHistory { |
| 4321 | private static final int INVALID_UID = -1; |
| 4322 | |
| 4323 | /** |
| 4324 | * Denotes maximum number of items this history can hold. |
| 4325 | */ |
| 4326 | private final int mMaxCapacity; |
| 4327 | /** |
| 4328 | * Used for storing the uid information. |
| 4329 | */ |
| 4330 | private final int[] mUids; |
| 4331 | /** |
| 4332 | * Used for storing the sequence numbers associated with {@link #mUids}. |
| 4333 | */ |
| 4334 | private final long[] mProcStateSeqs; |
| 4335 | /** |
| 4336 | * Points to the next available slot for writing (uid, procStateSeq) pair. |
| 4337 | */ |
| 4338 | private int mHistoryNext; |
| 4339 | |
| 4340 | public ProcStateSeqHistory(int maxCapacity) { |
| 4341 | mMaxCapacity = maxCapacity; |
| 4342 | mUids = new int[mMaxCapacity]; |
| 4343 | Arrays.fill(mUids, INVALID_UID); |
| 4344 | mProcStateSeqs = new long[mMaxCapacity]; |
| 4345 | } |
| 4346 | |
| 4347 | @GuardedBy("mUidRulesFirstLock") |
| 4348 | public void addProcStateSeqUL(int uid, long procStateSeq) { |
| 4349 | mUids[mHistoryNext] = uid; |
| 4350 | mProcStateSeqs[mHistoryNext] = procStateSeq; |
| 4351 | mHistoryNext = increaseNext(mHistoryNext, 1); |
| 4352 | } |
| 4353 | |
| 4354 | @GuardedBy("mUidRulesFirstLock") |
| 4355 | public void dumpUL(IndentingPrintWriter fout) { |
| 4356 | if (mUids[0] == INVALID_UID) { |
| 4357 | fout.println("NONE"); |
| 4358 | return; |
| 4359 | } |
| 4360 | int index = mHistoryNext; |
| 4361 | do { |
| 4362 | index = increaseNext(index, -1); |
| 4363 | if (mUids[index] == INVALID_UID) { |
| 4364 | break; |
| 4365 | } |
| 4366 | fout.println(getString(mUids[index], mProcStateSeqs[index])); |
| 4367 | } while (index != mHistoryNext); |
| 4368 | } |
| 4369 | |
| 4370 | public static String getString(int uid, long procStateSeq) { |
Sudheer Shanka | db9aaf03 | 2017-06-19 19:05:31 -0700 | [diff] [blame] | 4371 | return "UID=" + uid + " Seq=" + procStateSeq; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | private int increaseNext(int next, int increment) { |
| 4375 | next += increment; |
| 4376 | if (next >= mMaxCapacity) { |
| 4377 | next = 0; |
| 4378 | } else if (next < 0) { |
| 4379 | next = mMaxCapacity - 1; |
| 4380 | } |
| 4381 | return next; |
| 4382 | } |
| 4383 | } |
Chris Wren | 193ae6b | 2017-03-31 15:17:11 -0400 | [diff] [blame] | 4384 | |
| 4385 | private class NotificationId { |
| 4386 | private final String mTag; |
| 4387 | private final int mId; |
| 4388 | |
| 4389 | NotificationId(NetworkPolicy policy, int type) { |
| 4390 | mTag = buildNotificationTag(policy, type); |
| 4391 | mId = type; |
| 4392 | } |
| 4393 | |
| 4394 | @Override |
| 4395 | public boolean equals(Object o) { |
| 4396 | if (this == o) return true; |
| 4397 | if (!(o instanceof NotificationId)) return false; |
| 4398 | NotificationId that = (NotificationId) o; |
| 4399 | return Objects.equals(mTag, that.mTag); |
| 4400 | } |
| 4401 | |
| 4402 | @Override |
| 4403 | public int hashCode() { |
| 4404 | return Objects.hash(mTag); |
| 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 4409 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 4410 | */ |
| 4411 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
| 4412 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
| 4413 | } |
| 4414 | |
| 4415 | public String getTag() { |
| 4416 | return mTag; |
| 4417 | } |
| 4418 | |
| 4419 | public int getId() { |
| 4420 | return mId; |
| 4421 | } |
| 4422 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 4423 | } |