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