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