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