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