blob: 676e5f66f8f38d43d88d8c07cf2b090b6263c40b [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt42acef32009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.io.FileDescriptor;
47import java.io.PrintWriter;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import java.util.ArrayList;
49import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51/**
52 * @hide
53 */
54public class ConnectivityService extends IConnectivityManager.Stub {
55
Robert Greenwaltd8df1492009-10-06 14:12:53 -070056 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 private static final String TAG = "ConnectivityService";
58
Robert Greenwalt42acef32009-08-12 16:08:25 -070059 // how long to wait before switching back to a radio's default network
60 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
61 // system property that can override the above value
62 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
63 "android.telephony.apn-restore";
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 /**
66 * Sometimes we want to refer to the individual network state
67 * trackers separately, and sometimes we just want to treat them
68 * abstractly.
69 */
70 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070071
72 /**
73 * A per Net list of the PID's that requested access to the net
74 * used both as a refcount and for per-PID DNS selection
75 */
76 private List mNetRequestersPids[];
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 private WifiWatchdogService mWifiWatchdogService;
79
Robert Greenwalt42acef32009-08-12 16:08:25 -070080 // priority order of the nettrackers
81 // (excluding dynamically set mNetworkPreference)
82 // TODO - move mNetworkTypePreference into this
83 private int[] mPriorityList;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private Context mContext;
86 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070087 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 private boolean mTestMode;
92 private static ConnectivityService sServiceInstance;
93
Robert Greenwalt42acef32009-08-12 16:08:25 -070094 private Handler mHandler;
95
96 // list of DeathRecipients used to make sure features are turned off when
97 // a process dies
98 private List mFeatureUsers;
99
Mike Lockwood0f79b542009-08-14 14:18:49 -0400100 private boolean mSystemReady;
101 private ArrayList<Intent> mDeferredBroadcasts;
102
Robert Greenwalt42acef32009-08-12 16:08:25 -0700103 private class NetworkAttributes {
104 /**
105 * Class for holding settings read from resources.
106 */
107 public String mName;
108 public int mType;
109 public int mRadio;
110 public int mPriority;
111 public NetworkAttributes(String init) {
112 String fragments[] = init.split(",");
113 mName = fragments[0].toLowerCase();
114 if (fragments[1].toLowerCase().equals("wifi")) {
115 mRadio = ConnectivityManager.TYPE_WIFI;
116 } else {
117 mRadio = ConnectivityManager.TYPE_MOBILE;
118 }
119 if (mName.equals("default")) {
120 mType = mRadio;
121 } else if (mName.equals("mms")) {
122 mType = ConnectivityManager.TYPE_MOBILE_MMS;
123 } else if (mName.equals("supl")) {
124 mType = ConnectivityManager.TYPE_MOBILE_SUPL;
125 } else if (mName.equals("dun")) {
126 mType = ConnectivityManager.TYPE_MOBILE_DUN;
127 } else if (mName.equals("hipri")) {
128 mType = ConnectivityManager.TYPE_MOBILE_HIPRI;
129 }
130 mPriority = Integer.parseInt(fragments[2]);
131 }
132 public boolean isDefault() {
133 return (mType == mRadio);
134 }
135 }
136 NetworkAttributes[] mNetAttributes;
137
138 private class RadioAttributes {
139 public String mName;
140 public int mPriority;
141 public int mSimultaneity;
142 public int mType;
143 public RadioAttributes(String init) {
144 String fragments[] = init.split(",");
145 mName = fragments[0].toLowerCase();
146 mPriority = Integer.parseInt(fragments[1]);
147 mSimultaneity = Integer.parseInt(fragments[2]);
148 if (mName.equals("wifi")) {
149 mType = ConnectivityManager.TYPE_WIFI;
150 } else {
151 mType = ConnectivityManager.TYPE_MOBILE;
152 }
153 }
154 }
155 RadioAttributes[] mRadioAttributes;
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private static class ConnectivityThread extends Thread {
158 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private ConnectivityThread(Context context) {
161 super("ConnectivityThread");
162 mContext = context;
163 }
164
165 @Override
166 public void run() {
167 Looper.prepare();
168 synchronized (this) {
169 sServiceInstance = new ConnectivityService(mContext);
170 notifyAll();
171 }
172 Looper.loop();
173 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static ConnectivityService getServiceInstance(Context context) {
176 ConnectivityThread thread = new ConnectivityThread(context);
177 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 synchronized (thread) {
180 while (sServiceInstance == null) {
181 try {
182 // Wait until sServiceInstance has been initialized.
183 thread.wait();
184 } catch (InterruptedException ignore) {
185 Log.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700186 "Unexpected InterruptedException while waiting"+
187 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
189 }
190 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return sServiceInstance;
193 }
194 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static ConnectivityService getInstance(Context context) {
197 return ConnectivityThread.getServiceInstance(context);
198 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 private ConnectivityService(Context context) {
201 if (DBG) Log.v(TAG, "ConnectivityService starting up");
202 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700203 mNetTrackers = new NetworkStateTracker[
204 ConnectivityManager.MAX_NETWORK_TYPE+1];
205 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700208
Robert Greenwalt42acef32009-08-12 16:08:25 -0700209 // Load device network attributes from resources
210 mNetAttributes = new NetworkAttributes[
211 ConnectivityManager.MAX_NETWORK_TYPE+1];
212 mRadioAttributes = new RadioAttributes[
213 ConnectivityManager.MAX_RADIO_TYPE+1];
214 String[] naStrings = context.getResources().getStringArray(
215 com.android.internal.R.array.networkAttributes);
216 // TODO - what if the setting has gaps/unknown types?
217 for (String a : naStrings) {
218 NetworkAttributes n = new NetworkAttributes(a);
219 mNetAttributes[n.mType] = n;
220 }
221 String[] raStrings = context.getResources().getStringArray(
222 com.android.internal.R.array.radioAttributes);
223 for (String a : raStrings) {
224 RadioAttributes r = new RadioAttributes(a);
225 mRadioAttributes[r.mType] = r;
226 }
227
228 // high priority first
229 mPriorityList = new int[naStrings.length];
230 {
231 int priority = 0; //lowest
232 int nextPos = naStrings.length-1;
233 while (nextPos>-1) {
234 for (int i = 0; i < mNetAttributes.length; i++) {
235 if(mNetAttributes[i].mPriority == priority) {
236 mPriorityList[nextPos--] = i;
237 }
238 }
239 priority++;
240 }
241 }
242
243 mNetRequestersPids =
244 new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
245 for (int i=0; i<=ConnectivityManager.MAX_NETWORK_TYPE; i++) {
246 mNetRequestersPids[i] = new ArrayList();
247 }
248
249 mFeatureUsers = new ArrayList();
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /*
252 * Create the network state trackers for Wi-Fi and mobile
253 * data. Maybe this could be done with a factory class,
254 * but it's not clear that it's worth it, given that
255 * the number of different network types is not going
256 * to change very often.
257 */
258 if (DBG) Log.v(TAG, "Starting Wifi Service.");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700259 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
260 WifiService wifiService = new WifiService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700262 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
Robert Greenwalt42acef32009-08-12 16:08:25 -0700264 mNetTrackers[ConnectivityManager.TYPE_MOBILE] =
265 new MobileDataStateTracker(context, mHandler,
266 ConnectivityManager.TYPE_MOBILE, Phone.APN_TYPE_DEFAULT,
267 "MOBILE");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700268
Robert Greenwalt42acef32009-08-12 16:08:25 -0700269 mNetTrackers[ConnectivityManager.TYPE_MOBILE_MMS] =
270 new MobileDataStateTracker(context, mHandler,
271 ConnectivityManager.TYPE_MOBILE_MMS, Phone.APN_TYPE_MMS,
272 "MOBILE_MMS");
273
274 mNetTrackers[ConnectivityManager.TYPE_MOBILE_SUPL] =
275 new MobileDataStateTracker(context, mHandler,
276 ConnectivityManager.TYPE_MOBILE_SUPL, Phone.APN_TYPE_SUPL,
277 "MOBILE_SUPL");
278
279 mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] =
280 new MobileDataStateTracker(context, mHandler,
281 ConnectivityManager.TYPE_MOBILE_DUN, Phone.APN_TYPE_DUN,
282 "MOBILE_DUN");
283
284 mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI] =
285 new MobileDataStateTracker(context, mHandler,
286 ConnectivityManager.TYPE_MOBILE_HIPRI, Phone.APN_TYPE_HIPRI,
287 "MOBILE_HIPRI");
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 mNumDnsEntries = 0;
290
291 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
292 && SystemProperties.get("ro.build.type").equals("eng");
293
294 for (NetworkStateTracker t : mNetTrackers)
295 t.startMonitoring();
296
297 // Constructing this starts it too
Robert Greenwalt42acef32009-08-12 16:08:25 -0700298 mWifiWatchdogService = new WifiWatchdogService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300
301 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700302 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 * @param preference the new preference
304 */
305 public synchronized void setNetworkPreference(int preference) {
306 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700307 if (ConnectivityManager.isNetworkTypeValid(preference) &&
308 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 if (mNetworkPreference != preference) {
310 persistNetworkPreference(preference);
311 mNetworkPreference = preference;
312 enforcePreference();
313 }
314 }
315 }
316
317 public int getNetworkPreference() {
318 enforceAccessPermission();
319 return mNetworkPreference;
320 }
321
322 private void persistNetworkPreference(int networkPreference) {
323 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700324 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
325 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 private int getPersistedNetworkPreference() {
329 final ContentResolver cr = mContext.getContentResolver();
330
331 final int networkPrefSetting = Settings.Secure
332 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
333 if (networkPrefSetting != -1) {
334 return networkPrefSetting;
335 }
336
337 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
338 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700341 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * In this method, we only tear down a non-preferred network. Establishing
343 * a connection to the preferred network is taken care of when we handle
344 * the disconnect event from the non-preferred network
345 * (see {@link #handleDisconnect(NetworkInfo)}).
346 */
347 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700348 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 return;
350
Robert Greenwalt42acef32009-08-12 16:08:25 -0700351 if (!mNetTrackers[mNetworkPreference].isAvailable())
352 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
355 if (t != mNetworkPreference &&
356 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700357 if (DBG) {
358 Log.d(TAG, "tearing down " +
359 mNetTrackers[t].getNetworkInfo() +
360 " in enforcePreference");
361 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700362 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 }
365 }
366
367 private boolean teardown(NetworkStateTracker netTracker) {
368 if (netTracker.teardown()) {
369 netTracker.setTeardownRequested(true);
370 return true;
371 } else {
372 return false;
373 }
374 }
375
376 /**
377 * Return NetworkInfo for the active (i.e., connected) network interface.
378 * It is assumed that at most one network is active at a time. If more
379 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700380 * @return the info for the active network, or {@code null} if none is
381 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
383 public NetworkInfo getActiveNetworkInfo() {
384 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700385 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
386 if (!mNetAttributes[type].isDefault()) {
387 continue;
388 }
389 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 NetworkInfo info = t.getNetworkInfo();
391 if (info.isConnected()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700392 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
393 "connected default network is not " +
394 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 return info;
396 }
397 }
398 return null;
399 }
400
401 public NetworkInfo getNetworkInfo(int networkType) {
402 enforceAccessPermission();
403 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
404 NetworkStateTracker t = mNetTrackers[networkType];
405 if (t != null)
406 return t.getNetworkInfo();
407 }
408 return null;
409 }
410
411 public NetworkInfo[] getAllNetworkInfo() {
412 enforceAccessPermission();
413 NetworkInfo[] result = new NetworkInfo[mNetTrackers.length];
414 int i = 0;
415 for (NetworkStateTracker t : mNetTrackers) {
416 result[i++] = t.getNetworkInfo();
417 }
418 return result;
419 }
420
421 public boolean setRadios(boolean turnOn) {
422 boolean result = true;
423 enforceChangePermission();
424 for (NetworkStateTracker t : mNetTrackers) {
425 result = t.setRadio(turnOn) && result;
426 }
427 return result;
428 }
429
430 public boolean setRadio(int netType, boolean turnOn) {
431 enforceChangePermission();
432 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
433 return false;
434 }
435 NetworkStateTracker tracker = mNetTrackers[netType];
436 return tracker != null && tracker.setRadio(turnOn);
437 }
438
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700439 /**
440 * Used to notice when the calling process dies so we can self-expire
441 *
442 * Also used to know if the process has cleaned up after itself when
443 * our auto-expire timer goes off. The timer has a link to an object.
444 *
445 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700446 private class FeatureUser implements IBinder.DeathRecipient {
447 int mNetworkType;
448 String mFeature;
449 IBinder mBinder;
450 int mPid;
451 int mUid;
452
453 FeatureUser(int type, String feature, IBinder binder) {
454 super();
455 mNetworkType = type;
456 mFeature = feature;
457 mBinder = binder;
458 mPid = getCallingPid();
459 mUid = getCallingUid();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700460
Robert Greenwalt42acef32009-08-12 16:08:25 -0700461 try {
462 mBinder.linkToDeath(this, 0);
463 } catch (RemoteException e) {
464 binderDied();
465 }
466 }
467
468 void unlinkDeathRecipient() {
469 mBinder.unlinkToDeath(this, 0);
470 }
471
472 public void binderDied() {
473 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
474 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700475 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700476 }
477
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700478 public void expire() {
479 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
480 mNetworkType + ", " + mFeature + ", " + mBinder);
481 stopUsingNetworkFeature(this, false);
482 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700483 }
484
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700485 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700486 public int startUsingNetworkFeature(int networkType, String feature,
487 IBinder binder) {
488 if (DBG) {
489 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
490 ": " + feature);
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 enforceChangePermission();
493 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700494 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700496
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700497 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700498
499 // TODO - move this into the MobileDataStateTracker
500 int usedNetworkType = networkType;
501 if(networkType == ConnectivityManager.TYPE_MOBILE) {
502 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
503 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
504 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
505 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
506 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
507 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
508 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
509 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
510 }
511 }
512 NetworkStateTracker network = mNetTrackers[usedNetworkType];
513 if (network != null) {
514 if (usedNetworkType != networkType) {
515 Integer currentPid = new Integer(getCallingPid());
516
517 NetworkStateTracker radio = mNetTrackers[networkType];
518 NetworkInfo ni = network.getNetworkInfo();
519
520 if (ni.isAvailable() == false) {
521 if (DBG) Log.d(TAG, "special network not available");
522 return Phone.APN_TYPE_NOT_AVAILABLE;
523 }
524
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700525 synchronized(this) {
526 mFeatureUsers.add(f);
527 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
528 // this gets used for per-pid dns when connected
529 mNetRequestersPids[usedNetworkType].add(currentPid);
530 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700531 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700532 mHandler.sendMessageDelayed(mHandler.obtainMessage(
533 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
534 f), getRestoreDefaultNetworkDelay());
535
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536
Robert Greenwalta64bf832009-08-19 20:19:33 -0700537 if ((ni.isConnectedOrConnecting() == true) &&
538 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700539 if (ni.isConnected() == true) {
540 // add the pid-specific dns
541 handleDnsConfigurationChange();
542 if (DBG) Log.d(TAG, "special network already active");
543 return Phone.APN_ALREADY_ACTIVE;
544 }
545 if (DBG) Log.d(TAG, "special network already connecting");
546 return Phone.APN_REQUEST_STARTED;
547 }
548
549 // check if the radio in play can make another contact
550 // assume if cannot for now
551
Robert Greenwalt42acef32009-08-12 16:08:25 -0700552 if (DBG) Log.d(TAG, "reconnecting to special network");
553 network.reconnect();
554 return Phone.APN_REQUEST_STARTED;
555 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700556 synchronized(this) {
557 mFeatureUsers.add(f);
558 }
559 mHandler.sendMessageDelayed(mHandler.obtainMessage(
560 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
561 f), getRestoreDefaultNetworkDelay());
562
Robert Greenwalt42acef32009-08-12 16:08:25 -0700563 return network.startUsingNetworkFeature(feature,
564 getCallingPid(), getCallingUid());
565 }
566 }
567 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700570 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700572 enforceChangePermission();
573
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700574 int pid = getCallingPid();
575 int uid = getCallingUid();
576
577 FeatureUser u = null;
578 boolean found = false;
579
580 synchronized(this) {
581 for (int i = 0; i < mFeatureUsers.size() ; i++) {
582 u = (FeatureUser)mFeatureUsers.get(i);
583 if (uid == u.mUid && pid == u.mPid &&
584 networkType == u.mNetworkType &&
585 TextUtils.equals(feature, u.mFeature)) {
586 found = true;
587 break;
588 }
589 }
590 }
591 if (found && u != null) {
592 // stop regardless of how many other time this proc had called start
593 return stopUsingNetworkFeature(u, true);
594 } else {
595 // none found!
596 return 1;
597 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700598 }
599
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700600 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
601 int networkType = u.mNetworkType;
602 String feature = u.mFeature;
603 int pid = u.mPid;
604 int uid = u.mUid;
605
606 NetworkStateTracker tracker = null;
607 boolean callTeardown = false; // used to carry our decision outside of sync block
608
Robert Greenwalt42acef32009-08-12 16:08:25 -0700609 if (DBG) {
610 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
611 ": " + feature);
612 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
615 return -1;
616 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700617
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700618 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
619 // sync block
620 synchronized(this) {
621 // check if this process still has an outstanding start request
622 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700623 return 1;
624 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700625 u.unlinkDeathRecipient();
626 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
627 // If we care about duplicate requests, check for that here.
628 //
629 // This is done to support the extension of a request - the app
630 // can request we start the network feature again and renew the
631 // auto-shutoff delay. Normal "stop" calls from the app though
632 // do not pay attention to duplicate requests - in effect the
633 // API does not refcount and a single stop will counter multiple starts.
634 if (ignoreDups == false) {
635 for (int i = 0; i < mFeatureUsers.size() ; i++) {
636 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
637 if (x.mUid == u.mUid && x.mPid == u.mPid &&
638 x.mNetworkType == u.mNetworkType &&
639 TextUtils.equals(x.mFeature, u.mFeature)) {
640 return 1;
641 }
642 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700643 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700644
645 // TODO - move to MobileDataStateTracker
646 int usedNetworkType = networkType;
647 if (networkType == ConnectivityManager.TYPE_MOBILE) {
648 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
649 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
650 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
651 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
652 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
653 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
654 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
655 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
656 }
657 }
658 tracker = mNetTrackers[usedNetworkType];
659 if(usedNetworkType != networkType) {
660 Integer currentPid = new Integer(pid);
661 reassessPidDns(pid, true);
662 mNetRequestersPids[usedNetworkType].remove(currentPid);
663 if (mNetRequestersPids[usedNetworkType].size() != 0) {
664 if (DBG) Log.d(TAG, "not tearing down special network - " +
665 "others still using it");
666 return 1;
667 }
668 callTeardown = true;
669 }
670 }
671
672 if (callTeardown) {
673 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700674 return 1;
675 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700676 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677 return tracker.stopUsingNetworkFeature(feature, pid, uid);
678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 }
680
681 /**
682 * Ensure that a network route exists to deliver traffic to the specified
683 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700684 * @param networkType the type of the network over which traffic to the
685 * specified host is to be routed
686 * @param hostAddress the IP address of the host to which the route is
687 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * @return {@code true} on success, {@code false} on failure
689 */
690 public boolean requestRouteToHost(int networkType, int hostAddress) {
691 enforceChangePermission();
692 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
693 return false;
694 }
695 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700696
697 if (!tracker.getNetworkInfo().isConnected() || tracker.isTeardownRequested()) {
698 if (DBG) {
699 Log.d(TAG, "requestRouteToHost on down network (" + networkType + " - dropped");
700 }
701 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700703 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
706 /**
707 * @see ConnectivityManager#getBackgroundDataSetting()
708 */
709 public boolean getBackgroundDataSetting() {
710 return Settings.Secure.getInt(mContext.getContentResolver(),
711 Settings.Secure.BACKGROUND_DATA, 1) == 1;
712 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 /**
715 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
716 */
717 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
718 mContext.enforceCallingOrSelfPermission(
719 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
720 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
723
724 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700725 Settings.Secure.BACKGROUND_DATA,
726 allowBackgroundDataUsage ? 1 : 0);
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 Intent broadcast = new Intent(
729 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
730 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700731 }
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 private int getNumConnectedNetworks() {
734 int numConnectedNets = 0;
735
736 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700737 if (nt.getNetworkInfo().isConnected() &&
738 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 ++numConnectedNets;
740 }
741 }
742 return numConnectedNets;
743 }
744
745 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700746 mContext.enforceCallingOrSelfPermission(
747 android.Manifest.permission.ACCESS_NETWORK_STATE,
748 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
751 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700752 mContext.enforceCallingOrSelfPermission(
753 android.Manifest.permission.CHANGE_NETWORK_STATE,
754 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756
757 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700758 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
759 * network, we ignore it. If it is for the active network, we send out a
760 * broadcast. But first, we check whether it might be possible to connect
761 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 * @param info the {@code NetworkInfo} for the network
763 */
764 private void handleDisconnect(NetworkInfo info) {
765
Robert Greenwalt42acef32009-08-12 16:08:25 -0700766 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767
Robert Greenwalt42acef32009-08-12 16:08:25 -0700768 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 /*
770 * If the disconnected network is not the active one, then don't report
771 * this as a loss of connectivity. What probably happened is that we're
772 * getting the disconnect for a network that we explicitly disabled
773 * in accordance with network preference policies.
774 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700775 if (!mNetAttributes[prevNetType].isDefault()) {
776 List pids = mNetRequestersPids[prevNetType];
777 for (int i = 0; i<pids.size(); i++) {
778 Integer pid = (Integer)pids.get(i);
779 // will remove them because the net's no longer connected
780 // need to do this now as only now do we know the pids and
781 // can properly null things that are no longer referenced.
782 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
787 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
788 if (info.isFailover()) {
789 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
790 info.setFailover(false);
791 }
792 if (info.getReason() != null) {
793 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
794 }
795 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700796 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
797 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799
800 /*
801 * If this is a default network, check if other defaults are available
802 * or active
803 */
804 NetworkStateTracker newNet = null;
805 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700806 if (mActiveDefaultNetwork == prevNetType) {
807 mActiveDefaultNetwork = -1;
808 }
809
810 int newType = -1;
811 int newPriority = -1;
812 for (int checkType=0; checkType <=
813 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
814 if (checkType == prevNetType) {
815 continue;
816 }
817 if (mNetAttributes[checkType].isDefault()) {
818 /* TODO - if we have multiple nets we could use
819 * we may want to put more thought into which we choose
820 */
821 if (checkType == mNetworkPreference) {
822 newType = checkType;
823 break;
824 }
825 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
826 mPriority > newPriority) {
827 newType = checkType;
828 newPriority = mRadioAttributes[mNetAttributes[newType].
829 mRadio].mPriority;
830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700833
834 if (newType != -1) {
835 newNet = mNetTrackers[newType];
836 /**
837 * See if the other network is available to fail over to.
838 * If is not available, we enable it anyway, so that it
839 * will be able to connect when it does become available,
840 * but we report a total loss of connectivity rather than
841 * report that we are attempting to fail over.
842 */
843 if (newNet.isAvailable()) {
844 NetworkInfo switchTo = newNet.getNetworkInfo();
845 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700846 if (!switchTo.isConnectedOrConnecting() ||
847 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700848 newNet.reconnect();
849 }
850 if (DBG) {
851 if (switchTo.isConnected()) {
852 Log.v(TAG, "Switching to already connected " +
853 switchTo.getTypeName());
854 } else {
855 Log.v(TAG, "Attempting to switch to " +
856 switchTo.getTypeName());
857 }
858 }
859 intent.putExtra(ConnectivityManager.
860 EXTRA_OTHER_NETWORK_INFO, switchTo);
861 } else {
Robert Greenwaltc7d25302009-09-17 14:58:16 -0700862 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
863 true);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700864 newNet.reconnect();
865 }
866 } else {
867 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
868 true);
869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871
872 // do this before we broadcast the change
873 handleConnectivityChange();
874
Mike Lockwood0f79b542009-08-14 14:18:49 -0400875 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 /*
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700877 * If the failover network is already connected, then immediately send
878 * out a followup broadcast indicating successful failover
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700880 if (newNet != null && newNet.getNetworkInfo().isConnected())
881 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
884 private void sendConnectedBroadcast(NetworkInfo info) {
885 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
886 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
887 if (info.isFailover()) {
888 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
889 info.setFailover(false);
890 }
891 if (info.getReason() != null) {
892 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
893 }
894 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700895 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
896 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400898 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900
901 /**
902 * Called when an attempt to fail over to another network has failed.
903 * @param info the {@link NetworkInfo} for the failed network
904 */
905 private void handleConnectionFailure(NetworkInfo info) {
906 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
Robert Greenwalt42acef32009-08-12 16:08:25 -0700908 String reason = info.getReason();
909 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700910
Robert Greenwalt42acef32009-08-12 16:08:25 -0700911 if (DBG) {
912 String reasonText;
913 if (reason == null) {
914 reasonText = ".";
915 } else {
916 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700918 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
919 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700921
922 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
923 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
924 if (getActiveNetworkInfo() == null) {
925 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
926 }
927 if (reason != null) {
928 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
929 }
930 if (extraInfo != null) {
931 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
932 }
933 if (info.isFailover()) {
934 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
935 info.setFailover(false);
936 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400937 sendStickyBroadcast(intent);
938 }
939
940 private void sendStickyBroadcast(Intent intent) {
941 synchronized(this) {
942 if (mSystemReady) {
943 mContext.sendStickyBroadcast(intent);
944 } else {
945 if (mDeferredBroadcasts == null) {
946 mDeferredBroadcasts = new ArrayList<Intent>();
947 }
948 mDeferredBroadcasts.add(intent);
949 }
950 }
951 }
952
953 void systemReady() {
954 synchronized(this) {
955 mSystemReady = true;
956 if (mDeferredBroadcasts != null) {
957 int count = mDeferredBroadcasts.size();
958 for (int i = 0; i < count; i++) {
959 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
960 }
961 mDeferredBroadcasts = null;
962 }
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
966 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700967 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968
969 // snapshot isFailover, because sendConnectedBroadcast() resets it
970 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700971 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972
Robert Greenwalt42acef32009-08-12 16:08:25 -0700973 // if this is a default net and other default is running
974 // kill the one not preferred
975 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700976 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
977 if ((type != mNetworkPreference &&
978 mNetAttributes[mActiveDefaultNetwork].mPriority >
979 mNetAttributes[type].mPriority) ||
980 mNetworkPreference == mActiveDefaultNetwork) {
981 // don't accept this one
982 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
983 "to torn down network " + info.getTypeName());
984 teardown(thisNet);
985 return;
986 } else {
987 // tear down the other
988 NetworkStateTracker otherNet =
989 mNetTrackers[mActiveDefaultNetwork];
990 if (DBG) Log.v(TAG, "Policy requires " +
991 otherNet.getNetworkInfo().getTypeName() +
992 " teardown");
993 if (!teardown(otherNet)) {
994 Log.e(TAG, "Network declined teardown request");
995 return;
996 }
997 if (isFailover) {
998 otherNet.releaseWakeLock();
999 }
1000 }
1001 }
1002 mActiveDefaultNetwork = type;
1003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001005 thisNet.updateNetworkSettings();
1006 handleConnectivityChange();
1007 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
1010 private void handleScanResultsAvailable(NetworkInfo info) {
1011 int networkType = info.getType();
1012 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001013 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1014 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 mNetTrackers[networkType].interpretScanResultsAvailable();
1018 }
1019
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001020 private void handleNotificationChange(boolean visible, int id,
1021 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 NotificationManager notificationManager = (NotificationManager) mContext
1023 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (visible) {
1026 notificationManager.notify(id, notification);
1027 } else {
1028 notificationManager.cancel(id);
1029 }
1030 }
1031
1032 /**
1033 * After any kind of change in the connectivity state of any network,
1034 * make sure that anything that depends on the connectivity state of
1035 * more than one network is set up correctly. We're mainly concerned
1036 * with making sure that the list of DNS servers is set up according
1037 * to which networks are connected, and ensuring that the right routing
1038 * table entries exist.
1039 */
1040 private void handleConnectivityChange() {
1041 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001042 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001043 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 * If both mobile and wifi are enabled, add the host routes that
1045 * will allow MMS traffic to pass on the mobile network. But
1046 * remove the default route for the mobile network, so that there
1047 * will be only one default route, to ensure that all traffic
1048 * except MMS will travel via Wi-Fi.
1049 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001050 handleDnsConfigurationChange();
1051
1052 for (int netType : mPriorityList) {
1053 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1054 if (mNetAttributes[netType].isDefault()) {
1055 mNetTrackers[netType].addDefaultRoute();
1056 } else {
1057 mNetTrackers[netType].addPrivateDnsRoutes();
1058 }
1059 } else {
1060 if (mNetAttributes[netType].isDefault()) {
1061 mNetTrackers[netType].removeDefaultRoute();
1062 } else {
1063 mNetTrackers[netType].removePrivateDnsRoutes();
1064 }
1065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 }
1068
Robert Greenwalt42acef32009-08-12 16:08:25 -07001069 /**
1070 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1071 * on the highest priority active net which this process requested.
1072 * If there aren't any, clear it out
1073 */
1074 private void reassessPidDns(int myPid, boolean doBump)
1075 {
1076 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1077 for(int i : mPriorityList) {
1078 if (mNetAttributes[i].isDefault()) {
1079 continue;
1080 }
1081 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001082 if (nt.getNetworkInfo().isConnected() &&
1083 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001084 List pids = mNetRequestersPids[i];
1085 for (int j=0; j<pids.size(); j++) {
1086 Integer pid = (Integer)pids.get(j);
1087 if (pid.intValue() == myPid) {
1088 String[] dnsList = nt.getNameServers();
1089 writePidDns(dnsList, myPid);
1090 if (doBump) {
1091 bumpDns();
1092 }
1093 return;
1094 }
1095 }
1096 }
1097 }
1098 // nothing found - delete
1099 for (int i = 1; ; i++) {
1100 String prop = "net.dns" + i + "." + myPid;
1101 if (SystemProperties.get(prop).length() == 0) {
1102 if (doBump) {
1103 bumpDns();
1104 }
1105 return;
1106 }
1107 SystemProperties.set(prop, "");
1108 }
1109 }
1110
1111 private void writePidDns(String[] dnsList, int pid) {
1112 int j = 1;
1113 for (String dns : dnsList) {
1114 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1115 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1116 }
1117 }
1118 }
1119
1120 private void bumpDns() {
1121 /*
1122 * Bump the property that tells the name resolver library to reread
1123 * the DNS server list from the properties.
1124 */
1125 String propVal = SystemProperties.get("net.dnschange");
1126 int n = 0;
1127 if (propVal.length() != 0) {
1128 try {
1129 n = Integer.parseInt(propVal);
1130 } catch (NumberFormatException e) {}
1131 }
1132 SystemProperties.set("net.dnschange", "" + (n+1));
1133 }
1134
1135 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001136 // add default net's dns entries
1137 for (int x = mPriorityList.length-1; x>= 0; x--) {
1138 int netType = mPriorityList[x];
1139 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001140 if (nt != null && nt.getNetworkInfo().isConnected() &&
1141 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001143 if (mNetAttributes[netType].isDefault()) {
1144 int j = 1;
1145 for (String dns : dnsList) {
1146 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001147 if (DBG) {
1148 Log.d(TAG, "adding dns " + dns + " for " +
1149 nt.getNetworkInfo().getTypeName());
1150 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001151 SystemProperties.set("net.dns" + j++, dns);
1152 }
1153 }
1154 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001155 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1156 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001157 }
1158 mNumDnsEntries = j;
1159 } else {
1160 // set per-pid dns for attached secondary nets
1161 List pids = mNetRequestersPids[netType];
1162 for (int y=0; y< pids.size(); y++) {
1163 Integer pid = (Integer)pids.get(y);
1164 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
1167 }
1168 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001169
1170 bumpDns();
1171 }
1172
1173 private int getRestoreDefaultNetworkDelay() {
1174 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1175 NETWORK_RESTORE_DELAY_PROP_NAME);
1176 if(restoreDefaultNetworkDelayStr != null &&
1177 restoreDefaultNetworkDelayStr.length() != 0) {
1178 try {
1179 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1180 } catch (NumberFormatException e) {
1181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001183 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185
1186 @Override
1187 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001188 if (mContext.checkCallingOrSelfPermission(
1189 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001191 pw.println("Permission Denial: can't dump ConnectivityService " +
1192 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1193 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 return;
1195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 pw.println();
1197 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001198 if (nst.getNetworkInfo().isConnected()) {
1199 pw.println("Active network: " + nst.getNetworkInfo().
1200 getTypeName());
1201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 pw.println(nst.getNetworkInfo());
1203 pw.println(nst);
1204 pw.println();
1205 }
1206 }
1207
Robert Greenwalt42acef32009-08-12 16:08:25 -07001208 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 private class MyHandler extends Handler {
1210 @Override
1211 public void handleMessage(Message msg) {
1212 NetworkInfo info;
1213 switch (msg.what) {
1214 case NetworkStateTracker.EVENT_STATE_CHANGED:
1215 info = (NetworkInfo) msg.obj;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001216 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001217 info.getTypeName() + ": " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 info.getState() + "/" + info.getDetailedState());
1219
1220 // Connectivity state changed:
1221 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001222 // [12-9] Network subtype (for mobile network, as defined
1223 // by TelephonyManager)
1224 // [8-3] Detailed state ordinal (as defined by
1225 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 // [2-0] Network type (as defined by ConnectivityManager)
1227 int eventLogParam = (info.getType() & 0x7) |
1228 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1229 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001231 eventLogParam);
1232
1233 if (info.getDetailedState() ==
1234 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 handleConnectionFailure(info);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001236 } else if (info.getState() ==
1237 NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 handleDisconnect(info);
1239 } else if (info.getState() == NetworkInfo.State.SUSPENDED) {
1240 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001241 // the logic here is, handle SUSPENDED the same as
1242 // DISCONNECTED. The only difference being we are
1243 // broadcasting an intent with NetworkInfo that's
1244 // suspended. This allows the applications an
1245 // opportunity to handle DISCONNECTED and SUSPENDED
1246 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 handleDisconnect(info);
1248 } else if (info.getState() == NetworkInfo.State.CONNECTED) {
1249 handleConnect(info);
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 break;
1252
1253 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1254 info = (NetworkInfo) msg.obj;
1255 handleScanResultsAvailable(info);
1256 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001259 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1260 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261
1262 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001263 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 break;
1265
1266 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1267 // fill me in
1268 break;
1269
1270 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1271 // fill me in
1272 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001273 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001274 FeatureUser u = (FeatureUser)msg.obj;
1275 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001276 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278 }
1279 }
1280}