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