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