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