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