blob: aa3dfa6201c069c065b769e9a4240461bdc7050e [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package com.android.server;
18
Jeff Sharkey4434b0b2011-06-16 13:04:20 -070019import static android.Manifest.permission.MANAGE_NETWORK_POLICY;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070020import static android.net.ConnectivityManager.isNetworkTypeValid;
21import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070022import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070023
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080024import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070029import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080030import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080031import android.net.DummyDataStateTracker;
Benoit Goby211b5692010-12-22 14:29:40 -080032import android.net.EthernetDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080033import android.net.IConnectivityManager;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070034import android.net.INetworkPolicyListener;
35import android.net.INetworkPolicyManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080036import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.net.MobileDataStateTracker;
Robert Greenwalt34848c02011-03-25 13:09:25 -070038import android.net.NetworkConfig;
The Android Open Source Project28527d22009-03-03 19:31:44 -080039import android.net.NetworkInfo;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070040import android.net.NetworkInfo.DetailedState;
Jeff Sharkey21062e72011-05-28 20:56:34 -070041import android.net.NetworkState;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070043import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070044import android.net.Proxy;
45import android.net.ProxyProperties;
Robert Greenwalt5a901292011-04-28 14:28:50 -070046import android.net.RouteInfo;
Hung-ying Tyan4e723422011-01-19 16:48:38 +080047import android.net.vpn.VpnManager;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.net.wifi.WifiStateTracker;
49import android.os.Binder;
Mike Lockwood0d5916c2011-05-28 13:24:04 -040050import android.os.FileUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080051import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070052import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import android.os.IBinder;
Chia-chi Yeh4df51322011-05-11 16:35:13 -070054import android.os.INetworkManagementService;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import android.os.Looper;
56import android.os.Message;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070057import android.os.ParcelFileDescriptor;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070058import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070059import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemProperties;
62import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070063import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080064import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080065import android.util.Slog;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070066import android.util.SparseIntArray;
The Android Open Source Project28527d22009-03-03 19:31:44 -080067
Chia-chi Yeh75cacd52011-06-15 17:07:27 -070068import com.android.internal.net.VpnConfig;
Robert Greenwalt2034b912009-08-12 16:08:25 -070069import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080070import com.android.server.connectivity.Tethering;
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -070071import com.android.server.connectivity.Vpn;
72
Jeff Sharkey21062e72011-05-28 20:56:34 -070073import com.google.android.collect.Lists;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070074import com.google.android.collect.Sets;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080075
The Android Open Source Project28527d22009-03-03 19:31:44 -080076import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070077import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080078import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070079import java.net.InetAddress;
80import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070081import java.util.ArrayList;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070082import java.util.Arrays;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070083import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070084import java.util.GregorianCalendar;
Jeff Sharkeya47d7a12011-06-16 15:07:48 -070085import java.util.HashSet;
Robert Greenwalt2034b912009-08-12 16:08:25 -070086import java.util.List;
Jeff Sharkey921ebf22011-05-19 17:12:49 -070087import java.util.concurrent.atomic.AtomicBoolean;
The Android Open Source Project28527d22009-03-03 19:31:44 -080088
89/**
90 * @hide
91 */
92public class ConnectivityService extends IConnectivityManager.Stub {
93
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070094 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080095 private static final String TAG = "ConnectivityService";
96
Jeff Sharkey921ebf22011-05-19 17:12:49 -070097 private static final boolean LOGD_RULES = false;
98
Robert Greenwalt2034b912009-08-12 16:08:25 -070099 // how long to wait before switching back to a radio's default network
100 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
101 // system property that can override the above value
102 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
103 "android.telephony.apn-restore";
104
Robert Greenwaltbd492212011-05-06 17:10:53 -0700105 // used in recursive route setting to add gateways for the host for which
106 // a host route was requested.
107 private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10;
108
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800109 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800110 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800111
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700112 private Vpn mVpn;
113
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700114 /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
115 private Object mRulesLock = new Object();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700116 /** Currently active network rules by UID. */
117 private SparseIntArray mUidRules = new SparseIntArray();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700118 /** Set of ifaces that are costly. */
119 private HashSet<String> mMeteredIfaces = Sets.newHashSet();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700120
The Android Open Source Project28527d22009-03-03 19:31:44 -0800121 /**
122 * Sometimes we want to refer to the individual network state
123 * trackers separately, and sometimes we just want to treat them
124 * abstractly.
125 */
126 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -0700127
128 /**
129 * A per Net list of the PID's that requested access to the net
130 * used both as a refcount and for per-PID DNS selection
131 */
132 private List mNetRequestersPids[];
133
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700134 private WifiWatchdogService mWifiWatchdogService;
135
Robert Greenwalt2034b912009-08-12 16:08:25 -0700136 // priority order of the nettrackers
137 // (excluding dynamically set mNetworkPreference)
138 // TODO - move mNetworkTypePreference into this
139 private int[] mPriorityList;
140
The Android Open Source Project28527d22009-03-03 19:31:44 -0800141 private Context mContext;
142 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700143 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700144 // 0 is full bad, 100 is full good
145 private int mDefaultInetCondition = 0;
146 private int mDefaultInetConditionPublished = 0;
147 private boolean mInetConditionChangeInFlight = false;
148 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800149
150 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800151
152 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700153 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800154
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800155 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
156
Robert Greenwalt355205c2011-05-10 15:05:02 -0700157 private INetworkManagementService mNetd;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700158 private INetworkPolicyManager mPolicyManager;
Robert Greenwalt355205c2011-05-10 15:05:02 -0700159
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700160 private static final int ENABLED = 1;
161 private static final int DISABLED = 0;
162
163 // Share the event space with NetworkStateTracker (which can't see this
164 // internal class but sends us events). If you change these, change
165 // NetworkStateTracker.java too.
166 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
167 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
168
169 /**
170 * used internally as a delayed event to make us switch back to the
171 * default network
172 */
173 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
175
176 /**
177 * used internally to change our mobile data enabled flag
178 */
179 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
180 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
181
182 /**
183 * used internally to change our network preference setting
184 * arg1 = networkType to prefer
185 */
186 private static final int EVENT_SET_NETWORK_PREFERENCE =
187 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
188
189 /**
190 * used internally to synchronize inet condition reports
191 * arg1 = networkType
192 * arg2 = condition (0 bad, 100 good)
193 */
194 private static final int EVENT_INET_CONDITION_CHANGE =
195 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
196
197 /**
198 * used internally to mark the end of inet condition hold periods
199 * arg1 = networkType
200 */
201 private static final int EVENT_INET_CONDITION_HOLD_END =
202 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
203
204 /**
205 * used internally to set the background data preference
206 * arg1 = TRUE for enabled, FALSE for disabled
207 */
208 private static final int EVENT_SET_BACKGROUND_DATA =
209 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
210
211 /**
212 * used internally to set enable/disable cellular data
213 * arg1 = ENBALED or DISABLED
214 */
215 private static final int EVENT_SET_MOBILE_DATA =
216 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
217
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700218 /**
219 * used internally to clear a wakelock when transitioning
220 * from one net to another
221 */
222 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
223 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
224
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700225 /**
226 * used internally to reload global proxy settings
227 */
228 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
229 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
230
Robert Greenwalt34848c02011-03-25 13:09:25 -0700231 /**
232 * used internally to set external dependency met/unmet
233 * arg1 = ENABLED (met) or DISABLED (unmet)
234 * arg2 = NetworkType
235 */
236 private static final int EVENT_SET_DEPENDENCY_MET =
237 MAX_NETWORK_STATE_TRACKER_EVENT + 10;
238
Robert Greenwalt2034b912009-08-12 16:08:25 -0700239 private Handler mHandler;
240
241 // list of DeathRecipients used to make sure features are turned off when
242 // a process dies
243 private List mFeatureUsers;
244
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400245 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800246 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400247
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700248 private PowerManager.WakeLock mNetTransitionWakeLock;
249 private String mNetTransitionWakeLockCausedBy = "";
250 private int mNetTransitionWakeLockSerialNumber;
251 private int mNetTransitionWakeLockTimeout;
252
Robert Greenwalt94daa182010-09-01 11:34:05 -0700253 private InetAddress mDefaultDns;
254
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700255 // used in DBG mode to track inet condition reports
256 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
257 private ArrayList mInetLog;
258
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700259 // track the current default http proxy - tell the world if we get a new one (real change)
260 private ProxyProperties mDefaultProxy = null;
261 // track the global proxy.
262 private ProxyProperties mGlobalProxy = null;
263 private final Object mGlobalProxyLock = new Object();
264
265 private SettingsObserver mSettingsObserver;
266
Robert Greenwalt34848c02011-03-25 13:09:25 -0700267 NetworkConfig[] mNetConfigs;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700268 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700269
Robert Greenwalt12c44552009-12-07 11:33:18 -0800270 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700271 public int mSimultaneity;
272 public int mType;
273 public RadioAttributes(String init) {
274 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700275 mType = Integer.parseInt(fragments[0]);
276 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700277 }
278 }
279 RadioAttributes[] mRadioAttributes;
280
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700281 public ConnectivityService(
282 Context context, INetworkManagementService netd, INetworkPolicyManager policyManager) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800283 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800284
Wink Saville775aad62010-09-02 19:23:52 -0700285 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
286 handlerThread.start();
287 mHandler = new MyHandler(handlerThread.getLooper());
288
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800289 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
290 Settings.Secure.BACKGROUND_DATA, 1) == 1);
291
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800292 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800293 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
294 String id = Settings.Secure.getString(context.getContentResolver(),
295 Settings.Secure.ANDROID_ID);
296 if (id != null && id.length() > 0) {
297 String name = new String("android_").concat(id);
298 SystemProperties.set("net.hostname", name);
299 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800300 }
301
Robert Greenwalt94daa182010-09-01 11:34:05 -0700302 // read our default dns server ip
303 String dns = Settings.Secure.getString(context.getContentResolver(),
304 Settings.Secure.DEFAULT_DNS_SERVER);
305 if (dns == null || dns.length() == 0) {
306 dns = context.getResources().getString(
307 com.android.internal.R.string.config_default_dns_server);
308 }
309 try {
Robert Greenwalt35e34d12011-02-22 16:00:42 -0800310 mDefaultDns = NetworkUtils.numericToInetAddress(dns);
311 } catch (IllegalArgumentException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800312 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700313 }
314
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700315 mContext = checkNotNull(context, "missing Context");
316 mNetd = checkNotNull(netd, "missing INetworkManagementService");
317 mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager");
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700318
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700319 try {
320 mPolicyManager.registerListener(mPolicyListener);
321 } catch (RemoteException e) {
322 // ouch, no rules updates means some processes may never get network
323 Slog.e(TAG, "unable to register INetworkPolicyListener", e);
324 }
325
326 final PowerManager powerManager = (PowerManager) context.getSystemService(
327 Context.POWER_SERVICE);
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700328 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
329 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
330 com.android.internal.R.integer.config_networkTransitionTimeout);
331
Robert Greenwalt2034b912009-08-12 16:08:25 -0700332 mNetTrackers = new NetworkStateTracker[
333 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700334
The Android Open Source Project28527d22009-03-03 19:31:44 -0800335 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700336
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
Robert Greenwalt34848c02011-03-25 13:09:25 -0700338 mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700339
Robert Greenwalt2034b912009-08-12 16:08:25 -0700340 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700341 String[] raStrings = context.getResources().getStringArray(
342 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700343 for (String raString : raStrings) {
344 RadioAttributes r = new RadioAttributes(raString);
345 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800346 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700347 continue;
348 }
349 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800350 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700351 r.mType);
352 continue;
353 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700354 mRadioAttributes[r.mType] = r;
355 }
356
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700357 String[] naStrings = context.getResources().getStringArray(
358 com.android.internal.R.array.networkAttributes);
359 for (String naString : naStrings) {
360 try {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700361 NetworkConfig n = new NetworkConfig(naString);
Wink Savillef2a62832011-04-07 14:23:45 -0700362 if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800363 loge("Error in networkAttributes - ignoring attempt to define type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700364 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700365 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700366 }
Wink Savillef2a62832011-04-07 14:23:45 -0700367 if (mNetConfigs[n.type] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800368 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700369 n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700370 continue;
371 }
Wink Savillef2a62832011-04-07 14:23:45 -0700372 if (mRadioAttributes[n.radio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800373 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Wink Savillef2a62832011-04-07 14:23:45 -0700374 "radio " + n.radio + " in network type " + n.type);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700375 continue;
376 }
Wink Savillef2a62832011-04-07 14:23:45 -0700377 mNetConfigs[n.type] = n;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700378 mNetworksDefined++;
379 } catch(Exception e) {
380 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700381 }
382 }
383
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700384 // high priority first
385 mPriorityList = new int[mNetworksDefined];
386 {
387 int insertionPoint = mNetworksDefined-1;
388 int currentLowest = 0;
389 int nextLowest = 0;
390 while (insertionPoint > -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -0700391 for (NetworkConfig na : mNetConfigs) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700392 if (na == null) continue;
Wink Savillef2a62832011-04-07 14:23:45 -0700393 if (na.priority < currentLowest) continue;
394 if (na.priority > currentLowest) {
395 if (na.priority < nextLowest || nextLowest == 0) {
396 nextLowest = na.priority;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700397 }
398 continue;
399 }
Wink Savillef2a62832011-04-07 14:23:45 -0700400 mPriorityList[insertionPoint--] = na.type;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700401 }
402 currentLowest = nextLowest;
403 nextLowest = 0;
404 }
405 }
406
407 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
408 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700409 mNetRequestersPids[i] = new ArrayList();
410 }
411
412 mFeatureUsers = new ArrayList();
413
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700414 mNumDnsEntries = 0;
415
416 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
417 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800418 /*
419 * Create the network state trackers for Wi-Fi and mobile
420 * data. Maybe this could be done with a factory class,
421 * but it's not clear that it's worth it, given that
422 * the number of different network types is not going
423 * to change very often.
424 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700425 for (int netType : mPriorityList) {
Wink Savillef2a62832011-04-07 14:23:45 -0700426 switch (mNetConfigs[netType].radio) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700427 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800428 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700429 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700430 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700431 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700432 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700433 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700434 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700436 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700437 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700438
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700439 break;
440 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700441 mNetTrackers[netType] = new MobileDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700442 mNetConfigs[netType].name);
Wink Saville7fabfa22010-08-13 16:11:42 -0700443 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700444 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800445 case ConnectivityManager.TYPE_DUMMY:
446 mNetTrackers[netType] = new DummyDataStateTracker(netType,
Wink Savillef2a62832011-04-07 14:23:45 -0700447 mNetConfigs[netType].name);
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800448 mNetTrackers[netType].startMonitoring(context, mHandler);
449 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800450 case ConnectivityManager.TYPE_BLUETOOTH:
451 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
452 mNetTrackers[netType].startMonitoring(context, mHandler);
453 break;
Benoit Goby211b5692010-12-22 14:29:40 -0800454 case ConnectivityManager.TYPE_ETHERNET:
455 mNetTrackers[netType] = EthernetDataTracker.getInstance();
456 mNetTrackers[netType].startMonitoring(context, mHandler);
457 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700458 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800459 loge("Trying to create a DataStateTracker for an unknown radio type " +
Wink Savillef2a62832011-04-07 14:23:45 -0700460 mNetConfigs[netType].radio);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700461 continue;
462 }
463 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800464
Chia-chi Yeh4df51322011-05-11 16:35:13 -0700465 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
466 INetworkManagementService nmService = INetworkManagementService.Stub.asInterface(b);
467
468 mTethering = new Tethering(mContext, nmService, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800469 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
470 !mTethering.isDunRequired()) &&
471 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700472 mTethering.getTetherableWifiRegexs().length != 0 ||
473 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800474 mTethering.getUpstreamIfaceRegexs().length != 0);
475
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700476 mVpn = new Vpn(mContext, new VpnCallback());
477
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700478 try {
479 nmService.registerObserver(mTethering);
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -0700480 nmService.registerObserver(mVpn);
Chia-chi Yehf3204aa2011-05-23 15:08:29 -0700481 } catch (RemoteException e) {
482 loge("Error registering observer :" + e);
483 }
484
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700485 if (DBG) {
486 mInetLog = new ArrayList();
487 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700488
489 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
490 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800491
492 loadGlobalProxy();
Hung-ying Tyan4e723422011-01-19 16:48:38 +0800493
494 VpnManager.startVpnService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800495 }
496
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700497
The Android Open Source Project28527d22009-03-03 19:31:44 -0800498 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700499 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 * @param preference the new preference
501 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700502 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800503 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700504
505 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800506 }
507
508 public int getNetworkPreference() {
509 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700510 int preference;
511 synchronized(this) {
512 preference = mNetworkPreference;
513 }
514 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800515 }
516
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700517 private void handleSetNetworkPreference(int preference) {
518 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt34848c02011-03-25 13:09:25 -0700519 mNetConfigs[preference] != null &&
520 mNetConfigs[preference].isDefault()) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700521 if (mNetworkPreference != preference) {
522 final ContentResolver cr = mContext.getContentResolver();
523 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
524 synchronized(this) {
525 mNetworkPreference = preference;
526 }
527 enforcePreference();
528 }
529 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800530 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700531
The Android Open Source Project28527d22009-03-03 19:31:44 -0800532 private int getPersistedNetworkPreference() {
533 final ContentResolver cr = mContext.getContentResolver();
534
535 final int networkPrefSetting = Settings.Secure
536 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
537 if (networkPrefSetting != -1) {
538 return networkPrefSetting;
539 }
540
541 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
542 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700543
The Android Open Source Project28527d22009-03-03 19:31:44 -0800544 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700545 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800546 * In this method, we only tear down a non-preferred network. Establishing
547 * a connection to the preferred network is taken care of when we handle
548 * the disconnect event from the non-preferred network
549 * (see {@link #handleDisconnect(NetworkInfo)}).
550 */
551 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700552 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800553 return;
554
Robert Greenwalt2034b912009-08-12 16:08:25 -0700555 if (!mNetTrackers[mNetworkPreference].isAvailable())
556 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800557
Robert Greenwalt2034b912009-08-12 16:08:25 -0700558 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700559 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700560 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700561 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800562 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700563 " in enforcePreference");
564 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700565 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800566 }
567 }
568 }
569
570 private boolean teardown(NetworkStateTracker netTracker) {
571 if (netTracker.teardown()) {
572 netTracker.setTeardownRequested(true);
573 return true;
574 } else {
575 return false;
576 }
577 }
578
579 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700580 * Check if UID should be blocked from using the network represented by the
581 * given {@link NetworkStateTracker}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700582 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700583 private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) {
584 final String iface = tracker.getLinkProperties().getInterfaceName();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700585
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700586 final boolean networkCostly;
587 final int uidRules;
588 synchronized (mRulesLock) {
589 networkCostly = mMeteredIfaces.contains(iface);
590 uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700591 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700592
593 if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) {
594 return true;
595 }
596
597 // no restrictive rules; network is visible
598 return false;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700599 }
600
601 /**
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700602 * Return a filtered {@link NetworkInfo}, potentially marked
603 * {@link DetailedState#BLOCKED} based on
604 * {@link #isNetworkBlocked(NetworkStateTracker, int)}.
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700605 */
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700606 private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) {
607 NetworkInfo info = tracker.getNetworkInfo();
608 if (isNetworkBlocked(tracker, uid)) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700609 // network is blocked; clone and override state
610 info = new NetworkInfo(info);
611 info.setDetailedState(DetailedState.BLOCKED, null, null);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700612 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700613 return info;
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700614 }
615
616 /**
The Android Open Source Project28527d22009-03-03 19:31:44 -0800617 * Return NetworkInfo for the active (i.e., connected) network interface.
618 * It is assumed that at most one network is active at a time. If more
619 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700620 * @return the info for the active network, or {@code null} if none is
621 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800622 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700623 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800624 public NetworkInfo getActiveNetworkInfo() {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700625 enforceAccessPermission();
626 final int uid = Binder.getCallingUid();
627 return getNetworkInfo(mActiveDefaultNetwork, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800628 }
629
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700630 @Override
631 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
632 enforceConnectivityInternalPermission();
633 return getNetworkInfo(mActiveDefaultNetwork, uid);
634 }
635
636 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800637 public NetworkInfo getNetworkInfo(int networkType) {
638 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700639 final int uid = Binder.getCallingUid();
640 return getNetworkInfo(networkType, uid);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800641 }
642
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700643 private NetworkInfo getNetworkInfo(int networkType, int uid) {
644 NetworkInfo info = null;
645 if (isNetworkTypeValid(networkType)) {
646 final NetworkStateTracker tracker = mNetTrackers[networkType];
647 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700648 info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700649 }
650 }
651 return info;
652 }
653
654 @Override
The Android Open Source Project28527d22009-03-03 19:31:44 -0800655 public NetworkInfo[] getAllNetworkInfo() {
656 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700657 final int uid = Binder.getCallingUid();
Jeff Sharkey21062e72011-05-28 20:56:34 -0700658 final ArrayList<NetworkInfo> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700659 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700660 for (NetworkStateTracker tracker : mNetTrackers) {
661 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700662 result.add(getFilteredNetworkInfo(tracker, uid));
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700663 }
664 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800665 }
Jeff Sharkey21062e72011-05-28 20:56:34 -0700666 return result.toArray(new NetworkInfo[result.size()]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800667 }
668
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700669 /**
670 * Return LinkProperties for the active (i.e., connected) default
671 * network interface. It is assumed that at most one default network
672 * is active at a time. If more than one is active, it is indeterminate
673 * which will be returned.
674 * @return the ip properties for the active network, or {@code null} if
675 * none is active
676 */
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700677 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700678 public LinkProperties getActiveLinkProperties() {
Robert Greenwalte1544bb2011-05-20 12:23:41 -0700679 return getLinkProperties(mActiveDefaultNetwork);
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700680 }
681
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700682 @Override
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700683 public LinkProperties getLinkProperties(int networkType) {
684 enforceAccessPermission();
Jeff Sharkey921ebf22011-05-19 17:12:49 -0700685 if (isNetworkTypeValid(networkType)) {
686 final NetworkStateTracker tracker = mNetTrackers[networkType];
687 if (tracker != null) {
688 return tracker.getLinkProperties();
689 }
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700690 }
691 return null;
692 }
693
Jeff Sharkey21062e72011-05-28 20:56:34 -0700694 @Override
695 public NetworkState[] getAllNetworkState() {
696 enforceAccessPermission();
697 final int uid = Binder.getCallingUid();
698 final ArrayList<NetworkState> result = Lists.newArrayList();
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700699 synchronized (mRulesLock) {
Jeff Sharkey21062e72011-05-28 20:56:34 -0700700 for (NetworkStateTracker tracker : mNetTrackers) {
701 if (tracker != null) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -0700702 final NetworkInfo info = getFilteredNetworkInfo(tracker, uid);
Jeff Sharkey21062e72011-05-28 20:56:34 -0700703 result.add(new NetworkState(
704 info, tracker.getLinkProperties(), tracker.getLinkCapabilities()));
705 }
706 }
707 }
708 return result.toArray(new NetworkState[result.size()]);
709 }
710
The Android Open Source Project28527d22009-03-03 19:31:44 -0800711 public boolean setRadios(boolean turnOn) {
712 boolean result = true;
713 enforceChangePermission();
714 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700715 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800716 }
717 return result;
718 }
719
720 public boolean setRadio(int netType, boolean turnOn) {
721 enforceChangePermission();
722 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
723 return false;
724 }
725 NetworkStateTracker tracker = mNetTrackers[netType];
726 return tracker != null && tracker.setRadio(turnOn);
727 }
728
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700729 /**
730 * Used to notice when the calling process dies so we can self-expire
731 *
732 * Also used to know if the process has cleaned up after itself when
733 * our auto-expire timer goes off. The timer has a link to an object.
734 *
735 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700736 private class FeatureUser implements IBinder.DeathRecipient {
737 int mNetworkType;
738 String mFeature;
739 IBinder mBinder;
740 int mPid;
741 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800742 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700743
744 FeatureUser(int type, String feature, IBinder binder) {
745 super();
746 mNetworkType = type;
747 mFeature = feature;
748 mBinder = binder;
749 mPid = getCallingPid();
750 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800751 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700752
Robert Greenwalt2034b912009-08-12 16:08:25 -0700753 try {
754 mBinder.linkToDeath(this, 0);
755 } catch (RemoteException e) {
756 binderDied();
757 }
758 }
759
760 void unlinkDeathRecipient() {
761 mBinder.unlinkToDeath(this, 0);
762 }
763
764 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800765 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800766 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
767 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700768 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700769 }
770
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700771 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800772 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800773 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
774 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700775 stopUsingNetworkFeature(this, false);
776 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800777
778 public String toString() {
779 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
780 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
781 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700782 }
783
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700784 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700785 public int startUsingNetworkFeature(int networkType, String feature,
786 IBinder binder) {
787 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800788 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700789 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800790 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700791 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
Robert Greenwalt34848c02011-03-25 13:09:25 -0700792 mNetConfigs[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700793 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700795
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700796 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700797
798 // TODO - move this into the MobileDataStateTracker
799 int usedNetworkType = networkType;
800 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800801 usedNetworkType = convertFeatureToNetworkType(feature);
802 if (usedNetworkType < 0) {
803 Slog.e(TAG, "Can't match any netTracker!");
804 usedNetworkType = networkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700805 }
806 }
807 NetworkStateTracker network = mNetTrackers[usedNetworkType];
808 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800809 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700811 NetworkStateTracker radio = mNetTrackers[networkType];
812 NetworkInfo ni = network.getNetworkInfo();
813
814 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800815 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800816 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
817 return Phone.APN_TYPE_NOT_AVAILABLE;
818 } else {
819 // else make the attempt anyway - probably giving REQUEST_STARTED below
820 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700821 }
822
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700823 synchronized(this) {
824 mFeatureUsers.add(f);
825 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
826 // this gets used for per-pid dns when connected
827 mNetRequestersPids[usedNetworkType].add(currentPid);
828 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700829 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700830
Robert Greenwalt20f819c2011-05-03 19:02:44 -0700831 int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType);
832
833 if (restoreTimer >= 0) {
834 mHandler.sendMessageDelayed(
835 mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer);
836 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700837
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700838 if ((ni.isConnectedOrConnecting() == true) &&
839 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700840 if (ni.isConnected() == true) {
841 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700842 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800843 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700844 return Phone.APN_ALREADY_ACTIVE;
845 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800846 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700847 return Phone.APN_REQUEST_STARTED;
848 }
849
850 // check if the radio in play can make another contact
851 // assume if cannot for now
852
Wink Savillee70c6f52010-12-03 12:01:38 -0800853 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700854 network.reconnect();
855 return Phone.APN_REQUEST_STARTED;
856 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800857 // need to remember this unsupported request so we respond appropriately on stop
858 synchronized(this) {
859 mFeatureUsers.add(f);
860 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
861 // this gets used for per-pid dns when connected
862 mNetRequestersPids[usedNetworkType].add(currentPid);
863 }
864 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700865 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700866 }
867 }
868 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800869 }
870
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700871 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700873 enforceChangePermission();
874
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700875 int pid = getCallingPid();
876 int uid = getCallingUid();
877
878 FeatureUser u = null;
879 boolean found = false;
880
881 synchronized(this) {
882 for (int i = 0; i < mFeatureUsers.size() ; i++) {
883 u = (FeatureUser)mFeatureUsers.get(i);
884 if (uid == u.mUid && pid == u.mPid &&
885 networkType == u.mNetworkType &&
886 TextUtils.equals(feature, u.mFeature)) {
887 found = true;
888 break;
889 }
890 }
891 }
892 if (found && u != null) {
893 // stop regardless of how many other time this proc had called start
894 return stopUsingNetworkFeature(u, true);
895 } else {
896 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800897 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700898 return 1;
899 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700900 }
901
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700902 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
903 int networkType = u.mNetworkType;
904 String feature = u.mFeature;
905 int pid = u.mPid;
906 int uid = u.mUid;
907
908 NetworkStateTracker tracker = null;
909 boolean callTeardown = false; // used to carry our decision outside of sync block
910
Robert Greenwalt2034b912009-08-12 16:08:25 -0700911 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800912 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700913 ": " + feature);
914 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700915
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
917 return -1;
918 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700919
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700920 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
921 // sync block
922 synchronized(this) {
923 // check if this process still has an outstanding start request
924 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800925 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700926 return 1;
927 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700928 u.unlinkDeathRecipient();
929 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
930 // If we care about duplicate requests, check for that here.
931 //
932 // This is done to support the extension of a request - the app
933 // can request we start the network feature again and renew the
934 // auto-shutoff delay. Normal "stop" calls from the app though
935 // do not pay attention to duplicate requests - in effect the
936 // API does not refcount and a single stop will counter multiple starts.
937 if (ignoreDups == false) {
938 for (int i = 0; i < mFeatureUsers.size() ; i++) {
939 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
940 if (x.mUid == u.mUid && x.mPid == u.mPid &&
941 x.mNetworkType == u.mNetworkType &&
942 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800943 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700944 return 1;
945 }
946 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700947 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700948
949 // TODO - move to MobileDataStateTracker
950 int usedNetworkType = networkType;
951 if (networkType == ConnectivityManager.TYPE_MOBILE) {
Wink Savillef6b76692011-02-24 17:58:51 -0800952 usedNetworkType = convertFeatureToNetworkType(feature);
953 if (usedNetworkType < 0) {
954 usedNetworkType = networkType;
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700955 }
956 }
957 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700958 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800959 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700960 return -1;
961 }
962 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700963 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700964 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800965 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700966 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800967 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700968 "others still using it");
969 return 1;
970 }
971 callTeardown = true;
Robert Greenwalt9f3be4c2011-01-10 11:58:31 -0800972 } else {
973 if (DBG) log("not a known feature - dropping");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700974 }
975 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800976 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700977 if (callTeardown) {
978 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700979 return 1;
980 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700981 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700982 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800983 }
984
985 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700986 * @deprecated use requestRouteToHostAddress instead
987 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800988 * Ensure that a network route exists to deliver traffic to the specified
989 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700990 * @param networkType the type of the network over which traffic to the
991 * specified host is to be routed
992 * @param hostAddress the IP address of the host to which the route is
993 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800994 * @return {@code true} on success, {@code false} on failure
995 */
996 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700997 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
998
999 if (inetAddress == null) {
1000 return false;
1001 }
1002
1003 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
1004 }
1005
1006 /**
1007 * Ensure that a network route exists to deliver traffic to the specified
1008 * host via the specified network interface.
1009 * @param networkType the type of the network over which traffic to the
1010 * specified host is to be routed
1011 * @param hostAddress the IP address of the host to which the route is
1012 * desired
1013 * @return {@code true} on success, {@code false} on failure
1014 */
1015 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001016 enforceChangePermission();
1017 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
1018 return false;
1019 }
1020 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001021
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001022 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
1023 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001024 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001025 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001026 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -07001027 }
1028 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001029 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001030 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001031 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwaltbd492212011-05-06 17:10:53 -07001032 return addHostRoute(tracker, addr, 0);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001033 } catch (UnknownHostException e) {}
1034 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001035 }
1036
1037 /**
1038 * Ensure that a network route exists to deliver traffic to the specified
1039 * host via the mobile data network.
1040 * @param hostAddress the IP address of the host to which the route is desired,
1041 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001042 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001043 * @return {@code true} on success, {@code false} on failure
1044 */
Robert Greenwaltbd492212011-05-06 17:10:53 -07001045 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress, int cycleCount) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001046 LinkProperties lp = nt.getLinkProperties();
1047 if ((lp == null) || (hostAddress == null)) return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001048
Robert Greenwaltbd492212011-05-06 17:10:53 -07001049 String interfaceName = lp.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001050 if (DBG) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001051 log("Requested host route to " + hostAddress + "(" + interfaceName + "), cycleCount=" +
1052 cycleCount);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001053 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001054 if (interfaceName == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001055 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001056 return false;
1057 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001058
1059 RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getRoutes(), hostAddress);
Robert Greenwalt355205c2011-05-10 15:05:02 -07001060 InetAddress gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001061 if (bestRoute != null) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001062 gatewayAddress = bestRoute.getGateway();
Robert Greenwaltbd492212011-05-06 17:10:53 -07001063 // if the best route is ourself, don't relf-reference, just add the host route
Robert Greenwalt355205c2011-05-10 15:05:02 -07001064 if (hostAddress.equals(gatewayAddress)) gatewayAddress = null;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001065 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001066 if (gatewayAddress != null) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001067 if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) {
1068 loge("Error adding hostroute - too much recursion");
1069 return false;
1070 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001071 if (!addHostRoute(nt, gatewayAddress, cycleCount+1)) return false;
Robert Greenwaltbd492212011-05-06 17:10:53 -07001072 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001073
1074 RouteInfo route = RouteInfo.makeHostRoute(hostAddress, gatewayAddress);
1075
1076 try {
1077 mNetd.addRoute(interfaceName, route);
1078 return true;
1079 } catch (Exception ex) {
1080 return false;
1081 }
Robert Greenwaltbd492212011-05-06 17:10:53 -07001082 }
1083
1084 // TODO support the removal of single host routes. Keep a ref count of them so we
1085 // aren't over-zealous
1086 private boolean removeHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
1087 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001088 }
1089
1090 /**
1091 * @see ConnectivityManager#getBackgroundDataSetting()
1092 */
1093 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001094 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001096
The Android Open Source Project28527d22009-03-03 19:31:44 -08001097 /**
1098 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
1099 */
1100 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
1101 mContext.enforceCallingOrSelfPermission(
1102 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
1103 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -07001104
Robert Greenwaltd62c7002010-12-29 16:15:02 -08001105 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
1106
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001107 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
1108 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
1109 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001110
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001111 private void handleSetBackgroundData(boolean enabled) {
Robert Greenwalt0ffdef12011-02-25 13:44:09 -08001112 Settings.Secure.putInt(mContext.getContentResolver(),
1113 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
1114 Intent broadcast = new Intent(
1115 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
1116 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001117 }
1118
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001119 /**
1120 * @see ConnectivityManager#getMobileDataEnabled()
1121 */
1122 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -08001123 // TODO: This detail should probably be in DataConnectionTracker's
1124 // which is where we store the value and maybe make this
1125 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001126 enforceAccessPermission();
1127 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1128 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001129 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001130 return retVal;
1131 }
1132
Robert Greenwalt34848c02011-03-25 13:09:25 -07001133 public void setDataDependency(int networkType, boolean met) {
1134 enforceChangePermission();
1135 if (DBG) {
1136 log("setDataDependency(" + networkType + ", " + met + ")");
1137 }
1138 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET,
1139 (met ? ENABLED : DISABLED), networkType));
1140 }
1141
1142 private void handleSetDependencyMet(int networkType, boolean met) {
1143 if (mNetTrackers[networkType] != null) {
1144 if (DBG) {
1145 log("handleSetDependencyMet(" + networkType + ", " + met + ")");
1146 }
1147 mNetTrackers[networkType].setDependencyMet(met);
1148 }
1149 }
1150
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001151 private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
1152 @Override
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001153 public void onUidRulesChanged(int uid, int uidRules) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001154 // only someone like NPMS should only be calling us
Jeff Sharkey4434b0b2011-06-16 13:04:20 -07001155 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001156
1157 if (LOGD_RULES) {
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001158 Slog.d(TAG, "onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")");
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001159 }
1160
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001161 synchronized (mRulesLock) {
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001162 // skip update when we've already applied rules
1163 final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
1164 if (oldRules == uidRules) return;
1165
1166 mUidRules.put(uid, uidRules);
1167 }
1168
1169 // TODO: dispatch into NMS to push rules towards kernel module
1170 // TODO: notify UID when it has requested targeted updates
1171 }
Jeff Sharkeya47d7a12011-06-16 15:07:48 -07001172
1173 @Override
1174 public void onMeteredIfacesChanged(String[] meteredIfaces) {
1175 // only someone like NPMS should only be calling us
1176 mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
1177
1178 if (LOGD_RULES) {
1179 Slog.d(TAG,
1180 "onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")");
1181 }
1182
1183 synchronized (mRulesLock) {
1184 mMeteredIfaces.clear();
1185 for (String iface : meteredIfaces) {
1186 mMeteredIfaces.add(iface);
1187 }
1188 }
1189 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07001190 };
1191
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001192 /**
1193 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1194 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001195 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001196 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001197 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001198
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001199 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
Robert Greenwalt34848c02011-03-25 13:09:25 -07001200 (enabled ? ENABLED : DISABLED), 0));
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001201 }
1202
1203 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001204 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1205 if (DBG) {
1206 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001207 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001208 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001209 }
1210 }
1211
The Android Open Source Project28527d22009-03-03 19:31:44 -08001212 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001213 mContext.enforceCallingOrSelfPermission(
1214 android.Manifest.permission.ACCESS_NETWORK_STATE,
1215 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 }
1217
1218 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001219 mContext.enforceCallingOrSelfPermission(
1220 android.Manifest.permission.CHANGE_NETWORK_STATE,
1221 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222 }
1223
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001224 // TODO Make this a special check when it goes public
1225 private void enforceTetherChangePermission() {
1226 mContext.enforceCallingOrSelfPermission(
1227 android.Manifest.permission.CHANGE_NETWORK_STATE,
1228 "ConnectivityService");
1229 }
1230
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001231 private void enforceTetherAccessPermission() {
1232 mContext.enforceCallingOrSelfPermission(
1233 android.Manifest.permission.ACCESS_NETWORK_STATE,
1234 "ConnectivityService");
1235 }
1236
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001237 private void enforceConnectivityInternalPermission() {
1238 mContext.enforceCallingOrSelfPermission(
1239 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1240 "ConnectivityService");
1241 }
1242
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001244 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1245 * network, we ignore it. If it is for the active network, we send out a
1246 * broadcast. But first, we check whether it might be possible to connect
1247 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001248 * @param info the {@code NetworkInfo} for the network
1249 */
1250 private void handleDisconnect(NetworkInfo info) {
1251
Robert Greenwalt2034b912009-08-12 16:08:25 -07001252 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001253
Robert Greenwalt2034b912009-08-12 16:08:25 -07001254 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001255 /*
1256 * If the disconnected network is not the active one, then don't report
1257 * this as a loss of connectivity. What probably happened is that we're
1258 * getting the disconnect for a network that we explicitly disabled
1259 * in accordance with network preference policies.
1260 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001261 if (!mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001262 List pids = mNetRequestersPids[prevNetType];
1263 for (int i = 0; i<pids.size(); i++) {
1264 Integer pid = (Integer)pids.get(i);
1265 // will remove them because the net's no longer connected
1266 // need to do this now as only now do we know the pids and
1267 // can properly null things that are no longer referenced.
1268 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001269 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001270 }
1271
The Android Open Source Project28527d22009-03-03 19:31:44 -08001272 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1273 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1274 if (info.isFailover()) {
1275 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1276 info.setFailover(false);
1277 }
1278 if (info.getReason() != null) {
1279 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1280 }
1281 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001282 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1283 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001284 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001285
Robert Greenwalt34848c02011-03-25 13:09:25 -07001286 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001287 tryFailover(prevNetType);
1288 if (mActiveDefaultNetwork != -1) {
1289 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001290 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1291 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001292 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001293 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1294 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001295 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001296 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001297
1298 // Reset interface if no other connections are using the same interface
1299 boolean doReset = true;
1300 LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties();
1301 if (linkProperties != null) {
1302 String oldIface = linkProperties.getInterfaceName();
1303 if (TextUtils.isEmpty(oldIface) == false) {
1304 for (NetworkStateTracker networkStateTracker : mNetTrackers) {
1305 if (networkStateTracker == null) continue;
1306 NetworkInfo networkInfo = networkStateTracker.getNetworkInfo();
1307 if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) {
1308 LinkProperties l = networkStateTracker.getLinkProperties();
1309 if (l == null) continue;
1310 if (oldIface.equals(l.getInterfaceName())) {
1311 doReset = false;
1312 break;
1313 }
1314 }
1315 }
1316 }
1317 }
1318
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001319 // do this before we broadcast the change
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001320 handleConnectivityChange(prevNetType, doReset);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001321
1322 sendStickyBroadcast(intent);
1323 /*
1324 * If the failover network is already connected, then immediately send
1325 * out a followup broadcast indicating successful failover
1326 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001327 if (mActiveDefaultNetwork != -1) {
1328 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001329 }
1330 }
1331
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001332 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001334 * If this is a default network, check if other defaults are available.
1335 * Try to reconnect on all available and let them hash it out when
1336 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001337 */
Robert Greenwalt34848c02011-03-25 13:09:25 -07001338 if (mNetConfigs[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001339 if (mActiveDefaultNetwork == prevNetType) {
1340 mActiveDefaultNetwork = -1;
1341 }
1342
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001343 // don't signal a reconnect for anything lower or equal priority than our
1344 // current connected default
1345 // TODO - don't filter by priority now - nice optimization but risky
1346// int currentPriority = -1;
1347// if (mActiveDefaultNetwork != -1) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001348// currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001349// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001350 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001351 if (checkType == prevNetType) continue;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001352 if (mNetConfigs[checkType] == null) continue;
1353 if (!mNetConfigs[checkType].isDefault()) continue;
Wink Saville72a95b92011-01-26 15:43:49 -08001354
1355// Enabling the isAvailable() optimization caused mobile to not get
1356// selected if it was in the middle of error handling. Specifically
1357// a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL
1358// would not be available and we wouldn't get connected to anything.
1359// So removing the isAvailable() optimization below for now. TODO: This
1360// optimization should work and we need to investigate why it doesn't work.
1361// This could be related to how DEACTIVATE_DATA_CALL is reporting its
1362// complete before it is really complete.
1363// if (!mNetTrackers[checkType].isAvailable()) continue;
1364
Robert Greenwalt34848c02011-03-25 13:09:25 -07001365// if (currentPriority >= mNetConfigs[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001366
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001367 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1368 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1369 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1370 checkInfo.setFailover(true);
1371 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001373 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001374 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001375 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001376 }
1377
1378 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001379 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1380 }
1381
1382 private void sendInetConditionBroadcast(NetworkInfo info) {
1383 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1384 }
1385
1386 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1387 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001388 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1389 if (info.isFailover()) {
1390 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1391 info.setFailover(false);
1392 }
1393 if (info.getReason() != null) {
1394 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1395 }
1396 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001397 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1398 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001399 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001400 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001401 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001402 }
1403
1404 /**
1405 * Called when an attempt to fail over to another network has failed.
1406 * @param info the {@link NetworkInfo} for the failed network
1407 */
1408 private void handleConnectionFailure(NetworkInfo info) {
1409 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410
Robert Greenwalt2034b912009-08-12 16:08:25 -07001411 String reason = info.getReason();
1412 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001413
Robert Greenwalte981bc52010-10-08 16:35:52 -07001414 String reasonText;
1415 if (reason == null) {
1416 reasonText = ".";
1417 } else {
1418 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001419 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001420 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421
1422 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1423 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1424 if (getActiveNetworkInfo() == null) {
1425 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1426 }
1427 if (reason != null) {
1428 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1429 }
1430 if (extraInfo != null) {
1431 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1432 }
1433 if (info.isFailover()) {
1434 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1435 info.setFailover(false);
1436 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001437
Robert Greenwalt34848c02011-03-25 13:09:25 -07001438 if (mNetConfigs[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001439 tryFailover(info.getType());
1440 if (mActiveDefaultNetwork != -1) {
1441 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001442 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1443 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001444 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001445 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1446 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001447 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001448
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001449 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001450 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001451 /*
1452 * If the failover network is already connected, then immediately send
1453 * out a followup broadcast indicating successful failover
1454 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001455 if (mActiveDefaultNetwork != -1) {
1456 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001457 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001458 }
1459
1460 private void sendStickyBroadcast(Intent intent) {
1461 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001462 if (!mSystemReady) {
1463 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001464 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001465 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1466 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001467 }
1468 }
1469
1470 void systemReady() {
1471 synchronized(this) {
1472 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001473 if (mInitialBroadcast != null) {
1474 mContext.sendStickyBroadcast(mInitialBroadcast);
1475 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001476 }
1477 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001478 // load the global proxy at startup
1479 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001480 }
1481
1482 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001483 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001484
1485 // snapshot isFailover, because sendConnectedBroadcast() resets it
1486 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001487 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001488
Robert Greenwalt2034b912009-08-12 16:08:25 -07001489 // if this is a default net and other default is running
1490 // kill the one not preferred
Robert Greenwalt34848c02011-03-25 13:09:25 -07001491 if (mNetConfigs[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001492 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1493 if ((type != mNetworkPreference &&
Wink Savillef2a62832011-04-07 14:23:45 -07001494 mNetConfigs[mActiveDefaultNetwork].priority >
1495 mNetConfigs[type].priority) ||
Robert Greenwalt2034b912009-08-12 16:08:25 -07001496 mNetworkPreference == mActiveDefaultNetwork) {
1497 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001498 if (DBG) {
1499 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001500 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001501 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001502 teardown(thisNet);
1503 return;
1504 } else {
1505 // tear down the other
1506 NetworkStateTracker otherNet =
1507 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001508 if (DBG) {
1509 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001510 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001511 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001512 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001513 loge("Network declined teardown request");
Robert Greenwalt99910172011-03-29 11:36:28 -07001514 teardown(thisNet);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001515 return;
1516 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001517 }
1518 }
1519 synchronized (ConnectivityService.this) {
1520 // have a new default network, release the transition wakelock in a second
1521 // if it's held. The second pause is to allow apps to reconnect over the
1522 // new network
1523 if (mNetTransitionWakeLock.isHeld()) {
1524 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001525 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001526 mNetTransitionWakeLockSerialNumber, 0),
1527 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001528 }
1529 }
1530 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001531 // this will cause us to come up initially as unconnected and switching
1532 // to connected after our normal pause unless somebody reports us as reall
1533 // disconnected
1534 mDefaultInetConditionPublished = 0;
1535 mDefaultConnectionSequence++;
1536 mInetConditionChangeInFlight = false;
1537 // Don't do this - if we never sign in stay, grey
1538 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001539 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001540 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001541 updateNetworkSettings(thisNet);
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001542 handleConnectivityChange(type, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001543 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001544 }
1545
The Android Open Source Project28527d22009-03-03 19:31:44 -08001546 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001547 * After a change in the connectivity state of a network. We're mainly
1548 * concerned with making sure that the list of DNS servers is set up
1549 * according to which networks are connected, and ensuring that the
1550 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001551 */
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001552 private void handleConnectivityChange(int netType, boolean doReset) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001553 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001554 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001555 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001556 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001557 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001558
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001559 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001560 if (mNetConfigs[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001561 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001562 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001563 } else {
Robert Greenwalt1e2f2862011-04-01 10:51:22 -07001564 // many radios add a default route even when we don't want one.
1565 // remove the default route unless we need it for our active network
1566 if (mActiveDefaultNetwork != -1) {
1567 LinkProperties defaultLinkProperties =
1568 mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
1569 LinkProperties newLinkProperties =
1570 mNetTrackers[netType].getLinkProperties();
1571 String defaultIface = defaultLinkProperties.getInterfaceName();
1572 if (defaultIface != null &&
1573 !defaultIface.equals(newLinkProperties.getInterfaceName())) {
1574 removeDefaultRoute(mNetTrackers[netType]);
1575 }
1576 }
Michael Jurka837e3642011-03-30 19:54:08 -07001577 addPrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001578 }
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05001579
1580 /** Notify TetheringService if interface name has been changed. */
1581 if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(),
1582 Phone.REASON_LINK_PROPERTIES_CHANGED)) {
1583 handleTetherIfaceChange(netType);
1584 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001585 } else {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001586 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001587 removeDefaultRoute(mNetTrackers[netType]);
1588 } else {
1589 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001590 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001591 }
Robert Greenwalt36ea8692011-06-15 12:22:07 -07001592
1593 if (doReset) {
1594 LinkProperties linkProperties = mNetTrackers[netType].getLinkProperties();
1595 if (linkProperties != null) {
1596 String iface = linkProperties.getInterfaceName();
1597 if (TextUtils.isEmpty(iface) == false) {
1598 if (DBG) log("resetConnections(" + iface + ")");
1599 NetworkUtils.resetConnections(iface);
1600 }
1601 }
1602 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001603 }
1604
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001605 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001606 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001607 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001608 if (p == null) return;
1609 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001610
1611 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001612 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001613 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1614 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001615 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001616 Collection<InetAddress> dnsList = p.getDnses();
1617 for (InetAddress dns : dnsList) {
Robert Greenwaltbd492212011-05-06 17:10:53 -07001618 addHostRoute(nt, dns, 0);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001619 }
1620 nt.privateDnsRouteSet(true);
1621 }
1622 }
1623
1624 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001625 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001626 if (p == null) return;
1627 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001628 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1629 if (interfaceName != null && privateDnsRouteSet) {
1630 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001631 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001632 " (" + interfaceName + ")");
1633 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001634
1635 Collection<InetAddress> dnsList = p.getDnses();
1636 for (InetAddress dns : dnsList) {
1637 if (DBG) log(" removing " + dns);
1638 RouteInfo route = RouteInfo.makeHostRoute(dns);
1639 try {
1640 mNetd.removeRoute(interfaceName, route);
1641 } catch (Exception ex) {
1642 loge("error (" + ex + ") removing dns route " + route);
1643 }
1644 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001645 nt.privateDnsRouteSet(false);
1646 }
1647 }
1648
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001649
1650 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001651 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001652 if (p == null) return;
1653 String interfaceName = p.getInterfaceName();
Robert Greenwalt5c733972011-02-09 13:56:06 -08001654 if (TextUtils.isEmpty(interfaceName)) return;
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001655
Robert Greenwalt355205c2011-05-10 15:05:02 -07001656 for (RouteInfo route : p.getRoutes()) {
Robert Greenwalt5a901292011-04-28 14:28:50 -07001657 //TODO - handle non-default routes
1658 if (route.isDefaultRoute()) {
Robert Greenwalt355205c2011-05-10 15:05:02 -07001659 if (DBG) log("adding default route " + route);
Robert Greenwalt5a901292011-04-28 14:28:50 -07001660 InetAddress gateway = route.getGateway();
Robert Greenwalt355205c2011-05-10 15:05:02 -07001661 if (addHostRoute(nt, gateway, 0)) {
1662 try {
1663 mNetd.addRoute(interfaceName, route);
1664 } catch (Exception e) {
1665 loge("error adding default route " + route);
1666 continue;
1667 }
Robert Greenwalt5a901292011-04-28 14:28:50 -07001668 if (DBG) {
1669 NetworkInfo networkInfo = nt.getNetworkInfo();
1670 log("addDefaultRoute for " + networkInfo.getTypeName() +
1671 " (" + interfaceName + "), GatewayAddr=" +
1672 gateway.getHostAddress());
1673 }
Robert Greenwalt355205c2011-05-10 15:05:02 -07001674 } else {
1675 loge("error adding host route for default route " + route);
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001676 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001677 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001678 }
1679 }
1680
1681
1682 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001683 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001684 if (p == null) return;
1685 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001686
Robert Greenwalt355205c2011-05-10 15:05:02 -07001687 if (interfaceName == null) return;
1688
1689 for (RouteInfo route : p.getRoutes()) {
1690 //TODO - handle non-default routes
1691 if (route.isDefaultRoute()) {
1692 try {
1693 mNetd.removeRoute(interfaceName, route);
1694 } catch (Exception ex) {
1695 loge("error (" + ex + ") removing default route " + route);
1696 continue;
1697 }
Robert Greenwalt03d53da2011-03-22 18:47:42 -07001698 if (DBG) {
1699 NetworkInfo networkInfo = nt.getNetworkInfo();
1700 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1701 interfaceName + ")");
1702 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001703 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001704 }
1705 }
1706
1707 /**
1708 * Reads the network specific TCP buffer sizes from SystemProperties
1709 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1710 * wide use
1711 */
1712 public void updateNetworkSettings(NetworkStateTracker nt) {
1713 String key = nt.getTcpBufferSizesPropName();
1714 String bufferSizes = SystemProperties.get(key);
1715
1716 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001717 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001718
1719 // Setting to default values so we won't be stuck to previous values
1720 key = "net.tcp.buffersize.default";
1721 bufferSizes = SystemProperties.get(key);
1722 }
1723
1724 // Set values in kernel
1725 if (bufferSizes.length() != 0) {
1726 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001727 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001728 + "] which comes from [" + key + "]");
1729 }
1730 setBufferSize(bufferSizes);
1731 }
1732 }
1733
1734 /**
1735 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1736 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1737 *
1738 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1739 * writeMin, writeInitial, writeMax"
1740 */
1741 private void setBufferSize(String bufferSizes) {
1742 try {
1743 String[] values = bufferSizes.split(",");
1744
1745 if (values.length == 6) {
1746 final String prefix = "/sys/kernel/ipv4/tcp_";
Mike Lockwood0d5916c2011-05-28 13:24:04 -04001747 FileUtils.stringToFile(prefix + "rmem_min", values[0]);
1748 FileUtils.stringToFile(prefix + "rmem_def", values[1]);
1749 FileUtils.stringToFile(prefix + "rmem_max", values[2]);
1750 FileUtils.stringToFile(prefix + "wmem_min", values[3]);
1751 FileUtils.stringToFile(prefix + "wmem_def", values[4]);
1752 FileUtils.stringToFile(prefix + "wmem_max", values[5]);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001753 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001754 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001755 }
1756 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001757 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001758 }
1759 }
1760
Robert Greenwalt2034b912009-08-12 16:08:25 -07001761 /**
1762 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1763 * on the highest priority active net which this process requested.
1764 * If there aren't any, clear it out
1765 */
1766 private void reassessPidDns(int myPid, boolean doBump)
1767 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001768 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001769 for(int i : mPriorityList) {
Robert Greenwalt34848c02011-03-25 13:09:25 -07001770 if (mNetConfigs[i].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001771 continue;
1772 }
1773 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001774 if (nt.getNetworkInfo().isConnected() &&
1775 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001776 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001777 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001778 List pids = mNetRequestersPids[i];
1779 for (int j=0; j<pids.size(); j++) {
1780 Integer pid = (Integer)pids.get(j);
1781 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001782 Collection<InetAddress> dnses = p.getDnses();
1783 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001784 if (doBump) {
1785 bumpDns();
1786 }
1787 return;
1788 }
1789 }
1790 }
1791 }
1792 // nothing found - delete
1793 for (int i = 1; ; i++) {
1794 String prop = "net.dns" + i + "." + myPid;
1795 if (SystemProperties.get(prop).length() == 0) {
1796 if (doBump) {
1797 bumpDns();
1798 }
1799 return;
1800 }
1801 SystemProperties.set(prop, "");
1802 }
1803 }
1804
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001805 // return true if results in a change
1806 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001807 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001808 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001809 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001810 String dnsString = dns.getHostAddress();
1811 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1812 changed = true;
1813 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1814 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001815 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001816 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001817 }
1818
1819 private void bumpDns() {
1820 /*
1821 * Bump the property that tells the name resolver library to reread
1822 * the DNS server list from the properties.
1823 */
1824 String propVal = SystemProperties.get("net.dnschange");
1825 int n = 0;
1826 if (propVal.length() != 0) {
1827 try {
1828 n = Integer.parseInt(propVal);
1829 } catch (NumberFormatException e) {}
1830 }
1831 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001832 /*
1833 * Tell the VMs to toss their DNS caches
1834 */
1835 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1836 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001837 /*
1838 * Connectivity events can happen before boot has completed ...
1839 */
1840 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001841 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001842 }
1843
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001844 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001845 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001846 NetworkStateTracker nt = mNetTrackers[netType];
1847 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001848 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001849 if (p == null) return;
1850 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001851 boolean changed = false;
Robert Greenwalt34848c02011-03-25 13:09:25 -07001852 if (mNetConfigs[netType].isDefault()) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001853 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001854 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001855 String dnsString = mDefaultDns.getHostAddress();
1856 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1857 if (DBG) {
1858 log("no dns provided - using " + dnsString);
1859 }
1860 changed = true;
1861 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001862 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001863 j++;
1864 } else {
1865 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001866 String dnsString = dns.getHostAddress();
1867 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1868 j++;
1869 continue;
1870 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001871 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001872 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001873 nt.getNetworkInfo().getTypeName());
1874 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001875 changed = true;
1876 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001877 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001878 }
1879 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001880 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1881 if (DBG) log("erasing net.dns" + k);
1882 changed = true;
1883 SystemProperties.set("net.dns" + k, "");
1884 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001885 }
1886 mNumDnsEntries = j;
1887 } else {
1888 // set per-pid dns for attached secondary nets
1889 List pids = mNetRequestersPids[netType];
1890 for (int y=0; y< pids.size(); y++) {
1891 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001892 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001893 }
1894 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001895 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001896 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001897 }
1898
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001899 private int getRestoreDefaultNetworkDelay(int networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001900 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1901 NETWORK_RESTORE_DELAY_PROP_NAME);
1902 if(restoreDefaultNetworkDelayStr != null &&
1903 restoreDefaultNetworkDelayStr.length() != 0) {
1904 try {
1905 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1906 } catch (NumberFormatException e) {
1907 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001908 }
Robert Greenwalt20f819c2011-05-03 19:02:44 -07001909 // if the system property isn't set, use the value for the apn type
1910 int ret = RESTORE_DEFAULT_NETWORK_DELAY;
1911
1912 if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) &&
1913 (mNetConfigs[networkType] != null)) {
1914 ret = mNetConfigs[networkType].restoreTime;
1915 }
1916 return ret;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001917 }
1918
1919 @Override
1920 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001921 if (mContext.checkCallingOrSelfPermission(
1922 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001923 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001924 pw.println("Permission Denial: can't dump ConnectivityService " +
1925 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1926 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001927 return;
1928 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001929 pw.println();
1930 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001931 if (nst != null) {
1932 if (nst.getNetworkInfo().isConnected()) {
1933 pw.println("Active network: " + nst.getNetworkInfo().
1934 getTypeName());
1935 }
1936 pw.println(nst.getNetworkInfo());
1937 pw.println(nst);
1938 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001939 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001940 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001941
1942 pw.println("Network Requester Pids:");
1943 for (int net : mPriorityList) {
1944 String pidString = net + ": ";
1945 for (Object pid : mNetRequestersPids[net]) {
1946 pidString = pidString + pid.toString() + ", ";
1947 }
1948 pw.println(pidString);
1949 }
1950 pw.println();
1951
1952 pw.println("FeatureUsers:");
1953 for (Object requester : mFeatureUsers) {
1954 pw.println(requester.toString());
1955 }
1956 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001957
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001958 synchronized (this) {
1959 pw.println("NetworkTranstionWakeLock is currently " +
1960 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1961 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1962 }
1963 pw.println();
1964
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001965 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001966
1967 if (mInetLog != null) {
1968 pw.println();
1969 pw.println("Inet condition reports:");
1970 for(int i = 0; i < mInetLog.size(); i++) {
1971 pw.println(mInetLog.get(i));
1972 }
1973 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001974 }
1975
Robert Greenwalt2034b912009-08-12 16:08:25 -07001976 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001977 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001978 public MyHandler(Looper looper) {
1979 super(looper);
1980 }
1981
The Android Open Source Project28527d22009-03-03 19:31:44 -08001982 @Override
1983 public void handleMessage(Message msg) {
1984 NetworkInfo info;
1985 switch (msg.what) {
1986 case NetworkStateTracker.EVENT_STATE_CHANGED:
1987 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001988 int type = info.getType();
1989 NetworkInfo.State state = info.getState();
Robert Greenwalt12c44552009-12-07 11:33:18 -08001990
Wink Savillee70c6f52010-12-03 12:01:38 -08001991 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001992 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001993 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001994
1995 // Connectivity state changed:
1996 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001997 // [12-9] Network subtype (for mobile network, as defined
1998 // by TelephonyManager)
1999 // [8-3] Detailed state ordinal (as defined by
2000 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08002001 // [2-0] Network type (as defined by ConnectivityManager)
2002 int eventLogParam = (info.getType() & 0x7) |
2003 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
2004 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08002005 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07002006 eventLogParam);
2007
2008 if (info.getDetailedState() ==
2009 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002010 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002011 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002012 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002013 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002014 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07002015 // the logic here is, handle SUSPENDED the same as
2016 // DISCONNECTED. The only difference being we are
2017 // broadcasting an intent with NetworkInfo that's
2018 // suspended. This allows the applications an
2019 // opportunity to handle DISCONNECTED and SUSPENDED
2020 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08002021 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08002022 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08002023 handleConnect(info);
2024 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002025 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08002026 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07002027 info = (NetworkInfo) msg.obj;
Robert Greenwalt36ea8692011-06-15 12:22:07 -07002028 handleConnectivityChange(info.getType(), true);
The Android Open Source Project28527d22009-03-03 19:31:44 -08002029 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002030 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002031 String causedBy = null;
2032 synchronized (ConnectivityService.this) {
2033 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
2034 mNetTransitionWakeLock.isHeld()) {
2035 mNetTransitionWakeLock.release();
2036 causedBy = mNetTransitionWakeLockCausedBy;
2037 }
2038 }
2039 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002040 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002041 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07002042 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002043 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07002044 FeatureUser u = (FeatureUser)msg.obj;
2045 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07002046 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002047 case EVENT_INET_CONDITION_CHANGE:
2048 {
2049 int netType = msg.arg1;
2050 int condition = msg.arg2;
2051 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002052 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002053 }
2054 case EVENT_INET_CONDITION_HOLD_END:
2055 {
2056 int netType = msg.arg1;
2057 int sequence = msg.arg2;
2058 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07002059 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002060 }
2061 case EVENT_SET_NETWORK_PREFERENCE:
2062 {
2063 int preference = msg.arg1;
2064 handleSetNetworkPreference(preference);
2065 break;
2066 }
2067 case EVENT_SET_BACKGROUND_DATA:
2068 {
2069 boolean enabled = (msg.arg1 == ENABLED);
2070 handleSetBackgroundData(enabled);
2071 break;
2072 }
2073 case EVENT_SET_MOBILE_DATA:
2074 {
2075 boolean enabled = (msg.arg1 == ENABLED);
2076 handleSetMobileData(enabled);
2077 break;
2078 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002079 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
2080 {
2081 handleDeprecatedGlobalHttpProxy();
Robert Greenwalt34848c02011-03-25 13:09:25 -07002082 break;
2083 }
2084 case EVENT_SET_DEPENDENCY_MET:
2085 {
2086 boolean met = (msg.arg1 == ENABLED);
2087 handleSetDependencyMet(msg.arg2, met);
2088 break;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002089 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002090 }
2091 }
2092 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002093
2094 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002095 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002096 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002097
2098 if (isTetheringSupported()) {
2099 return mTethering.tether(iface);
2100 } else {
2101 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2102 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002103 }
2104
2105 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002106 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002107 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002108
2109 if (isTetheringSupported()) {
2110 return mTethering.untether(iface);
2111 } else {
2112 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2113 }
2114 }
2115
2116 // javadoc from interface
2117 public int getLastTetherError(String iface) {
2118 enforceTetherAccessPermission();
2119
2120 if (isTetheringSupported()) {
2121 return mTethering.getLastTetherError(iface);
2122 } else {
2123 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
2124 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002125 }
2126
2127 // TODO - proper iface API for selection by property, inspection, etc
2128 public String[] getTetherableUsbRegexs() {
2129 enforceTetherAccessPermission();
2130 if (isTetheringSupported()) {
2131 return mTethering.getTetherableUsbRegexs();
2132 } else {
2133 return new String[0];
2134 }
2135 }
2136
2137 public String[] getTetherableWifiRegexs() {
2138 enforceTetherAccessPermission();
2139 if (isTetheringSupported()) {
2140 return mTethering.getTetherableWifiRegexs();
2141 } else {
2142 return new String[0];
2143 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002144 }
2145
Danica Chang96567052010-08-11 14:54:43 -07002146 public String[] getTetherableBluetoothRegexs() {
2147 enforceTetherAccessPermission();
2148 if (isTetheringSupported()) {
2149 return mTethering.getTetherableBluetoothRegexs();
2150 } else {
2151 return new String[0];
2152 }
2153 }
2154
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002155 // TODO - move iface listing, queries, etc to new module
2156 // javadoc from interface
2157 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002158 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002159 return mTethering.getTetherableIfaces();
2160 }
2161
2162 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002163 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08002164 return mTethering.getTetheredIfaces();
2165 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002166
Robert Greenwalt4283ded2010-03-02 17:25:02 -08002167 public String[] getTetheringErroredIfaces() {
2168 enforceTetherAccessPermission();
2169 return mTethering.getErroredIfaces();
2170 }
2171
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002172 // if ro.tether.denied = true we default to no tethering
2173 // gservices could set the secure setting to 1 though to enable it on a build where it
2174 // had previously been turned off.
2175 public boolean isTetheringSupported() {
2176 enforceTetherAccessPermission();
2177 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08002178 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
2179 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
2180 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08002181 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002182
2183 // An API NetworkStateTrackers can call when they lose their network.
2184 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
2185 // whichever happens first. The timer is started by the first caller and not
2186 // restarted by subsequent callers.
2187 public void requestNetworkTransitionWakelock(String forWhom) {
2188 enforceConnectivityInternalPermission();
2189 synchronized (this) {
2190 if (mNetTransitionWakeLock.isHeld()) return;
2191 mNetTransitionWakeLockSerialNumber++;
2192 mNetTransitionWakeLock.acquire();
2193 mNetTransitionWakeLockCausedBy = forWhom;
2194 }
2195 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07002196 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07002197 mNetTransitionWakeLockSerialNumber, 0),
2198 mNetTransitionWakeLockTimeout);
2199 return;
2200 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07002201
Robert Greenwalt986c7412010-09-08 15:24:47 -07002202 // 100 percent is full good, 0 is full bad.
2203 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002204 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07002205 mContext.enforceCallingOrSelfPermission(
2206 android.Manifest.permission.STATUS_BAR,
2207 "ConnectivityService");
2208
Robert Greenwalt0e80be12010-09-20 14:35:25 -07002209 if (DBG) {
2210 int pid = getCallingPid();
2211 int uid = getCallingUid();
2212 String s = pid + "(" + uid + ") reports inet is " +
2213 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
2214 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
2215 mInetLog.add(s);
2216 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
2217 mInetLog.remove(0);
2218 }
2219 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07002220 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002221 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
2222 }
2223
2224 private void handleInetConditionChange(int netType, int condition) {
2225 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002226 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002227 netType + ", condition=" + condition +
2228 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2229 }
2230 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002231 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002232 return;
2233 }
2234 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002235 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002236 return;
2237 }
2238 mDefaultInetCondition = condition;
2239 int delay;
2240 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002241 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002242 // setup a new hold to debounce this
2243 if (mDefaultInetCondition > 50) {
2244 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2245 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2246 } else {
2247 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2248 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2249 }
2250 mInetConditionChangeInFlight = true;
2251 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2252 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2253 } else {
2254 // we've set the new condition, when this hold ends that will get
2255 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002256 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002257 }
2258 }
2259
2260 private void handleInetConditionHoldEnd(int netType, int sequence) {
2261 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002262 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002263 ", condition =" + mDefaultInetCondition +
2264 ", published condition =" + mDefaultInetConditionPublished);
2265 }
2266 mInetConditionChangeInFlight = false;
2267
2268 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002269 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002270 return;
2271 }
2272 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002273 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002274 return;
2275 }
2276 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002277 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002278 return;
2279 }
2280 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2281 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002282 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002283 return;
2284 }
2285 mDefaultInetConditionPublished = mDefaultInetCondition;
2286 sendInetConditionBroadcast(networkInfo);
2287 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002288 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002289
2290 public synchronized ProxyProperties getProxy() {
2291 if (mGlobalProxy != null) return mGlobalProxy;
2292 if (mDefaultProxy != null) return mDefaultProxy;
2293 return null;
2294 }
2295
2296 public void setGlobalProxy(ProxyProperties proxyProperties) {
2297 enforceChangePermission();
2298 synchronized (mGlobalProxyLock) {
2299 if (proxyProperties == mGlobalProxy) return;
2300 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2301 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2302
2303 String host = "";
2304 int port = 0;
2305 String exclList = "";
2306 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2307 mGlobalProxy = new ProxyProperties(proxyProperties);
2308 host = mGlobalProxy.getHost();
2309 port = mGlobalProxy.getPort();
2310 exclList = mGlobalProxy.getExclusionList();
2311 } else {
2312 mGlobalProxy = null;
2313 }
2314 ContentResolver res = mContext.getContentResolver();
2315 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2316 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002317 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002318 exclList);
2319 }
2320
2321 if (mGlobalProxy == null) {
2322 proxyProperties = mDefaultProxy;
2323 }
2324 sendProxyBroadcast(proxyProperties);
2325 }
2326
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002327 private void loadGlobalProxy() {
2328 ContentResolver res = mContext.getContentResolver();
2329 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2330 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2331 String exclList = Settings.Secure.getString(res,
2332 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2333 if (!TextUtils.isEmpty(host)) {
2334 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2335 synchronized (mGlobalProxyLock) {
2336 mGlobalProxy = proxyProperties;
2337 }
2338 }
2339 }
2340
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002341 public ProxyProperties getGlobalProxy() {
2342 synchronized (mGlobalProxyLock) {
2343 return mGlobalProxy;
2344 }
2345 }
2346
2347 private void handleApplyDefaultProxy(int type) {
2348 // check if new default - push it out to all VM if so
2349 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2350 synchronized (this) {
2351 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2352 if (mDefaultProxy == proxy) return;
2353 if (!TextUtils.isEmpty(proxy.getHost())) {
2354 mDefaultProxy = proxy;
2355 } else {
2356 mDefaultProxy = null;
2357 }
2358 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002359 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002360 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2361 if (mGlobalProxy != null) return;
2362 sendProxyBroadcast(proxy);
2363 }
2364
2365 private void handleDeprecatedGlobalHttpProxy() {
2366 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2367 Settings.Secure.HTTP_PROXY);
2368 if (!TextUtils.isEmpty(proxy)) {
2369 String data[] = proxy.split(":");
2370 String proxyHost = data[0];
2371 int proxyPort = 8080;
2372 if (data.length > 1) {
2373 try {
2374 proxyPort = Integer.parseInt(data[1]);
2375 } catch (NumberFormatException e) {
2376 return;
2377 }
2378 }
2379 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2380 setGlobalProxy(p);
2381 }
2382 }
2383
2384 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002385 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002386 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002387 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
Stan Chesnutt1f2a2ac2011-01-06 11:00:19 -08002388 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
2389 Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002390 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002391 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002392 }
2393
2394 private static class SettingsObserver extends ContentObserver {
2395 private int mWhat;
2396 private Handler mHandler;
2397 SettingsObserver(Handler handler, int what) {
2398 super(handler);
2399 mHandler = handler;
2400 mWhat = what;
2401 }
2402
2403 void observe(Context context) {
2404 ContentResolver resolver = context.getContentResolver();
2405 resolver.registerContentObserver(Settings.Secure.getUriFor(
2406 Settings.Secure.HTTP_PROXY), false, this);
2407 }
2408
2409 @Override
2410 public void onChange(boolean selfChange) {
2411 mHandler.obtainMessage(mWhat).sendToTarget();
2412 }
2413 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002414
Kazuhiro Ondo3a340412011-04-30 20:10:57 -05002415 private void handleTetherIfaceChange(int type) {
2416 String iface = mNetTrackers[type].getLinkProperties().getInterfaceName();
2417
2418 if (isTetheringSupported()) {
2419 mTethering.handleTetherIfaceChange(iface);
2420 }
2421 }
2422
Wink Savillee70c6f52010-12-03 12:01:38 -08002423 private void log(String s) {
2424 Slog.d(TAG, s);
2425 }
2426
2427 private void loge(String s) {
2428 Slog.e(TAG, s);
2429 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002430
Wink Savillef6b76692011-02-24 17:58:51 -08002431 int convertFeatureToNetworkType(String feature){
2432 int networkType = -1;
2433 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
2434 networkType = ConnectivityManager.TYPE_MOBILE_MMS;
2435 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
2436 networkType = ConnectivityManager.TYPE_MOBILE_SUPL;
2437 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
2438 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
2439 networkType = ConnectivityManager.TYPE_MOBILE_DUN;
2440 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
2441 networkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
2442 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) {
2443 networkType = ConnectivityManager.TYPE_MOBILE_FOTA;
2444 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) {
2445 networkType = ConnectivityManager.TYPE_MOBILE_IMS;
2446 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) {
2447 networkType = ConnectivityManager.TYPE_MOBILE_CBS;
2448 }
2449 return networkType;
2450 }
Jeff Sharkey921ebf22011-05-19 17:12:49 -07002451
2452 private static <T> T checkNotNull(T value, String message) {
2453 if (value == null) {
2454 throw new NullPointerException(message);
2455 }
2456 return value;
2457 }
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002458
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002459 /**
2460 * Protect a socket from VPN routing rules. This method is used by
2461 * VpnBuilder and not available in ConnectivityManager. Permission
2462 * checks are done in Vpn class.
2463 * @hide
2464 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002465 @Override
2466 public void protectVpn(ParcelFileDescriptor socket) {
2467 mVpn.protect(socket, getDefaultInterface());
2468 }
2469
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002470 /**
2471 * Prepare for a VPN application. This method is used by VpnDialogs
2472 * and not available in ConnectivityManager. Permission checks are
2473 * done in Vpn class.
2474 * @hide
2475 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002476 @Override
2477 public String prepareVpn(String packageName) {
2478 return mVpn.prepare(packageName);
2479 }
2480
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002481 /**
2482 * Configure a TUN interface and return its file descriptor. Parameters
2483 * are encoded and opaque to this class. This method is used by VpnBuilder
2484 * and not available in ConnectivityManager. Permission checks are done
2485 * in Vpn class.
2486 * @hide
2487 */
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002488 @Override
Chia-chi Yeh75cacd52011-06-15 17:07:27 -07002489 public ParcelFileDescriptor establishVpn(VpnConfig config) {
Chia-chi Yeh9a4ad7d2011-05-23 17:26:46 -07002490 return mVpn.establish(config);
2491 }
2492
2493 private String getDefaultInterface() {
2494 if (ConnectivityManager.isNetworkTypeValid(mActiveDefaultNetwork)) {
2495 NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork];
2496 if (tracker != null) {
2497 LinkProperties properties = tracker.getLinkProperties();
2498 if (properties != null) {
2499 return properties.getInterfaceName();
2500 }
2501 }
2502 }
2503 throw new IllegalStateException("No default interface");
2504 }
2505
2506 /**
2507 * Callback for VPN subsystem. Currently VPN is not adapted to the service
2508 * through NetworkStateTracker since it works differently. For example, it
2509 * needs to override DNS servers but never takes the default routes. It
2510 * relies on another data network, and it could keep existing connections
2511 * alive after reconnecting, switching between networks, or even resuming
2512 * from deep sleep. Calls from applications should be done synchronously
2513 * to avoid race conditions. As these are all hidden APIs, refactoring can
2514 * be done whenever a better abstraction is developed.
2515 */
2516 public class VpnCallback {
2517
2518 private VpnCallback() {
2519 }
2520
2521 public synchronized void override(String[] dnsServers) {
2522 // TODO: override DNS servers and http proxy.
2523 }
2524
2525 public synchronized void restore() {
2526 // TODO: restore VPN changes.
2527 }
2528 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002529}