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 | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 22 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
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; |
| 36 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 37 | import static android.net.ConnectivityManager.isNetworkTypeMobile; |
| 38 | import static android.net.NetworkPolicy.CYCLE_NONE; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 39 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 40 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 42 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 43 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_DOZABLE; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 44 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_POWERSAVE; |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 45 | import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_STANDBY; |
| 46 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_ALLOW; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 47 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 48 | import static android.net.NetworkPolicyManager.FIREWALL_RULE_DENY; |
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.MASK_METERED_NETWORKS; |
| 54 | import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS; |
| 55 | import static android.net.NetworkPolicyManager.RULE_NONE; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 56 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 57 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 58 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 59 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 60 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 61 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 62 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 63 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 64 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 65 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 66 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 67 | import static android.net.wifi.WifiManager.CHANGE_REASON_ADDED; |
| 68 | import static android.net.wifi.WifiManager.CHANGE_REASON_REMOVED; |
| 69 | import static android.net.wifi.WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION; |
| 70 | import static android.net.wifi.WifiManager.EXTRA_CHANGE_REASON; |
| 71 | import static android.net.wifi.WifiManager.EXTRA_NETWORK_INFO; |
| 72 | import static android.net.wifi.WifiManager.EXTRA_WIFI_CONFIGURATION; |
| 73 | import static android.net.wifi.WifiManager.EXTRA_WIFI_INFO; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 74 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 75 | |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 76 | import static com.android.internal.util.ArrayUtils.appendInt; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 77 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | ded7b75 | 2013-03-22 13:43:41 -0700 | [diff] [blame] | 78 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 79 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 80 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
| 81 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 82 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 83 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 84 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 85 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 86 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 87 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 88 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 89 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 90 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 91 | import android.Manifest; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 92 | import android.annotation.IntDef; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 93 | import android.app.ActivityManager; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 94 | import android.app.AppGlobals; |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 95 | import android.app.AppOpsManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 96 | import android.app.IActivityManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 97 | import android.app.INotificationManager; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 98 | import android.app.IUidObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 99 | import android.app.Notification; |
| 100 | import android.app.PendingIntent; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 101 | import android.app.usage.UsageStatsManagerInternal; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 102 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 103 | import android.content.ComponentName; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 104 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 105 | import android.content.Intent; |
| 106 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 107 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 108 | import android.content.pm.IPackageManager; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 109 | import android.content.pm.PackageManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 110 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 111 | import android.content.pm.UserInfo; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 112 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 113 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 114 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 115 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 116 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 117 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 118 | import android.net.INetworkStatsService; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 119 | import android.net.LinkProperties; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 120 | import android.net.NetworkIdentity; |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 121 | import android.net.NetworkInfo; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 122 | import android.net.NetworkPolicy; |
Jeff Sharkey | 7c3e442 | 2015-10-12 18:07:38 -0700 | [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 | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 127 | import android.net.wifi.WifiConfiguration; |
| 128 | import android.net.wifi.WifiInfo; |
| 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 | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 136 | import android.os.IPowerManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 137 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 138 | import android.os.MessageQueue.IdleHandler; |
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 | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 141 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 142 | import android.os.RemoteException; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 143 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 144 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 145 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 146 | import android.os.UserManager; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 147 | import android.provider.Settings; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 148 | import android.telephony.SubscriptionManager; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 149 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 150 | import android.text.format.Formatter; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 151 | import android.text.format.Time; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 152 | import android.util.ArrayMap; |
| 153 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 154 | import android.util.AtomicFile; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 155 | import android.util.DebugUtils; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 156 | import android.util.Log; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 157 | import android.util.NtpTrustedTime; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 158 | import android.util.Pair; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 159 | import android.util.Slog; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 160 | import android.util.SparseBooleanArray; |
| 161 | import android.util.SparseIntArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 162 | import android.util.TrustedTime; |
| 163 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 164 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 165 | import com.android.internal.R; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 166 | import com.android.internal.annotations.VisibleForTesting; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 167 | import com.android.internal.content.PackageMonitor; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 168 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 169 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 170 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 171 | import com.android.server.DeviceIdleController; |
| 172 | import com.android.server.EventLogTags; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 173 | import com.android.server.LocalServices; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 174 | import com.android.server.SystemConfig; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 175 | |
| 176 | import libcore.io.IoUtils; |
| 177 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 178 | import com.google.android.collect.Lists; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 179 | |
| 180 | import org.xmlpull.v1.XmlPullParser; |
| 181 | import org.xmlpull.v1.XmlPullParserException; |
| 182 | import org.xmlpull.v1.XmlSerializer; |
| 183 | |
| 184 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 185 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 186 | import java.io.FileInputStream; |
| 187 | import java.io.FileNotFoundException; |
| 188 | import java.io.FileOutputStream; |
| 189 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 190 | import java.io.PrintWriter; |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 191 | import java.lang.annotation.Retention; |
| 192 | import java.lang.annotation.RetentionPolicy; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 193 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 194 | import java.util.ArrayList; |
| 195 | import java.util.Arrays; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 196 | import java.util.List; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 198 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 199 | * Service that maintains low-level network policy rules, using |
| 200 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 201 | * <p> |
| 202 | * Derives active rules by combining a given policy with other system status, |
| 203 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 204 | * enforcement. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 205 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 206 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 207 | static final String TAG = "NetworkPolicy"; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 208 | private static final boolean LOGD = false; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 209 | private static final boolean LOGV = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 210 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 211 | private static final int VERSION_INIT = 1; |
| 212 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 213 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 214 | private static final int VERSION_ADDED_METERED = 4; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 215 | private static final int VERSION_SPLIT_SNOOZE = 5; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 216 | private static final int VERSION_ADDED_TIMEZONE = 6; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 217 | private static final int VERSION_ADDED_INFERRED = 7; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 218 | private static final int VERSION_SWITCH_APP_ID = 8; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 219 | private static final int VERSION_ADDED_NETWORK_ID = 9; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 220 | private static final int VERSION_SWITCH_UID = 10; |
| 221 | private static final int VERSION_LATEST = VERSION_SWITCH_UID; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 222 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 223 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 224 | public static final int TYPE_WARNING = 0x1; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 225 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 226 | public static final int TYPE_LIMIT = 0x2; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 227 | @VisibleForTesting |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 228 | public static final int TYPE_LIMIT_SNOOZED = 0x3; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 229 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 230 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 231 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
| 232 | private static final String TAG_UID_POLICY = "uid-policy"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 233 | private static final String TAG_APP_POLICY = "app-policy"; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 234 | private static final String TAG_WHITELIST = "whitelist"; |
| 235 | private static final String TAG_RESTRICT_BACKGROUND = "restrict-background"; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 236 | private static final String TAG_REVOKED_RESTRICT_BACKGROUND = "revoked-restrict-background"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 237 | |
| 238 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 239 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 240 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 241 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 242 | private static final String ATTR_NETWORK_ID = "networkId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 243 | private static final String ATTR_CYCLE_DAY = "cycleDay"; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 244 | private static final String ATTR_CYCLE_TIMEZONE = "cycleTimezone"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 245 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 246 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 247 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 248 | private static final String ATTR_LAST_WARNING_SNOOZE = "lastWarningSnooze"; |
| 249 | private static final String ATTR_LAST_LIMIT_SNOOZE = "lastLimitSnooze"; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 250 | private static final String ATTR_METERED = "metered"; |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 251 | private static final String ATTR_INFERRED = "inferred"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 252 | private static final String ATTR_UID = "uid"; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 253 | private static final String ATTR_APP_ID = "appId"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 254 | private static final String ATTR_POLICY = "policy"; |
| 255 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 256 | private static final String ACTION_ALLOW_BACKGROUND = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 257 | "com.android.server.net.action.ALLOW_BACKGROUND"; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 258 | private static final String ACTION_SNOOZE_WARNING = |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 259 | "com.android.server.net.action.SNOOZE_WARNING"; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 260 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 261 | private static final long TIME_CACHE_MAX_AGE = DAY_IN_MILLIS; |
| 262 | |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 263 | private static final int MSG_RULES_CHANGED = 1; |
| 264 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 265 | private static final int MSG_LIMIT_REACHED = 5; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 266 | private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6; |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 267 | private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7; |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 268 | private static final int MSG_SCREEN_ON_CHANGED = 8; |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 269 | private static final int MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED = 9; |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 270 | private static final int MSG_UPDATE_INTERFACE_QUOTA = 10; |
| 271 | private static final int MSG_REMOVE_INTERFACE_QUOTA = 11; |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 272 | private static final int MSG_RESTRICT_BACKGROUND_BLACKLIST_CHANGED = 12; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 273 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 274 | private final Context mContext; |
| 275 | private final IActivityManager mActivityManager; |
| 276 | private final IPowerManager mPowerManager; |
| 277 | private final INetworkStatsService mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 278 | private final INetworkManagementService mNetworkManager; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 279 | private UsageStatsManagerInternal mUsageStats; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 280 | private final TrustedTime mTime; |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 281 | private final UserManager mUserManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 282 | |
| 283 | private IConnectivityManager mConnManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 284 | private INotificationManager mNotifManager; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 285 | private PowerManagerInternal mPowerManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 286 | private IDeviceIdleController mDeviceIdleController; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 287 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 288 | final Object mRulesLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 289 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 290 | volatile boolean mSystemReady; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 291 | volatile boolean mScreenOn; |
| 292 | volatile boolean mRestrictBackground; |
| 293 | volatile boolean mRestrictPower; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 294 | volatile boolean mDeviceIdleMode; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 295 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 296 | private final boolean mSuppressDefaultPolicy; |
| 297 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 298 | /** Defined network policies. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 299 | final ArrayMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = new ArrayMap<>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 300 | /** Currently active network rules for ifaces. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 301 | final ArrayMap<NetworkPolicy, String[]> mNetworkRules = new ArrayMap<>(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 302 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 303 | /** Defined UID policies. */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 304 | final SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 305 | /** Currently derived rules for each UID. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 306 | final SparseIntArray mUidRules = new SparseIntArray(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 307 | |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 308 | final SparseIntArray mUidFirewallStandbyRules = new SparseIntArray(); |
| 309 | final SparseIntArray mUidFirewallDozableRules = new SparseIntArray(); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 310 | final SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray(); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 311 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 312 | /** Set of states for the child firewall chains. True if the chain is active. */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 313 | final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray(); |
| 314 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 315 | /** |
| 316 | * 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] | 317 | * in power save mode, except device idle (doze) still applies. |
| 318 | * TODO: An int array might be sufficient |
| 319 | */ |
| 320 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 321 | |
| 322 | /** |
| 323 | * 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] | 324 | * in power save mode. |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 325 | * TODO: An int array might be sufficient |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 326 | */ |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 327 | private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 328 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 329 | private final SparseBooleanArray mPowerSaveTempWhitelistAppIds = new SparseBooleanArray(); |
| 330 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 331 | /** |
| 332 | * UIDs that have been white-listed to avoid restricted background. |
| 333 | */ |
| 334 | private final SparseBooleanArray mRestrictBackgroundWhitelistUids = new SparseBooleanArray(); |
| 335 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 336 | /** |
| 337 | * UIDs that have been initially white-listed by system to avoid restricted background. |
| 338 | */ |
| 339 | private final SparseBooleanArray mDefaultRestrictBackgroundWhitelistUids = |
| 340 | new SparseBooleanArray(); |
| 341 | |
| 342 | /** |
| 343 | * UIDs that have been initially white-listed by system to avoid restricted background, |
| 344 | * but later revoked by user. |
| 345 | */ |
| 346 | private final SparseBooleanArray mRestrictBackgroundWhitelistRevokedUids = |
| 347 | new SparseBooleanArray(); |
| 348 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 349 | /** Set of ifaces that are metered. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 350 | private ArraySet<String> mMeteredIfaces = new ArraySet<>(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 351 | /** Set of over-limit templates that have been notified. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 352 | private final ArraySet<NetworkTemplate> mOverLimitNotified = new ArraySet<>(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 353 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 354 | /** Set of currently active {@link Notification} tags. */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 355 | private final ArraySet<String> mActiveNotifs = new ArraySet<String>(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 356 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 357 | /** Foreground at UID granularity. */ |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 358 | final SparseIntArray mUidState = new SparseIntArray(); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 359 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 360 | /** Higher priority listener before general event dispatch */ |
| 361 | private INetworkPolicyListener mConnectivityListener; |
| 362 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 363 | private final RemoteCallbackList<INetworkPolicyListener> |
| 364 | mListeners = new RemoteCallbackList<>(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 365 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 366 | final Handler mHandler; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 367 | |
| 368 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 369 | |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 370 | private final AppOpsManager mAppOps; |
| 371 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 372 | private final MyPackageMonitor mPackageMonitor; |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 373 | private final IPackageManager mIPm; |
| 374 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 375 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 376 | // TODO: keep whitelist of system-critical services that should never have |
| 377 | // rules enforced, such as system, phone, and radio UIDs. |
| 378 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 379 | // TODO: migrate notifications to SystemUI |
| 380 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 381 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 382 | IPowerManager powerManager, INetworkStatsService networkStats, |
| 383 | INetworkManagementService networkManagement) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 384 | this(context, activityManager, powerManager, networkStats, networkManagement, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 385 | NtpTrustedTime.getInstance(context), getSystemDir(), false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private static File getSystemDir() { |
| 389 | return new File(Environment.getDataDirectory(), "system"); |
| 390 | } |
| 391 | |
| 392 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 393 | IPowerManager powerManager, INetworkStatsService networkStats, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 394 | INetworkManagementService networkManagement, TrustedTime time, File systemDir, |
| 395 | boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 396 | mContext = checkNotNull(context, "missing context"); |
| 397 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
| 398 | mPowerManager = checkNotNull(powerManager, "missing powerManager"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 399 | mNetworkStats = checkNotNull(networkStats, "missing networkStats"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 400 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 401 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService( |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 402 | Context.DEVICE_IDLE_CONTROLLER)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 403 | mTime = checkNotNull(time, "missing TrustedTime"); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 404 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 405 | mIPm = AppGlobals.getPackageManager(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 406 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 407 | HandlerThread thread = new HandlerThread(TAG); |
| 408 | thread.start(); |
| 409 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 410 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 411 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 412 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 413 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml")); |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 414 | |
| 415 | mAppOps = context.getSystemService(AppOpsManager.class); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 416 | |
| 417 | mPackageMonitor = new MyPackageMonitor(); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 418 | |
| 419 | // Expose private service for system components to use. |
| 420 | LocalServices.addService(NetworkPolicyManagerInternal.class, |
| 421 | new NetworkPolicyManagerInternalImpl()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 425 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 426 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 427 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 428 | public void bindNotificationManager(INotificationManager notifManager) { |
| 429 | mNotifManager = checkNotNull(notifManager, "missing INotificationManager"); |
| 430 | } |
| 431 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 432 | void updatePowerSaveWhitelistLocked() { |
| 433 | try { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 434 | int[] whitelist = mDeviceIdleController.getAppIdWhitelistExceptIdle(); |
| 435 | mPowerSaveWhitelistExceptIdleAppIds.clear(); |
| 436 | if (whitelist != null) { |
| 437 | for (int uid : whitelist) { |
| 438 | mPowerSaveWhitelistExceptIdleAppIds.put(uid, true); |
| 439 | } |
| 440 | } |
| 441 | whitelist = mDeviceIdleController.getAppIdWhitelist(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 442 | mPowerSaveWhitelistAppIds.clear(); |
| 443 | if (whitelist != null) { |
| 444 | for (int uid : whitelist) { |
| 445 | mPowerSaveWhitelistAppIds.put(uid, true); |
| 446 | } |
| 447 | } |
| 448 | } catch (RemoteException e) { |
| 449 | } |
| 450 | } |
| 451 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 452 | /** |
| 453 | * Whitelists pre-defined apps for restrict background, but only if the user didn't already |
| 454 | * revoke the whitelist. |
| 455 | * |
| 456 | * @return whether any uid has been added to {@link #mRestrictBackgroundWhitelistUids}. |
| 457 | */ |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 458 | boolean addDefaultRestrictBackgroundWhitelistUidsLocked() { |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 459 | final List<UserInfo> users = mUserManager.getUsers(); |
| 460 | final int numberUsers = users.size(); |
| 461 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 462 | boolean changed = false; |
| 463 | for (int i = 0; i < numberUsers; i++) { |
| 464 | final UserInfo user = users.get(i); |
| 465 | changed = addDefaultRestrictBackgroundWhitelistUidsLocked(user.id) || changed; |
| 466 | } |
| 467 | return changed; |
| 468 | } |
| 469 | |
| 470 | private boolean addDefaultRestrictBackgroundWhitelistUidsLocked(int userId) { |
| 471 | final SystemConfig sysConfig = SystemConfig.getInstance(); |
| 472 | final PackageManager pm = mContext.getPackageManager(); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 473 | final ArraySet<String> allowDataUsage = sysConfig.getAllowInDataUsageSave(); |
| 474 | boolean changed = false; |
| 475 | for (int i = 0; i < allowDataUsage.size(); i++) { |
| 476 | final String pkg = allowDataUsage.valueAt(i); |
| 477 | if (LOGD) |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 478 | Slog.d(TAG, "checking restricted background whitelisting for package " + pkg |
| 479 | + " and user " + userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 480 | final ApplicationInfo app; |
| 481 | try { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 482 | app = pm.getApplicationInfoAsUser(pkg, PackageManager.MATCH_SYSTEM_ONLY, userId); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 483 | } catch (PackageManager.NameNotFoundException e) { |
| 484 | // Should not happen |
| 485 | Slog.wtf(TAG, "No ApplicationInfo for package " + pkg); |
| 486 | continue; |
| 487 | } |
| 488 | if (!app.isPrivilegedApp()) { |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 489 | Slog.wtf(TAG, "pm.getApplicationInfoAsUser() returned non-privileged app: " + pkg); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 490 | continue; |
| 491 | } |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 492 | final int uid = UserHandle.getUid(userId, app.uid); |
| 493 | mDefaultRestrictBackgroundWhitelistUids.append(uid, true); |
| 494 | if (LOGD) |
| 495 | Slog.d(TAG, "Adding uid " + uid + " (user " + userId + ") to default restricted " |
| 496 | + "background whitelist. Revoked status: " |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 497 | + mRestrictBackgroundWhitelistRevokedUids.get(uid)); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 498 | if (!mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 499 | Slog.i(TAG, "adding default package " + pkg + " (uid " + uid + " for user " |
| 500 | + userId + ") to restrict background whitelist"); |
| 501 | mRestrictBackgroundWhitelistUids.append(uid, true); |
| 502 | changed = true; |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | return changed; |
| 506 | } |
| 507 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 508 | void updatePowerSaveTempWhitelistLocked() { |
| 509 | try { |
Amith Yamasani | 06f0806 | 2015-06-12 13:23:33 -0700 | [diff] [blame] | 510 | // Clear the states of the current whitelist |
| 511 | final int N = mPowerSaveTempWhitelistAppIds.size(); |
| 512 | for (int i = 0; i < N; i++) { |
| 513 | mPowerSaveTempWhitelistAppIds.setValueAt(i, false); |
| 514 | } |
| 515 | // Update the states with the new whitelist |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 516 | final int[] whitelist = mDeviceIdleController.getAppIdTempWhitelist(); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 517 | if (whitelist != null) { |
| 518 | for (int uid : whitelist) { |
| 519 | mPowerSaveTempWhitelistAppIds.put(uid, true); |
| 520 | } |
| 521 | } |
| 522 | } catch (RemoteException e) { |
| 523 | } |
| 524 | } |
| 525 | |
Amith Yamasani | 06f0806 | 2015-06-12 13:23:33 -0700 | [diff] [blame] | 526 | /** |
| 527 | * Remove unnecessary entries in the temp whitelist |
| 528 | */ |
| 529 | void purgePowerSaveTempWhitelistLocked() { |
| 530 | final int N = mPowerSaveTempWhitelistAppIds.size(); |
| 531 | for (int i = N - 1; i >= 0; i--) { |
| 532 | if (mPowerSaveTempWhitelistAppIds.valueAt(i) == false) { |
| 533 | mPowerSaveTempWhitelistAppIds.removeAt(i); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 538 | public void systemReady() { |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 539 | if (!isBandwidthControlEnabled()) { |
| 540 | Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy"); |
| 541 | return; |
| 542 | } |
| 543 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 544 | mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
| 545 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 546 | mPackageMonitor.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
| 547 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 548 | synchronized (mRulesLock) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 549 | updatePowerSaveWhitelistLocked(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 550 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 551 | mPowerManagerInternal.registerLowPowerModeObserver( |
| 552 | new PowerManagerInternal.LowPowerModeListener() { |
| 553 | @Override |
| 554 | public void onLowPowerModeChanged(boolean enabled) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 555 | if (LOGD) Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")"); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 556 | synchronized (mRulesLock) { |
| 557 | if (mRestrictPower != enabled) { |
| 558 | mRestrictPower = enabled; |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 559 | updateRulesForRestrictPowerLocked(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | }); |
| 564 | mRestrictPower = mPowerManagerInternal.getLowPowerModeEnabled(); |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 565 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 566 | mSystemReady = true; |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 567 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 568 | // read policy from disk |
| 569 | readPolicyLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 570 | |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 571 | if (addDefaultRestrictBackgroundWhitelistUidsLocked()) { |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 572 | writePolicyLocked(); |
| 573 | } |
| 574 | |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 575 | setRestrictBackgroundLocked(mRestrictBackground); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 576 | updateRulesForGlobalChangeLocked(false); |
| 577 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 578 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 579 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 580 | updateScreenOn(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 581 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 582 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 583 | mActivityManager.registerUidObserver(mUidObserver, |
| 584 | ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 585 | mNetworkManager.registerObserver(mAlertObserver); |
| 586 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 587 | // ignored; both services live in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 590 | // TODO: traverse existing processes to know foreground state, or have |
| 591 | // activitymanager dispatch current state when new observer attached. |
| 592 | |
| 593 | final IntentFilter screenFilter = new IntentFilter(); |
| 594 | screenFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 595 | screenFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 596 | mContext.registerReceiver(mScreenReceiver, screenFilter); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 597 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 598 | // listen for changes to power save whitelist |
| 599 | final IntentFilter whitelistFilter = new IntentFilter( |
| 600 | PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 601 | mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler); |
| 602 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 603 | DeviceIdleController.LocalService deviceIdleService |
| 604 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
| 605 | deviceIdleService.setNetworkPolicyTempWhitelistCallback(mTempPowerSaveChangedCallback); |
| 606 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 607 | // watch for network interfaces to be claimed |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 608 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 609 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 610 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 611 | // listen for package changes to update policy |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 612 | final IntentFilter packageFilter = new IntentFilter(); |
| 613 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 614 | packageFilter.addDataScheme("package"); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 615 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 616 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 617 | // listen for UID changes to update policy |
| 618 | mContext.registerReceiver( |
| 619 | mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler); |
| 620 | |
| 621 | // listen for user changes to update policy |
| 622 | final IntentFilter userFilter = new IntentFilter(); |
| 623 | userFilter.addAction(ACTION_USER_ADDED); |
| 624 | userFilter.addAction(ACTION_USER_REMOVED); |
| 625 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 626 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 627 | // listen for stats update events |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 628 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 629 | mContext.registerReceiver( |
| 630 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 631 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 632 | // listen for restrict background changes from notifications |
| 633 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 634 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 635 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 636 | // listen for snooze warning from notifications |
| 637 | final IntentFilter snoozeWarningFilter = new IntentFilter(ACTION_SNOOZE_WARNING); |
| 638 | mContext.registerReceiver(mSnoozeWarningReceiver, snoozeWarningFilter, |
| 639 | MANAGE_NETWORK_POLICY, mHandler); |
| 640 | |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 641 | // listen for configured wifi networks to be removed |
| 642 | final IntentFilter wifiConfigFilter = new IntentFilter(CONFIGURED_NETWORKS_CHANGED_ACTION); |
Vinit Deshpande | 92d141f | 2014-09-10 18:05:10 -0700 | [diff] [blame] | 643 | mContext.registerReceiver(mWifiConfigReceiver, wifiConfigFilter, null, mHandler); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 644 | |
| 645 | // listen for wifi state changes to catch metered hint |
| 646 | final IntentFilter wifiStateFilter = new IntentFilter( |
| 647 | WifiManager.NETWORK_STATE_CHANGED_ACTION); |
Vinit Deshpande | 92d141f | 2014-09-10 18:05:10 -0700 | [diff] [blame] | 648 | mContext.registerReceiver(mWifiStateReceiver, wifiStateFilter, null, mHandler); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 649 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 650 | mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener()); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 651 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 654 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 655 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 656 | synchronized (mRulesLock) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 657 | updateUidStateLocked(uid, procState); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 658 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 661 | @Override public void onUidGone(int uid) throws RemoteException { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 662 | synchronized (mRulesLock) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 663 | removeUidStateLocked(uid); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 664 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 665 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 666 | |
| 667 | @Override public void onUidActive(int uid) throws RemoteException { |
| 668 | } |
| 669 | |
| 670 | @Override public void onUidIdle(int uid) throws RemoteException { |
| 671 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 672 | }; |
| 673 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 674 | final private BroadcastReceiver mPowerSaveWhitelistReceiver = new BroadcastReceiver() { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 675 | @Override |
| 676 | public void onReceive(Context context, Intent intent) { |
| 677 | // on background handler thread, and POWER_SAVE_WHITELIST_CHANGED is protected |
| 678 | synchronized (mRulesLock) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 679 | updatePowerSaveWhitelistLocked(); |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 680 | updateRulesForRestrictPowerLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | }; |
| 684 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 685 | final private Runnable mTempPowerSaveChangedCallback = new Runnable() { |
| 686 | @Override |
| 687 | public void run() { |
| 688 | synchronized (mRulesLock) { |
| 689 | updatePowerSaveTempWhitelistLocked(); |
| 690 | updateRulesForTempWhitelistChangeLocked(); |
| 691 | purgePowerSaveTempWhitelistLocked(); |
| 692 | } |
| 693 | } |
| 694 | }; |
| 695 | |
| 696 | final private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 697 | @Override |
| 698 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 29afa14 | 2012-12-04 17:21:21 -0800 | [diff] [blame] | 699 | // screen-related broadcasts are protected by system, no need |
| 700 | // for permissions check. |
| 701 | mHandler.obtainMessage(MSG_SCREEN_ON_CHANGED).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 702 | } |
| 703 | }; |
| 704 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 705 | final private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 706 | @Override |
| 707 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 708 | // on background handler thread, and PACKAGE_ADDED is protected |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 709 | |
| 710 | final String action = intent.getAction(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 711 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 712 | if (uid == -1) return; |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 713 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 714 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 715 | // update rules for UID, since it might be subject to |
| 716 | // global background data policy |
| 717 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
| 718 | synchronized (mRulesLock) { |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 719 | updateRestrictionRulesForUidLocked(uid); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 720 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | }; |
| 724 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 725 | final private BroadcastReceiver mUidRemovedReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 726 | @Override |
| 727 | public void onReceive(Context context, Intent intent) { |
| 728 | // on background handler thread, and UID_REMOVED is protected |
| 729 | |
| 730 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 731 | if (uid == -1) return; |
| 732 | |
| 733 | // remove any policy and update rules to clean up |
| 734 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
| 735 | synchronized (mRulesLock) { |
| 736 | mUidPolicy.delete(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 737 | updateRestrictionRulesForUidLocked(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 738 | writePolicyLocked(); |
| 739 | } |
| 740 | } |
| 741 | }; |
| 742 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 743 | final private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 744 | @Override |
| 745 | public void onReceive(Context context, Intent intent) { |
| 746 | // on background handler thread, and USER_ADDED and USER_REMOVED |
| 747 | // broadcasts are protected |
| 748 | |
| 749 | final String action = intent.getAction(); |
| 750 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 751 | if (userId == -1) return; |
| 752 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 753 | switch (action) { |
| 754 | case ACTION_USER_REMOVED: |
| 755 | case ACTION_USER_ADDED: |
| 756 | synchronized (mRulesLock) { |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 757 | // 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] | 758 | // USER_REMOVED, and one last sanity check during USER_ADDED |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 759 | removeUserStateLocked(userId, true); |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 760 | if (action == ACTION_USER_ADDED) { |
| 761 | // Add apps that are whitelisted by default. |
| 762 | addDefaultRestrictBackgroundWhitelistUidsLocked(userId); |
| 763 | } |
| 764 | // Update global restrict for that user |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 765 | updateRulesForGlobalChangeLocked(true); |
| 766 | } |
| 767 | break; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | }; |
| 771 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 772 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 773 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 774 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 775 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 776 | final private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 777 | @Override |
| 778 | public void onReceive(Context context, Intent intent) { |
| 779 | // on background handler thread, and verified |
| 780 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 781 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 782 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 783 | synchronized (mRulesLock) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 784 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 785 | updateNotificationsLocked(); |
| 786 | } |
| 787 | } |
| 788 | }; |
| 789 | |
| 790 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 791 | * Receiver that watches for {@link Notification} control of |
| 792 | * {@link #mRestrictBackground}. |
| 793 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 794 | final private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 795 | @Override |
| 796 | public void onReceive(Context context, Intent intent) { |
| 797 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 798 | // permission above. |
| 799 | |
| 800 | setRestrictBackground(false); |
| 801 | } |
| 802 | }; |
| 803 | |
| 804 | /** |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 805 | * Receiver that watches for {@link Notification} control of |
| 806 | * {@link NetworkPolicy#lastWarningSnooze}. |
| 807 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 808 | final private BroadcastReceiver mSnoozeWarningReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 809 | @Override |
| 810 | public void onReceive(Context context, Intent intent) { |
| 811 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 812 | // permission above. |
| 813 | |
| 814 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 815 | performSnooze(template, TYPE_WARNING); |
| 816 | } |
| 817 | }; |
| 818 | |
| 819 | /** |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 820 | * Receiver that watches for {@link WifiConfiguration} to be changed. |
| 821 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 822 | final private BroadcastReceiver mWifiConfigReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 823 | @Override |
| 824 | public void onReceive(Context context, Intent intent) { |
| 825 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 826 | // permission above. |
| 827 | |
| 828 | final int reason = intent.getIntExtra(EXTRA_CHANGE_REASON, CHANGE_REASON_ADDED); |
| 829 | if (reason == CHANGE_REASON_REMOVED) { |
| 830 | final WifiConfiguration config = intent.getParcelableExtra( |
| 831 | EXTRA_WIFI_CONFIGURATION); |
Irfan Sheriff | 00a10a1 | 2012-04-27 21:24:17 -0700 | [diff] [blame] | 832 | if (config.SSID != null) { |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 833 | final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(config.SSID); |
Irfan Sheriff | 00a10a1 | 2012-04-27 21:24:17 -0700 | [diff] [blame] | 834 | synchronized (mRulesLock) { |
| 835 | if (mNetworkPolicy.containsKey(template)) { |
| 836 | mNetworkPolicy.remove(template); |
| 837 | writePolicyLocked(); |
| 838 | } |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | }; |
| 844 | |
| 845 | /** |
| 846 | * Receiver that watches {@link WifiInfo} state changes to infer metered |
| 847 | * state. Ignores hints when policy is user-defined. |
| 848 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 849 | final private BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 850 | @Override |
| 851 | public void onReceive(Context context, Intent intent) { |
| 852 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 853 | // permission above. |
| 854 | |
| 855 | // ignore when not connected |
| 856 | final NetworkInfo netInfo = intent.getParcelableExtra(EXTRA_NETWORK_INFO); |
| 857 | if (!netInfo.isConnected()) return; |
| 858 | |
| 859 | final WifiInfo info = intent.getParcelableExtra(EXTRA_WIFI_INFO); |
| 860 | final boolean meteredHint = info.getMeteredHint(); |
| 861 | |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 862 | final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(info.getSSID()); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 863 | synchronized (mRulesLock) { |
| 864 | NetworkPolicy policy = mNetworkPolicy.get(template); |
| 865 | if (policy == null && meteredHint) { |
| 866 | // policy doesn't exist, and AP is hinting that it's |
| 867 | // metered: create an inferred policy. |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 868 | policy = newWifiPolicy(template, meteredHint); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 869 | addNetworkPolicyLocked(policy); |
| 870 | |
| 871 | } else if (policy != null && policy.inferred) { |
| 872 | // policy exists, and was inferred: update its current |
| 873 | // metered state. |
| 874 | policy.metered = meteredHint; |
| 875 | |
| 876 | // since this is inferred for each wifi session, just update |
| 877 | // rules without persisting. |
| 878 | updateNetworkRulesLocked(); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | }; |
| 883 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 884 | static NetworkPolicy newWifiPolicy(NetworkTemplate template, boolean metered) { |
| 885 | return new NetworkPolicy(template, CYCLE_NONE, Time.TIMEZONE_UTC, |
| 886 | WARNING_DISABLED, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, |
| 887 | metered, true); |
| 888 | } |
| 889 | |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 890 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 891 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 892 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 893 | final private INetworkManagementEventObserver mAlertObserver |
| 894 | = new BaseNetworkObserver() { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 895 | @Override |
| 896 | public void limitReached(String limitName, String iface) { |
| 897 | // only someone like NMS should be calling us |
| 898 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 899 | |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 900 | if (!LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 901 | mHandler.obtainMessage(MSG_LIMIT_REACHED, iface).sendToTarget(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | }; |
| 905 | |
| 906 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 907 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 908 | * to show visible notifications as needed. |
| 909 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 910 | void updateNotificationsLocked() { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 911 | if (LOGV) Slog.v(TAG, "updateNotificationsLocked()"); |
| 912 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 913 | // keep track of previously active notifications |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 914 | final ArraySet<String> beforeNotifs = new ArraySet<String>(mActiveNotifs); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 915 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 916 | |
| 917 | // TODO: when switching to kernel notifications, compute next future |
| 918 | // cycle boundary to recompute notifications. |
| 919 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 920 | // examine stats for each active policy |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 921 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 922 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 923 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 924 | // ignore policies that aren't relevant to user |
| 925 | if (!isTemplateRelevant(policy.template)) continue; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 926 | if (!policy.hasCycle()) continue; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 927 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 928 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 929 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 930 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 931 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 932 | if (policy.isOverLimit(totalBytes)) { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 933 | if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 934 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes); |
| 935 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 936 | enqueueNotification(policy, TYPE_LIMIT, totalBytes); |
| 937 | notifyOverLimitLocked(policy.template); |
| 938 | } |
| 939 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 940 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 941 | notifyUnderLimitLocked(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 942 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 943 | if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 944 | enqueueNotification(policy, TYPE_WARNING, totalBytes); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 945 | } |
| 946 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 949 | // cancel stale notifications that we didn't renew above |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 950 | for (int i = beforeNotifs.size()-1; i >= 0; i--) { |
| 951 | final String tag = beforeNotifs.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 952 | if (!mActiveNotifs.contains(tag)) { |
| 953 | cancelNotification(tag); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Test if given {@link NetworkTemplate} is relevant to user based on |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 960 | * current device state, such as when |
| 961 | * {@link TelephonyManager#getSubscriberId()} matches. This is regardless of |
| 962 | * data connection status. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 963 | */ |
| 964 | private boolean isTemplateRelevant(NetworkTemplate template) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 965 | if (template.isMatchRuleMobile()) { |
| 966 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 967 | final SubscriptionManager sub = SubscriptionManager.from(mContext); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 968 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 969 | // Mobile template is relevant when any active subscriber matches |
| 970 | final int[] subIds = sub.getActiveSubscriptionIdList(); |
| 971 | for (int subId : subIds) { |
| 972 | final String subscriberId = tele.getSubscriberId(subId); |
| 973 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 974 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 975 | if (template.matches(probeIdent)) { |
| 976 | return true; |
Jeff Sharkey | 3a66cf3 | 2012-03-20 17:00:01 -0700 | [diff] [blame] | 977 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 978 | } |
| 979 | return false; |
| 980 | } else { |
| 981 | return true; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 982 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 986 | * Notify that given {@link NetworkTemplate} is over |
| 987 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 988 | */ |
| 989 | private void notifyOverLimitLocked(NetworkTemplate template) { |
| 990 | if (!mOverLimitNotified.contains(template)) { |
| 991 | mContext.startActivity(buildNetworkOverLimitIntent(template)); |
| 992 | mOverLimitNotified.add(template); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | private void notifyUnderLimitLocked(NetworkTemplate template) { |
| 997 | mOverLimitNotified.remove(template); |
| 998 | } |
| 999 | |
| 1000 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1001 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 1002 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 1003 | */ |
| 1004 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1005 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 1010 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 1011 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1012 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1013 | final String tag = buildNotificationTag(policy, type); |
| 1014 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 1015 | builder.setOnlyAlertOnce(true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1016 | builder.setWhen(0L); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 1017 | builder.setColor(mContext.getColor( |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 1018 | com.android.internal.R.color.system_notification_accent_color)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1019 | |
| 1020 | final Resources res = mContext.getResources(); |
| 1021 | switch (type) { |
| 1022 | case TYPE_WARNING: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1023 | final CharSequence title = res.getText(R.string.data_usage_warning_title); |
Jeff Sharkey | 8ca953d | 2011-09-14 19:56:11 -0700 | [diff] [blame] | 1024 | final CharSequence body = res.getString(R.string.data_usage_warning_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1025 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1026 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1027 | builder.setTicker(title); |
| 1028 | builder.setContentTitle(title); |
| 1029 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1030 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1031 | final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template); |
| 1032 | builder.setDeleteIntent(PendingIntent.getBroadcast( |
| 1033 | mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1034 | |
| 1035 | final Intent viewIntent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1036 | builder.setContentIntent(PendingIntent.getActivity( |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1037 | mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1038 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1039 | break; |
| 1040 | } |
| 1041 | case TYPE_LIMIT: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1042 | final CharSequence body = res.getText(R.string.data_usage_limit_body); |
| 1043 | |
| 1044 | final CharSequence title; |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1045 | int icon = R.drawable.stat_notify_disabled_data; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1046 | switch (policy.template.getMatchRule()) { |
| 1047 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1048 | title = res.getText(R.string.data_usage_3g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1049 | break; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1050 | case MATCH_MOBILE_4G: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1051 | title = res.getText(R.string.data_usage_4g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1052 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1053 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1054 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1055 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1056 | case MATCH_WIFI: |
| 1057 | title = res.getText(R.string.data_usage_wifi_limit_title); |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1058 | icon = R.drawable.stat_notify_error; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1059 | break; |
| 1060 | default: |
| 1061 | title = null; |
| 1062 | break; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1065 | builder.setOngoing(true); |
John Spurlock | aedebda | 2014-07-14 14:36:32 -0400 | [diff] [blame] | 1066 | builder.setSmallIcon(icon); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1067 | builder.setTicker(title); |
| 1068 | builder.setContentTitle(title); |
| 1069 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1070 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1071 | final Intent intent = buildNetworkOverLimitIntent(policy.template); |
| 1072 | builder.setContentIntent(PendingIntent.getActivity( |
| 1073 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 1074 | break; |
| 1075 | } |
| 1076 | case TYPE_LIMIT_SNOOZED: { |
| 1077 | final long overBytes = totalBytes - policy.limitBytes; |
| 1078 | final CharSequence body = res.getString(R.string.data_usage_limit_snoozed_body, |
| 1079 | Formatter.formatFileSize(mContext, overBytes)); |
| 1080 | |
| 1081 | final CharSequence title; |
| 1082 | switch (policy.template.getMatchRule()) { |
| 1083 | case MATCH_MOBILE_3G_LOWER: |
| 1084 | title = res.getText(R.string.data_usage_3g_limit_snoozed_title); |
| 1085 | break; |
| 1086 | case MATCH_MOBILE_4G: |
| 1087 | title = res.getText(R.string.data_usage_4g_limit_snoozed_title); |
| 1088 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1089 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1090 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 1091 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1092 | case MATCH_WIFI: |
| 1093 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 1094 | break; |
| 1095 | default: |
| 1096 | title = null; |
| 1097 | break; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1100 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1101 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1102 | builder.setTicker(title); |
| 1103 | builder.setContentTitle(title); |
| 1104 | builder.setContentText(body); |
| 1105 | |
| 1106 | final Intent intent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 1107 | builder.setContentIntent(PendingIntent.getActivity( |
| 1108 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // TODO: move to NotificationManager once we can mock it |
| 1114 | try { |
| 1115 | final String packageName = mContext.getPackageName(); |
| 1116 | final int[] idReceived = new int[1]; |
| 1117 | mNotifManager.enqueueNotificationWithTag( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1118 | packageName, packageName, tag, 0x0, builder.getNotification(), idReceived, |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 1119 | UserHandle.USER_ALL); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1120 | mActiveNotifs.add(tag); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1121 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1122 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1126 | private void cancelNotification(String tag) { |
| 1127 | // TODO: move to NotificationManager once we can mock it |
| 1128 | try { |
| 1129 | final String packageName = mContext.getPackageName(); |
| 1130 | mNotifManager.cancelNotificationWithTag( |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 1131 | packageName, tag, 0x0, UserHandle.USER_ALL); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1132 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1133 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1138 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1139 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1140 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1141 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1142 | @Override |
| 1143 | public void onReceive(Context context, Intent intent) { |
| 1144 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 1145 | // permission above. |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1146 | |
| 1147 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1148 | synchronized (mRulesLock) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1149 | ensureActiveMobilePolicyLocked(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1150 | normalizePoliciesLocked(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1151 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1152 | updateNetworkRulesLocked(); |
| 1153 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | }; |
| 1157 | |
| 1158 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1159 | * Proactively control network data connections when they exceed |
| 1160 | * {@link NetworkPolicy#limitBytes}. |
| 1161 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1162 | void updateNetworkEnabledLocked() { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1163 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledLocked()"); |
| 1164 | |
| 1165 | // TODO: reset any policy-disabled networks when any policy is removed |
| 1166 | // completely, which is currently rare case. |
| 1167 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1168 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1169 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 1170 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1171 | // shortcut when policy has no limit |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1172 | if (policy.limitBytes == LIMIT_DISABLED || !policy.hasCycle()) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1173 | setNetworkTemplateEnabled(policy.template, true); |
| 1174 | continue; |
| 1175 | } |
| 1176 | |
| 1177 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 1178 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1179 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1180 | |
| 1181 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1182 | final boolean overLimitWithoutSnooze = policy.isOverLimit(totalBytes) |
| 1183 | && policy.lastLimitSnooze < start; |
| 1184 | final boolean networkEnabled = !overLimitWithoutSnooze; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1185 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1186 | setNetworkTemplateEnabled(policy.template, networkEnabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1191 | * Proactively disable networks that match the given |
| 1192 | * {@link NetworkTemplate}. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1193 | */ |
| 1194 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1195 | // TODO: reach into ConnectivityManager to proactively disable bringing |
| 1196 | // up this network, since we know that traffic will be blocked. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1200 | * Examine all connected {@link NetworkState}, looking for |
| 1201 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 1202 | * remaining quota based on usage cycle and historical stats. |
| 1203 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1204 | void updateNetworkRulesLocked() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1205 | if (LOGV) Slog.v(TAG, "updateNetworkRulesLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1206 | |
| 1207 | final NetworkState[] states; |
| 1208 | try { |
| 1209 | states = mConnManager.getAllNetworkState(); |
| 1210 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1211 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1212 | return; |
| 1213 | } |
| 1214 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1215 | // First, generate identities of all connected networks so we can |
| 1216 | // quickly compare them against all defined policies below. |
| 1217 | final ArrayList<Pair<String, NetworkIdentity>> connIdents = new ArrayList<>(states.length); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1218 | final ArraySet<String> connIfaces = new ArraySet<String>(states.length); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1219 | for (NetworkState state : states) { |
Wei Liu | b8eaf45 | 2016-01-25 10:32:27 -0800 | [diff] [blame] | 1220 | if (state.networkInfo != null && state.networkInfo.isConnected()) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1221 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1222 | |
| 1223 | final String baseIface = state.linkProperties.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1224 | if (baseIface != null) { |
| 1225 | connIdents.add(Pair.create(baseIface, ident)); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | // Stacked interfaces are considered to have same identity as |
| 1229 | // their parent network. |
| 1230 | final List<LinkProperties> stackedLinks = state.linkProperties.getStackedLinks(); |
| 1231 | for (LinkProperties stackedLink : stackedLinks) { |
| 1232 | final String stackedIface = stackedLink.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1233 | if (stackedIface != null) { |
| 1234 | connIdents.add(Pair.create(stackedIface, ident)); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1235 | } |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1236 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1240 | // Apply policies against all connected interfaces found above |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1241 | mNetworkRules.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1242 | final ArrayList<String> ifaceList = Lists.newArrayList(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1243 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1244 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1245 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1246 | ifaceList.clear(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1247 | for (int j = connIdents.size() - 1; j >= 0; j--) { |
| 1248 | final Pair<String, NetworkIdentity> ident = connIdents.get(j); |
| 1249 | if (policy.template.matches(ident.second)) { |
| 1250 | ifaceList.add(ident.first); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | if (ifaceList.size() > 0) { |
| 1255 | final String[] ifaces = ifaceList.toArray(new String[ifaceList.size()]); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1256 | mNetworkRules.put(policy, ifaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1260 | long lowestRule = Long.MAX_VALUE; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1261 | final ArraySet<String> newMeteredIfaces = new ArraySet<String>(states.length); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1262 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1263 | // apply each policy that we found ifaces for; compute remaining data |
| 1264 | // based on current cycle and historical stats, and push to kernel. |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1265 | final long currentTime = currentTimeMillis(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1266 | for (int i = mNetworkRules.size()-1; i >= 0; i--) { |
| 1267 | final NetworkPolicy policy = mNetworkRules.keyAt(i); |
| 1268 | final String[] ifaces = mNetworkRules.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1269 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1270 | final long start; |
| 1271 | final long totalBytes; |
| 1272 | if (policy.hasCycle()) { |
| 1273 | start = computeLastCycleBoundary(currentTime, policy); |
| 1274 | totalBytes = getTotalBytes(policy.template, start, currentTime); |
| 1275 | } else { |
| 1276 | start = Long.MAX_VALUE; |
| 1277 | totalBytes = 0; |
| 1278 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1279 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1280 | if (LOGD) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 1281 | Slog.d(TAG, "applying policy " + policy + " to ifaces " + Arrays.toString(ifaces)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1284 | final boolean hasWarning = policy.warningBytes != LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1285 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1286 | if (hasLimit || policy.metered) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1287 | final long quotaBytes; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1288 | if (!hasLimit) { |
| 1289 | // metered network, but no policy limit; we still need to |
| 1290 | // restrict apps, so push really high quota. |
| 1291 | quotaBytes = Long.MAX_VALUE; |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1292 | } else if (policy.lastLimitSnooze >= start) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1293 | // snoozing past quota, but we still need to restrict apps, |
| 1294 | // so push really high quota. |
| 1295 | quotaBytes = Long.MAX_VALUE; |
| 1296 | } else { |
| 1297 | // remaining "quota" bytes are based on total usage in |
| 1298 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 1299 | // set 1-byte quota and disable the radio later. |
| 1300 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 1301 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1302 | |
| 1303 | if (ifaces.length > 1) { |
| 1304 | // TODO: switch to shared quota once NMS supports |
| 1305 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1308 | for (String iface : ifaces) { |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1309 | // long quotaBytes split up into two ints to fit in message |
| 1310 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, |
| 1311 | (int) (quotaBytes >> 32), (int) (quotaBytes & 0xFFFFFFFF), iface) |
| 1312 | .sendToTarget(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1313 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1316 | |
| 1317 | // keep track of lowest warning or limit of active policies |
| 1318 | if (hasWarning && policy.warningBytes < lowestRule) { |
| 1319 | lowestRule = policy.warningBytes; |
| 1320 | } |
| 1321 | if (hasLimit && policy.limitBytes < lowestRule) { |
| 1322 | lowestRule = policy.limitBytes; |
| 1323 | } |
| 1324 | } |
| 1325 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1326 | for (int i = connIfaces.size()-1; i >= 0; i--) { |
| 1327 | String iface = connIfaces.valueAt(i); |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1328 | // long quotaBytes split up into two ints to fit in message |
| 1329 | mHandler.obtainMessage(MSG_UPDATE_INTERFACE_QUOTA, |
| 1330 | (int) (Long.MAX_VALUE >> 32), (int) (Long.MAX_VALUE & 0xFFFFFFFF), iface) |
| 1331 | .sendToTarget(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1332 | newMeteredIfaces.add(iface); |
| 1333 | } |
| 1334 | |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 1335 | mHandler.obtainMessage(MSG_ADVISE_PERSIST_THRESHOLD, lowestRule).sendToTarget(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1336 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1337 | // remove quota on any trailing interfaces |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1338 | for (int i = mMeteredIfaces.size() - 1; i >= 0; i--) { |
| 1339 | final String iface = mMeteredIfaces.valueAt(i); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1340 | if (!newMeteredIfaces.contains(iface)) { |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 1341 | mHandler.obtainMessage(MSG_REMOVE_INTERFACE_QUOTA, iface) |
| 1342 | .sendToTarget(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | mMeteredIfaces = newMeteredIfaces; |
| 1346 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1347 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1348 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 1353 | * have at least a default mobile policy defined. |
| 1354 | */ |
| 1355 | private void ensureActiveMobilePolicyLocked() { |
| 1356 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyLocked()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1357 | if (mSuppressDefaultPolicy) return; |
| 1358 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1359 | final TelephonyManager tele = TelephonyManager.from(mContext); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1360 | final SubscriptionManager sub = SubscriptionManager.from(mContext); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1361 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1362 | final int[] subIds = sub.getActiveSubscriptionIdList(); |
| 1363 | for (int subId : subIds) { |
| 1364 | final String subscriberId = tele.getSubscriberId(subId); |
| 1365 | ensureActiveMobilePolicyLocked(subscriberId); |
| 1366 | } |
| 1367 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1368 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1369 | private void ensureActiveMobilePolicyLocked(String subscriberId) { |
| 1370 | // Poke around to see if we already have a policy |
| 1371 | final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE, |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 1372 | TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1373 | for (int i = mNetworkPolicy.size() - 1; i >= 0; i--) { |
| 1374 | final NetworkTemplate template = mNetworkPolicy.keyAt(i); |
| 1375 | if (template.matches(probeIdent)) { |
| 1376 | if (LOGD) { |
| 1377 | Slog.d(TAG, "Found template " + template + " which matches subscriber " |
| 1378 | + NetworkIdentity.scrubSubscriberId(subscriberId)); |
| 1379 | } |
| 1380 | return; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1384 | Slog.i(TAG, "No policy for subscriber " + NetworkIdentity.scrubSubscriberId(subscriberId) |
| 1385 | + "; generating default policy"); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1386 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1387 | // Build default mobile policy, and assume usage cycle starts today |
| 1388 | final long warningBytes = mContext.getResources().getInteger( |
| 1389 | com.android.internal.R.integer.config_networkPolicyDefaultWarning) * MB_IN_BYTES; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1390 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1391 | final Time time = new Time(); |
| 1392 | time.setToNow(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1393 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1394 | final int cycleDay = time.monthDay; |
| 1395 | final String cycleTimezone = time.timezone; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1396 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1397 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
| 1398 | final NetworkPolicy policy = new NetworkPolicy(template, cycleDay, cycleTimezone, |
| 1399 | warningBytes, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, true); |
| 1400 | addNetworkPolicyLocked(policy); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1403 | private void readPolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1404 | if (LOGV) Slog.v(TAG, "readPolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1405 | |
| 1406 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1407 | mNetworkPolicy.clear(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1408 | mUidPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1409 | |
| 1410 | FileInputStream fis = null; |
| 1411 | try { |
| 1412 | fis = mPolicyFile.openRead(); |
| 1413 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1414 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1415 | |
| 1416 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1417 | int version = VERSION_INIT; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1418 | boolean insideWhitelist = false; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1419 | while ((type = in.next()) != END_DOCUMENT) { |
| 1420 | final String tag = in.getName(); |
| 1421 | if (type == START_TAG) { |
| 1422 | if (TAG_POLICY_LIST.equals(tag)) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1423 | final boolean oldValue = mRestrictBackground; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1424 | version = readIntAttribute(in, ATTR_VERSION); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1425 | if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) { |
| 1426 | mRestrictBackground = readBooleanAttribute( |
| 1427 | in, ATTR_RESTRICT_BACKGROUND); |
| 1428 | } else { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1429 | mRestrictBackground = false; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1430 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1431 | if (mRestrictBackground != oldValue) { |
| 1432 | // Some early services may have read the default value, |
| 1433 | // so notify them that it's changed |
| 1434 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, |
| 1435 | mRestrictBackground ? 1 : 0, 0).sendToTarget(); |
| 1436 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1437 | |
| 1438 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 1439 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 1440 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1441 | final String networkId; |
| 1442 | if (version >= VERSION_ADDED_NETWORK_ID) { |
| 1443 | networkId = in.getAttributeValue(null, ATTR_NETWORK_ID); |
| 1444 | } else { |
| 1445 | networkId = null; |
| 1446 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1447 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1448 | final String cycleTimezone; |
| 1449 | if (version >= VERSION_ADDED_TIMEZONE) { |
| 1450 | cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE); |
| 1451 | } else { |
| 1452 | cycleTimezone = Time.TIMEZONE_UTC; |
| 1453 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1454 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 1455 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1456 | final long lastLimitSnooze; |
| 1457 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1458 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE); |
| 1459 | } else if (version >= VERSION_ADDED_SNOOZE) { |
| 1460 | lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1461 | } else { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1462 | lastLimitSnooze = SNOOZE_NEVER; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1463 | } |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1464 | final boolean metered; |
| 1465 | if (version >= VERSION_ADDED_METERED) { |
| 1466 | metered = readBooleanAttribute(in, ATTR_METERED); |
| 1467 | } else { |
| 1468 | switch (networkTemplate) { |
| 1469 | case MATCH_MOBILE_3G_LOWER: |
| 1470 | case MATCH_MOBILE_4G: |
| 1471 | case MATCH_MOBILE_ALL: |
| 1472 | metered = true; |
| 1473 | break; |
| 1474 | default: |
| 1475 | metered = false; |
| 1476 | } |
| 1477 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1478 | final long lastWarningSnooze; |
| 1479 | if (version >= VERSION_SPLIT_SNOOZE) { |
| 1480 | lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE); |
| 1481 | } else { |
| 1482 | lastWarningSnooze = SNOOZE_NEVER; |
| 1483 | } |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1484 | final boolean inferred; |
| 1485 | if (version >= VERSION_ADDED_INFERRED) { |
| 1486 | inferred = readBooleanAttribute(in, ATTR_INFERRED); |
| 1487 | } else { |
| 1488 | inferred = false; |
| 1489 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1490 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1491 | final NetworkTemplate template = new NetworkTemplate(networkTemplate, |
| 1492 | subscriberId, networkId); |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1493 | if (template.isPersistable()) { |
| 1494 | mNetworkPolicy.put(template, new NetworkPolicy(template, cycleDay, |
| 1495 | cycleTimezone, warningBytes, limitBytes, lastWarningSnooze, |
| 1496 | lastLimitSnooze, metered, inferred)); |
| 1497 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1498 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1499 | } else if (TAG_UID_POLICY.equals(tag)) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1500 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1501 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1502 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1503 | if (UserHandle.isApp(uid)) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1504 | setUidPolicyUncheckedLocked(uid, policy, false); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1505 | } else { |
| 1506 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 1507 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1508 | } else if (TAG_APP_POLICY.equals(tag)) { |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1509 | final int appId = readIntAttribute(in, ATTR_APP_ID); |
| 1510 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 1511 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1512 | // TODO: set for other users during upgrade |
Xiaohui Chen | be3b067 | 2015-09-02 13:29:22 -0700 | [diff] [blame] | 1513 | // app policy is deprecated so this is only used in pre system user split. |
| 1514 | final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1515 | if (UserHandle.isApp(uid)) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1516 | setUidPolicyUncheckedLocked(uid, policy, false); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1517 | } else { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1518 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 1519 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1520 | } else if (TAG_WHITELIST.equals(tag)) { |
| 1521 | insideWhitelist = true; |
| 1522 | } else if (TAG_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1523 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1524 | mRestrictBackgroundWhitelistUids.put(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 1525 | } else if (TAG_REVOKED_RESTRICT_BACKGROUND.equals(tag) && insideWhitelist) { |
| 1526 | final int uid = readIntAttribute(in, ATTR_UID); |
| 1527 | mRestrictBackgroundWhitelistRevokedUids.put(uid, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1528 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1529 | } else if (type == END_TAG) { |
| 1530 | if (TAG_WHITELIST.equals(tag)) { |
| 1531 | insideWhitelist = false; |
| 1532 | } |
| 1533 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | } catch (FileNotFoundException e) { |
| 1538 | // missing policy is okay, probably first boot |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1539 | upgradeLegacyBackgroundData(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1540 | } catch (IOException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1541 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1542 | } catch (XmlPullParserException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1543 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1544 | } finally { |
| 1545 | IoUtils.closeQuietly(fis); |
| 1546 | } |
| 1547 | } |
| 1548 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1549 | /** |
| 1550 | * Upgrade legacy background data flags, notifying listeners of one last |
| 1551 | * change to always-true. |
| 1552 | */ |
| 1553 | private void upgradeLegacyBackgroundData() { |
| 1554 | mRestrictBackground = Settings.Secure.getInt( |
| 1555 | mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, 1) != 1; |
| 1556 | |
| 1557 | // kick off one last broadcast if restricted |
| 1558 | if (mRestrictBackground) { |
| 1559 | final Intent broadcast = new Intent( |
| 1560 | ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1561 | mContext.sendBroadcastAsUser(broadcast, UserHandle.ALL); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1565 | void writePolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1566 | if (LOGV) Slog.v(TAG, "writePolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1567 | |
| 1568 | FileOutputStream fos = null; |
| 1569 | try { |
| 1570 | fos = mPolicyFile.startWrite(); |
| 1571 | |
| 1572 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1573 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1574 | out.startDocument(null, true); |
| 1575 | |
| 1576 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1577 | writeIntAttribute(out, ATTR_VERSION, VERSION_LATEST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1578 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1579 | |
| 1580 | // write all known network policies |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1581 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 1582 | final NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1583 | final NetworkTemplate template = policy.template; |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 1584 | if (!template.isPersistable()) continue; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1585 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1586 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1587 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 1588 | final String subscriberId = template.getSubscriberId(); |
| 1589 | if (subscriberId != null) { |
| 1590 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1591 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1592 | final String networkId = template.getNetworkId(); |
| 1593 | if (networkId != null) { |
| 1594 | out.attribute(null, ATTR_NETWORK_ID, networkId); |
| 1595 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1596 | writeIntAttribute(out, ATTR_CYCLE_DAY, policy.cycleDay); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1597 | out.attribute(null, ATTR_CYCLE_TIMEZONE, policy.cycleTimezone); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1598 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 1599 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1600 | writeLongAttribute(out, ATTR_LAST_WARNING_SNOOZE, policy.lastWarningSnooze); |
| 1601 | writeLongAttribute(out, ATTR_LAST_LIMIT_SNOOZE, policy.lastLimitSnooze); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1602 | writeBooleanAttribute(out, ATTR_METERED, policy.metered); |
Jeff Sharkey | 837f924 | 2012-03-20 16:52:20 -0700 | [diff] [blame] | 1603 | writeBooleanAttribute(out, ATTR_INFERRED, policy.inferred); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1604 | out.endTag(null, TAG_NETWORK_POLICY); |
| 1605 | } |
| 1606 | |
| 1607 | // write all known uid policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1608 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1609 | final int uid = mUidPolicy.keyAt(i); |
| 1610 | final int policy = mUidPolicy.valueAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1611 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1612 | // skip writing empty policies |
| 1613 | if (policy == POLICY_NONE) continue; |
| 1614 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1615 | out.startTag(null, TAG_UID_POLICY); |
| 1616 | writeIntAttribute(out, ATTR_UID, uid); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1617 | writeIntAttribute(out, ATTR_POLICY, policy); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1618 | out.endTag(null, TAG_UID_POLICY); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | out.endTag(null, TAG_POLICY_LIST); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1622 | |
| 1623 | // write all whitelists |
| 1624 | out.startTag(null, TAG_WHITELIST); |
| 1625 | |
| 1626 | // restrict background whitelist |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 1627 | int size = mRestrictBackgroundWhitelistUids.size(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1628 | for (int i = 0; i < size; i++) { |
| 1629 | final int uid = mRestrictBackgroundWhitelistUids.keyAt(i); |
| 1630 | out.startTag(null, TAG_RESTRICT_BACKGROUND); |
| 1631 | writeIntAttribute(out, ATTR_UID, uid); |
| 1632 | out.endTag(null, TAG_RESTRICT_BACKGROUND); |
| 1633 | } |
| 1634 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 1635 | // revoked restrict background whitelist |
| 1636 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 1637 | for (int i = 0; i < size; i++) { |
| 1638 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 1639 | out.startTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 1640 | writeIntAttribute(out, ATTR_UID, uid); |
| 1641 | out.endTag(null, TAG_REVOKED_RESTRICT_BACKGROUND); |
| 1642 | } |
| 1643 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 1644 | out.endTag(null, TAG_WHITELIST); |
| 1645 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1646 | out.endDocument(); |
| 1647 | |
| 1648 | mPolicyFile.finishWrite(fos); |
| 1649 | } catch (IOException e) { |
| 1650 | if (fos != null) { |
| 1651 | mPolicyFile.failWrite(fos); |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1656 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1657 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1658 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1659 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1660 | if (!UserHandle.isApp(uid)) { |
| 1661 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1664 | synchronized (mRulesLock) { |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 1665 | final long token = Binder.clearCallingIdentity(); |
| 1666 | try { |
| 1667 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1668 | if (oldPolicy != policy) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1669 | setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true); |
Julia Reynolds | 72f83d6 | 2015-07-27 15:10:42 -0400 | [diff] [blame] | 1670 | } |
| 1671 | } finally { |
| 1672 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1677 | @Override |
| 1678 | public void addUidPolicy(int uid, int policy) { |
| 1679 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1680 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1681 | if (!UserHandle.isApp(uid)) { |
| 1682 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 1683 | } |
| 1684 | |
| 1685 | synchronized (mRulesLock) { |
| 1686 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1687 | policy |= oldPolicy; |
| 1688 | if (oldPolicy != policy) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1689 | setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1690 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1691 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | @Override |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1695 | public void removeUidPolicy(int uid, int policy) { |
| 1696 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1697 | |
| 1698 | if (!UserHandle.isApp(uid)) { |
| 1699 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 1700 | } |
| 1701 | |
| 1702 | synchronized (mRulesLock) { |
| 1703 | final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 1704 | policy = oldPolicy & ~policy; |
| 1705 | if (oldPolicy != policy) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1706 | setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1711 | private void setUidPolicyUncheckedLocked(int uid, int oldPolicy, int policy, boolean persist) { |
| 1712 | setUidPolicyUncheckedLocked(uid, policy, persist); |
| 1713 | |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 1714 | final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND; |
| 1715 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_BLACKLIST_CHANGED, uid, |
| 1716 | isBlacklisted ? 1 : 0).sendToTarget(); |
| 1717 | |
| 1718 | final boolean wasBlacklisted = oldPolicy == POLICY_REJECT_METERED_BACKGROUND; |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1719 | // Checks if app was added or removed to the blacklist. |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 1720 | if ((oldPolicy == POLICY_NONE && isBlacklisted) |
| 1721 | || (wasBlacklisted && policy == POLICY_NONE)) { |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 1722 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 1, null) |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 1723 | .sendToTarget(); |
| 1724 | } |
| 1725 | } |
| 1726 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1727 | private void setUidPolicyUncheckedLocked(int uid, int policy, boolean persist) { |
| 1728 | mUidPolicy.put(uid, policy); |
| 1729 | |
| 1730 | // uid policy changed, recompute rules and persist policy. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 1731 | updateRulesForDataUsageRestrictionsLocked(uid); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 1732 | if (persist) { |
| 1733 | writePolicyLocked(); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1738 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1739 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1740 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1741 | synchronized (mRulesLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1742 | return mUidPolicy.get(uid, POLICY_NONE); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1743 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1746 | @Override |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1747 | public int[] getUidsWithPolicy(int policy) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1748 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1749 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1750 | int[] uids = new int[0]; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1751 | synchronized (mRulesLock) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1752 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1753 | final int uid = mUidPolicy.keyAt(i); |
| 1754 | final int uidPolicy = mUidPolicy.valueAt(i); |
| 1755 | if (uidPolicy == policy) { |
| 1756 | uids = appendInt(uids, uid); |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1760 | return uids; |
| 1761 | } |
| 1762 | |
| 1763 | /** |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1764 | * Removes any persistable state associated with given {@link UserHandle}, persisting |
| 1765 | * if any changes that are made. |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1766 | */ |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1767 | boolean removeUserStateLocked(int userId, boolean writePolicy) { |
| 1768 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1769 | if (LOGV) Slog.v(TAG, "removeUserStateLocked()"); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1770 | boolean changed = false; |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1771 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1772 | // Remove entries from restricted background UID whitelist |
| 1773 | int[] wlUids = new int[0]; |
| 1774 | for (int i = 0; i < mRestrictBackgroundWhitelistUids.size(); i++) { |
| 1775 | final int uid = mRestrictBackgroundWhitelistUids.keyAt(i); |
| 1776 | if (UserHandle.getUserId(uid) == userId) { |
| 1777 | wlUids = appendInt(wlUids, uid); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (wlUids.length > 0) { |
| 1782 | for (int uid : wlUids) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 1783 | removeRestrictBackgroundWhitelistedUidLocked(uid, false, false); |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1784 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1785 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1786 | } |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1787 | |
| 1788 | // Remove entries from revoked default restricted background UID whitelist |
| 1789 | for (int i = mRestrictBackgroundWhitelistRevokedUids.size() - 1; i >= 0; i--) { |
| 1790 | final int uid = mRestrictBackgroundWhitelistRevokedUids.keyAt(i); |
| 1791 | if (UserHandle.getUserId(uid) == userId) { |
| 1792 | mRestrictBackgroundWhitelistRevokedUids.removeAt(i); |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1793 | changed = true; |
Felipe Leme | a110eec | 2016-04-29 09:58:06 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1797 | // Remove associated UID policies |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1798 | int[] uids = new int[0]; |
| 1799 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1800 | final int uid = mUidPolicy.keyAt(i); |
| 1801 | if (UserHandle.getUserId(uid) == userId) { |
| 1802 | uids = appendInt(uids, uid); |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | if (uids.length > 0) { |
| 1807 | for (int uid : uids) { |
| 1808 | mUidPolicy.delete(uid); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1809 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1810 | changed = true; |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 1813 | updateRulesForGlobalChangeLocked(true); |
| 1814 | |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1815 | if (writePolicy && changed) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 1816 | writePolicyLocked(); |
| 1817 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 1818 | return changed; |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1822 | public void setConnectivityListener(INetworkPolicyListener listener) { |
| 1823 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1824 | if (mConnectivityListener != null) { |
| 1825 | throw new IllegalStateException("Connectivity listener already registered"); |
| 1826 | } |
| 1827 | mConnectivityListener = listener; |
| 1828 | } |
| 1829 | |
| 1830 | @Override |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1831 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1832 | // TODO: create permission for observing network policy |
| 1833 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1834 | mListeners.register(listener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | @Override |
| 1838 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1839 | // TODO: create permission for observing network policy |
| 1840 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1841 | mListeners.unregister(listener); |
| 1842 | } |
| 1843 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1844 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1845 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1846 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1847 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 1848 | final long token = Binder.clearCallingIdentity(); |
| 1849 | try { |
| 1850 | maybeRefreshTrustedTime(); |
| 1851 | synchronized (mRulesLock) { |
| 1852 | normalizePoliciesLocked(policies); |
| 1853 | updateNetworkEnabledLocked(); |
| 1854 | updateNetworkRulesLocked(); |
| 1855 | updateNotificationsLocked(); |
| 1856 | writePolicyLocked(); |
| 1857 | } |
| 1858 | } finally { |
| 1859 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1863 | void addNetworkPolicyLocked(NetworkPolicy policy) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1864 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1865 | policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy); |
| 1866 | setNetworkPolicies(policies); |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1869 | @Override |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1870 | public NetworkPolicy[] getNetworkPolicies(String callingPackage) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1871 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 1872 | try { |
Amit Mahajan | a9e72a7 | 2015-07-30 16:04:13 -0700 | [diff] [blame] | 1873 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, TAG); |
| 1874 | // SKIP checking run-time OP_READ_PHONE_STATE since caller or self has PRIVILEGED |
| 1875 | // permission |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 1876 | } catch (SecurityException e) { |
| 1877 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1878 | |
Amit Mahajan | 7c5befa | 2015-07-14 10:26:00 -0700 | [diff] [blame] | 1879 | if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(), |
| 1880 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1881 | return new NetworkPolicy[0]; |
| 1882 | } |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1885 | synchronized (mRulesLock) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1886 | final int size = mNetworkPolicy.size(); |
| 1887 | final NetworkPolicy[] policies = new NetworkPolicy[size]; |
| 1888 | for (int i = 0; i < size; i++) { |
| 1889 | policies[i] = mNetworkPolicy.valueAt(i); |
| 1890 | } |
| 1891 | return policies; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | private void normalizePoliciesLocked() { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1896 | normalizePoliciesLocked(getNetworkPolicies(mContext.getOpPackageName())); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | private void normalizePoliciesLocked(NetworkPolicy[] policies) { |
| 1900 | final TelephonyManager tele = TelephonyManager.from(mContext); |
| 1901 | final String[] merged = tele.getMergedSubscriberIds(); |
| 1902 | |
| 1903 | mNetworkPolicy.clear(); |
| 1904 | for (NetworkPolicy policy : policies) { |
| 1905 | // When two normalized templates conflict, prefer the most |
| 1906 | // restrictive policy |
| 1907 | policy.template = NetworkTemplate.normalize(policy.template, merged); |
| 1908 | final NetworkPolicy existing = mNetworkPolicy.get(policy.template); |
| 1909 | if (existing == null || existing.compareTo(policy) > 0) { |
| 1910 | if (existing != null) { |
| 1911 | Slog.d(TAG, "Normalization replaced " + existing + " with " + policy); |
| 1912 | } |
| 1913 | mNetworkPolicy.put(policy.template, policy); |
| 1914 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | @Override |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1919 | public void snoozeLimit(NetworkTemplate template) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1920 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 6c0b4f3 | 2012-06-12 21:06:30 -0700 | [diff] [blame] | 1921 | |
| 1922 | final long token = Binder.clearCallingIdentity(); |
| 1923 | try { |
| 1924 | performSnooze(template, TYPE_LIMIT); |
| 1925 | } finally { |
| 1926 | Binder.restoreCallingIdentity(token); |
| 1927 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1928 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1929 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 1930 | void performSnooze(NetworkTemplate template, int type) { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1931 | maybeRefreshTrustedTime(); |
| 1932 | final long currentTime = currentTimeMillis(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1933 | synchronized (mRulesLock) { |
| 1934 | // find and snooze local policy that matches |
| 1935 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 1936 | if (policy == null) { |
| 1937 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 1938 | } |
| 1939 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1940 | switch (type) { |
| 1941 | case TYPE_WARNING: |
| 1942 | policy.lastWarningSnooze = currentTime; |
| 1943 | break; |
| 1944 | case TYPE_LIMIT: |
| 1945 | policy.lastLimitSnooze = currentTime; |
| 1946 | break; |
| 1947 | default: |
| 1948 | throw new IllegalArgumentException("unexpected type"); |
| 1949 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1950 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1951 | normalizePoliciesLocked(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1952 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1953 | updateNetworkRulesLocked(); |
| 1954 | updateNotificationsLocked(); |
| 1955 | writePolicyLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | @Override |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1960 | public void onTetheringChanged(String iface, boolean tethering) { |
| 1961 | // No need to enforce permission because setRestrictBackground() will do it. |
| 1962 | if (LOGD) Log.d(TAG, "onTetherStateChanged(" + iface + ", " + tethering + ")"); |
| 1963 | synchronized (mRulesLock) { |
| 1964 | if (mRestrictBackground && tethering) { |
| 1965 | Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver"); |
| 1966 | setRestrictBackground(false); |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1972 | public void setRestrictBackground(boolean restrictBackground) { |
| 1973 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 1974 | final long token = Binder.clearCallingIdentity(); |
| 1975 | try { |
| 1976 | maybeRefreshTrustedTime(); |
| 1977 | synchronized (mRulesLock) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 1978 | if (restrictBackground == mRestrictBackground) { |
| 1979 | // Ideally, UI should never allow this scenario... |
| 1980 | Slog.w(TAG, "setRestrictBackground: already " + restrictBackground); |
| 1981 | return; |
| 1982 | } |
| 1983 | setRestrictBackgroundLocked(restrictBackground); |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 1984 | } |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1985 | |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 1986 | } finally { |
| 1987 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1988 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1989 | |
| 1990 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_CHANGED, restrictBackground ? 1 : 0, 0) |
| 1991 | .sendToTarget(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 1994 | private void setRestrictBackgroundLocked(boolean restrictBackground) { |
| 1995 | final boolean oldRestrictBackground = mRestrictBackground; |
| 1996 | mRestrictBackground = restrictBackground; |
| 1997 | // Must whitelist foreground apps before turning data saver mode on. |
| 1998 | // TODO: there is no need to iterate through all apps here, just those in the foreground, |
| 1999 | // so it could call AM to get the UIDs of such apps, and iterate through them instead. |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2000 | updateRulesForAllAppsLocked(TYPE_RESTRICT_BACKGROUND); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2001 | try { |
| 2002 | if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) { |
| 2003 | Slog.e(TAG, "Could not change Data Saver Mode on NMS to " + mRestrictBackground); |
| 2004 | mRestrictBackground = oldRestrictBackground; |
| 2005 | // TODO: if it knew the foreground apps (see TODO above), it could call |
| 2006 | // updateRulesForRestrictBackgroundLocked() again to restore state. |
| 2007 | return; |
| 2008 | } |
| 2009 | } catch (RemoteException e) { |
| 2010 | // ignored; service lives in system_server |
| 2011 | } |
| 2012 | updateNotificationsLocked(); |
| 2013 | writePolicyLocked(); |
| 2014 | } |
| 2015 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2016 | @Override |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2017 | public void addRestrictBackgroundWhitelistedUid(int uid) { |
| 2018 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2019 | final boolean oldStatus; |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2020 | final boolean needFirewallRules; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2021 | synchronized (mRulesLock) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2022 | oldStatus = mRestrictBackgroundWhitelistUids.get(uid); |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2023 | if (oldStatus) { |
| 2024 | if (LOGD) Slog.d(TAG, "uid " + uid + " is already whitelisted"); |
| 2025 | return; |
| 2026 | } |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2027 | needFirewallRules = isUidValidForWhitelistRules(uid); |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2028 | Slog.i(TAG, "adding uid " + uid + " to restrict background whitelist"); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2029 | mRestrictBackgroundWhitelistUids.append(uid, true); |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2030 | if (mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2031 | && mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2032 | if (LOGD) Slog.d(TAG, "Removing uid " + uid |
| 2033 | + " from revoked restrict background whitelist"); |
| 2034 | mRestrictBackgroundWhitelistRevokedUids.delete(uid); |
| 2035 | } |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2036 | if (needFirewallRules) { |
| 2037 | // Only update firewall rules if necessary... |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2038 | updateRulesForDataUsageRestrictionsLocked(uid); |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2039 | } |
| 2040 | // ...but always persists the whitelist request. |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2041 | writePolicyLocked(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2042 | } |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 2043 | int changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0; |
| 2044 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed, |
| 2045 | Boolean.TRUE).sendToTarget(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | @Override |
| 2049 | public void removeRestrictBackgroundWhitelistedUid(int uid) { |
| 2050 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2051 | final boolean changed; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2052 | synchronized (mRulesLock) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2053 | changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2054 | } |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 2055 | mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed ? 1 : 0, |
| 2056 | Boolean.FALSE).sendToTarget(); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2059 | /** |
| 2060 | * Removes a uid from the restricted background whitelist, returning whether its current |
| 2061 | * {@link ConnectivityManager.RestrictBackgroundStatus} changed. |
| 2062 | */ |
| 2063 | private boolean removeRestrictBackgroundWhitelistedUidLocked(int uid, boolean uidDeleted, |
| 2064 | boolean updateNow) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2065 | final boolean oldStatus = mRestrictBackgroundWhitelistUids.get(uid); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2066 | if (!oldStatus && !uidDeleted) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2067 | if (LOGD) Slog.d(TAG, "uid " + uid + " was not whitelisted before"); |
| 2068 | return false; |
| 2069 | } |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2070 | final boolean needFirewallRules = uidDeleted || isUidValidForWhitelistRules(uid); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2071 | if (oldStatus) { |
| 2072 | Slog.i(TAG, "removing uid " + uid + " from restrict background whitelist"); |
| 2073 | mRestrictBackgroundWhitelistUids.delete(uid); |
| 2074 | } |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2075 | if (mDefaultRestrictBackgroundWhitelistUids.get(uid) |
| 2076 | && !mRestrictBackgroundWhitelistRevokedUids.get(uid)) { |
| 2077 | if (LOGD) Slog.d(TAG, "Adding uid " + uid |
| 2078 | + " to revoked restrict background whitelist"); |
| 2079 | mRestrictBackgroundWhitelistRevokedUids.append(uid, true); |
| 2080 | } |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2081 | if (needFirewallRules) { |
| 2082 | // Only update firewall rules if necessary... |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2083 | updateRulesForDataUsageRestrictionsLocked(uid, uidDeleted); |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2084 | } |
Felipe Leme | 3f52cd5 | 2016-02-03 13:36:30 -0800 | [diff] [blame] | 2085 | if (updateNow) { |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2086 | // ...but always persists the whitelist request. |
Fyodor Kupolov | a31c591 | 2016-01-22 11:26:09 -0800 | [diff] [blame] | 2087 | writePolicyLocked(); |
| 2088 | } |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2089 | // Status only changes if Data Saver is turned on (otherwise it is DISABLED, even if the |
| 2090 | // app was whitelisted before). |
Felipe Leme | 01e05e7 | 2016-03-31 10:09:24 -0700 | [diff] [blame] | 2091 | return mRestrictBackground && needFirewallRules; |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | @Override |
| 2095 | public int[] getRestrictBackgroundWhitelistedUids() { |
| 2096 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2097 | synchronized (mRulesLock) { |
| 2098 | final int size = mRestrictBackgroundWhitelistUids.size(); |
| 2099 | final int[] whitelist = new int[size]; |
| 2100 | for (int i = 0; i < size; i++) { |
| 2101 | whitelist[i] = mRestrictBackgroundWhitelistUids.keyAt(i); |
| 2102 | } |
| 2103 | if (LOGV) { |
| 2104 | Slog.v(TAG, "getRestrictBackgroundWhitelistedUids(): " |
| 2105 | + mRestrictBackgroundWhitelistUids); |
| 2106 | } |
| 2107 | return whitelist; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | @Override |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2112 | public int getRestrictBackgroundByCaller() { |
| 2113 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2114 | final int uid = Binder.getCallingUid(); |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2115 | |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2116 | synchronized (mRulesLock) { |
Felipe Leme | 923845f | 2016-03-02 13:42:48 -0800 | [diff] [blame] | 2117 | // Must clear identity because getUidPolicy() is restricted to system. |
| 2118 | final long token = Binder.clearCallingIdentity(); |
| 2119 | final int policy; |
| 2120 | try { |
| 2121 | policy = getUidPolicy(uid); |
| 2122 | } finally { |
| 2123 | Binder.restoreCallingIdentity(token); |
| 2124 | } |
| 2125 | if (policy == POLICY_REJECT_METERED_BACKGROUND) { |
| 2126 | // App is blacklisted. |
| 2127 | return RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2128 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 2129 | if (!mRestrictBackground) { |
| 2130 | return RESTRICT_BACKGROUND_STATUS_DISABLED; |
| 2131 | } |
| 2132 | return mRestrictBackgroundWhitelistUids.get(uid) |
| 2133 | ? RESTRICT_BACKGROUND_STATUS_WHITELISTED |
| 2134 | : RESTRICT_BACKGROUND_STATUS_ENABLED; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2139 | public boolean getRestrictBackground() { |
| 2140 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2141 | |
| 2142 | synchronized (mRulesLock) { |
| 2143 | return mRestrictBackground; |
| 2144 | } |
| 2145 | } |
| 2146 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2147 | @Override |
| 2148 | public void setDeviceIdleMode(boolean enabled) { |
| 2149 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2150 | |
| 2151 | synchronized (mRulesLock) { |
| 2152 | if (mDeviceIdleMode != enabled) { |
| 2153 | mDeviceIdleMode = enabled; |
| 2154 | if (mSystemReady) { |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2155 | // Device idle change means we need to rebuild rules for all |
| 2156 | // known apps, so do a global refresh. |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2157 | updateRulesForRestrictPowerLocked(); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2158 | } |
| 2159 | if (enabled) { |
| 2160 | EventLogTags.writeDeviceIdleOnPhase("net"); |
| 2161 | } else { |
| 2162 | EventLogTags.writeDeviceIdleOffPhase("net"); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2168 | private NetworkPolicy findPolicyForNetworkLocked(NetworkIdentity ident) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2169 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 2170 | NetworkPolicy policy = mNetworkPolicy.valueAt(i); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2171 | if (policy.template.matches(ident)) { |
| 2172 | return policy; |
| 2173 | } |
| 2174 | } |
| 2175 | return null; |
| 2176 | } |
| 2177 | |
| 2178 | @Override |
| 2179 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 2180 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 2181 | |
| 2182 | // only returns usage summary, so we don't require caller to have |
| 2183 | // READ_NETWORK_USAGE_HISTORY. |
| 2184 | final long token = Binder.clearCallingIdentity(); |
| 2185 | try { |
| 2186 | return getNetworkQuotaInfoUnchecked(state); |
| 2187 | } finally { |
| 2188 | Binder.restoreCallingIdentity(token); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | private NetworkQuotaInfo getNetworkQuotaInfoUnchecked(NetworkState state) { |
| 2193 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 2194 | |
| 2195 | final NetworkPolicy policy; |
| 2196 | synchronized (mRulesLock) { |
| 2197 | policy = findPolicyForNetworkLocked(ident); |
| 2198 | } |
| 2199 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2200 | if (policy == null || !policy.hasCycle()) { |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2201 | // missing policy means we can't derive useful quota info |
| 2202 | return null; |
| 2203 | } |
| 2204 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 2205 | final long currentTime = currentTimeMillis(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2206 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2207 | // find total bytes used under policy |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2208 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 2209 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2210 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2211 | |
| 2212 | // report soft and hard limits under policy |
| 2213 | final long softLimitBytes = policy.warningBytes != WARNING_DISABLED ? policy.warningBytes |
| 2214 | : NetworkQuotaInfo.NO_LIMIT; |
| 2215 | final long hardLimitBytes = policy.limitBytes != LIMIT_DISABLED ? policy.limitBytes |
| 2216 | : NetworkQuotaInfo.NO_LIMIT; |
| 2217 | |
| 2218 | return new NetworkQuotaInfo(totalBytes, softLimitBytes, hardLimitBytes); |
| 2219 | } |
| 2220 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2221 | @Override |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2222 | public boolean isNetworkMetered(NetworkState state) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2223 | if (state.networkInfo == null) { |
| 2224 | return false; |
| 2225 | } |
| 2226 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2227 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 2228 | |
Jeff Sharkey | f166f48 | 2012-04-30 15:59:21 -0700 | [diff] [blame] | 2229 | // roaming networks are always considered metered |
| 2230 | if (ident.getRoaming()) { |
| 2231 | return true; |
| 2232 | } |
| 2233 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2234 | final NetworkPolicy policy; |
| 2235 | synchronized (mRulesLock) { |
| 2236 | policy = findPolicyForNetworkLocked(ident); |
| 2237 | } |
| 2238 | |
| 2239 | if (policy != null) { |
| 2240 | return policy.metered; |
| 2241 | } else { |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 2242 | final int type = state.networkInfo.getType(); |
| 2243 | if (isNetworkTypeMobile(type) || type == TYPE_WIMAX) { |
| 2244 | return true; |
| 2245 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2246 | return false; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | @Override |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2251 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 2252 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2253 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2254 | final IndentingPrintWriter fout = new IndentingPrintWriter(writer, " "); |
| 2255 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2256 | final ArraySet<String> argSet = new ArraySet<String>(args.length); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2257 | for (String arg : args) { |
| 2258 | argSet.add(arg); |
| 2259 | } |
| 2260 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2261 | synchronized (mRulesLock) { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2262 | if (argSet.contains("--unsnooze")) { |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2263 | for (int i = mNetworkPolicy.size()-1; i >= 0; i--) { |
| 2264 | mNetworkPolicy.valueAt(i).clearSnooze(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2265 | } |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2266 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 2267 | normalizePoliciesLocked(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2268 | updateNetworkEnabledLocked(); |
| 2269 | updateNetworkRulesLocked(); |
| 2270 | updateNotificationsLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2271 | writePolicyLocked(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 2272 | |
| 2273 | fout.println("Cleared snooze timestamps"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 2274 | return; |
| 2275 | } |
| 2276 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2277 | fout.print("System ready: "); fout.println(mSystemReady); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 2278 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2279 | fout.print("Restrict power: "); fout.println(mRestrictPower); |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2280 | fout.print("Device idle: "); fout.println(mDeviceIdleMode); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2281 | fout.println("Network policies:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2282 | fout.increaseIndent(); |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2283 | for (int i = 0; i < mNetworkPolicy.size(); i++) { |
| 2284 | fout.println(mNetworkPolicy.valueAt(i).toString()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2285 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2286 | fout.decreaseIndent(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2287 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 2288 | fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces)); |
| 2289 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2290 | fout.println("Policy for UIDs:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2291 | fout.increaseIndent(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2292 | int size = mUidPolicy.size(); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2293 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2294 | final int uid = mUidPolicy.keyAt(i); |
| 2295 | final int policy = mUidPolicy.valueAt(i); |
| 2296 | fout.print("UID="); |
| 2297 | fout.print(uid); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2298 | fout.print(" policy="); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2299 | fout.print(DebugUtils.flagsToString(NetworkPolicyManager.class, "POLICY_", policy)); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2300 | fout.println(); |
| 2301 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2302 | fout.decreaseIndent(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2303 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2304 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 2305 | if (size > 0) { |
| 2306 | fout.println("Power save whitelist (except idle) app ids:"); |
| 2307 | fout.increaseIndent(); |
| 2308 | for (int i = 0; i < size; i++) { |
| 2309 | fout.print("UID="); |
| 2310 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 2311 | fout.print(": "); |
| 2312 | fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i)); |
| 2313 | fout.println(); |
| 2314 | } |
| 2315 | fout.decreaseIndent(); |
| 2316 | } |
| 2317 | |
Dianne Hackborn | be7c50e | 2014-06-30 14:43:28 -0700 | [diff] [blame] | 2318 | size = mPowerSaveWhitelistAppIds.size(); |
| 2319 | if (size > 0) { |
| 2320 | fout.println("Power save whitelist app ids:"); |
| 2321 | fout.increaseIndent(); |
| 2322 | for (int i = 0; i < size; i++) { |
| 2323 | fout.print("UID="); |
| 2324 | fout.print(mPowerSaveWhitelistAppIds.keyAt(i)); |
| 2325 | fout.print(": "); |
| 2326 | fout.print(mPowerSaveWhitelistAppIds.valueAt(i)); |
| 2327 | fout.println(); |
| 2328 | } |
| 2329 | fout.decreaseIndent(); |
| 2330 | } |
| 2331 | |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 2332 | size = mRestrictBackgroundWhitelistUids.size(); |
| 2333 | if (size > 0) { |
| 2334 | fout.println("Restrict background whitelist uids:"); |
| 2335 | fout.increaseIndent(); |
| 2336 | for (int i = 0; i < size; i++) { |
| 2337 | fout.print("UID="); |
| 2338 | fout.print(mRestrictBackgroundWhitelistUids.keyAt(i)); |
| 2339 | fout.println(); |
| 2340 | } |
| 2341 | fout.decreaseIndent(); |
| 2342 | } |
| 2343 | |
Felipe Leme | a9505cc | 2016-02-26 10:28:41 -0800 | [diff] [blame] | 2344 | size = mDefaultRestrictBackgroundWhitelistUids.size(); |
| 2345 | if (size > 0) { |
| 2346 | fout.println("Default restrict background whitelist uids:"); |
| 2347 | fout.increaseIndent(); |
| 2348 | for (int i = 0; i < size; i++) { |
| 2349 | fout.print("UID="); |
| 2350 | fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i)); |
| 2351 | fout.println(); |
| 2352 | } |
| 2353 | fout.decreaseIndent(); |
| 2354 | } |
| 2355 | |
| 2356 | size = mRestrictBackgroundWhitelistRevokedUids.size(); |
| 2357 | if (size > 0) { |
| 2358 | fout.println("Default restrict background whitelist uids revoked by users:"); |
| 2359 | fout.increaseIndent(); |
| 2360 | for (int i = 0; i < size; i++) { |
| 2361 | fout.print("UID="); |
| 2362 | fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i)); |
| 2363 | fout.println(); |
| 2364 | } |
| 2365 | fout.decreaseIndent(); |
| 2366 | } |
| 2367 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2368 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2369 | collectKeys(mUidState, knownUids); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2370 | collectKeys(mUidRules, knownUids); |
| 2371 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2372 | fout.println("Status for all known UIDs:"); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2373 | fout.increaseIndent(); |
Jeff Sharkey | 8a8b581 | 2012-03-21 18:13:36 -0700 | [diff] [blame] | 2374 | size = knownUids.size(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2375 | for (int i = 0; i < size; i++) { |
| 2376 | final int uid = knownUids.keyAt(i); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2377 | fout.print("UID="); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2378 | fout.print(uid); |
| 2379 | |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2380 | final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2381 | fout.print(" state="); |
| 2382 | fout.print(state); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2383 | if (state <= ActivityManager.PROCESS_STATE_TOP) { |
| 2384 | fout.print(" (fg)"); |
| 2385 | } else { |
| 2386 | fout.print(state <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 2387 | ? " (fg svc)" : " (bg)"); |
| 2388 | } |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2389 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2390 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2391 | fout.print(" rules="); |
| 2392 | fout.print(uidRulesToString(uidRules)); |
| 2393 | fout.println(); |
| 2394 | } |
| 2395 | fout.decreaseIndent(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2396 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2397 | fout.println("Status for just UIDs with rules:"); |
| 2398 | fout.increaseIndent(); |
| 2399 | size = mUidRules.size(); |
| 2400 | for (int i = 0; i < size; i++) { |
| 2401 | final int uid = mUidRules.keyAt(i); |
| 2402 | fout.print("UID="); |
| 2403 | fout.print(uid); |
| 2404 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2405 | fout.print(" rules="); |
| 2406 | fout.print(uidRulesToString(uidRules)); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2407 | fout.println(); |
| 2408 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2409 | fout.decreaseIndent(); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2412 | |
| 2413 | @Override |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2414 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 2415 | String[] args, ResultReceiver resultReceiver) throws RemoteException { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 2416 | (new NetworkPolicyManagerShellCommand(mContext, this)).exec( |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 2417 | this, in, out, err, args, resultReceiver); |
| 2418 | } |
| 2419 | |
| 2420 | @Override |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2421 | public boolean isUidForeground(int uid) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 2422 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2423 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2424 | synchronized (mRulesLock) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2425 | return isUidForegroundLocked(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2426 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2429 | private boolean isUidForegroundLocked(int uid) { |
| 2430 | return isUidStateForegroundLocked( |
| 2431 | mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY)); |
| 2432 | } |
| 2433 | |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2434 | private boolean isUidForegroundOnRestrictBackgroundLocked(int uid) { |
| 2435 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2436 | return isProcStateAllowedWhileOnRestrictBackgroundLocked(procState); |
| 2437 | } |
| 2438 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2439 | private boolean isUidForegroundOnRestrictPowerLocked(int uid) { |
| 2440 | final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2441 | return isProcStateAllowedWhileIdleOrPowerSaveMode(procState); |
| 2442 | } |
| 2443 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2444 | private boolean isUidStateForegroundLocked(int state) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2445 | // only really in foreground when screen is also on |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2446 | return mScreenOn && state <= ActivityManager.PROCESS_STATE_TOP; |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2449 | /** |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2450 | * Process state of UID changed; if needed, will trigger |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2451 | * {@link #updateRulesForDataUsageRestrictionsLocked(int)} and |
| 2452 | * {@link #updateRulesForPowerRestrictionsLocked(int)} |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2453 | */ |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2454 | private void updateUidStateLocked(int uid, int uidState) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2455 | final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
| 2456 | if (oldUidState != uidState) { |
| 2457 | // state changed, push updated rules |
| 2458 | mUidState.put(uid, uidState); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2459 | updateRestrictBackgroundRulesOnUidStatusChangedLocked(uid, oldUidState, uidState); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2460 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState) |
| 2461 | != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2462 | if (isUidIdle(uid)) { |
| 2463 | updateRuleForAppIdleLocked(uid); |
| 2464 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2465 | if (mDeviceIdleMode) { |
| 2466 | updateRuleForDeviceIdleLocked(uid); |
| 2467 | } |
| 2468 | if (mRestrictPower) { |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2469 | updateRuleForRestrictPowerLocked(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2470 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2471 | updateRulesForPowerRestrictionsLocked(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2472 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2473 | updateNetworkStats(uid, isUidStateForegroundLocked(uidState)); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2477 | private void removeUidStateLocked(int uid) { |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2478 | final int index = mUidState.indexOfKey(uid); |
| 2479 | if (index >= 0) { |
| 2480 | final int oldUidState = mUidState.valueAt(index); |
| 2481 | mUidState.removeAt(index); |
| 2482 | if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) { |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2483 | updateRestrictBackgroundRulesOnUidStatusChangedLocked(uid, oldUidState, |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2484 | ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2485 | if (mDeviceIdleMode) { |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2486 | updateRuleForDeviceIdleLocked(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2487 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2488 | if (mRestrictPower) { |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2489 | updateRuleForRestrictPowerLocked(uid); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2490 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2491 | updateRulesForPowerRestrictionsLocked(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2492 | updateNetworkStats(uid, false); |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2493 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2497 | // adjust stats accounting based on foreground status |
| 2498 | private void updateNetworkStats(int uid, boolean uidForeground) { |
| 2499 | try { |
| 2500 | mNetworkStats.setUidForeground(uid, uidForeground); |
| 2501 | } catch (RemoteException e) { |
| 2502 | // ignored; service lives in system_server |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | private void updateRestrictBackgroundRulesOnUidStatusChangedLocked(int uid, int oldUidState, |
| 2507 | int newUidState) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2508 | final boolean oldForeground = |
| 2509 | isProcStateAllowedWhileOnRestrictBackgroundLocked(oldUidState); |
| 2510 | final boolean newForeground = |
| 2511 | isProcStateAllowedWhileOnRestrictBackgroundLocked(newUidState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2512 | if (oldForeground != newForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2513 | updateRulesForDataUsageRestrictionsLocked(uid); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2517 | private void updateScreenOn() { |
| 2518 | synchronized (mRulesLock) { |
| 2519 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2520 | mScreenOn = mPowerManager.isInteractive(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2521 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 2522 | // ignored; service lives in system_server |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2523 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2524 | updateRulesForScreenLocked(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * Update rules that might be changed by {@link #mScreenOn} value. |
| 2530 | */ |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 2531 | private void updateRulesForScreenLocked() { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2532 | // only update rules for anyone with foreground activities |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2533 | final int size = mUidState.size(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2534 | for (int i = 0; i < size; i++) { |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2535 | if (mUidState.valueAt(i) <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) { |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2536 | final int uid = mUidState.keyAt(i); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2537 | updateRestrictionRulesForUidLocked(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2542 | static boolean isProcStateAllowedWhileIdleOrPowerSaveMode(int procState) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2543 | return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; |
| 2544 | } |
| 2545 | |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2546 | static boolean isProcStateAllowedWhileOnRestrictBackgroundLocked(int procState) { |
| 2547 | return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE; |
| 2548 | } |
| 2549 | |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2550 | void updateRulesForPowerSaveLocked() { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2551 | updateRulesForWhitelistedPowerSaveLocked(mRestrictPower, FIREWALL_CHAIN_POWERSAVE, |
| 2552 | mUidFirewallPowerSaveRules); |
| 2553 | } |
| 2554 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2555 | void updateRuleForRestrictPowerLocked(int uid) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2556 | updateRulesForWhitelistedPowerSaveLocked(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE); |
| 2557 | } |
| 2558 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2559 | void updateRulesForDeviceIdleLocked() { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2560 | updateRulesForWhitelistedPowerSaveLocked(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE, |
| 2561 | mUidFirewallDozableRules); |
| 2562 | } |
| 2563 | |
| 2564 | void updateRuleForDeviceIdleLocked(int uid) { |
| 2565 | updateRulesForWhitelistedPowerSaveLocked(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE); |
| 2566 | } |
| 2567 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2568 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 2569 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2570 | private void updateRulesForWhitelistedPowerSaveLocked(boolean enabled, int chain, |
| 2571 | SparseIntArray rules) { |
| 2572 | if (enabled) { |
| 2573 | // 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] | 2574 | // we are disabling the chain. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2575 | final SparseIntArray uidRules = rules; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2576 | uidRules.clear(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2577 | final List<UserInfo> users = mUserManager.getUsers(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2578 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 2579 | UserInfo user = users.get(ui); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2580 | for (int i = mPowerSaveTempWhitelistAppIds.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2581 | if (mPowerSaveTempWhitelistAppIds.valueAt(i)) { |
| 2582 | int appId = mPowerSaveTempWhitelistAppIds.keyAt(i); |
| 2583 | int uid = UserHandle.getUid(user.id, appId); |
| 2584 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 2585 | } |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2586 | } |
| 2587 | for (int i = mPowerSaveWhitelistAppIds.size() - 1; i >= 0; i--) { |
| 2588 | int appId = mPowerSaveWhitelistAppIds.keyAt(i); |
| 2589 | int uid = UserHandle.getUid(user.id, appId); |
| 2590 | uidRules.put(uid, FIREWALL_RULE_ALLOW); |
| 2591 | } |
| 2592 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2593 | for (int i = mUidState.size() - 1; i >= 0; i--) { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2594 | if (isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.valueAt(i))) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2595 | uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW); |
| 2596 | } |
| 2597 | } |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2598 | setUidFirewallRules(chain, uidRules); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2599 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2600 | |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2601 | enableFirewallChainLocked(chain, enabled); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2604 | private void updateRulesForNonMeteredNetworksLocked() { |
| 2605 | |
| 2606 | } |
| 2607 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2608 | private boolean isWhitelistedBatterySaverLocked(int uid) { |
| 2609 | final int appId = UserHandle.getAppId(uid); |
| 2610 | return mPowerSaveTempWhitelistAppIds.get(appId) || mPowerSaveWhitelistAppIds.get(appId); |
| 2611 | } |
| 2612 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2613 | // NOTE: since both fw_dozable and fw_powersave uses the same map |
| 2614 | // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method. |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2615 | private void updateRulesForWhitelistedPowerSaveLocked(int uid, boolean enabled, int chain) { |
| 2616 | if (enabled) { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2617 | if (isWhitelistedBatterySaverLocked(uid) |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2618 | || isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.get(uid))) { |
| 2619 | setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2620 | } else { |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2621 | setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | void updateRulesForAppIdleLocked() { |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2627 | final SparseIntArray uidRules = mUidFirewallStandbyRules; |
| 2628 | uidRules.clear(); |
| 2629 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2630 | // Fully update the app idle firewall chain. |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2631 | final List<UserInfo> users = mUserManager.getUsers(); |
| 2632 | for (int ui = users.size() - 1; ui >= 0; ui--) { |
| 2633 | UserInfo user = users.get(ui); |
| 2634 | int[] idleUids = mUsageStats.getIdleUidsForUser(user.id); |
| 2635 | for (int uid : idleUids) { |
| 2636 | if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) { |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 2637 | // quick check: if this uid doesn't have INTERNET permission, it |
| 2638 | // doesn't have network access anyway, so it is a waste to mess |
| 2639 | // with it here. |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 2640 | if (hasInternetPermissions(uid)) { |
| 2641 | uidRules.put(uid, FIREWALL_RULE_DENY); |
Soi, Yoshinari | a065da1 | 2015-12-22 12:02:18 +0900 | [diff] [blame] | 2642 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2643 | } |
| 2644 | } |
| 2645 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 2646 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2647 | setUidFirewallRules(FIREWALL_CHAIN_STANDBY, uidRules); |
| 2648 | } |
| 2649 | |
| 2650 | void updateRuleForAppIdleLocked(int uid) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2651 | if (!isUidValidForBlacklistRules(uid)) return; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2652 | |
| 2653 | int appId = UserHandle.getAppId(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2654 | if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid) |
| 2655 | && !isUidForegroundOnRestrictPowerLocked(uid)) { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2656 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY); |
| 2657 | } else { |
| 2658 | setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT); |
| 2659 | } |
| 2660 | } |
| 2661 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 2662 | void updateRulesForAppIdleParoleLocked() { |
| 2663 | boolean enableChain = !mUsageStats.isAppIdleParoleOn(); |
| 2664 | enableFirewallChainLocked(FIREWALL_CHAIN_STANDBY, enableChain); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2667 | /** |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 2668 | * Update rules that might be changed by {@link #mRestrictBackground}, |
| 2669 | * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2670 | */ |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2671 | private void updateRulesForGlobalChangeLocked(boolean restrictedNetworksChanged) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 2672 | long start; |
| 2673 | if (LOGD) start = System.currentTimeMillis(); |
| 2674 | |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 2675 | updateRulesForRestrictPowerLocked(); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2676 | updateRulesForRestrictBackgroundLocked(); |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2677 | |
| 2678 | // If the set of restricted networks may have changed, re-evaluate those. |
| 2679 | if (restrictedNetworksChanged) { |
| 2680 | normalizePoliciesLocked(); |
| 2681 | updateNetworkRulesLocked(); |
| 2682 | } |
| 2683 | if (LOGD) { |
| 2684 | final long delta = System.currentTimeMillis() - start; |
| 2685 | Slog.d(TAG, "updateRulesForGlobalChangeLocked(" + restrictedNetworksChanged + ") took " |
| 2686 | + delta + "ms"); |
| 2687 | } |
| 2688 | } |
| 2689 | |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2690 | private void updateRulesForRestrictPowerLocked() { |
| 2691 | updateRulesForDeviceIdleLocked(); |
| 2692 | updateRulesForAppIdleLocked(); |
| 2693 | updateRulesForPowerSaveLocked(); |
| 2694 | updateRulesForAllAppsLocked(TYPE_RESTRICT_POWER); |
| 2695 | } |
| 2696 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2697 | private void updateRulesForRestrictBackgroundLocked() { |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2698 | updateRulesForAllAppsLocked(TYPE_RESTRICT_BACKGROUND); |
| 2699 | } |
| 2700 | |
| 2701 | private static final int TYPE_RESTRICT_BACKGROUND = 1; |
| 2702 | private static final int TYPE_RESTRICT_POWER = 2; |
| 2703 | @Retention(RetentionPolicy.SOURCE) |
| 2704 | @IntDef(flag = false, value = { |
| 2705 | TYPE_RESTRICT_BACKGROUND, |
| 2706 | TYPE_RESTRICT_POWER, |
| 2707 | }) |
| 2708 | public @interface RestrictType { |
| 2709 | } |
| 2710 | |
| 2711 | // TODO: refactor / consolidate all those updateXyz methods, there are way too many of them... |
| 2712 | private void updateRulesForAllAppsLocked(@RestrictType int type) { |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2713 | final PackageManager pm = mContext.getPackageManager(); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 2714 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2715 | // update rules for all installed applications |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 2716 | final List<UserInfo> users = mUserManager.getUsers(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2717 | final List<ApplicationInfo> apps = pm.getInstalledApplications( |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 2718 | PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_DISABLED_COMPONENTS |
| 2719 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 2720 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2721 | |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 2722 | final int usersSize = users.size(); |
| 2723 | final int appsSize = apps.size(); |
| 2724 | for (int i = 0; i < usersSize; i++) { |
| 2725 | final UserInfo user = users.get(i); |
| 2726 | for (int j = 0; j < appsSize; j++) { |
| 2727 | final ApplicationInfo app = apps.get(j); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2728 | final int uid = UserHandle.getUid(user.id, app.uid); |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2729 | switch (type) { |
| 2730 | case TYPE_RESTRICT_BACKGROUND: |
| 2731 | updateRulesForDataUsageRestrictionsLocked(uid); |
| 2732 | break; |
| 2733 | case TYPE_RESTRICT_POWER: |
| 2734 | updateRulesForPowerRestrictionsLocked(uid); |
| 2735 | break; |
| 2736 | default: |
| 2737 | Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type); |
| 2738 | } |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 2739 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2740 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2743 | private void updateRulesForTempWhitelistChangeLocked() { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2744 | final List<UserInfo> users = mUserManager.getUsers(); |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 2745 | for (int i = 0; i < users.size(); i++) { |
| 2746 | final UserInfo user = users.get(i); |
Rakesh Iyer | 4f3fc21 | 2016-03-03 20:16:41 -0800 | [diff] [blame] | 2747 | for (int j = mPowerSaveTempWhitelistAppIds.size() - 1; j >= 0; j--) { |
Felipe Leme | 03e689d | 2016-03-02 16:17:38 -0800 | [diff] [blame] | 2748 | int appId = mPowerSaveTempWhitelistAppIds.keyAt(j); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2749 | int uid = UserHandle.getUid(user.id, appId); |
Felipe Leme | f3e4064 | 2016-06-07 17:28:08 -0700 | [diff] [blame^] | 2750 | updateRulesForRestrictPowerLocked(); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2751 | // Update external firewall rules. |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2752 | updateRuleForAppIdleLocked(uid); |
| 2753 | updateRuleForDeviceIdleLocked(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2754 | updateRuleForRestrictPowerLocked(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2755 | // Update internal rules. |
| 2756 | updateRulesForPowerRestrictionsLocked(uid); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2761 | // TODO: the MEDIA / DRM restriction might not be needed anymore, in which case both |
| 2762 | // methods below could be merged into a isUidValidForRules() method. |
| 2763 | private boolean isUidValidForBlacklistRules(int uid) { |
| 2764 | // allow rules on specific system services, and any apps |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 2765 | 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] | 2766 | || (UserHandle.isApp(uid) && hasInternetPermissions(uid))) { |
Jeff Sharkey | 5294a2f | 2012-04-24 17:07:22 -0700 | [diff] [blame] | 2767 | return true; |
| 2768 | } |
| 2769 | |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2773 | private boolean isUidValidForWhitelistRules(int uid) { |
| 2774 | return UserHandle.isApp(uid) && hasInternetPermissions(uid); |
| 2775 | } |
| 2776 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 2777 | private boolean isUidIdle(int uid) { |
| 2778 | final String[] packages = mContext.getPackageManager().getPackagesForUid(uid); |
| 2779 | final int userId = UserHandle.getUserId(uid); |
| 2780 | |
Jeff Sharkey | 377ded0f | 2016-01-10 13:15:41 -0700 | [diff] [blame] | 2781 | if (!ArrayUtils.isEmpty(packages)) { |
| 2782 | for (String packageName : packages) { |
| 2783 | if (!mUsageStats.isAppIdle(packageName, uid, userId)) { |
| 2784 | return false; |
| 2785 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | return true; |
| 2789 | } |
| 2790 | |
| 2791 | /** |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2792 | * Checks if an uid has INTERNET permissions. |
| 2793 | * <p> |
| 2794 | * 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] | 2795 | */ |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2796 | private boolean hasInternetPermissions(int uid) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 2797 | try { |
Amith Yamasani | 2a4ac4e | 2016-02-12 12:43:15 -0800 | [diff] [blame] | 2798 | if (mIPm.checkUidPermission(Manifest.permission.INTERNET, uid) |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 2799 | != PackageManager.PERMISSION_GRANTED) { |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2800 | return false; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 2801 | } |
| 2802 | } catch (RemoteException e) { |
| 2803 | } |
Felipe Leme | 47585ba | 2016-02-09 16:56:32 -0800 | [diff] [blame] | 2804 | return true; |
| 2805 | } |
| 2806 | |
| 2807 | /** |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2808 | * Applies network rules to bandwidth and firewall controllers based on uid policy. |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2809 | * |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2810 | * <p>There are currently 4 types of restriction rules: |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2811 | * <ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2812 | * <li>Doze mode |
| 2813 | * <li>App idle mode |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2814 | * <li>Battery Saver Mode (also referred as power save). |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2815 | * <li>Data Saver Mode (The Feature Formerly Known As 'Restrict Background Data'). |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2816 | * </ul> |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2817 | * |
| 2818 | * <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] | 2819 | */ |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2820 | private void updateRestrictionRulesForUidLocked(int uid) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2821 | // Methods below only changes the firewall rules for the power-related modes. |
| 2822 | updateRuleForDeviceIdleLocked(uid); |
| 2823 | updateRuleForAppIdleLocked(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2824 | updateRuleForRestrictPowerLocked(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2825 | |
| 2826 | // Update internal state for power-related modes. |
| 2827 | updateRulesForPowerRestrictionsLocked(uid); |
| 2828 | |
| 2829 | // Update firewall and internal rules for Data Saver Mode. |
| 2830 | updateRulesForDataUsageRestrictionsLocked(uid); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2833 | /** |
| 2834 | * Applies network rules to bandwidth controllers based on process state and user-defined |
| 2835 | * restrictions (blacklist / whitelist). |
| 2836 | * |
| 2837 | * <p> |
| 2838 | * {@code netd} defines 3 firewall chains that govern whether an app has access to metered |
| 2839 | * networks: |
| 2840 | * <ul> |
| 2841 | * <li>@{code bw_penalty_box}: UIDs added to this chain do not have access (blacklist). |
| 2842 | * <li>@{code bw_happy_box}: UIDs added to this chain have access (whitelist), unless they're |
| 2843 | * also blacklisted. |
| 2844 | * <li>@{code bw_data_saver}: when enabled (through {@link #setRestrictBackground(boolean)}), |
| 2845 | * no UIDs other those whitelisted will have access. |
| 2846 | * <ul> |
| 2847 | * |
| 2848 | * <p>The @{code bw_penalty_box} and @{code bw_happy_box} are primarily managed through the |
| 2849 | * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} / |
| 2850 | * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist |
| 2851 | * respectively): these methods set the proper internal state (blacklist / whitelist), then call |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2852 | * this ({@link #updateRulesForDataUsageRestrictionsLocked(int)}) to propagate the rules to |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2853 | * {@link INetworkManagementService}, but this method should also be called in events (like |
| 2854 | * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the |
| 2855 | * following rules should also be applied: |
| 2856 | * |
| 2857 | * <ul> |
| 2858 | * <li>When Data Saver mode is on, the foreground app should be temporarily added to |
| 2859 | * {@code bw_happy_box} before the @{code bw_data_saver} chain is enabled. |
| 2860 | * <li>If the foreground app is blacklisted by the user, it should be temporarily removed from |
| 2861 | * {@code bw_penalty_box}. |
| 2862 | * <li>When the app leaves foreground state, the temporary changes above should be reverted. |
| 2863 | * </ul> |
| 2864 | * |
| 2865 | * <p>For optimization, the rules are only applied on user apps that have internet access |
| 2866 | * permission, since there is no need to change the {@code iptables} rule if the app does not |
| 2867 | * have permission to use the internet. |
| 2868 | * |
| 2869 | * <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] | 2870 | * |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2871 | */ |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2872 | private void updateRulesForDataUsageRestrictionsLocked(int uid) { |
| 2873 | updateRulesForDataUsageRestrictionsLocked(uid, false); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | /** |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2877 | * Overloaded version of {@link #updateRulesForDataUsageRestrictionsLocked(int)} called when an |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2878 | * app is removed - it ignores the UID validity check. |
| 2879 | */ |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2880 | private void updateRulesForDataUsageRestrictionsLocked(int uid, boolean uidDeleted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2881 | if (!uidDeleted && !isUidValidForWhitelistRules(uid)) { |
| 2882 | if (LOGD) Slog.d(TAG, "no need to update restrict data rules for uid " + uid); |
| 2883 | return; |
| 2884 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 2885 | |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 2886 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2887 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
Felipe Leme | ef89c90 | 2016-03-30 15:11:31 -0700 | [diff] [blame] | 2888 | final boolean isForeground = isUidForegroundOnRestrictBackgroundLocked(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 2889 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2890 | final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
| 2891 | final boolean isWhitelisted = mRestrictBackgroundWhitelistUids.get(uid); |
| 2892 | final int oldRule = oldUidRules & MASK_METERED_NETWORKS; |
| 2893 | int newRule = RULE_NONE; |
Felipe Leme | 76010a3 | 2016-03-17 13:03:11 -0700 | [diff] [blame] | 2894 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2895 | // First step: define the new rule based on user restrictions and foreground state. |
| 2896 | if (isForeground) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2897 | if (isBlacklisted || (mRestrictBackground && !isWhitelisted)) { |
| 2898 | newRule = RULE_TEMPORARY_ALLOW_METERED; |
| 2899 | } else if (isWhitelisted) { |
| 2900 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 2901 | } |
| 2902 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2903 | if (isBlacklisted) { |
| 2904 | newRule = RULE_REJECT_METERED; |
| 2905 | } else if (mRestrictBackground && isWhitelisted) { |
| 2906 | newRule = RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 2907 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 2908 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2909 | final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2910 | |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2911 | if (LOGV) { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2912 | Log.v(TAG, "updateRuleForRestrictBackgroundLocked(" + uid + ")" |
| 2913 | + ": isForeground=" +isForeground |
| 2914 | + ", isBlacklisted=" + isBlacklisted |
| 2915 | + ", isWhitelisted=" + isWhitelisted |
| 2916 | + ", oldRule=" + uidRulesToString(oldRule) |
| 2917 | + ", newRule=" + uidRulesToString(newRule) |
| 2918 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 2919 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 2920 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2921 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2922 | if (newUidRules == RULE_NONE) { |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 2923 | mUidRules.delete(uid); |
| 2924 | } else { |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2925 | mUidRules.put(uid, newUidRules); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 2926 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2927 | |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 2928 | boolean changed = false; |
| 2929 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2930 | // Second step: apply bw changes based on change of state. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2931 | if (newRule != oldRule) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 2932 | changed = true; |
| 2933 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2934 | if ((newRule & RULE_TEMPORARY_ALLOW_METERED) != 0) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2935 | // Temporarily whitelist foreground app, removing from blacklist if necessary |
| 2936 | // (since bw_penalty_box prevails over bw_happy_box). |
| 2937 | |
| 2938 | setMeteredNetworkWhitelist(uid, true); |
| 2939 | // TODO: if statement below is used to avoid an unnecessary call to netd / iptables, |
| 2940 | // but ideally it should be just: |
| 2941 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2942 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2943 | setMeteredNetworkBlacklist(uid, false); |
| 2944 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2945 | } else if ((oldRule & RULE_TEMPORARY_ALLOW_METERED) != 0) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2946 | // Remove temporary whitelist from app that is not on foreground anymore. |
| 2947 | |
| 2948 | // TODO: if statements below are used to avoid unnecessary calls to netd / iptables, |
| 2949 | // but ideally they should be just: |
| 2950 | // setMeteredNetworkWhitelist(uid, isWhitelisted); |
| 2951 | // setMeteredNetworkBlacklist(uid, isBlacklisted); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2952 | if (!isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2953 | setMeteredNetworkWhitelist(uid, false); |
| 2954 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2955 | if (isBlacklisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2956 | setMeteredNetworkBlacklist(uid, true); |
| 2957 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2958 | } else if ((newRule & RULE_REJECT_METERED) != 0 |
| 2959 | || (oldRule & RULE_REJECT_METERED) != 0) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2960 | // Flip state because app was explicitly added or removed to blacklist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2961 | setMeteredNetworkBlacklist(uid, isBlacklisted); |
| 2962 | if ((oldRule & RULE_REJECT_METERED) != 0 && isWhitelisted) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2963 | // Since blacklist prevails over whitelist, we need to handle the special case |
| 2964 | // where app is whitelisted and blacklisted at the same time (although such |
| 2965 | // scenario should be blocked by the UI), then blacklist is removed. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2966 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2967 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2968 | } else if ((newRule & RULE_ALLOW_METERED) != 0 |
| 2969 | || (oldRule & RULE_ALLOW_METERED) != 0) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2970 | // Flip state because app was explicitly added or removed to whitelist. |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2971 | setMeteredNetworkWhitelist(uid, isWhitelisted); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2972 | } else { |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2973 | // All scenarios should have been covered above. |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 2974 | Log.wtf(TAG, "Unexpected change of metered UID state for " + uid |
| 2975 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2976 | + ", whitelisted=" + isWhitelisted |
| 2977 | + ", blacklisted=" + isBlacklisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 2978 | + ", newRule=" + uidRulesToString(newUidRules) |
| 2979 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 2980 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 2981 | |
| 2982 | // Dispatch changed rule to existing listeners. |
| 2983 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | /** |
| 2988 | * Updates the power-related part of the {@link #mUidRules} for a given map, and notify external |
| 2989 | * listeners in case of change. |
| 2990 | * <p> |
| 2991 | * There are 3 power-related rules that affects whether an app has background access on |
| 2992 | * non-metered networks, and when the condition applies and the UID is not whitelisted for power |
| 2993 | * restriction, it's added to the equivalent firewall chain: |
| 2994 | * <ul> |
| 2995 | * <li>App is idle: {@code fw_standby} firewall chain. |
| 2996 | * <li>Device is idle: {@code fw_dozable} firewall chain. |
| 2997 | * <li>Battery Saver Mode is on: {@code fw_powersave} firewall chain. |
| 2998 | * </ul> |
| 2999 | * <p> |
| 3000 | * This method updates the power-related part of the {@link #mUidRules} for a given uid based on |
| 3001 | * these modes, the UID process state (foreground or not), and the UIDwhitelist state. |
| 3002 | * <p> |
| 3003 | * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}. |
| 3004 | */ |
| 3005 | private void updateRulesForPowerRestrictionsLocked(int uid) { |
| 3006 | if (!isUidValidForBlacklistRules(uid)) { |
| 3007 | if (LOGD) Slog.d(TAG, "no need to update restrict power rules for uid " + uid); |
| 3008 | return; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3009 | } |
Felipe Leme | f28983d | 2016-03-25 12:18:23 -0700 | [diff] [blame] | 3010 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3011 | final boolean isIdle = isUidIdle(uid); |
| 3012 | final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode; |
| 3013 | final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE); |
| 3014 | final int oldUidRules = mUidRules.get(uid, RULE_NONE); |
| 3015 | final boolean isForeground = isUidForegroundOnRestrictPowerLocked(uid); |
| 3016 | |
| 3017 | final boolean isWhitelisted = isWhitelistedBatterySaverLocked(uid); |
| 3018 | final int oldRule = oldUidRules & MASK_ALL_NETWORKS; |
| 3019 | int newRule = RULE_NONE; |
| 3020 | |
| 3021 | // First step: define the new rule based on user restrictions and foreground state. |
| 3022 | |
| 3023 | // NOTE: if statements below could be inlined, but it's easier to understand the logic |
| 3024 | // by considering the foreground and non-foreground states. |
| 3025 | if (isForeground) { |
| 3026 | if (restrictMode) { |
| 3027 | newRule = RULE_ALLOW_ALL; |
| 3028 | } |
| 3029 | } else if (restrictMode) { |
| 3030 | newRule = isWhitelisted ? RULE_ALLOW_ALL : RULE_REJECT_ALL; |
| 3031 | } |
| 3032 | |
| 3033 | final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule; |
| 3034 | |
| 3035 | if (LOGV) { |
| 3036 | Log.v(TAG, "updateRulesForNonMeteredNetworksLocked(" + uid + ")" |
| 3037 | + ", isIdle: " + isIdle |
| 3038 | + ", mRestrictPower: " + mRestrictPower |
| 3039 | + ", mDeviceIdleMode: " + mDeviceIdleMode |
| 3040 | + ", isForeground=" + isForeground |
| 3041 | + ", isWhitelisted=" + isWhitelisted |
| 3042 | + ", oldRule=" + uidRulesToString(oldRule) |
| 3043 | + ", newRule=" + uidRulesToString(newRule) |
| 3044 | + ", newUidRules=" + uidRulesToString(newUidRules) |
| 3045 | + ", oldUidRules=" + uidRulesToString(oldUidRules)); |
| 3046 | } |
| 3047 | |
| 3048 | if (newUidRules == RULE_NONE) { |
| 3049 | mUidRules.delete(uid); |
| 3050 | } else { |
| 3051 | mUidRules.put(uid, newUidRules); |
| 3052 | } |
| 3053 | |
| 3054 | // Second step: notify listeners if state changed. |
| 3055 | if (newRule != oldRule) { |
| 3056 | if (newRule == RULE_NONE || (newRule & RULE_ALLOW_ALL) != 0) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3057 | if (LOGV) Log.v(TAG, "Allowing non-metered access for UID " + uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3058 | } else if ((newRule & RULE_REJECT_ALL) != 0) { |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3059 | if (LOGV) Log.v(TAG, "Rejecting non-metered access for UID " + uid); |
| 3060 | } else { |
| 3061 | // All scenarios should have been covered above |
| 3062 | Log.wtf(TAG, "Unexpected change of non-metered UID state for " + uid |
| 3063 | + ": foreground=" + isForeground |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3064 | + ", whitelisted=" + isWhitelisted |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 3065 | + ", newRule=" + uidRulesToString(newUidRules) |
| 3066 | + ", oldRule=" + uidRulesToString(oldUidRules)); |
| 3067 | } |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 3068 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget(); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3069 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3072 | private class AppIdleStateChangeListener |
| 3073 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 3074 | |
| 3075 | @Override |
| 3076 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle) { |
| 3077 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 3078 | final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName, |
| 3079 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3080 | if (LOGV) Log.v(TAG, "onAppIdleStateChanged(): uid=" + uid + ", idle=" + idle); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3081 | synchronized (mRulesLock) { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3082 | updateRuleForAppIdleLocked(uid); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3083 | updateRulesForPowerRestrictionsLocked(uid); |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3084 | } |
| 3085 | } catch (NameNotFoundException nnfe) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3086 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | @Override |
| 3090 | public void onParoleStateChanged(boolean isParoleOn) { |
| 3091 | synchronized (mRulesLock) { |
| 3092 | updateRulesForAppIdleParoleLocked(); |
| 3093 | } |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3097 | private void dispatchUidRulesChanged(INetworkPolicyListener listener, int uid, int uidRules) { |
| 3098 | if (listener != null) { |
| 3099 | try { |
| 3100 | listener.onUidRulesChanged(uid, uidRules); |
| 3101 | } catch (RemoteException ignored) { |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | private void dispatchMeteredIfacesChanged(INetworkPolicyListener listener, |
| 3107 | String[] meteredIfaces) { |
| 3108 | if (listener != null) { |
| 3109 | try { |
| 3110 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 3111 | } catch (RemoteException ignored) { |
| 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | private void dispatchRestrictBackgroundChanged(INetworkPolicyListener listener, |
| 3117 | boolean restrictBackground) { |
| 3118 | if (listener != null) { |
| 3119 | try { |
| 3120 | listener.onRestrictBackgroundChanged(restrictBackground); |
| 3121 | } catch (RemoteException ignored) { |
| 3122 | } |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | private void dispatchRestrictBackgroundWhitelistChanged(INetworkPolicyListener listener, |
| 3127 | int uid, boolean whitelisted) { |
| 3128 | if (listener != null) { |
| 3129 | try { |
| 3130 | listener.onRestrictBackgroundWhitelistChanged(uid, whitelisted); |
| 3131 | } catch (RemoteException ignored) { |
| 3132 | } |
| 3133 | } |
| 3134 | } |
| 3135 | |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3136 | private void dispatchRestrictBackgroundBlacklistChanged(INetworkPolicyListener listener, |
| 3137 | int uid, boolean blacklisted) { |
| 3138 | if (listener != null) { |
| 3139 | try { |
| 3140 | listener.onRestrictBackgroundBlacklistChanged(uid, blacklisted); |
| 3141 | } catch (RemoteException ignored) { |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3146 | private Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 3147 | @Override |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3148 | public boolean handleMessage(Message msg) { |
| 3149 | switch (msg.what) { |
| 3150 | case MSG_RULES_CHANGED: { |
| 3151 | final int uid = msg.arg1; |
| 3152 | final int uidRules = msg.arg2; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3153 | dispatchUidRulesChanged(mConnectivityListener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3154 | final int length = mListeners.beginBroadcast(); |
| 3155 | for (int i = 0; i < length; i++) { |
| 3156 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3157 | dispatchUidRulesChanged(listener, uid, uidRules); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3158 | } |
| 3159 | mListeners.finishBroadcast(); |
| 3160 | return true; |
| 3161 | } |
| 3162 | case MSG_METERED_IFACES_CHANGED: { |
| 3163 | final String[] meteredIfaces = (String[]) msg.obj; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3164 | dispatchMeteredIfacesChanged(mConnectivityListener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3165 | final int length = mListeners.beginBroadcast(); |
| 3166 | for (int i = 0; i < length; i++) { |
| 3167 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3168 | dispatchMeteredIfacesChanged(listener, meteredIfaces); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3169 | } |
| 3170 | mListeners.finishBroadcast(); |
| 3171 | return true; |
| 3172 | } |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3173 | case MSG_LIMIT_REACHED: { |
| 3174 | final String iface = (String) msg.obj; |
| 3175 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3176 | maybeRefreshTrustedTime(); |
Jeff Sharkey | 7e25b0e | 2011-11-08 15:43:12 -0800 | [diff] [blame] | 3177 | synchronized (mRulesLock) { |
| 3178 | if (mMeteredIfaces.contains(iface)) { |
| 3179 | try { |
| 3180 | // force stats update to make sure we have |
| 3181 | // numbers that caused alert to trigger. |
| 3182 | mNetworkStats.forceUpdate(); |
| 3183 | } catch (RemoteException e) { |
| 3184 | // ignored; service lives in system_server |
| 3185 | } |
| 3186 | |
| 3187 | updateNetworkEnabledLocked(); |
| 3188 | updateNotificationsLocked(); |
| 3189 | } |
| 3190 | } |
| 3191 | return true; |
| 3192 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3193 | case MSG_RESTRICT_BACKGROUND_CHANGED: { |
| 3194 | final boolean restrictBackground = msg.arg1 != 0; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3195 | dispatchRestrictBackgroundChanged(mConnectivityListener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3196 | final int length = mListeners.beginBroadcast(); |
| 3197 | for (int i = 0; i < length; i++) { |
| 3198 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3199 | dispatchRestrictBackgroundChanged(listener, restrictBackground); |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3200 | } |
| 3201 | mListeners.finishBroadcast(); |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3202 | final Intent intent = |
| 3203 | new Intent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3204 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3205 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 3206 | return true; |
| 3207 | } |
| 3208 | case MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED: { |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3209 | // MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED can be called in 2 occasions: |
| 3210 | // - when an app is whitelisted |
| 3211 | // - when an app is blacklisted |
| 3212 | // |
| 3213 | // Whether the internal listeners (INetworkPolicyListener implementations) or |
| 3214 | // app broadcast receivers are notified depend on the following rules: |
| 3215 | // |
| 3216 | // - App receivers are only notified when the app status changed (msg.arg2 = 1) |
| 3217 | // - Listeners are only notified when app was whitelisted (msg.obj is not null), |
| 3218 | // since blacklist notifications are handled through MSG_RULES_CHANGED). |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3219 | final int uid = msg.arg1; |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3220 | final boolean changed = msg.arg2 == 1; |
| 3221 | final Boolean whitelisted = (Boolean) msg.obj; |
| 3222 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3223 | // First notify internal listeners... |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3224 | if (whitelisted != null) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3225 | final boolean whitelistedBool = whitelisted.booleanValue(); |
| 3226 | dispatchRestrictBackgroundWhitelistChanged(mConnectivityListener, uid, |
| 3227 | whitelistedBool); |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3228 | final int length = mListeners.beginBroadcast(); |
| 3229 | for (int i = 0; i < length; i++) { |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3230 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 3231 | dispatchRestrictBackgroundWhitelistChanged(listener, uid, |
| 3232 | whitelistedBool); |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3233 | } |
| 3234 | mListeners.finishBroadcast(); |
| 3235 | } |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3236 | final PackageManager pm = mContext.getPackageManager(); |
| 3237 | final String[] packages = pm.getPackagesForUid(uid); |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 3238 | if (changed && packages != null) { |
| 3239 | // ...then notify apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED |
Felipe Leme | 86e5a01 | 2016-02-16 16:26:05 -0800 | [diff] [blame] | 3240 | final int userId = UserHandle.getUserId(uid); |
| 3241 | for (String packageName : packages) { |
| 3242 | final Intent intent = new Intent( |
| 3243 | ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 3244 | intent.setPackage(packageName); |
| 3245 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3246 | mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); |
| 3247 | } |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 3248 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 3249 | return true; |
| 3250 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 3251 | case MSG_RESTRICT_BACKGROUND_BLACKLIST_CHANGED: { |
| 3252 | final int uid = msg.arg1; |
| 3253 | final boolean blacklisted = msg.arg2 == 1; |
| 3254 | |
| 3255 | dispatchRestrictBackgroundBlacklistChanged(mConnectivityListener, uid, |
| 3256 | blacklisted); |
| 3257 | final int length = mListeners.beginBroadcast(); |
| 3258 | for (int i = 0; i < length; i++) { |
| 3259 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 3260 | dispatchRestrictBackgroundBlacklistChanged(listener, uid, |
| 3261 | blacklisted); |
| 3262 | } |
| 3263 | mListeners.finishBroadcast(); |
| 3264 | return true; |
| 3265 | } |
Jeff Sharkey | e19f39b | 2012-05-24 10:21:16 -0700 | [diff] [blame] | 3266 | case MSG_ADVISE_PERSIST_THRESHOLD: { |
| 3267 | final long lowestRule = (Long) msg.obj; |
| 3268 | try { |
| 3269 | // make sure stats are recorded frequently enough; we aim |
| 3270 | // for 2MB threshold for 2GB/month rules. |
| 3271 | final long persistThreshold = lowestRule / 1000; |
| 3272 | mNetworkStats.advisePersistThreshold(persistThreshold); |
| 3273 | } catch (RemoteException e) { |
| 3274 | // ignored; service lives in system_server |
| 3275 | } |
| 3276 | return true; |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 3277 | } |
Jeff Sharkey | 0abe556 | 2012-06-19 13:32:22 -0700 | [diff] [blame] | 3278 | case MSG_SCREEN_ON_CHANGED: { |
| 3279 | updateScreenOn(); |
| 3280 | return true; |
| 3281 | } |
Amith Yamasani | 3646cbd | 2016-04-13 14:04:53 -0700 | [diff] [blame] | 3282 | case MSG_UPDATE_INTERFACE_QUOTA: { |
| 3283 | removeInterfaceQuota((String) msg.obj); |
| 3284 | // int params need to be stitched back into a long |
| 3285 | setInterfaceQuota((String) msg.obj, |
| 3286 | ((long) msg.arg1 << 32) | (msg.arg2 & 0xFFFFFFFFL)); |
| 3287 | return true; |
| 3288 | } |
| 3289 | case MSG_REMOVE_INTERFACE_QUOTA: { |
| 3290 | removeInterfaceQuota((String) msg.obj); |
| 3291 | return true; |
| 3292 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3293 | default: { |
| 3294 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 3298 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 3299 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3300 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3301 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3302 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3303 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3304 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3305 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3306 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | private void removeInterfaceQuota(String iface) { |
| 3311 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3312 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3313 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3314 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3315 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3316 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3320 | private void setMeteredNetworkBlacklist(int uid, boolean enable) { |
| 3321 | if (LOGV) Slog.v(TAG, "setMeteredNetworkBlacklist " + uid + ": " + enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3322 | try { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3323 | mNetworkManager.setUidMeteredNetworkBlacklist(uid, enable); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3324 | } catch (IllegalStateException e) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3325 | Log.wtf(TAG, "problem setting blacklist (" + enable + ") rules for " + uid, e); |
| 3326 | } catch (RemoteException e) { |
| 3327 | // ignored; service lives in system_server |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | private void setMeteredNetworkWhitelist(int uid, boolean enable) { |
| 3332 | if (LOGV) Slog.v(TAG, "setMeteredNetworkWhitelist " + uid + ": " + enable); |
| 3333 | try { |
| 3334 | mNetworkManager.setUidMeteredNetworkWhitelist(uid, enable); |
| 3335 | } catch (IllegalStateException e) { |
| 3336 | Log.wtf(TAG, "problem setting whitelist (" + enable + ") rules for " + uid, e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3337 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3338 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3342 | /** |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3343 | * Set uid rules on a particular firewall chain. This is going to synchronize the rules given |
| 3344 | * here to netd. It will clean up dead rules and make sure the target chain only contains rules |
| 3345 | * specified here. |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3346 | */ |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3347 | private void setUidFirewallRules(int chain, SparseIntArray uidRules) { |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3348 | try { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3349 | int size = uidRules.size(); |
| 3350 | int[] uids = new int[size]; |
| 3351 | int[] rules = new int[size]; |
| 3352 | for(int index = size - 1; index >= 0; --index) { |
| 3353 | uids[index] = uidRules.keyAt(index); |
| 3354 | rules[index] = uidRules.valueAt(index); |
| 3355 | } |
| 3356 | mNetworkManager.setFirewallUidRules(chain, uids, rules); |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 3357 | } catch (IllegalStateException e) { |
| 3358 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 3359 | } catch (RemoteException e) { |
| 3360 | // ignored; service lives in system_server |
| 3361 | } |
| 3362 | } |
| 3363 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3364 | /** |
| 3365 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 3366 | */ |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3367 | private void setUidFirewallRule(int chain, int uid, int rule) { |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3368 | if (chain == FIREWALL_CHAIN_DOZABLE) { |
| 3369 | mUidFirewallDozableRules.put(uid, rule); |
| 3370 | } else if (chain == FIREWALL_CHAIN_STANDBY) { |
| 3371 | mUidFirewallStandbyRules.put(uid, rule); |
Felipe Leme | 011b98f | 2016-02-10 17:28:31 -0800 | [diff] [blame] | 3372 | } else if (chain == FIREWALL_CHAIN_POWERSAVE) { |
| 3373 | mUidFirewallPowerSaveRules.put(uid, rule); |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3376 | try { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3377 | mNetworkManager.setFirewallUidRule(chain, uid, rule); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3378 | } catch (IllegalStateException e) { |
| 3379 | Log.wtf(TAG, "problem setting firewall uid rules", e); |
| 3380 | } catch (RemoteException e) { |
| 3381 | // ignored; service lives in system_server |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | /** |
| 3386 | * Add or remove a uid to the firewall blacklist for all network ifaces. |
| 3387 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3388 | private void enableFirewallChainLocked(int chain, boolean enable) { |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3389 | if (mFirewallChainStates.indexOfKey(chain) >= 0 && |
| 3390 | mFirewallChainStates.get(chain) == enable) { |
| 3391 | // All is the same, nothing to do. |
| 3392 | return; |
| 3393 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 3394 | mFirewallChainStates.put(chain, enable); |
Xiaohui Chen | b41c9f7 | 2015-06-17 15:55:37 -0700 | [diff] [blame] | 3395 | try { |
| 3396 | mNetworkManager.setFirewallChainEnabled(chain, enable); |
| 3397 | } catch (IllegalStateException e) { |
| 3398 | Log.wtf(TAG, "problem enable firewall chain", e); |
| 3399 | } catch (RemoteException e) { |
| 3400 | // ignored; service lives in system_server |
| 3401 | } |
| 3402 | } |
| 3403 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3404 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
| 3405 | try { |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 3406 | return mNetworkStats.getNetworkTotalBytes(template, start, end); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 3407 | } catch (RuntimeException e) { |
| 3408 | Slog.w(TAG, "problem reading network stats: " + e); |
| 3409 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3410 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 3411 | // ignored; service lives in system_server |
| 3412 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
Jeff Sharkey | 8c1dc72 | 2012-05-04 14:49:37 -0700 | [diff] [blame] | 3416 | private boolean isBandwidthControlEnabled() { |
| 3417 | final long token = Binder.clearCallingIdentity(); |
| 3418 | try { |
| 3419 | return mNetworkManager.isBandwidthControlEnabled(); |
| 3420 | } catch (RemoteException e) { |
| 3421 | // ignored; service lives in system_server |
| 3422 | return false; |
| 3423 | } finally { |
| 3424 | Binder.restoreCallingIdentity(token); |
| 3425 | } |
| 3426 | } |
| 3427 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3428 | /** |
| 3429 | * Try refreshing {@link #mTime} when stale. |
| 3430 | */ |
Dianne Hackborn | 497175b | 2014-07-01 12:56:08 -0700 | [diff] [blame] | 3431 | void maybeRefreshTrustedTime() { |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3432 | if (mTime.getCacheAge() > TIME_CACHE_MAX_AGE) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3433 | mTime.forceRefresh(); |
| 3434 | } |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3435 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3436 | |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 3437 | private long currentTimeMillis() { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3438 | return mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); |
| 3439 | } |
| 3440 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 3441 | private static Intent buildAllowBackgroundDataIntent() { |
| 3442 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 3443 | } |
| 3444 | |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 3445 | private static Intent buildSnoozeWarningIntent(NetworkTemplate template) { |
| 3446 | final Intent intent = new Intent(ACTION_SNOOZE_WARNING); |
| 3447 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 3448 | return intent; |
| 3449 | } |
| 3450 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 3451 | private static Intent buildNetworkOverLimitIntent(NetworkTemplate template) { |
| 3452 | final Intent intent = new Intent(); |
| 3453 | intent.setComponent(new ComponentName( |
| 3454 | "com.android.systemui", "com.android.systemui.net.NetworkOverLimitActivity")); |
| 3455 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3456 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 3457 | return intent; |
| 3458 | } |
| 3459 | |
| 3460 | private static Intent buildViewDataUsageIntent(NetworkTemplate template) { |
| 3461 | final Intent intent = new Intent(); |
| 3462 | intent.setComponent(new ComponentName( |
| 3463 | "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); |
| 3464 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3465 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 3466 | return intent; |
| 3467 | } |
| 3468 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 3469 | @VisibleForTesting |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 3470 | public void addIdleHandler(IdleHandler handler) { |
| 3471 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 3472 | } |
| 3473 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 3474 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 3475 | final int size = source.size(); |
| 3476 | for (int i = 0; i < size; i++) { |
| 3477 | target.put(source.keyAt(i), true); |
| 3478 | } |
| 3479 | } |
| 3480 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 3481 | @Override |
| 3482 | public void factoryReset(String subscriber) { |
| 3483 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 3484 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 3485 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 3486 | return; |
| 3487 | } |
| 3488 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 3489 | // Turn mobile data limit off |
Stuart Scott | 9a9a1d9 | 2015-04-20 11:33:06 -0700 | [diff] [blame] | 3490 | NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName()); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 3491 | NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriber); |
| 3492 | for (NetworkPolicy policy : policies) { |
| 3493 | if (policy.template.equals(template)) { |
| 3494 | policy.limitBytes = NetworkPolicy.LIMIT_DISABLED; |
| 3495 | policy.inferred = false; |
| 3496 | policy.clearSnooze(); |
| 3497 | } |
| 3498 | } |
| 3499 | setNetworkPolicies(policies); |
| 3500 | |
| 3501 | // Turn restrict background data off |
| 3502 | setRestrictBackground(false); |
| 3503 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 3504 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_APPS_CONTROL)) { |
| 3505 | // Remove app's "restrict background data" flag |
| 3506 | for (int uid : getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) { |
| 3507 | setUidPolicy(uid, POLICY_NONE); |
| 3508 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 3509 | } |
| 3510 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 3511 | |
| 3512 | private class MyPackageMonitor extends PackageMonitor { |
| 3513 | |
| 3514 | @Override |
| 3515 | public void onPackageRemoved(String packageName, int uid) { |
| 3516 | if (LOGV) Slog.v(TAG, "onPackageRemoved: " + packageName + " ->" + uid); |
| 3517 | synchronized (mRulesLock) { |
Felipe Leme | 70c57c2 | 2016-03-29 10:45:13 -0700 | [diff] [blame] | 3518 | removeRestrictBackgroundWhitelistedUidLocked(uid, true, true); |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 3519 | updateRestrictionRulesForUidLocked(uid); |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 3520 | } |
| 3521 | } |
Felipe Leme | b85a637 | 2016-01-14 16:16:16 -0800 | [diff] [blame] | 3522 | } |
Felipe Leme | d17fda4 | 2016-04-29 11:12:45 -0700 | [diff] [blame] | 3523 | |
| 3524 | private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal { |
| 3525 | |
| 3526 | @Override |
| 3527 | public void resetUserState(int userId) { |
| 3528 | synchronized (mRulesLock) { |
| 3529 | boolean changed = removeUserStateLocked(userId, false); |
| 3530 | changed = addDefaultRestrictBackgroundWhitelistUidsLocked(userId) || changed; |
| 3531 | if (changed) { |
| 3532 | writePolicyLocked(); |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3537 | } |