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