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 | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.MANAGE_APP_TOKENS; |
| 21 | import static android.Manifest.permission.UPDATE_DEVICE_STATS; |
| 22 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicyManager.POLICY_REJECT_PAID_BACKGROUND; |
| 24 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
| 25 | import static android.net.NetworkPolicyManager.RULE_REJECT_PAID; |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.dumpPolicy; |
| 27 | import static android.net.NetworkPolicyManager.dumpRules; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 28 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 29 | import android.app.IActivityManager; |
| 30 | import android.app.IProcessObserver; |
| 31 | import android.content.BroadcastReceiver; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 35 | import android.net.ConnectivityManager; |
| 36 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 37 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 38 | import android.net.INetworkStatsService; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 39 | import android.os.IPowerManager; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 40 | import android.os.RemoteCallbackList; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 42 | import android.util.Slog; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 43 | import android.util.SparseArray; |
| 44 | import android.util.SparseBooleanArray; |
| 45 | import android.util.SparseIntArray; |
| 46 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 47 | import java.io.FileDescriptor; |
| 48 | import java.io.PrintWriter; |
| 49 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Service that maintains low-level network policy rules and collects usage |
| 52 | * statistics to drive those rules. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 53 | * <p> |
| 54 | * Derives active rules by combining a given policy with other system status, |
| 55 | * and delivers to listeners, such as {@link ConnectivityManager}, for |
| 56 | * enforcement. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 57 | */ |
| 58 | public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { |
| 59 | private static final String TAG = "NetworkPolicy"; |
| 60 | private static final boolean LOGD = true; |
| 61 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 62 | private final Context mContext; |
| 63 | private final IActivityManager mActivityManager; |
| 64 | private final IPowerManager mPowerManager; |
| 65 | private final INetworkStatsService mNetworkStats; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 66 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 67 | private final Object mRulesLock = new Object(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 68 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 69 | private boolean mScreenOn; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 70 | |
| 71 | /** Current network policy for each UID. */ |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 72 | private SparseIntArray mUidPolicy = new SparseIntArray(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 73 | /** Current derived network rules for each UID. */ |
| 74 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 75 | |
| 76 | /** Foreground at both UID and PID granularity. */ |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 77 | private SparseBooleanArray mUidForeground = new SparseBooleanArray(); |
| 78 | private SparseArray<SparseBooleanArray> mUidPidForeground = new SparseArray< |
| 79 | SparseBooleanArray>(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 80 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 81 | private final RemoteCallbackList<INetworkPolicyListener> mListeners = new RemoteCallbackList< |
| 82 | INetworkPolicyListener>(); |
| 83 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 84 | // TODO: save/restore policy information from disk |
| 85 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 86 | // TODO: keep whitelist of system-critical services that should never have |
| 87 | // rules enforced, such as system, phone, and radio UIDs. |
| 88 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 89 | // TODO: keep record of billing cycle details, and limit rules |
| 90 | // TODO: keep map of interfaces-to-billing-relationship |
| 91 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 92 | // TODO: dispatch callbacks through handler when locked |
| 93 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 94 | public NetworkPolicyManagerService(Context context, IActivityManager activityManager, |
| 95 | IPowerManager powerManager, INetworkStatsService networkStats) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 96 | mContext = checkNotNull(context, "missing context"); |
| 97 | mActivityManager = checkNotNull(activityManager, "missing activityManager"); |
| 98 | mPowerManager = checkNotNull(powerManager, "missing powerManager"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 99 | mNetworkStats = checkNotNull(networkStats, "missing networkStats"); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 100 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 101 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 102 | public void systemReady() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 103 | // TODO: read current policy from disk |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 104 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 105 | updateScreenOn(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 106 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 107 | try { |
| 108 | mActivityManager.registerProcessObserver(mProcessObserver); |
| 109 | } catch (RemoteException e) { |
| 110 | // ouch, no foregroundActivities updates means some processes may |
| 111 | // never get network access. |
| 112 | Slog.e(TAG, "unable to register IProcessObserver", e); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 113 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 114 | |
| 115 | // TODO: traverse existing processes to know foreground state, or have |
| 116 | // activitymanager dispatch current state when new observer attached. |
| 117 | |
| 118 | final IntentFilter screenFilter = new IntentFilter(); |
| 119 | screenFilter.addAction(Intent.ACTION_SCREEN_ON); |
| 120 | screenFilter.addAction(Intent.ACTION_SCREEN_OFF); |
| 121 | mContext.registerReceiver(mScreenReceiver, screenFilter); |
| 122 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 125 | private IProcessObserver mProcessObserver = new IProcessObserver.Stub() { |
| 126 | @Override |
| 127 | public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 128 | // only someone like AMS should only be calling us |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 129 | mContext.enforceCallingOrSelfPermission(MANAGE_APP_TOKENS, TAG); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 131 | synchronized (mRulesLock) { |
| 132 | // because a uid can have multiple pids running inside, we need to |
| 133 | // remember all pid states and summarize foreground at uid level. |
| 134 | |
| 135 | // record foreground for this specific pid |
| 136 | SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 137 | if (pidForeground == null) { |
| 138 | pidForeground = new SparseBooleanArray(2); |
| 139 | mUidPidForeground.put(uid, pidForeground); |
| 140 | } |
| 141 | pidForeground.put(pid, foregroundActivities); |
| 142 | computeUidForegroundL(uid); |
| 143 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 144 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 145 | |
| 146 | @Override |
| 147 | public void onProcessDied(int pid, int uid) { |
| 148 | // only someone like AMS should only be calling us |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 149 | mContext.enforceCallingOrSelfPermission(MANAGE_APP_TOKENS, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 150 | |
| 151 | synchronized (mRulesLock) { |
| 152 | // clear records and recompute, when they exist |
| 153 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 154 | if (pidForeground != null) { |
| 155 | pidForeground.delete(pid); |
| 156 | computeUidForegroundL(uid); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() { |
| 163 | @Override |
| 164 | public void onReceive(Context context, Intent intent) { |
| 165 | synchronized (mRulesLock) { |
| 166 | // screen-related broadcasts are protected by system, no need |
| 167 | // for permissions check. |
| 168 | updateScreenOn(); |
| 169 | } |
| 170 | } |
| 171 | }; |
| 172 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 173 | @Override |
| 174 | public void setUidPolicy(int uid, int policy) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 175 | // TODO: create permission for modifying data policy |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 176 | mContext.enforceCallingOrSelfPermission(UPDATE_DEVICE_STATS, TAG); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 177 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 178 | final int oldPolicy; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 179 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 180 | oldPolicy = getUidPolicy(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 181 | mUidPolicy.put(uid, policy); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 182 | updateRulesForUidL(uid); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 183 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 184 | |
| 185 | // TODO: consider dispatching BACKGROUND_DATA_SETTING broadcast |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public int getUidPolicy(int uid) { |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 190 | synchronized (mRulesLock) { |
| 191 | return mUidPolicy.get(uid, POLICY_NONE); |
| 192 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public void registerListener(INetworkPolicyListener listener) { |
| 197 | mListeners.register(listener); |
| 198 | |
| 199 | synchronized (mRulesLock) { |
| 200 | // dispatch any existing rules to new listeners |
| 201 | final int size = mUidRules.size(); |
| 202 | for (int i = 0; i < size; i++) { |
| 203 | final int uid = mUidRules.keyAt(i); |
| 204 | final int uidRules = mUidRules.valueAt(i); |
| 205 | if (uidRules != RULE_ALLOW_ALL) { |
| 206 | try { |
| 207 | listener.onRulesChanged(uid, uidRules); |
| 208 | } catch (RemoteException e) { |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void unregisterListener(INetworkPolicyListener listener) { |
| 217 | mListeners.unregister(listener); |
| 218 | } |
| 219 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 220 | @Override |
| 221 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 222 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 223 | |
| 224 | synchronized (mRulesLock) { |
| 225 | fout.println("Policy status for known UIDs:"); |
| 226 | |
| 227 | final SparseBooleanArray knownUids = new SparseBooleanArray(); |
| 228 | collectKeys(mUidPolicy, knownUids); |
| 229 | collectKeys(mUidForeground, knownUids); |
| 230 | collectKeys(mUidRules, knownUids); |
| 231 | |
| 232 | final int size = knownUids.size(); |
| 233 | for (int i = 0; i < size; i++) { |
| 234 | final int uid = knownUids.keyAt(i); |
| 235 | fout.print(" UID="); |
| 236 | fout.print(uid); |
| 237 | |
| 238 | fout.print(" policy="); |
| 239 | final int policyIndex = mUidPolicy.indexOfKey(uid); |
| 240 | if (policyIndex < 0) { |
| 241 | fout.print("UNKNOWN"); |
| 242 | } else { |
| 243 | dumpPolicy(fout, mUidPolicy.valueAt(policyIndex)); |
| 244 | } |
| 245 | |
| 246 | fout.print(" foreground="); |
| 247 | final int foregroundIndex = mUidPidForeground.indexOfKey(uid); |
| 248 | if (foregroundIndex < 0) { |
| 249 | fout.print("UNKNOWN"); |
| 250 | } else { |
| 251 | dumpSparseBooleanArray(fout, mUidPidForeground.valueAt(foregroundIndex)); |
| 252 | } |
| 253 | |
| 254 | fout.print(" rules="); |
| 255 | final int rulesIndex = mUidRules.indexOfKey(uid); |
| 256 | if (rulesIndex < 0) { |
| 257 | fout.print("UNKNOWN"); |
| 258 | } else { |
| 259 | dumpRules(fout, mUidRules.valueAt(rulesIndex)); |
| 260 | } |
| 261 | |
| 262 | fout.println(); |
| 263 | } |
| 264 | } |
| 265 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 266 | |
| 267 | @Override |
| 268 | public boolean isUidForeground(int uid) { |
| 269 | synchronized (mRulesLock) { |
| 270 | // only really in foreground when screen is also on |
| 271 | return mUidForeground.get(uid, false) && mScreenOn; |
| 272 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 275 | /** |
| 276 | * Foreground for PID changed; recompute foreground at UID level. If |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 277 | * changed, will trigger {@link #updateRulesForUidL(int)}. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 278 | */ |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 279 | private void computeUidForegroundL(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 280 | final SparseBooleanArray pidForeground = mUidPidForeground.get(uid); |
| 281 | |
| 282 | // current pid is dropping foreground; examine other pids |
| 283 | boolean uidForeground = false; |
| 284 | final int size = pidForeground.size(); |
| 285 | for (int i = 0; i < size; i++) { |
| 286 | if (pidForeground.valueAt(i)) { |
| 287 | uidForeground = true; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | final boolean oldUidForeground = mUidForeground.get(uid, false); |
| 293 | if (oldUidForeground != uidForeground) { |
| 294 | // foreground changed, push updated rules |
| 295 | mUidForeground.put(uid, uidForeground); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 296 | updateRulesForUidL(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 300 | private void updateScreenOn() { |
| 301 | synchronized (mRulesLock) { |
| 302 | try { |
| 303 | mScreenOn = mPowerManager.isScreenOn(); |
| 304 | } catch (RemoteException e) { |
| 305 | } |
| 306 | updateRulesForScreenL(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Update rules that might be changed by {@link #mScreenOn} value. |
| 312 | */ |
| 313 | private void updateRulesForScreenL() { |
| 314 | // only update rules for anyone with foreground activities |
| 315 | final int size = mUidForeground.size(); |
| 316 | for (int i = 0; i < size; i++) { |
| 317 | if (mUidForeground.valueAt(i)) { |
| 318 | final int uid = mUidForeground.keyAt(i); |
| 319 | updateRulesForUidL(uid); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | private void updateRulesForUidL(int uid) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 325 | final int uidPolicy = getUidPolicy(uid); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 326 | final boolean uidForeground = isUidForeground(uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 327 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 328 | // derive active rules based on policy and active state |
| 329 | int uidRules = RULE_ALLOW_ALL; |
| 330 | if (!uidForeground && (uidPolicy & POLICY_REJECT_PAID_BACKGROUND) != 0) { |
| 331 | // uid in background, and policy says to block paid data |
| 332 | uidRules = RULE_REJECT_PAID; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 335 | // TODO: only dispatch when rules actually change |
| 336 | |
| 337 | // record rule locally to dispatch to new listeners |
| 338 | mUidRules.put(uid, uidRules); |
| 339 | |
| 340 | // dispatch changed rule to existing listeners |
| 341 | final int length = mListeners.beginBroadcast(); |
| 342 | for (int i = 0; i < length; i++) { |
| 343 | final INetworkPolicyListener listener = mListeners.getBroadcastItem(i); |
| 344 | if (listener != null) { |
| 345 | try { |
| 346 | listener.onRulesChanged(uid, uidRules); |
| 347 | } catch (RemoteException e) { |
| 348 | } |
| 349 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 350 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 351 | mListeners.finishBroadcast(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 354 | private static <T> T checkNotNull(T value, String message) { |
| 355 | if (value == null) { |
| 356 | throw new NullPointerException(message); |
| 357 | } |
| 358 | return value; |
| 359 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 360 | |
Jeff Sharkey | 1b86127 | 2011-05-22 00:34:52 -0700 | [diff] [blame] | 361 | private static void collectKeys(SparseIntArray source, SparseBooleanArray target) { |
| 362 | final int size = source.size(); |
| 363 | for (int i = 0; i < size; i++) { |
| 364 | target.put(source.keyAt(i), true); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | private static void collectKeys(SparseBooleanArray source, SparseBooleanArray target) { |
| 369 | final int size = source.size(); |
| 370 | for (int i = 0; i < size; i++) { |
| 371 | target.put(source.keyAt(i), true); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private static void dumpSparseBooleanArray(PrintWriter fout, SparseBooleanArray value) { |
| 376 | fout.print("["); |
| 377 | final int size = value.size(); |
| 378 | for (int i = 0; i < size; i++) { |
| 379 | fout.print(value.keyAt(i) + "=" + value.valueAt(i)); |
| 380 | if (i < size - 1) fout.print(","); |
| 381 | } |
| 382 | fout.print("]"); |
| 383 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 384 | } |