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