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