Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.net; |
| 18 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_NETWORK_STATE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 22 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 23 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 24 | import static android.Manifest.permission.READ_PHONE_STATE; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 25 | import static android.content.Intent.ACTION_PACKAGE_ADDED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 26 | import static android.content.Intent.ACTION_UID_REMOVED; |
| 27 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 28 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 32 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 33 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 34 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 35 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 36 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 37 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 38 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 39 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 40 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 42 | import static android.net.NetworkPolicyManager.dumpPolicy; |
| 43 | import static android.net.NetworkPolicyManager.dumpRules; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 44 | import static android.net.NetworkPolicyManager.isUidValidForPolicy; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 45 | import static android.net.NetworkTemplate.MATCH_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 46 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 47 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 48 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 49 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 50 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 51 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 52 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 53 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 54 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readBooleanAttribute; |
| 55 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readIntAttribute; |
| 56 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.readLongAttribute; |
| 57 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeBooleanAttribute; |
| 58 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeIntAttribute; |
| 59 | import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeLongAttribute; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 60 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_UPDATED; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 61 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 62 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 63 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 64 | import android.app.IActivityManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 65 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 66 | import android.app.IProcessObserver; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 67 | import android.app.Notification; |
| 68 | import android.app.PendingIntent; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 69 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 70 | import android.content.ComponentName; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 71 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 72 | import android.content.Intent; |
| 73 | import android.content.IntentFilter; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 74 | import android.content.pm.ApplicationInfo; |
| 75 | import android.content.pm.PackageManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 76 | import android.content.res.Resources; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 77 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 78 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 79 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 80 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 81 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 82 | import android.net.INetworkStatsService; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 83 | import android.net.NetworkIdentity; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 84 | import android.net.NetworkPolicy; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 85 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 86 | import android.net.NetworkState; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 87 | import android.net.NetworkTemplate; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 88 | import android.os.Binder; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 89 | import android.os.Environment; |
| 90 | import android.os.Handler; |
| 91 | import android.os.HandlerThread; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 92 | import android.os.INetworkManagementService; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 93 | import android.os.IPowerManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 94 | import android.os.Message; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 95 | import android.os.MessageQueue.IdleHandler; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 96 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 97 | import android.os.RemoteException; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 98 | import android.provider.Settings; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 99 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 100 | import android.text.format.Formatter; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 101 | import android.text.format.Time; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 102 | import android.util.Log; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 103 | import android.util.NtpTrustedTime; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 104 | import android.util.Slog; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 105 | import android.util.SparseArray; |
| 106 | import android.util.SparseBooleanArray; |
| 107 | import android.util.SparseIntArray; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 108 | import android.util.TrustedTime; |
| 109 | import android.util.Xml; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 110 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 111 | import com.android.internal.R; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 112 | import com.android.internal.os.AtomicFile; |
| 113 | import com.android.internal.util.FastXmlSerializer; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 114 | import com.android.internal.util.Objects; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 115 | import com.google.android.collect.Lists; |
| 116 | import com.google.android.collect.Maps; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 117 | import com.google.android.collect.Sets; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 118 | |
| 119 | import org.xmlpull.v1.XmlPullParser; |
| 120 | import org.xmlpull.v1.XmlPullParserException; |
| 121 | import org.xmlpull.v1.XmlSerializer; |
| 122 | |
| 123 | import java.io.File; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 124 | import java.io.FileDescriptor; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 125 | import java.io.FileInputStream; |
| 126 | import java.io.FileNotFoundException; |
| 127 | import java.io.FileOutputStream; |
| 128 | import java.io.IOException; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 129 | import java.io.PrintWriter; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 130 | import java.net.ProtocolException; |
| 131 | import java.util.ArrayList; |
| 132 | import java.util.Arrays; |
| 133 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 134 | import java.util.HashSet; |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 135 | import java.util.List; |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 136 | import java.util.Map; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 137 | |
| 138 | import libcore.io.IoUtils; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 140 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 141 | * Service that maintains low-level network policy rules, using |
| 142 | * {@link NetworkStatsService} statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 143 | * <p> |
| 144 | * Derives active rules by combining a given policy with other system status, |
| 145 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 146 | * enforcement. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 147 | */ |
| 148 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
| 149 | private static final String TAG = "NetworkPolicy"; |
| 150 | private static final boolean LOGD = true; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 151 | private static final boolean LOGV = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 153 | private static final int VERSION_INIT = 1; |
| 154 | private static final int VERSION_ADDED_SNOOZE = 2; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 155 | private static final int VERSION_ADDED_RESTRICT_BACKGROUND = 3; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 156 | |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 157 | private static final long KB_IN_BYTES = 1024; |
| 158 | private static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
| 159 | private static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
| 160 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 161 | // @VisibleForTesting |
| 162 | public static final int TYPE_WARNING = 0x1; |
| 163 | public static final int TYPE_LIMIT = 0x2; |
| 164 | public static final int TYPE_LIMIT_SNOOZED = 0x3; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 166 | private static final String TAG_POLICY_LIST = "policy-list"; |
| 167 | private static final String TAG_NETWORK_POLICY = "network-policy"; |
| 168 | private static final String TAG_UID_POLICY = "uid-policy"; |
| 169 | |
| 170 | private static final String ATTR_VERSION = "version"; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 171 | private static final String ATTR_RESTRICT_BACKGROUND = "restrictBackground"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 172 | private static final String ATTR_NETWORK_TEMPLATE = "networkTemplate"; |
| 173 | private static final String ATTR_SUBSCRIBER_ID = "subscriberId"; |
| 174 | private static final String ATTR_CYCLE_DAY = "cycleDay"; |
| 175 | private static final String ATTR_WARNING_BYTES = "warningBytes"; |
| 176 | private static final String ATTR_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 177 | private static final String ATTR_LAST_SNOOZE = "lastSnooze"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 178 | private static final String ATTR_UID = "uid"; |
| 179 | private static final String ATTR_POLICY = "policy"; |
| 180 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 181 | private static final String TAG_ALLOW_BACKGROUND = TAG + ":allowBackground"; |
| 182 | |
| 183 | // @VisibleForTesting |
| 184 | public static final String ACTION_ALLOW_BACKGROUND = |
| 185 | "com.android.server.action.ACTION_ALLOW_BACKGROUND"; |
| 186 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 187 | private static final long TIME_CACHE_MAX_AGE = DAY_IN_MILLIS; |
| 188 | |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 189 | private static final int MSG_RULES_CHANGED = 1; |
| 190 | private static final int MSG_METERED_IFACES_CHANGED = 2; |
| 191 | private static final int MSG_FOREGROUND_ACTIVITIES_CHANGED = 3; |
| 192 | private static final int MSG_PROCESS_DIED = 4; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 194 | private final Context mContext; |
| 195 | private final IActivityManager mActivityManager; |
| 196 | private final IPowerManager mPowerManager; |
| 197 | private final INetworkStatsService mNetworkStats; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 198 | private final INetworkManagementService mNetworkManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 199 | private final TrustedTime mTime; |
| 200 | |
| 201 | private IConnectivityManager mConnManager; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 202 | private INotificationManager mNotifManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 203 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 204 | private final Object mRulesLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 205 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 206 | private volatile boolean mScreenOn; |
| 207 | private volatile boolean mRestrictBackground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 208 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 209 | private final boolean mSuppressDefaultPolicy; |
| 210 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 211 | /** Defined network policies. */ |
| 212 | private HashMap<NetworkTemplate, NetworkPolicy> mNetworkPolicy = Maps.newHashMap(); |
| 213 | /** Currently active network rules for ifaces. */ |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 214 | private HashMap<NetworkPolicy, String[]> mNetworkRules = Maps.newHashMap(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 215 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 216 | /** Defined UID policies. */ |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 217 | private SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 218 | /** Currently derived rules for each UID. */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 219 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 220 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 221 | /** Set of ifaces that are metered. */ |
| 222 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 223 | /** Set of over-limit templates that have been notified. */ |
| 224 | private HashSet<NetworkTemplate> mOverLimitNotified = Sets.newHashSet(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 226 | /** Set of currently active {@link Notification} tags. */ |
| 227 | private HashSet<String> mActiveNotifs = Sets.newHashSet(); |
| 228 | /** Current values from {@link #setPolicyDataEnable(int, boolean)}. */ |
| 229 | private SparseBooleanArray mActiveNetworkEnabled = new SparseBooleanArray(); |
| 230 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 231 | /** Foreground at both UID and PID granularity. */ |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 232 | private SparseBooleanArray mUidForeground = new SparseBooleanArray(); |
| 233 | private SparseArray<SparseBooleanArray> mUidPidForeground = new SparseArray< |
| 234 | SparseBooleanArray>(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 235 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 236 | private final RemoteCallbackList<INetworkPolicyListener> mListeners = new RemoteCallbackList< |
| 237 | INetworkPolicyListener>(); |
| 238 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 239 | private final HandlerThread mHandlerThread; |
| 240 | private final Handler mHandler; |
| 241 | |
| 242 | private final AtomicFile mPolicyFile; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 243 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 244 | // TODO: keep whitelist of system-critical services that should never have |
| 245 | // rules enforced, such as system, phone, and radio UIDs. |
| 246 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 247 | // TODO: migrate notifications to SystemUI |
| 248 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 249 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 250 | IPowerManager powerManager, INetworkStatsService networkStats, |
| 251 | INetworkManagementService networkManagement) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 252 | this(context, activityManager, powerManager, networkStats, networkManagement, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 253 | NtpTrustedTime.getInstance(context), getSystemDir(), false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | private static File getSystemDir() { |
| 257 | return new File(Environment.getDataDirectory(), "system"); |
| 258 | } |
| 259 | |
| 260 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 261 | IPowerManager powerManager, INetworkStatsService networkStats, |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 262 | INetworkManagementService networkManagement, TrustedTime time, File systemDir, |
| 263 | boolean suppressDefaultPolicy) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 264 | mContext = checkNotNull(context, "missing context"); |
| 265 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
| 266 | mPowerManager = checkNotNull(powerManager, "missing powerManager"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 267 | mNetworkStats = checkNotNull(networkStats, "missing networkStats"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 268 | mNetworkManager = checkNotNull(networkManagement, "missing networkManagement"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 269 | mTime = checkNotNull(time, "missing TrustedTime"); |
| 270 | |
| 271 | mHandlerThread = new HandlerThread(TAG); |
| 272 | mHandlerThread.start(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 273 | mHandler = new Handler(mHandlerThread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 274 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 275 | mSuppressDefaultPolicy = suppressDefaultPolicy; |
| 276 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 277 | mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml")); |
| 278 | } |
| 279 | |
| 280 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 281 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 282 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 283 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 284 | public void bindNotificationManager(INotificationManager notifManager) { |
| 285 | mNotifManager = checkNotNull(notifManager, "missing INotificationManager"); |
| 286 | } |
| 287 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 288 | public void systemReady() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 289 | synchronized (mRulesLock) { |
| 290 | // read policy from disk |
| 291 | readPolicyLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 292 | |
| 293 | if (mRestrictBackground) { |
| 294 | updateRulesForRestrictBackgroundLocked(); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 295 | updateNotificationsLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 296 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 297 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 298 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 299 | updateScreenOn(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 300 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 301 | try { |
| 302 | mActivityManager.registerProcessObserver(mProcessObserver); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 303 | mNetworkManager.registerObserver(mAlertObserver); |
| 304 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 305 | // ignored; both services live in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 308 | // TODO: traverse existing processes to know foreground state, or have |
| 309 | // activitymanager dispatch current state when new observer attached. |
| 310 | |
| 311 | final IntentFilter screenFilter = new IntentFilter(); |
| 312 | screenFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 313 | screenFilter.addAction(Intent.ACTION_SCREEN_OFF); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 314 | mContext.registerReceiver(mScreenReceiver, screenFilter, null, mHandler); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 315 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 316 | // watch for network interfaces to be claimed |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 317 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 318 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 319 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 320 | // listen for package/uid changes to update policy |
| 321 | final IntentFilter packageFilter = new IntentFilter(); |
| 322 | packageFilter.addAction(ACTION_PACKAGE_ADDED); |
| 323 | packageFilter.addAction(ACTION_UID_REMOVED); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 324 | mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 325 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 326 | // listen for stats update events |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 327 | final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED); |
| 328 | mContext.registerReceiver( |
| 329 | mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
| 330 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 331 | // listen for restrict background changes from notifications |
| 332 | final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND); |
| 333 | mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler); |
| 334 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 337 | private IProcessObserver mProcessObserver = new IProcessObserver.Stub() { |
| 338 | @Override |
| 339 | public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 340 | mHandler.obtainMessage(MSG_FOREGROUND_ACTIVITIES_CHANGED, |
| 341 | pid, uid, foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 342 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 343 | |
| 344 | @Override |
| 345 | public void onProcessDied(int pid, int uid) { |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 346 | mHandler.obtainMessage(MSG_PROCESS_DIED, pid, uid).sendToTarget(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 347 | } |
| 348 | }; |
| 349 | |
| 350 | private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 351 | @Override |
| 352 | public void onReceive(Context context, Intent intent) { |
| 353 | synchronized (mRulesLock) { |
| 354 | // screen-related broadcasts are protected by system, no need |
| 355 | // for permissions check. |
| 356 | updateScreenOn(); |
| 357 | } |
| 358 | } |
| 359 | }; |
| 360 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 361 | private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 362 | @Override |
| 363 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 364 | // on background handler thread, and PACKAGE_ADDED and UID_REMOVED |
| 365 | // are protected broadcasts. |
| 366 | |
| 367 | final String action = intent.getAction(); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 368 | final int uid = intent.getIntExtra(EXTRA_UID, 0); |
| 369 | synchronized (mRulesLock) { |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 370 | if (ACTION_PACKAGE_ADDED.equals(action)) { |
| 371 | // update rules for UID, since it might be subject to |
| 372 | // global background data policy. |
| 373 | if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid); |
| 374 | updateRulesForUidLocked(uid); |
| 375 | |
| 376 | } else if (ACTION_UID_REMOVED.equals(action)) { |
| 377 | // remove any policy and update rules to clean up. |
| 378 | if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid); |
| 379 | mUidPolicy.delete(uid); |
| 380 | updateRulesForUidLocked(uid); |
| 381 | writePolicyLocked(); |
| 382 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | }; |
| 386 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 387 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 388 | * Receiver that watches for {@link INetworkStatsService} updates, which we |
| 389 | * use to check against {@link NetworkPolicy#warningBytes}. |
| 390 | */ |
| 391 | private BroadcastReceiver mStatsReceiver = new BroadcastReceiver() { |
| 392 | @Override |
| 393 | public void onReceive(Context context, Intent intent) { |
| 394 | // on background handler thread, and verified |
| 395 | // READ_NETWORK_USAGE_HISTORY permission above. |
| 396 | |
| 397 | synchronized (mRulesLock) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 398 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 399 | updateNotificationsLocked(); |
| 400 | } |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 405 | * Receiver that watches for {@link Notification} control of |
| 406 | * {@link #mRestrictBackground}. |
| 407 | */ |
| 408 | private BroadcastReceiver mAllowReceiver = new BroadcastReceiver() { |
| 409 | @Override |
| 410 | public void onReceive(Context context, Intent intent) { |
| 411 | // on background handler thread, and verified MANAGE_NETWORK_POLICY |
| 412 | // permission above. |
| 413 | |
| 414 | setRestrictBackground(false); |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 419 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 420 | */ |
| 421 | private INetworkManagementEventObserver mAlertObserver = new NetworkAlertObserver() { |
| 422 | @Override |
| 423 | public void limitReached(String limitName, String iface) { |
| 424 | // only someone like NMS should be calling us |
| 425 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 426 | |
| 427 | synchronized (mRulesLock) { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 428 | if (mMeteredIfaces.contains(iface) && !LIMIT_GLOBAL_ALERT.equals(limitName)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 429 | try { |
| 430 | // force stats update to make sure we have numbers that |
| 431 | // caused alert to trigger. |
| 432 | mNetworkStats.forceUpdate(); |
| 433 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 434 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 437 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 438 | updateNotificationsLocked(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | }; |
| 443 | |
| 444 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 445 | * Check {@link NetworkPolicy} against current {@link INetworkStatsService} |
| 446 | * to show visible notifications as needed. |
| 447 | */ |
| 448 | private void updateNotificationsLocked() { |
| 449 | if (LOGV) Slog.v(TAG, "updateNotificationsLocked()"); |
| 450 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 451 | // keep track of previously active notifications |
| 452 | final HashSet<String> beforeNotifs = Sets.newHashSet(); |
| 453 | beforeNotifs.addAll(mActiveNotifs); |
| 454 | mActiveNotifs.clear(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 455 | |
| 456 | // TODO: when switching to kernel notifications, compute next future |
| 457 | // cycle boundary to recompute notifications. |
| 458 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 459 | // examine stats for each active policy |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 460 | final long currentTime = currentTimeMillis(true); |
| 461 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
| 462 | // ignore policies that aren't relevant to user |
| 463 | if (!isTemplateRelevant(policy.template)) continue; |
| 464 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 465 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 466 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 467 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 468 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 469 | if (policy.isOverLimit(totalBytes)) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 470 | if (policy.lastSnooze >= start) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 471 | enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes); |
| 472 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 473 | enqueueNotification(policy, TYPE_LIMIT, totalBytes); |
| 474 | notifyOverLimitLocked(policy.template); |
| 475 | } |
| 476 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 477 | } else { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 478 | notifyUnderLimitLocked(policy.template); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 479 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 480 | if (policy.warningBytes != WARNING_DISABLED && totalBytes >= policy.warningBytes) { |
| 481 | enqueueNotification(policy, TYPE_WARNING, totalBytes); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 486 | // ongoing notification when restricting background data |
| 487 | if (mRestrictBackground) { |
| 488 | enqueueRestrictedNotification(TAG_ALLOW_BACKGROUND); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 489 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 490 | |
| 491 | // cancel stale notifications that we didn't renew above |
| 492 | for (String tag : beforeNotifs) { |
| 493 | if (!mActiveNotifs.contains(tag)) { |
| 494 | cancelNotification(tag); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Test if given {@link NetworkTemplate} is relevant to user based on |
| 501 | * current device state, such as when {@link #getActiveSubscriberId()} |
| 502 | * matches. This is regardless of data connection status. |
| 503 | */ |
| 504 | private boolean isTemplateRelevant(NetworkTemplate template) { |
| 505 | switch (template.getMatchRule()) { |
| 506 | case MATCH_MOBILE_3G_LOWER: |
| 507 | case MATCH_MOBILE_4G: |
| 508 | case MATCH_MOBILE_ALL: |
| 509 | // mobile templates are relevant when subscriberid is active |
| 510 | return Objects.equal(getActiveSubscriberId(), template.getSubscriberId()); |
| 511 | } |
| 512 | return true; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 516 | * Notify that given {@link NetworkTemplate} is over |
| 517 | * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user. |
| 518 | */ |
| 519 | private void notifyOverLimitLocked(NetworkTemplate template) { |
| 520 | if (!mOverLimitNotified.contains(template)) { |
| 521 | mContext.startActivity(buildNetworkOverLimitIntent(template)); |
| 522 | mOverLimitNotified.add(template); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | private void notifyUnderLimitLocked(NetworkTemplate template) { |
| 527 | mOverLimitNotified.remove(template); |
| 528 | } |
| 529 | |
| 530 | /** |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 531 | * Build unique tag that identifies an active {@link NetworkPolicy} |
| 532 | * notification of a specific type, like {@link #TYPE_LIMIT}. |
| 533 | */ |
| 534 | private String buildNotificationTag(NetworkPolicy policy, int type) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 535 | return TAG + ":" + policy.template.hashCode() + ":" + type; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Show notification for combined {@link NetworkPolicy} and specific type, |
| 540 | * like {@link #TYPE_LIMIT}. Okay to call multiple times. |
| 541 | */ |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 542 | private void enqueueNotification(NetworkPolicy policy, int type, long totalBytes) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 543 | final String tag = buildNotificationTag(policy, type); |
| 544 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 545 | builder.setOnlyAlertOnce(true); |
| 546 | builder.setOngoing(true); |
| 547 | |
| 548 | final Resources res = mContext.getResources(); |
| 549 | switch (type) { |
| 550 | case TYPE_WARNING: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 551 | final CharSequence title = res.getText(R.string.data_usage_warning_title); |
Jeff Sharkey | 8ca953d | 2011-09-14 19:56:11 -0700 | [diff] [blame] | 552 | final CharSequence body = res.getString(R.string.data_usage_warning_body); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 553 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 554 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 555 | builder.setTicker(title); |
| 556 | builder.setContentTitle(title); |
| 557 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 558 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 559 | final Intent intent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 560 | builder.setContentIntent(PendingIntent.getActivity( |
| 561 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 562 | break; |
| 563 | } |
| 564 | case TYPE_LIMIT: { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 565 | final CharSequence body = res.getText(R.string.data_usage_limit_body); |
| 566 | |
| 567 | final CharSequence title; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 568 | switch (policy.template.getMatchRule()) { |
| 569 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 570 | title = res.getText(R.string.data_usage_3g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 571 | break; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 572 | case MATCH_MOBILE_4G: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 573 | title = res.getText(R.string.data_usage_4g_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 574 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 575 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 576 | title = res.getText(R.string.data_usage_mobile_limit_title); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 577 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 578 | case MATCH_WIFI: |
| 579 | title = res.getText(R.string.data_usage_wifi_limit_title); |
| 580 | break; |
| 581 | default: |
| 582 | title = null; |
| 583 | break; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 586 | builder.setSmallIcon(R.drawable.stat_notify_disabled); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 587 | builder.setTicker(title); |
| 588 | builder.setContentTitle(title); |
| 589 | builder.setContentText(body); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 590 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 591 | final Intent intent = buildNetworkOverLimitIntent(policy.template); |
| 592 | builder.setContentIntent(PendingIntent.getActivity( |
| 593 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
| 594 | break; |
| 595 | } |
| 596 | case TYPE_LIMIT_SNOOZED: { |
| 597 | final long overBytes = totalBytes - policy.limitBytes; |
| 598 | final CharSequence body = res.getString(R.string.data_usage_limit_snoozed_body, |
| 599 | Formatter.formatFileSize(mContext, overBytes)); |
| 600 | |
| 601 | final CharSequence title; |
| 602 | switch (policy.template.getMatchRule()) { |
| 603 | case MATCH_MOBILE_3G_LOWER: |
| 604 | title = res.getText(R.string.data_usage_3g_limit_snoozed_title); |
| 605 | break; |
| 606 | case MATCH_MOBILE_4G: |
| 607 | title = res.getText(R.string.data_usage_4g_limit_snoozed_title); |
| 608 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 609 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 610 | title = res.getText(R.string.data_usage_mobile_limit_snoozed_title); |
| 611 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 612 | case MATCH_WIFI: |
| 613 | title = res.getText(R.string.data_usage_wifi_limit_snoozed_title); |
| 614 | break; |
| 615 | default: |
| 616 | title = null; |
| 617 | break; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 620 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 621 | builder.setTicker(title); |
| 622 | builder.setContentTitle(title); |
| 623 | builder.setContentText(body); |
| 624 | |
| 625 | final Intent intent = buildViewDataUsageIntent(policy.template); |
Jeff Sharkey | 14711eb | 2011-06-15 10:29:17 -0700 | [diff] [blame] | 626 | builder.setContentIntent(PendingIntent.getActivity( |
| 627 | mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // TODO: move to NotificationManager once we can mock it |
| 633 | try { |
| 634 | final String packageName = mContext.getPackageName(); |
| 635 | final int[] idReceived = new int[1]; |
| 636 | mNotifManager.enqueueNotificationWithTag( |
| 637 | packageName, tag, 0x0, builder.getNotification(), idReceived); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 638 | mActiveNotifs.add(tag); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 639 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 640 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 645 | * Show ongoing notification to reflect that {@link #mRestrictBackground} |
| 646 | * has been enabled. |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 647 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 648 | private void enqueueRestrictedNotification(String tag) { |
| 649 | final Resources res = mContext.getResources(); |
| 650 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 651 | |
| 652 | final CharSequence title = res.getText(R.string.data_usage_restricted_title); |
| 653 | final CharSequence body = res.getString(R.string.data_usage_restricted_body); |
| 654 | |
| 655 | builder.setOnlyAlertOnce(true); |
| 656 | builder.setOngoing(true); |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 657 | builder.setSmallIcon(R.drawable.stat_notify_error); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 658 | builder.setTicker(title); |
| 659 | builder.setContentTitle(title); |
| 660 | builder.setContentText(body); |
| 661 | |
| 662 | final Intent intent = buildAllowBackgroundDataIntent(); |
| 663 | builder.setContentIntent( |
| 664 | PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 665 | |
| 666 | // TODO: move to NotificationManager once we can mock it |
| 667 | try { |
| 668 | final String packageName = mContext.getPackageName(); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 669 | final int[] idReceived = new int[1]; |
| 670 | mNotifManager.enqueueNotificationWithTag(packageName, tag, |
| 671 | 0x0, builder.getNotification(), idReceived); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 672 | mActiveNotifs.add(tag); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 673 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 674 | // ignored; service lives in system_server |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 678 | private void cancelNotification(String tag) { |
| 679 | // TODO: move to NotificationManager once we can mock it |
| 680 | try { |
| 681 | final String packageName = mContext.getPackageName(); |
| 682 | mNotifManager.cancelNotificationWithTag( |
| 683 | packageName, tag, 0x0); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 684 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 685 | // ignored; service lives in system_server |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 690 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 691 | * interfaces. Used to apply {@link NetworkPolicy} to matching networks. |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 692 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 693 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 694 | @Override |
| 695 | public void onReceive(Context context, Intent intent) { |
| 696 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 697 | // permission above. |
| 698 | synchronized (mRulesLock) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 699 | ensureActiveMobilePolicyLocked(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 700 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 701 | updateNetworkRulesLocked(); |
| 702 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | }; |
| 706 | |
| 707 | /** |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 708 | * Proactively control network data connections when they exceed |
| 709 | * {@link NetworkPolicy#limitBytes}. |
| 710 | */ |
| 711 | private void updateNetworkEnabledLocked() { |
| 712 | if (LOGV) Slog.v(TAG, "updateNetworkEnabledLocked()"); |
| 713 | |
| 714 | // TODO: reset any policy-disabled networks when any policy is removed |
| 715 | // completely, which is currently rare case. |
| 716 | |
| 717 | final long currentTime = currentTimeMillis(true); |
| 718 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
| 719 | // shortcut when policy has no limit |
| 720 | if (policy.limitBytes == LIMIT_DISABLED) { |
| 721 | setNetworkTemplateEnabled(policy.template, true); |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 726 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 727 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 728 | |
| 729 | // disable data connection when over limit and not snoozed |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 730 | final boolean overLimit = policy.isOverLimit(totalBytes) && policy.lastSnooze < start; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 731 | final boolean enabled = !overLimit; |
| 732 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 733 | setNetworkTemplateEnabled(policy.template, enabled); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Control {@link IConnectivityManager#setPolicyDataEnable(int, boolean)} |
| 739 | * for the given {@link NetworkTemplate}. |
| 740 | */ |
| 741 | private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 742 | switch (template.getMatchRule()) { |
| 743 | case MATCH_MOBILE_3G_LOWER: |
| 744 | case MATCH_MOBILE_4G: |
| 745 | case MATCH_MOBILE_ALL: |
| 746 | // TODO: offer more granular control over radio states once |
| 747 | // 4965893 is available. |
| 748 | if (Objects.equal(getActiveSubscriberId(), template.getSubscriberId())) { |
| 749 | setPolicyDataEnable(TYPE_MOBILE, enabled); |
| 750 | setPolicyDataEnable(TYPE_WIMAX, enabled); |
| 751 | } |
| 752 | break; |
| 753 | case MATCH_WIFI: |
| 754 | setPolicyDataEnable(TYPE_WIFI, enabled); |
| 755 | break; |
| 756 | case MATCH_ETHERNET: |
| 757 | setPolicyDataEnable(TYPE_ETHERNET, enabled); |
| 758 | break; |
| 759 | default: |
| 760 | throw new IllegalArgumentException("unexpected template"); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /** |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 765 | * Examine all connected {@link NetworkState}, looking for |
| 766 | * {@link NetworkPolicy} that need to be enforced. When matches found, set |
| 767 | * remaining quota based on usage cycle and historical stats. |
| 768 | */ |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 769 | private void updateNetworkRulesLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 770 | if (LOGV) Slog.v(TAG, "updateIfacesLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 771 | |
| 772 | final NetworkState[] states; |
| 773 | try { |
| 774 | states = mConnManager.getAllNetworkState(); |
| 775 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 776 | // ignored; service lives in system_server |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 777 | return; |
| 778 | } |
| 779 | |
| 780 | // first, derive identity for all connected networks, which can be used |
| 781 | // to match against templates. |
| 782 | final HashMap<NetworkIdentity, String> networks = Maps.newHashMap(); |
| 783 | for (NetworkState state : states) { |
| 784 | // stash identity and iface away for later use |
| 785 | if (state.networkInfo.isConnected()) { |
| 786 | final String iface = state.linkProperties.getInterfaceName(); |
| 787 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 788 | networks.put(ident, iface); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // build list of rules and ifaces to enforce them against |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 793 | mNetworkRules.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 794 | final ArrayList<String> ifaceList = Lists.newArrayList(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 795 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 796 | |
| 797 | // collect all active ifaces that match this template |
| 798 | ifaceList.clear(); |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 799 | for (Map.Entry<NetworkIdentity, String> entry : networks.entrySet()) { |
| 800 | final NetworkIdentity ident = entry.getKey(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 801 | if (policy.template.matches(ident)) { |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 802 | final String iface = entry.getValue(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 803 | ifaceList.add(iface); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (ifaceList.size() > 0) { |
| 808 | final String[] ifaces = ifaceList.toArray(new String[ifaceList.size()]); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 809 | mNetworkRules.put(policy, ifaces); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 813 | final HashSet<String> newMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 814 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 815 | // apply each policy that we found ifaces for; compute remaining data |
| 816 | // based on current cycle and historical stats, and push to kernel. |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 817 | final long currentTime = currentTimeMillis(true); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 818 | for (NetworkPolicy policy : mNetworkRules.keySet()) { |
| 819 | final String[] ifaces = mNetworkRules.get(policy); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 820 | |
| 821 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 822 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 823 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 824 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 825 | if (LOGD) { |
| 826 | Slog.d(TAG, "applying policy " + policy.toString() + " to ifaces " |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 827 | + Arrays.toString(ifaces)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 830 | final boolean hasLimit = policy.limitBytes != LIMIT_DISABLED; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 831 | if (hasLimit) { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 832 | final long quotaBytes; |
| 833 | if (policy.lastSnooze >= start) { |
| 834 | // snoozing past quota, but we still need to restrict apps, |
| 835 | // so push really high quota. |
| 836 | quotaBytes = Long.MAX_VALUE; |
| 837 | } else { |
| 838 | // remaining "quota" bytes are based on total usage in |
| 839 | // current cycle. kernel doesn't like 0-byte rules, so we |
| 840 | // set 1-byte quota and disable the radio later. |
| 841 | quotaBytes = Math.max(1, policy.limitBytes - totalBytes); |
| 842 | } |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 843 | |
| 844 | if (ifaces.length > 1) { |
| 845 | // TODO: switch to shared quota once NMS supports |
| 846 | Slog.w(TAG, "shared quota unsupported; generating rule for each iface"); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 849 | for (String iface : ifaces) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 850 | removeInterfaceQuota(iface); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 851 | setInterfaceQuota(iface, quotaBytes); |
| 852 | newMeteredIfaces.add(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 855 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 856 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 857 | // remove quota on any trailing interfaces |
| 858 | for (String iface : mMeteredIfaces) { |
| 859 | if (!newMeteredIfaces.contains(iface)) { |
| 860 | removeInterfaceQuota(iface); |
| 861 | } |
| 862 | } |
| 863 | mMeteredIfaces = newMeteredIfaces; |
| 864 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 865 | final String[] meteredIfaces = mMeteredIfaces.toArray(new String[mMeteredIfaces.size()]); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 866 | mHandler.obtainMessage(MSG_METERED_IFACES_CHANGED, meteredIfaces).sendToTarget(); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we |
| 871 | * have at least a default mobile policy defined. |
| 872 | */ |
| 873 | private void ensureActiveMobilePolicyLocked() { |
| 874 | if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyLocked()"); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 875 | if (mSuppressDefaultPolicy) return; |
| 876 | |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 877 | final String subscriberId = getActiveSubscriberId(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 878 | final NetworkIdentity probeIdent = new NetworkIdentity( |
Jeff Sharkey | 5dc0c26 | 2011-06-19 22:21:05 -0700 | [diff] [blame] | 879 | TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, false); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 880 | |
| 881 | // examine to see if any policy is defined for active mobile |
| 882 | boolean mobileDefined = false; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 883 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 884 | if (policy.template.matches(probeIdent)) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 885 | mobileDefined = true; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | if (!mobileDefined) { |
| 890 | Slog.i(TAG, "no policy for active mobile network; generating default policy"); |
| 891 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 892 | // build default mobile policy, and assume usage cycle starts today |
| 893 | final long warningBytes = mContext.getResources().getInteger( |
| 894 | com.android.internal.R.integer.config_networkPolicyDefaultWarning) |
| 895 | * MB_IN_BYTES; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 896 | |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 897 | final Time time = new Time(Time.TIMEZONE_UTC); |
| 898 | time.setToNow(); |
| 899 | final int cycleDay = time.monthDay; |
| 900 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 901 | final NetworkTemplate template = buildTemplateMobileAll(subscriberId); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 902 | mNetworkPolicy.put(template, new NetworkPolicy( |
| 903 | template, cycleDay, warningBytes, LIMIT_DISABLED, SNOOZE_NEVER)); |
Jeff Sharkey | fcc7977 | 2011-06-13 22:28:34 -0700 | [diff] [blame] | 904 | writePolicyLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 908 | private void readPolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 909 | if (LOGV) Slog.v(TAG, "readPolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 910 | |
| 911 | // clear any existing policy and read from disk |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 912 | mNetworkPolicy.clear(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 913 | mUidPolicy.clear(); |
| 914 | |
| 915 | FileInputStream fis = null; |
| 916 | try { |
| 917 | fis = mPolicyFile.openRead(); |
| 918 | final XmlPullParser in = Xml.newPullParser(); |
| 919 | in.setInput(fis, null); |
| 920 | |
| 921 | int type; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 922 | int version = VERSION_INIT; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 923 | while ((type = in.next()) != END_DOCUMENT) { |
| 924 | final String tag = in.getName(); |
| 925 | if (type == START_TAG) { |
| 926 | if (TAG_POLICY_LIST.equals(tag)) { |
| 927 | version = readIntAttribute(in, ATTR_VERSION); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 928 | if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) { |
| 929 | mRestrictBackground = readBooleanAttribute( |
| 930 | in, ATTR_RESTRICT_BACKGROUND); |
| 931 | } else { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 932 | mRestrictBackground = false; |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 933 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 934 | |
| 935 | } else if (TAG_NETWORK_POLICY.equals(tag)) { |
| 936 | final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE); |
| 937 | final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 938 | final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY); |
| 939 | final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES); |
| 940 | final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 941 | final long lastSnooze; |
| 942 | if (version >= VERSION_ADDED_SNOOZE) { |
| 943 | lastSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE); |
| 944 | } else { |
| 945 | lastSnooze = SNOOZE_NEVER; |
| 946 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 947 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 948 | final NetworkTemplate template = new NetworkTemplate( |
| 949 | networkTemplate, subscriberId); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 950 | mNetworkPolicy.put(template, new NetworkPolicy( |
| 951 | template, cycleDay, warningBytes, limitBytes, lastSnooze)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 952 | |
| 953 | } else if (TAG_UID_POLICY.equals(tag)) { |
| 954 | final int uid = readIntAttribute(in, ATTR_UID); |
| 955 | final int policy = readIntAttribute(in, ATTR_POLICY); |
| 956 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 957 | if (isUidValidForPolicy(mContext, uid)) { |
| 958 | setUidPolicyUnchecked(uid, policy, false); |
| 959 | } else { |
| 960 | Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring"); |
| 961 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | } catch (FileNotFoundException e) { |
| 967 | // missing policy is okay, probably first boot |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 968 | upgradeLegacyBackgroundData(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 969 | } catch (IOException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 970 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 971 | } catch (XmlPullParserException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 972 | Log.wtf(TAG, "problem reading network policy", e); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 973 | } finally { |
| 974 | IoUtils.closeQuietly(fis); |
| 975 | } |
| 976 | } |
| 977 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 978 | /** |
| 979 | * Upgrade legacy background data flags, notifying listeners of one last |
| 980 | * change to always-true. |
| 981 | */ |
| 982 | private void upgradeLegacyBackgroundData() { |
| 983 | mRestrictBackground = Settings.Secure.getInt( |
| 984 | mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, 1) != 1; |
| 985 | |
| 986 | // kick off one last broadcast if restricted |
| 987 | if (mRestrictBackground) { |
| 988 | final Intent broadcast = new Intent( |
| 989 | ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
| 990 | mContext.sendBroadcast(broadcast); |
| 991 | } |
| 992 | } |
| 993 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 994 | private void writePolicyLocked() { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 995 | if (LOGV) Slog.v(TAG, "writePolicyLocked()"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 996 | |
| 997 | FileOutputStream fos = null; |
| 998 | try { |
| 999 | fos = mPolicyFile.startWrite(); |
| 1000 | |
| 1001 | XmlSerializer out = new FastXmlSerializer(); |
| 1002 | out.setOutput(fos, "utf-8"); |
| 1003 | out.startDocument(null, true); |
| 1004 | |
| 1005 | out.startTag(null, TAG_POLICY_LIST); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1006 | writeIntAttribute(out, ATTR_VERSION, VERSION_ADDED_RESTRICT_BACKGROUND); |
| 1007 | writeBooleanAttribute(out, ATTR_RESTRICT_BACKGROUND, mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1008 | |
| 1009 | // write all known network policies |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1010 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1011 | final NetworkTemplate template = policy.template; |
| 1012 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1013 | out.startTag(null, TAG_NETWORK_POLICY); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1014 | writeIntAttribute(out, ATTR_NETWORK_TEMPLATE, template.getMatchRule()); |
| 1015 | final String subscriberId = template.getSubscriberId(); |
| 1016 | if (subscriberId != null) { |
| 1017 | out.attribute(null, ATTR_SUBSCRIBER_ID, subscriberId); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1018 | } |
| 1019 | writeIntAttribute(out, ATTR_CYCLE_DAY, policy.cycleDay); |
| 1020 | writeLongAttribute(out, ATTR_WARNING_BYTES, policy.warningBytes); |
| 1021 | writeLongAttribute(out, ATTR_LIMIT_BYTES, policy.limitBytes); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1022 | writeLongAttribute(out, ATTR_LAST_SNOOZE, policy.lastSnooze); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1023 | out.endTag(null, TAG_NETWORK_POLICY); |
| 1024 | } |
| 1025 | |
| 1026 | // write all known uid policies |
| 1027 | for (int i = 0; i < mUidPolicy.size(); i++) { |
| 1028 | final int uid = mUidPolicy.keyAt(i); |
| 1029 | final int policy = mUidPolicy.valueAt(i); |
| 1030 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1031 | // skip writing empty policies |
| 1032 | if (policy == POLICY_NONE) continue; |
| 1033 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1034 | out.startTag(null, TAG_UID_POLICY); |
| 1035 | writeIntAttribute(out, ATTR_UID, uid); |
| 1036 | writeIntAttribute(out, ATTR_POLICY, policy); |
| 1037 | out.endTag(null, TAG_UID_POLICY); |
| 1038 | } |
| 1039 | |
| 1040 | out.endTag(null, TAG_POLICY_LIST); |
| 1041 | out.endDocument(); |
| 1042 | |
| 1043 | mPolicyFile.finishWrite(fos); |
| 1044 | } catch (IOException e) { |
| 1045 | if (fos != null) { |
| 1046 | mPolicyFile.failWrite(fos); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1051 | @Override |
| 1052 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1053 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1054 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1055 | if (!isUidValidForPolicy(mContext, uid)) { |
| 1056 | throw new IllegalArgumentException("cannot apply policy to UID " + uid); |
| 1057 | } |
| 1058 | |
| 1059 | setUidPolicyUnchecked(uid, policy, true); |
| 1060 | } |
| 1061 | |
| 1062 | private void setUidPolicyUnchecked(int uid, int policy, boolean persist) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1063 | final int oldPolicy; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1064 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1065 | oldPolicy = getUidPolicy(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1066 | mUidPolicy.put(uid, policy); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1067 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1068 | // uid policy changed, recompute rules and persist policy. |
| 1069 | updateRulesForUidLocked(uid); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1070 | if (persist) { |
| 1071 | writePolicyLocked(); |
| 1072 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1073 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | @Override |
| 1077 | public int getUidPolicy(int uid) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1078 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1079 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1080 | synchronized (mRulesLock) { |
| 1081 | return mUidPolicy.get(uid, POLICY_NONE); |
| 1082 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1085 | @Override |
| 1086 | public void registerListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1087 | // TODO: create permission for observing network policy |
| 1088 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1089 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1090 | mListeners.register(listener); |
| 1091 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1092 | // TODO: consider dispatching existing rules to new listeners |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | @Override |
| 1096 | public void unregisterListener(INetworkPolicyListener listener) { |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 1097 | // TODO: create permission for observing network policy |
| 1098 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1099 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1100 | mListeners.unregister(listener); |
| 1101 | } |
| 1102 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1103 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1104 | public void setNetworkPolicies(NetworkPolicy[] policies) { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1105 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1106 | |
| 1107 | synchronized (mRulesLock) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1108 | mNetworkPolicy.clear(); |
| 1109 | for (NetworkPolicy policy : policies) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1110 | mNetworkPolicy.put(policy.template, policy); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1111 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1112 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1113 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 1114 | updateNetworkRulesLocked(); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1115 | updateNotificationsLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1116 | writePolicyLocked(); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | @Override |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1121 | public NetworkPolicy[] getNetworkPolicies() { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1122 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1123 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, TAG); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1124 | |
| 1125 | synchronized (mRulesLock) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1126 | return mNetworkPolicy.values().toArray(new NetworkPolicy[mNetworkPolicy.size()]); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | @Override |
| 1131 | public void snoozePolicy(NetworkTemplate template) { |
| 1132 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1133 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1134 | final long currentTime = currentTimeMillis(true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1135 | synchronized (mRulesLock) { |
| 1136 | // find and snooze local policy that matches |
| 1137 | final NetworkPolicy policy = mNetworkPolicy.get(template); |
| 1138 | if (policy == null) { |
| 1139 | throw new IllegalArgumentException("unable to find policy for " + template); |
| 1140 | } |
| 1141 | |
| 1142 | policy.lastSnooze = currentTime; |
| 1143 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1144 | updateNetworkEnabledLocked(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1145 | updateNetworkRulesLocked(); |
| 1146 | updateNotificationsLocked(); |
| 1147 | writePolicyLocked(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1152 | public void setRestrictBackground(boolean restrictBackground) { |
| 1153 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1154 | |
| 1155 | synchronized (mRulesLock) { |
| 1156 | mRestrictBackground = restrictBackground; |
| 1157 | updateRulesForRestrictBackgroundLocked(); |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1158 | updateNotificationsLocked(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1159 | writePolicyLocked(); |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | @Override |
| 1164 | public boolean getRestrictBackground() { |
| 1165 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1166 | |
| 1167 | synchronized (mRulesLock) { |
| 1168 | return mRestrictBackground; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1172 | private NetworkPolicy findPolicyForNetworkLocked(NetworkIdentity ident) { |
| 1173 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
| 1174 | if (policy.template.matches(ident)) { |
| 1175 | return policy; |
| 1176 | } |
| 1177 | } |
| 1178 | return null; |
| 1179 | } |
| 1180 | |
| 1181 | @Override |
| 1182 | public NetworkQuotaInfo getNetworkQuotaInfo(NetworkState state) { |
| 1183 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 1184 | |
| 1185 | // only returns usage summary, so we don't require caller to have |
| 1186 | // READ_NETWORK_USAGE_HISTORY. |
| 1187 | final long token = Binder.clearCallingIdentity(); |
| 1188 | try { |
| 1189 | return getNetworkQuotaInfoUnchecked(state); |
| 1190 | } finally { |
| 1191 | Binder.restoreCallingIdentity(token); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | private NetworkQuotaInfo getNetworkQuotaInfoUnchecked(NetworkState state) { |
| 1196 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state); |
| 1197 | |
| 1198 | final NetworkPolicy policy; |
| 1199 | synchronized (mRulesLock) { |
| 1200 | policy = findPolicyForNetworkLocked(ident); |
| 1201 | } |
| 1202 | |
| 1203 | if (policy == null) { |
| 1204 | // missing policy means we can't derive useful quota info |
| 1205 | return null; |
| 1206 | } |
| 1207 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1208 | final long currentTime = currentTimeMillis(false); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1209 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1210 | // find total bytes used under policy |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1211 | final long start = computeLastCycleBoundary(currentTime, policy); |
| 1212 | final long end = currentTime; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1213 | final long totalBytes = getTotalBytes(policy.template, start, end); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1214 | |
| 1215 | // report soft and hard limits under policy |
| 1216 | final long softLimitBytes = policy.warningBytes != WARNING_DISABLED ? policy.warningBytes |
| 1217 | : NetworkQuotaInfo.NO_LIMIT; |
| 1218 | final long hardLimitBytes = policy.limitBytes != LIMIT_DISABLED ? policy.limitBytes |
| 1219 | : NetworkQuotaInfo.NO_LIMIT; |
| 1220 | |
| 1221 | return new NetworkQuotaInfo(totalBytes, softLimitBytes, hardLimitBytes); |
| 1222 | } |
| 1223 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1224 | @Override |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1225 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1226 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1227 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1228 | final HashSet<String> argSet = new HashSet<String>(); |
| 1229 | for (String arg : args) { |
| 1230 | argSet.add(arg); |
| 1231 | } |
| 1232 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1233 | synchronized (mRulesLock) { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1234 | if (argSet.contains("unsnooze")) { |
| 1235 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
| 1236 | policy.lastSnooze = SNOOZE_NEVER; |
| 1237 | } |
| 1238 | writePolicyLocked(); |
| 1239 | fout.println("Wiped snooze timestamps"); |
| 1240 | return; |
| 1241 | } |
| 1242 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1243 | fout.print("Restrict background: "); fout.println(mRestrictBackground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1244 | fout.println("Network policies:"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1245 | for (NetworkPolicy policy : mNetworkPolicy.values()) { |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1246 | fout.print(" "); fout.println(policy.toString()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1249 | fout.println("Policy status for known UIDs:"); |
| 1250 | |
| 1251 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 1252 | collectKeys(mUidPolicy, knownUids); |
| 1253 | collectKeys(mUidForeground, knownUids); |
| 1254 | collectKeys(mUidRules, knownUids); |
| 1255 | |
| 1256 | final int size = knownUids.size(); |
| 1257 | for (int i = 0; i < size; i++) { |
| 1258 | final int uid = knownUids.keyAt(i); |
| 1259 | fout.print(" UID="); |
| 1260 | fout.print(uid); |
| 1261 | |
| 1262 | fout.print(" policy="); |
| 1263 | final int policyIndex = mUidPolicy.indexOfKey(uid); |
| 1264 | if (policyIndex < 0) { |
| 1265 | fout.print("UNKNOWN"); |
| 1266 | } else { |
| 1267 | dumpPolicy(fout, mUidPolicy.valueAt(policyIndex)); |
| 1268 | } |
| 1269 | |
| 1270 | fout.print(" foreground="); |
| 1271 | final int foregroundIndex = mUidPidForeground.indexOfKey(uid); |
| 1272 | if (foregroundIndex < 0) { |
| 1273 | fout.print("UNKNOWN"); |
| 1274 | } else { |
| 1275 | dumpSparseBooleanArray(fout, mUidPidForeground.valueAt(foregroundIndex)); |
| 1276 | } |
| 1277 | |
| 1278 | fout.print(" rules="); |
| 1279 | final int rulesIndex = mUidRules.indexOfKey(uid); |
| 1280 | if (rulesIndex < 0) { |
| 1281 | fout.print("UNKNOWN"); |
| 1282 | } else { |
| 1283 | dumpRules(fout, mUidRules.valueAt(rulesIndex)); |
| 1284 | } |
| 1285 | |
| 1286 | fout.println(); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1290 | |
| 1291 | @Override |
| 1292 | public boolean isUidForeground(int uid) { |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1293 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1294 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1295 | synchronized (mRulesLock) { |
| 1296 | // only really in foreground when screen is also on |
| 1297 | return mUidForeground.get(uid, false) && mScreenOn; |
| 1298 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1301 | /** |
| 1302 | * Foreground for PID changed; recompute foreground at UID level. If |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1303 | * changed, will trigger {@link #updateRulesForUidLocked(int)}. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1304 | */ |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1305 | private void computeUidForegroundLocked(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1306 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 1307 | |
| 1308 | // current pid is dropping foreground; examine other pids |
| 1309 | boolean uidForeground = false; |
| 1310 | final int size = pidForeground.size(); |
| 1311 | for (int i = 0; i < size; i++) { |
| 1312 | if (pidForeground.valueAt(i)) { |
| 1313 | uidForeground = true; |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | final boolean oldUidForeground = mUidForeground.get(uid, false); |
| 1319 | if (oldUidForeground != uidForeground) { |
| 1320 | // foreground changed, push updated rules |
| 1321 | mUidForeground.put(uid, uidForeground); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1322 | updateRulesForUidLocked(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1326 | private void updateScreenOn() { |
| 1327 | synchronized (mRulesLock) { |
| 1328 | try { |
| 1329 | mScreenOn = mPowerManager.isScreenOn(); |
| 1330 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1331 | // ignored; service lives in system_server |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1332 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1333 | updateRulesForScreenLocked(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * Update rules that might be changed by {@link #mScreenOn} value. |
| 1339 | */ |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1340 | private void updateRulesForScreenLocked() { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1341 | // only update rules for anyone with foreground activities |
| 1342 | final int size = mUidForeground.size(); |
| 1343 | for (int i = 0; i < size; i++) { |
| 1344 | if (mUidForeground.valueAt(i)) { |
| 1345 | final int uid = mUidForeground.keyAt(i); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1346 | updateRulesForUidLocked(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1351 | /** |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1352 | * Update rules that might be changed by {@link #mRestrictBackground} value. |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1353 | */ |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1354 | private void updateRulesForRestrictBackgroundLocked() { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1355 | // update rules for all installed applications |
| 1356 | final PackageManager pm = mContext.getPackageManager(); |
| 1357 | final List<ApplicationInfo> apps = pm.getInstalledApplications(0); |
| 1358 | for (ApplicationInfo app : apps) { |
| 1359 | updateRulesForUidLocked(app.uid); |
| 1360 | } |
| 1361 | |
| 1362 | // and catch system UIDs |
| 1363 | // TODO: keep in sync with android_filesystem_config.h |
| 1364 | for (int uid = 1000; uid <= 1025; uid++) { |
| 1365 | updateRulesForUidLocked(uid); |
| 1366 | } |
| 1367 | for (int uid = 2000; uid <= 2002; uid++) { |
| 1368 | updateRulesForUidLocked(uid); |
| 1369 | } |
| 1370 | for (int uid = 3000; uid <= 3007; uid++) { |
| 1371 | updateRulesForUidLocked(uid); |
| 1372 | } |
| 1373 | for (int uid = 9998; uid <= 9999; uid++) { |
| 1374 | updateRulesForUidLocked(uid); |
| 1375 | } |
| 1376 | } |
| 1377 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1378 | private void updateRulesForUidLocked(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1379 | final int uidPolicy = getUidPolicy(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1380 | final boolean uidForeground = isUidForeground(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1381 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1382 | // derive active rules based on policy and active state |
| 1383 | int uidRules = RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1384 | if (!uidForeground && (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0) { |
| 1385 | // uid in background, and policy says to block metered data |
| 1386 | uidRules = RULE_REJECT_METERED; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1387 | } |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1388 | if (!uidForeground && mRestrictBackground) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1389 | // uid in background, and global background disabled |
| 1390 | uidRules = RULE_REJECT_METERED; |
| 1391 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1392 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1393 | // TODO: only dispatch when rules actually change |
| 1394 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1395 | if (uidRules == RULE_ALLOW_ALL) { |
| 1396 | mUidRules.delete(uid); |
| 1397 | } else { |
| 1398 | mUidRules.put(uid, uidRules); |
| 1399 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1400 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1401 | final boolean rejectMetered = (uidRules & RULE_REJECT_METERED) != 0; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1402 | setUidNetworkRules(uid, rejectMetered); |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1403 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1404 | // dispatch changed rule to existing listeners |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1405 | mHandler.obtainMessage(MSG_RULES_CHANGED, uid, uidRules).sendToTarget(); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1406 | |
| 1407 | try { |
| 1408 | // adjust stats accounting based on foreground status |
| 1409 | mNetworkStats.setUidForeground(uid, uidForeground); |
| 1410 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1411 | // ignored; service lives in system_server |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1412 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | private Handler.Callback mHandlerCallback = new Handler.Callback() { |
| 1416 | /** {@inheritDoc} */ |
| 1417 | public boolean handleMessage(Message msg) { |
| 1418 | switch (msg.what) { |
| 1419 | case MSG_RULES_CHANGED: { |
| 1420 | final int uid = msg.arg1; |
| 1421 | final int uidRules = msg.arg2; |
| 1422 | final int length = mListeners.beginBroadcast(); |
| 1423 | for (int i = 0; i < length; i++) { |
| 1424 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 1425 | if (listener != null) { |
| 1426 | try { |
| 1427 | listener.onUidRulesChanged(uid, uidRules); |
| 1428 | } catch (RemoteException e) { |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | mListeners.finishBroadcast(); |
| 1433 | return true; |
| 1434 | } |
| 1435 | case MSG_METERED_IFACES_CHANGED: { |
| 1436 | final String[] meteredIfaces = (String[]) msg.obj; |
| 1437 | final int length = mListeners.beginBroadcast(); |
| 1438 | for (int i = 0; i < length; i++) { |
| 1439 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 1440 | if (listener != null) { |
| 1441 | try { |
| 1442 | listener.onMeteredIfacesChanged(meteredIfaces); |
| 1443 | } catch (RemoteException e) { |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | mListeners.finishBroadcast(); |
| 1448 | return true; |
| 1449 | } |
Jeff Sharkey | 6f7af03 | 2011-11-01 18:25:15 -0700 | [diff] [blame] | 1450 | case MSG_FOREGROUND_ACTIVITIES_CHANGED: { |
| 1451 | final int pid = msg.arg1; |
| 1452 | final int uid = msg.arg2; |
| 1453 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1454 | |
| 1455 | synchronized (mRulesLock) { |
| 1456 | // because a uid can have multiple pids running inside, we need to |
| 1457 | // remember all pid states and summarize foreground at uid level. |
| 1458 | |
| 1459 | // record foreground for this specific pid |
| 1460 | SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 1461 | if (pidForeground == null) { |
| 1462 | pidForeground = new SparseBooleanArray(2); |
| 1463 | mUidPidForeground.put(uid, pidForeground); |
| 1464 | } |
| 1465 | pidForeground.put(pid, foregroundActivities); |
| 1466 | computeUidForegroundLocked(uid); |
| 1467 | } |
| 1468 | return true; |
| 1469 | } |
| 1470 | case MSG_PROCESS_DIED: { |
| 1471 | final int pid = msg.arg1; |
| 1472 | final int uid = msg.arg2; |
| 1473 | |
| 1474 | synchronized (mRulesLock) { |
| 1475 | // clear records and recompute, when they exist |
| 1476 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 1477 | if (pidForeground != null) { |
| 1478 | pidForeground.delete(pid); |
| 1479 | computeUidForegroundLocked(uid); |
| 1480 | } |
| 1481 | } |
| 1482 | return true; |
| 1483 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1484 | default: { |
| 1485 | return false; |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1489 | }; |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1490 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1491 | private void setInterfaceQuota(String iface, long quotaBytes) { |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1492 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1493 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1494 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1495 | Log.wtf(TAG, "problem setting interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1496 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1497 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | private void removeInterfaceQuota(String iface) { |
| 1502 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1503 | mNetworkManager.removeInterfaceQuota(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1504 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1505 | Log.wtf(TAG, "problem removing interface quota", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1506 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1507 | // ignored; service lives in system_server |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1511 | private void setUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) { |
| 1512 | try { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1513 | mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1514 | } catch (IllegalStateException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1515 | Log.wtf(TAG, "problem setting uid rules", e); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1516 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1517 | // ignored; service lives in system_server |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1521 | /** |
| 1522 | * Control {@link IConnectivityManager#setPolicyDataEnable(int, boolean)}, |
| 1523 | * dispatching only when actually changed. |
| 1524 | */ |
| 1525 | private void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1526 | synchronized (mActiveNetworkEnabled) { |
| 1527 | final boolean prevEnabled = mActiveNetworkEnabled.get(networkType, true); |
| 1528 | if (prevEnabled == enabled) return; |
| 1529 | |
| 1530 | try { |
| 1531 | mConnManager.setPolicyDataEnable(networkType, enabled); |
| 1532 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1533 | // ignored; service lives in system_server |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | mActiveNetworkEnabled.put(networkType, enabled); |
| 1537 | } |
| 1538 | } |
| 1539 | |
Jeff Sharkey | 22c055e | 2011-06-12 21:13:51 -0700 | [diff] [blame] | 1540 | private String getActiveSubscriberId() { |
| 1541 | final TelephonyManager telephony = (TelephonyManager) mContext.getSystemService( |
| 1542 | Context.TELEPHONY_SERVICE); |
| 1543 | return telephony.getSubscriberId(); |
| 1544 | } |
| 1545 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1546 | private long getTotalBytes(NetworkTemplate template, long start, long end) { |
| 1547 | try { |
Jeff Sharkey | 50e7e51 | 2011-10-10 16:50:35 -0700 | [diff] [blame] | 1548 | return mNetworkStats.getSummaryForNetwork(template, start, end).getTotalBytes(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1549 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1550 | // ignored; service lives in system_server |
| 1551 | return 0; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | private long currentTimeMillis(boolean allowRefresh) { |
| 1556 | // try refreshing time source when stale |
| 1557 | if (mTime.getCacheAge() > TIME_CACHE_MAX_AGE && allowRefresh) { |
| 1558 | mTime.forceRefresh(); |
| 1559 | } |
| 1560 | |
| 1561 | return mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis(); |
| 1562 | } |
| 1563 | |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1564 | private static Intent buildAllowBackgroundDataIntent() { |
| 1565 | return new Intent(ACTION_ALLOW_BACKGROUND); |
| 1566 | } |
| 1567 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1568 | private static Intent buildNetworkOverLimitIntent(NetworkTemplate template) { |
| 1569 | final Intent intent = new Intent(); |
| 1570 | intent.setComponent(new ComponentName( |
| 1571 | "com.android.systemui", "com.android.systemui.net.NetworkOverLimitActivity")); |
| 1572 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1573 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 1574 | return intent; |
| 1575 | } |
| 1576 | |
| 1577 | private static Intent buildViewDataUsageIntent(NetworkTemplate template) { |
| 1578 | final Intent intent = new Intent(); |
| 1579 | intent.setComponent(new ComponentName( |
| 1580 | "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); |
| 1581 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1582 | intent.putExtra(EXTRA_NETWORK_TEMPLATE, template); |
| 1583 | return intent; |
| 1584 | } |
| 1585 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1586 | // @VisibleForTesting |
| 1587 | public void addIdleHandler(IdleHandler handler) { |
| 1588 | mHandler.getLooper().getQueue().addIdleHandler(handler); |
| 1589 | } |
| 1590 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 1591 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 1592 | final int size = source.size(); |
| 1593 | for (int i = 0; i < size; i++) { |
| 1594 | target.put(source.keyAt(i), true); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | private static void collectKeys(SparseBooleanArray source, SparseBooleanArray target) { |
| 1599 | final int size = source.size(); |
| 1600 | for (int i = 0; i < size; i++) { |
| 1601 | target.put(source.keyAt(i), true); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | private static void dumpSparseBooleanArray(PrintWriter fout, SparseBooleanArray value) { |
| 1606 | fout.print("["); |
| 1607 | final int size = value.size(); |
| 1608 | for (int i = 0; i < size; i++) { |
| 1609 | fout.print(value.keyAt(i) + "=" + value.valueAt(i)); |
| 1610 | if (i < size - 1) fout.print(","); |
| 1611 | } |
| 1612 | fout.print("]"); |
| 1613 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1614 | |
Jeff Sharkey | 4664500 | 2011-07-27 21:11:21 -0700 | [diff] [blame] | 1615 | public static class XmlUtils { |
| 1616 | public static int readIntAttribute(XmlPullParser in, String name) throws IOException { |
| 1617 | final String value = in.getAttributeValue(null, name); |
| 1618 | try { |
| 1619 | return Integer.parseInt(value); |
| 1620 | } catch (NumberFormatException e) { |
| 1621 | throw new ProtocolException("problem parsing " + name + "=" + value + " as int"); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | public static void writeIntAttribute(XmlSerializer out, String name, int value) |
| 1626 | throws IOException { |
| 1627 | out.attribute(null, name, Integer.toString(value)); |
| 1628 | } |
| 1629 | |
| 1630 | public static long readLongAttribute(XmlPullParser in, String name) throws IOException { |
| 1631 | final String value = in.getAttributeValue(null, name); |
| 1632 | try { |
| 1633 | return Long.parseLong(value); |
| 1634 | } catch (NumberFormatException e) { |
| 1635 | throw new ProtocolException("problem parsing " + name + "=" + value + " as long"); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | public static void writeLongAttribute(XmlSerializer out, String name, long value) |
| 1640 | throws IOException { |
| 1641 | out.attribute(null, name, Long.toString(value)); |
| 1642 | } |
| 1643 | |
| 1644 | public static boolean readBooleanAttribute(XmlPullParser in, String name) { |
| 1645 | final String value = in.getAttributeValue(null, name); |
| 1646 | return Boolean.parseBoolean(value); |
| 1647 | } |
| 1648 | |
| 1649 | public static void writeBooleanAttribute(XmlSerializer out, String name, boolean value) |
| 1650 | throws IOException { |
| 1651 | out.attribute(null, name, Boolean.toString(value)); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1652 | } |
| 1653 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1654 | } |