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