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