blob: df37d35a74f64af1ea67e5f4bddce61a79e15e2f [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 Greenwalt42acef32009-08-12 16:08:25 -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
59 // Event log tags (must be in sync with event-log-tags)
60 private static final int EVENTLOG_CONNECTIVITY_STATE_CHANGED = 50020;
61
Robert Greenwalt42acef32009-08-12 16:08:25 -070062 // how long to wait before switching back to a radio's default network
63 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
64 // system property that can override the above value
65 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
66 "android.telephony.apn-restore";
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
69 * Sometimes we want to refer to the individual network state
70 * trackers separately, and sometimes we just want to treat them
71 * abstractly.
72 */
73 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070074
75 /**
76 * A per Net list of the PID's that requested access to the net
77 * used both as a refcount and for per-PID DNS selection
78 */
79 private List mNetRequestersPids[];
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private WifiWatchdogService mWifiWatchdogService;
82
Robert Greenwalt42acef32009-08-12 16:08:25 -070083 // priority order of the nettrackers
84 // (excluding dynamically set mNetworkPreference)
85 // TODO - move mNetworkTypePreference into this
86 private int[] mPriorityList;
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private Context mContext;
89 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070090 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private boolean mTestMode;
95 private static ConnectivityService sServiceInstance;
96
Robert Greenwalt42acef32009-08-12 16:08:25 -070097 private Handler mHandler;
98
99 // list of DeathRecipients used to make sure features are turned off when
100 // a process dies
101 private List mFeatureUsers;
102
Mike Lockwood0f79b542009-08-14 14:18:49 -0400103 private boolean mSystemReady;
104 private ArrayList<Intent> mDeferredBroadcasts;
105
Robert Greenwalt42acef32009-08-12 16:08:25 -0700106 private class NetworkAttributes {
107 /**
108 * Class for holding settings read from resources.
109 */
110 public String mName;
111 public int mType;
112 public int mRadio;
113 public int mPriority;
114 public NetworkAttributes(String init) {
115 String fragments[] = init.split(",");
116 mName = fragments[0].toLowerCase();
117 if (fragments[1].toLowerCase().equals("wifi")) {
118 mRadio = ConnectivityManager.TYPE_WIFI;
119 } else {
120 mRadio = ConnectivityManager.TYPE_MOBILE;
121 }
122 if (mName.equals("default")) {
123 mType = mRadio;
124 } else if (mName.equals("mms")) {
125 mType = ConnectivityManager.TYPE_MOBILE_MMS;
126 } else if (mName.equals("supl")) {
127 mType = ConnectivityManager.TYPE_MOBILE_SUPL;
128 } else if (mName.equals("dun")) {
129 mType = ConnectivityManager.TYPE_MOBILE_DUN;
130 } else if (mName.equals("hipri")) {
131 mType = ConnectivityManager.TYPE_MOBILE_HIPRI;
132 }
133 mPriority = Integer.parseInt(fragments[2]);
134 }
135 public boolean isDefault() {
136 return (mType == mRadio);
137 }
138 }
139 NetworkAttributes[] mNetAttributes;
140
141 private class RadioAttributes {
142 public String mName;
143 public int mPriority;
144 public int mSimultaneity;
145 public int mType;
146 public RadioAttributes(String init) {
147 String fragments[] = init.split(",");
148 mName = fragments[0].toLowerCase();
149 mPriority = Integer.parseInt(fragments[1]);
150 mSimultaneity = Integer.parseInt(fragments[2]);
151 if (mName.equals("wifi")) {
152 mType = ConnectivityManager.TYPE_WIFI;
153 } else {
154 mType = ConnectivityManager.TYPE_MOBILE;
155 }
156 }
157 }
158 RadioAttributes[] mRadioAttributes;
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private static class ConnectivityThread extends Thread {
161 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private ConnectivityThread(Context context) {
164 super("ConnectivityThread");
165 mContext = context;
166 }
167
168 @Override
169 public void run() {
170 Looper.prepare();
171 synchronized (this) {
172 sServiceInstance = new ConnectivityService(mContext);
173 notifyAll();
174 }
175 Looper.loop();
176 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static ConnectivityService getServiceInstance(Context context) {
179 ConnectivityThread thread = new ConnectivityThread(context);
180 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 synchronized (thread) {
183 while (sServiceInstance == null) {
184 try {
185 // Wait until sServiceInstance has been initialized.
186 thread.wait();
187 } catch (InterruptedException ignore) {
188 Log.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700189 "Unexpected InterruptedException while waiting"+
190 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192 }
193 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 return sServiceInstance;
196 }
197 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static ConnectivityService getInstance(Context context) {
200 return ConnectivityThread.getServiceInstance(context);
201 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private ConnectivityService(Context context) {
204 if (DBG) Log.v(TAG, "ConnectivityService starting up");
205 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700206 mNetTrackers = new NetworkStateTracker[
207 ConnectivityManager.MAX_NETWORK_TYPE+1];
208 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700211
Robert Greenwalt42acef32009-08-12 16:08:25 -0700212 // Load device network attributes from resources
213 mNetAttributes = new NetworkAttributes[
214 ConnectivityManager.MAX_NETWORK_TYPE+1];
215 mRadioAttributes = new RadioAttributes[
216 ConnectivityManager.MAX_RADIO_TYPE+1];
217 String[] naStrings = context.getResources().getStringArray(
218 com.android.internal.R.array.networkAttributes);
219 // TODO - what if the setting has gaps/unknown types?
220 for (String a : naStrings) {
221 NetworkAttributes n = new NetworkAttributes(a);
222 mNetAttributes[n.mType] = n;
223 }
224 String[] raStrings = context.getResources().getStringArray(
225 com.android.internal.R.array.radioAttributes);
226 for (String a : raStrings) {
227 RadioAttributes r = new RadioAttributes(a);
228 mRadioAttributes[r.mType] = r;
229 }
230
231 // high priority first
232 mPriorityList = new int[naStrings.length];
233 {
234 int priority = 0; //lowest
235 int nextPos = naStrings.length-1;
236 while (nextPos>-1) {
237 for (int i = 0; i < mNetAttributes.length; i++) {
238 if(mNetAttributes[i].mPriority == priority) {
239 mPriorityList[nextPos--] = i;
240 }
241 }
242 priority++;
243 }
244 }
245
246 mNetRequestersPids =
247 new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
248 for (int i=0; i<=ConnectivityManager.MAX_NETWORK_TYPE; i++) {
249 mNetRequestersPids[i] = new ArrayList();
250 }
251
252 mFeatureUsers = new ArrayList();
253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /*
255 * Create the network state trackers for Wi-Fi and mobile
256 * data. Maybe this could be done with a factory class,
257 * but it's not clear that it's worth it, given that
258 * the number of different network types is not going
259 * to change very often.
260 */
261 if (DBG) Log.v(TAG, "Starting Wifi Service.");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700262 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
263 WifiService wifiService = new WifiService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700265 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Robert Greenwalt42acef32009-08-12 16:08:25 -0700267 mNetTrackers[ConnectivityManager.TYPE_MOBILE] =
268 new MobileDataStateTracker(context, mHandler,
269 ConnectivityManager.TYPE_MOBILE, Phone.APN_TYPE_DEFAULT,
270 "MOBILE");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700271
Robert Greenwalt42acef32009-08-12 16:08:25 -0700272 mNetTrackers[ConnectivityManager.TYPE_MOBILE_MMS] =
273 new MobileDataStateTracker(context, mHandler,
274 ConnectivityManager.TYPE_MOBILE_MMS, Phone.APN_TYPE_MMS,
275 "MOBILE_MMS");
276
277 mNetTrackers[ConnectivityManager.TYPE_MOBILE_SUPL] =
278 new MobileDataStateTracker(context, mHandler,
279 ConnectivityManager.TYPE_MOBILE_SUPL, Phone.APN_TYPE_SUPL,
280 "MOBILE_SUPL");
281
282 mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] =
283 new MobileDataStateTracker(context, mHandler,
284 ConnectivityManager.TYPE_MOBILE_DUN, Phone.APN_TYPE_DUN,
285 "MOBILE_DUN");
286
287 mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI] =
288 new MobileDataStateTracker(context, mHandler,
289 ConnectivityManager.TYPE_MOBILE_HIPRI, Phone.APN_TYPE_HIPRI,
290 "MOBILE_HIPRI");
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 mNumDnsEntries = 0;
293
294 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
295 && SystemProperties.get("ro.build.type").equals("eng");
296
297 for (NetworkStateTracker t : mNetTrackers)
298 t.startMonitoring();
299
300 // Constructing this starts it too
Robert Greenwalt42acef32009-08-12 16:08:25 -0700301 mWifiWatchdogService = new WifiWatchdogService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303
304 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700305 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * @param preference the new preference
307 */
308 public synchronized void setNetworkPreference(int preference) {
309 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700310 if (ConnectivityManager.isNetworkTypeValid(preference) &&
311 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 if (mNetworkPreference != preference) {
313 persistNetworkPreference(preference);
314 mNetworkPreference = preference;
315 enforcePreference();
316 }
317 }
318 }
319
320 public int getNetworkPreference() {
321 enforceAccessPermission();
322 return mNetworkPreference;
323 }
324
325 private void persistNetworkPreference(int networkPreference) {
326 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700327 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
328 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 private int getPersistedNetworkPreference() {
332 final ContentResolver cr = mContext.getContentResolver();
333
334 final int networkPrefSetting = Settings.Secure
335 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
336 if (networkPrefSetting != -1) {
337 return networkPrefSetting;
338 }
339
340 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
341 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700344 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 * In this method, we only tear down a non-preferred network. Establishing
346 * a connection to the preferred network is taken care of when we handle
347 * the disconnect event from the non-preferred network
348 * (see {@link #handleDisconnect(NetworkInfo)}).
349 */
350 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700351 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return;
353
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 if (!mNetTrackers[mNetworkPreference].isAvailable())
355 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
Robert Greenwalt42acef32009-08-12 16:08:25 -0700357 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
358 if (t != mNetworkPreference &&
359 mNetTrackers[t].getNetworkInfo().isConnected()) {
360 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363 }
364
365 private boolean teardown(NetworkStateTracker netTracker) {
366 if (netTracker.teardown()) {
367 netTracker.setTeardownRequested(true);
368 return true;
369 } else {
370 return false;
371 }
372 }
373
374 /**
375 * Return NetworkInfo for the active (i.e., connected) network interface.
376 * It is assumed that at most one network is active at a time. If more
377 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700378 * @return the info for the active network, or {@code null} if none is
379 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 */
381 public NetworkInfo getActiveNetworkInfo() {
382 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700383 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
384 if (!mNetAttributes[type].isDefault()) {
385 continue;
386 }
387 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 NetworkInfo info = t.getNetworkInfo();
389 if (info.isConnected()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700390 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
391 "connected default network is not " +
392 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return info;
394 }
395 }
396 return null;
397 }
398
399 public NetworkInfo getNetworkInfo(int networkType) {
400 enforceAccessPermission();
401 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
402 NetworkStateTracker t = mNetTrackers[networkType];
403 if (t != null)
404 return t.getNetworkInfo();
405 }
406 return null;
407 }
408
409 public NetworkInfo[] getAllNetworkInfo() {
410 enforceAccessPermission();
411 NetworkInfo[] result = new NetworkInfo[mNetTrackers.length];
412 int i = 0;
413 for (NetworkStateTracker t : mNetTrackers) {
414 result[i++] = t.getNetworkInfo();
415 }
416 return result;
417 }
418
419 public boolean setRadios(boolean turnOn) {
420 boolean result = true;
421 enforceChangePermission();
422 for (NetworkStateTracker t : mNetTrackers) {
423 result = t.setRadio(turnOn) && result;
424 }
425 return result;
426 }
427
428 public boolean setRadio(int netType, boolean turnOn) {
429 enforceChangePermission();
430 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
431 return false;
432 }
433 NetworkStateTracker tracker = mNetTrackers[netType];
434 return tracker != null && tracker.setRadio(turnOn);
435 }
436
Robert Greenwalt42acef32009-08-12 16:08:25 -0700437 private class FeatureUser implements IBinder.DeathRecipient {
438 int mNetworkType;
439 String mFeature;
440 IBinder mBinder;
441 int mPid;
442 int mUid;
443
444 FeatureUser(int type, String feature, IBinder binder) {
445 super();
446 mNetworkType = type;
447 mFeature = feature;
448 mBinder = binder;
449 mPid = getCallingPid();
450 mUid = getCallingUid();
451 try {
452 mBinder.linkToDeath(this, 0);
453 } catch (RemoteException e) {
454 binderDied();
455 }
456 }
457
458 void unlinkDeathRecipient() {
459 mBinder.unlinkToDeath(this, 0);
460 }
461
462 public void binderDied() {
463 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
464 mNetworkType + ", " + mFeature + ", " + mBinder);
465 stopUsingNetworkFeature(mNetworkType, mFeature, mPid, mUid);
466 }
467
468 }
469
470 public int startUsingNetworkFeature(int networkType, String feature,
471 IBinder binder) {
472 if (DBG) {
473 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
474 ": " + feature);
475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 enforceChangePermission();
477 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700478 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700480
481 synchronized (mFeatureUsers) {
482 mFeatureUsers.add(new FeatureUser(networkType, feature, binder));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700484
485 // TODO - move this into the MobileDataStateTracker
486 int usedNetworkType = networkType;
487 if(networkType == ConnectivityManager.TYPE_MOBILE) {
488 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
489 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
490 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
491 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
492 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
493 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
494 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
495 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
496 }
497 }
498 NetworkStateTracker network = mNetTrackers[usedNetworkType];
499 if (network != null) {
500 if (usedNetworkType != networkType) {
501 Integer currentPid = new Integer(getCallingPid());
502
503 NetworkStateTracker radio = mNetTrackers[networkType];
504 NetworkInfo ni = network.getNetworkInfo();
505
506 if (ni.isAvailable() == false) {
507 if (DBG) Log.d(TAG, "special network not available");
508 return Phone.APN_TYPE_NOT_AVAILABLE;
509 }
510
511 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
512 // this gets used for per-pid dns when connected
513 mNetRequestersPids[usedNetworkType].add(currentPid);
514 }
515
Robert Greenwalta64bf832009-08-19 20:19:33 -0700516 if ((ni.isConnectedOrConnecting() == true) &&
517 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700518 if (ni.isConnected() == true) {
519 // add the pid-specific dns
520 handleDnsConfigurationChange();
521 if (DBG) Log.d(TAG, "special network already active");
522 return Phone.APN_ALREADY_ACTIVE;
523 }
524 if (DBG) Log.d(TAG, "special network already connecting");
525 return Phone.APN_REQUEST_STARTED;
526 }
527
528 // check if the radio in play can make another contact
529 // assume if cannot for now
530
531 // since we have to drop the default on this radio, setup
532 // an automatic event to switch back
533 if(mHandler.hasMessages(NetworkStateTracker.
534 EVENT_RESTORE_DEFAULT_NETWORK, radio) ||
535 radio.getNetworkInfo().isConnectedOrConnecting()) {
536 mHandler.removeMessages(NetworkStateTracker.
537 EVENT_RESTORE_DEFAULT_NETWORK,
538 radio);
539 mHandler.sendMessageDelayed(mHandler.obtainMessage(
540 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
541 radio), getRestoreDefaultNetworkDelay());
542 }
543 if (DBG) Log.d(TAG, "reconnecting to special network");
544 network.reconnect();
545 return Phone.APN_REQUEST_STARTED;
546 } else {
547 return network.startUsingNetworkFeature(feature,
548 getCallingPid(), getCallingUid());
549 }
550 }
551 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553
554 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700555 return stopUsingNetworkFeature(networkType, feature, getCallingPid(),
556 getCallingUid());
557 }
558
559 private int stopUsingNetworkFeature(int networkType, String feature,
560 int pid, int uid) {
561 if (DBG) {
562 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
563 ": " + feature);
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 enforceChangePermission();
566 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
567 return -1;
568 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700569
570 synchronized (mFeatureUsers) {
571 for (int i=0; i < mFeatureUsers.size(); i++) {
572 FeatureUser u = (FeatureUser)mFeatureUsers.get(i);
573 if (uid == u.mUid && pid == u.mPid &&
574 networkType == u.mNetworkType &&
575 TextUtils.equals(feature, u.mFeature)) {
576 u.unlinkDeathRecipient();
577 mFeatureUsers.remove(i);
578 break;
579 }
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700582
583 // TODO - move to MobileDataStateTracker
584 int usedNetworkType = networkType;
585 if (networkType == ConnectivityManager.TYPE_MOBILE) {
586 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
587 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
588 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
589 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
590 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
591 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
592 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
593 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
594 }
595 }
596 NetworkStateTracker tracker = mNetTrackers[usedNetworkType];
597 if(usedNetworkType != networkType) {
598 Integer currentPid = new Integer(pid);
599 if (mNetRequestersPids[usedNetworkType].remove(currentPid)) {
600 reassessPidDns(pid, true);
601 }
602 if (mNetRequestersPids[usedNetworkType].size() != 0) {
603 if (DBG) Log.d(TAG, "not tearing down special network - " +
604 "others still using it");
605 return 1;
606 }
607
608 tracker.teardown();
609 NetworkStateTracker radio = mNetTrackers[networkType];
610 // Check if we want to revert to the default
611 if (mHandler.hasMessages(NetworkStateTracker.
612 EVENT_RESTORE_DEFAULT_NETWORK, radio)) {
613 mHandler.removeMessages(NetworkStateTracker.
614 EVENT_RESTORE_DEFAULT_NETWORK, radio);
615 radio.reconnect();
616 }
617 return 1;
618 } else {
619 return tracker.stopUsingNetworkFeature(feature, pid, uid);
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
623 /**
624 * Ensure that a network route exists to deliver traffic to the specified
625 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700626 * @param networkType the type of the network over which traffic to the
627 * specified host is to be routed
628 * @param hostAddress the IP address of the host to which the route is
629 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 * @return {@code true} on success, {@code false} on failure
631 */
632 public boolean requestRouteToHost(int networkType, int hostAddress) {
633 enforceChangePermission();
634 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
635 return false;
636 }
637 NetworkStateTracker tracker = mNetTrackers[networkType];
638 /*
639 * If there's only one connected network, and it's the one requested,
640 * then we don't have to do anything - the requested route already
641 * exists. If it's not the requested network, then it's not possible
642 * to establish the requested route. Finally, if there is more than
643 * one connected network, then we must insert an entry in the routing
644 * table.
645 */
646 if (getNumConnectedNetworks() > 1) {
647 return tracker.requestRouteToHost(hostAddress);
648 } else {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700649 return (mNetAttributes[networkType].isDefault() &&
650 tracker.getNetworkInfo().isConnected());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
652 }
653
654 /**
655 * @see ConnectivityManager#getBackgroundDataSetting()
656 */
657 public boolean getBackgroundDataSetting() {
658 return Settings.Secure.getInt(mContext.getContentResolver(),
659 Settings.Secure.BACKGROUND_DATA, 1) == 1;
660 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 /**
663 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
664 */
665 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
666 mContext.enforceCallingOrSelfPermission(
667 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
668 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
671
672 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700673 Settings.Secure.BACKGROUND_DATA,
674 allowBackgroundDataUsage ? 1 : 0);
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 Intent broadcast = new Intent(
677 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
678 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700679 }
680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 private int getNumConnectedNetworks() {
682 int numConnectedNets = 0;
683
684 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700685 if (nt.getNetworkInfo().isConnected() &&
686 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 ++numConnectedNets;
688 }
689 }
Robert Greenwalta64bf832009-08-19 20:19:33 -0700690 if (DBG) Log.d(TAG, "numConnectedNets returning "+numConnectedNets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 return numConnectedNets;
692 }
693
694 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700695 mContext.enforceCallingOrSelfPermission(
696 android.Manifest.permission.ACCESS_NETWORK_STATE,
697 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
699
700 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700701 mContext.enforceCallingOrSelfPermission(
702 android.Manifest.permission.CHANGE_NETWORK_STATE,
703 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
706 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700707 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
708 * network, we ignore it. If it is for the active network, we send out a
709 * broadcast. But first, we check whether it might be possible to connect
710 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 * @param info the {@code NetworkInfo} for the network
712 */
713 private void handleDisconnect(NetworkInfo info) {
714
715 if (DBG) Log.v(TAG, "Handle DISCONNECT for " + info.getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -0700716 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
Robert Greenwalt42acef32009-08-12 16:08:25 -0700718 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 /*
720 * If the disconnected network is not the active one, then don't report
721 * this as a loss of connectivity. What probably happened is that we're
722 * getting the disconnect for a network that we explicitly disabled
723 * in accordance with network preference policies.
724 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725 if (!mNetAttributes[prevNetType].isDefault()) {
726 List pids = mNetRequestersPids[prevNetType];
727 for (int i = 0; i<pids.size(); i++) {
728 Integer pid = (Integer)pids.get(i);
729 // will remove them because the net's no longer connected
730 // need to do this now as only now do we know the pids and
731 // can properly null things that are no longer referenced.
732 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
737 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
738 if (info.isFailover()) {
739 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
740 info.setFailover(false);
741 }
742 if (info.getReason() != null) {
743 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
744 }
745 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700746 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
747 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700749
750 /*
751 * If this is a default network, check if other defaults are available
752 * or active
753 */
754 NetworkStateTracker newNet = null;
755 if (mNetAttributes[prevNetType].isDefault()) {
756 if (DBG) Log.d(TAG, "disconnecting a default network");
757 if (mActiveDefaultNetwork == prevNetType) {
758 mActiveDefaultNetwork = -1;
759 }
760
761 int newType = -1;
762 int newPriority = -1;
763 for (int checkType=0; checkType <=
764 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
765 if (checkType == prevNetType) {
766 continue;
767 }
768 if (mNetAttributes[checkType].isDefault()) {
769 /* TODO - if we have multiple nets we could use
770 * we may want to put more thought into which we choose
771 */
772 if (checkType == mNetworkPreference) {
773 newType = checkType;
774 break;
775 }
776 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
777 mPriority > newPriority) {
778 newType = checkType;
779 newPriority = mRadioAttributes[mNetAttributes[newType].
780 mRadio].mPriority;
781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 }
783 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700784
785 if (newType != -1) {
786 newNet = mNetTrackers[newType];
787 /**
788 * See if the other network is available to fail over to.
789 * If is not available, we enable it anyway, so that it
790 * will be able to connect when it does become available,
791 * but we report a total loss of connectivity rather than
792 * report that we are attempting to fail over.
793 */
794 if (newNet.isAvailable()) {
795 NetworkInfo switchTo = newNet.getNetworkInfo();
796 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700797 if (!switchTo.isConnectedOrConnecting() ||
798 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700799 newNet.reconnect();
800 }
801 if (DBG) {
802 if (switchTo.isConnected()) {
803 Log.v(TAG, "Switching to already connected " +
804 switchTo.getTypeName());
805 } else {
806 Log.v(TAG, "Attempting to switch to " +
807 switchTo.getTypeName());
808 }
809 }
810 intent.putExtra(ConnectivityManager.
811 EXTRA_OTHER_NETWORK_INFO, switchTo);
812 } else {
813 newNet.reconnect();
814 }
815 } else {
816 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
817 true);
818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700820
821 // do this before we broadcast the change
822 handleConnectivityChange();
823
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700824 if (DBG) Log.v(TAG, "Sending DISCONNECT bcast for " +
825 info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700826 (newNet == null || !newNet.isAvailable() ? "" : " other=" +
827 newNet.getNetworkInfo().getTypeName()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
Mike Lockwood0f79b542009-08-14 14:18:49 -0400829 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 /*
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700831 * If the failover network is already connected, then immediately send
832 * out a followup broadcast indicating successful failover
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700834 if (newNet != null && newNet.getNetworkInfo().isConnected())
835 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837
838 private void sendConnectedBroadcast(NetworkInfo info) {
839 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
840 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
841 if (info.isFailover()) {
842 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
843 info.setFailover(false);
844 }
845 if (info.getReason() != null) {
846 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
847 }
848 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700849 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
850 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400852 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
854
855 /**
856 * Called when an attempt to fail over to another network has failed.
857 * @param info the {@link NetworkInfo} for the failed network
858 */
859 private void handleConnectionFailure(NetworkInfo info) {
860 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861
Robert Greenwalt42acef32009-08-12 16:08:25 -0700862 String reason = info.getReason();
863 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700864
Robert Greenwalt42acef32009-08-12 16:08:25 -0700865 if (DBG) {
866 String reasonText;
867 if (reason == null) {
868 reasonText = ".";
869 } else {
870 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700872 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
873 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700875
876 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
877 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
878 if (getActiveNetworkInfo() == null) {
879 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
880 }
881 if (reason != null) {
882 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
883 }
884 if (extraInfo != null) {
885 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
886 }
887 if (info.isFailover()) {
888 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
889 info.setFailover(false);
890 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400891 sendStickyBroadcast(intent);
892 }
893
894 private void sendStickyBroadcast(Intent intent) {
895 synchronized(this) {
896 if (mSystemReady) {
897 mContext.sendStickyBroadcast(intent);
898 } else {
899 if (mDeferredBroadcasts == null) {
900 mDeferredBroadcasts = new ArrayList<Intent>();
901 }
902 mDeferredBroadcasts.add(intent);
903 }
904 }
905 }
906
907 void systemReady() {
908 synchronized(this) {
909 mSystemReady = true;
910 if (mDeferredBroadcasts != null) {
911 int count = mDeferredBroadcasts.size();
912 for (int i = 0; i < count; i++) {
913 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
914 }
915 mDeferredBroadcasts = null;
916 }
917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919
920 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700921 if (DBG) Log.d(TAG, "Handle CONNECT for " + info.getTypeName());
922
923 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924
925 // snapshot isFailover, because sendConnectedBroadcast() resets it
926 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700927 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928
Robert Greenwalt42acef32009-08-12 16:08:25 -0700929 // if this is a default net and other default is running
930 // kill the one not preferred
931 if (mNetAttributes[type].isDefault()) {
932 if (DBG) Log.d(TAG, "connecting to a default network");
933 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
934 if ((type != mNetworkPreference &&
935 mNetAttributes[mActiveDefaultNetwork].mPriority >
936 mNetAttributes[type].mPriority) ||
937 mNetworkPreference == mActiveDefaultNetwork) {
938 // don't accept this one
939 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
940 "to torn down network " + info.getTypeName());
941 teardown(thisNet);
942 return;
943 } else {
944 // tear down the other
945 NetworkStateTracker otherNet =
946 mNetTrackers[mActiveDefaultNetwork];
947 if (DBG) Log.v(TAG, "Policy requires " +
948 otherNet.getNetworkInfo().getTypeName() +
949 " teardown");
950 if (!teardown(otherNet)) {
951 Log.e(TAG, "Network declined teardown request");
952 return;
953 }
954 if (isFailover) {
955 otherNet.releaseWakeLock();
956 }
957 }
958 }
959 mActiveDefaultNetwork = type;
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700962 if (DBG) Log.d(TAG, "Sending CONNECT bcast for " + info.getTypeName());
963 thisNet.updateNetworkSettings();
964 handleConnectivityChange();
965 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
967
968 private void handleScanResultsAvailable(NetworkInfo info) {
969 int networkType = info.getType();
970 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700971 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
972 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 mNetTrackers[networkType].interpretScanResultsAvailable();
976 }
977
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700978 private void handleNotificationChange(boolean visible, int id,
979 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 NotificationManager notificationManager = (NotificationManager) mContext
981 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 if (visible) {
984 notificationManager.notify(id, notification);
985 } else {
986 notificationManager.cancel(id);
987 }
988 }
989
990 /**
991 * After any kind of change in the connectivity state of any network,
992 * make sure that anything that depends on the connectivity state of
993 * more than one network is set up correctly. We're mainly concerned
994 * with making sure that the list of DNS servers is set up according
995 * to which networks are connected, and ensuring that the right routing
996 * table entries exist.
997 */
998 private void handleConnectivityChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700999 if (DBG) Log.d(TAG, "handleConnectivityChange");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001001 * If a non-default network is enabled, add the host routes that
1002 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * If both mobile and wifi are enabled, add the host routes that
1004 * will allow MMS traffic to pass on the mobile network. But
1005 * remove the default route for the mobile network, so that there
1006 * will be only one default route, to ensure that all traffic
1007 * except MMS will travel via Wi-Fi.
1008 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001009 handleDnsConfigurationChange();
1010
1011 for (int netType : mPriorityList) {
1012 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1013 if (mNetAttributes[netType].isDefault()) {
1014 mNetTrackers[netType].addDefaultRoute();
1015 } else {
1016 mNetTrackers[netType].addPrivateDnsRoutes();
1017 }
1018 } else {
1019 if (mNetAttributes[netType].isDefault()) {
1020 mNetTrackers[netType].removeDefaultRoute();
1021 } else {
1022 mNetTrackers[netType].removePrivateDnsRoutes();
1023 }
1024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 }
1027
Robert Greenwalt42acef32009-08-12 16:08:25 -07001028 /**
1029 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1030 * on the highest priority active net which this process requested.
1031 * If there aren't any, clear it out
1032 */
1033 private void reassessPidDns(int myPid, boolean doBump)
1034 {
1035 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1036 for(int i : mPriorityList) {
1037 if (mNetAttributes[i].isDefault()) {
1038 continue;
1039 }
1040 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001041 if (nt.getNetworkInfo().isConnected() &&
1042 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001043 List pids = mNetRequestersPids[i];
1044 for (int j=0; j<pids.size(); j++) {
1045 Integer pid = (Integer)pids.get(j);
1046 if (pid.intValue() == myPid) {
1047 String[] dnsList = nt.getNameServers();
1048 writePidDns(dnsList, myPid);
1049 if (doBump) {
1050 bumpDns();
1051 }
1052 return;
1053 }
1054 }
1055 }
1056 }
1057 // nothing found - delete
1058 for (int i = 1; ; i++) {
1059 String prop = "net.dns" + i + "." + myPid;
1060 if (SystemProperties.get(prop).length() == 0) {
1061 if (doBump) {
1062 bumpDns();
1063 }
1064 return;
1065 }
1066 SystemProperties.set(prop, "");
1067 }
1068 }
1069
1070 private void writePidDns(String[] dnsList, int pid) {
1071 int j = 1;
1072 for (String dns : dnsList) {
1073 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1074 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1075 }
1076 }
1077 }
1078
1079 private void bumpDns() {
1080 /*
1081 * Bump the property that tells the name resolver library to reread
1082 * the DNS server list from the properties.
1083 */
1084 String propVal = SystemProperties.get("net.dnschange");
1085 int n = 0;
1086 if (propVal.length() != 0) {
1087 try {
1088 n = Integer.parseInt(propVal);
1089 } catch (NumberFormatException e) {}
1090 }
1091 SystemProperties.set("net.dnschange", "" + (n+1));
1092 }
1093
1094 private void handleDnsConfigurationChange() {
1095 if (DBG) Log.d(TAG, "handleDnsConfig Change");
1096 // add default net's dns entries
1097 for (int x = mPriorityList.length-1; x>= 0; x--) {
1098 int netType = mPriorityList[x];
1099 NetworkStateTracker nt = mNetTrackers[netType];
1100 if (DBG) Log.d(TAG, " checking " + nt);
1101 if (nt != null && nt.getNetworkInfo().isConnected() &&
1102 !nt.isTeardownRequested()) {
1103 if (DBG) Log.d(TAG, " connected");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001105 if (mNetAttributes[netType].isDefault()) {
1106 int j = 1;
1107 for (String dns : dnsList) {
1108 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1109 SystemProperties.set("net.dns" + j++, dns);
1110 }
1111 }
1112 for (int k=j ; k<mNumDnsEntries; k++) {
1113 SystemProperties.set("net.dns" + j, "");
1114 }
1115 mNumDnsEntries = j;
1116 } else {
1117 // set per-pid dns for attached secondary nets
1118 List pids = mNetRequestersPids[netType];
1119 for (int y=0; y< pids.size(); y++) {
1120 Integer pid = (Integer)pids.get(y);
1121 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 }
1124 }
1125 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001126
1127 bumpDns();
1128 }
1129
1130 private int getRestoreDefaultNetworkDelay() {
1131 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1132 NETWORK_RESTORE_DELAY_PROP_NAME);
1133 if(restoreDefaultNetworkDelayStr != null &&
1134 restoreDefaultNetworkDelayStr.length() != 0) {
1135 try {
1136 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1137 } catch (NumberFormatException e) {
1138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001140 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142
1143 @Override
1144 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001145 if (mContext.checkCallingOrSelfPermission(
1146 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001148 pw.println("Permission Denial: can't dump ConnectivityService " +
1149 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1150 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 return;
1152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 pw.println();
1154 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001155 if (nst.getNetworkInfo().isConnected()) {
1156 pw.println("Active network: " + nst.getNetworkInfo().
1157 getTypeName());
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 pw.println(nst.getNetworkInfo());
1160 pw.println(nst);
1161 pw.println();
1162 }
1163 }
1164
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 private class MyHandler extends Handler {
1167 @Override
1168 public void handleMessage(Message msg) {
1169 NetworkInfo info;
1170 switch (msg.what) {
1171 case NetworkStateTracker.EVENT_STATE_CHANGED:
1172 info = (NetworkInfo) msg.obj;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001174 info.getTypeName() + ": " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 info.getState() + "/" + info.getDetailedState());
1176
1177 // Connectivity state changed:
1178 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001179 // [12-9] Network subtype (for mobile network, as defined
1180 // by TelephonyManager)
1181 // [8-3] Detailed state ordinal (as defined by
1182 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 // [2-0] Network type (as defined by ConnectivityManager)
1184 int eventLogParam = (info.getType() & 0x7) |
1185 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1186 (info.getSubtype() << 9);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001187 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1188 eventLogParam);
1189
1190 if (info.getDetailedState() ==
1191 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 handleConnectionFailure(info);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001193 } else if (info.getState() ==
1194 NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 handleDisconnect(info);
1196 } else if (info.getState() == NetworkInfo.State.SUSPENDED) {
1197 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001198 // the logic here is, handle SUSPENDED the same as
1199 // DISCONNECTED. The only difference being we are
1200 // broadcasting an intent with NetworkInfo that's
1201 // suspended. This allows the applications an
1202 // opportunity to handle DISCONNECTED and SUSPENDED
1203 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 handleDisconnect(info);
1205 } else if (info.getState() == NetworkInfo.State.CONNECTED) {
1206 handleConnect(info);
1207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 break;
1209
1210 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1211 info = (NetworkInfo) msg.obj;
1212 handleScanResultsAvailable(info);
1213 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001216 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1217 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218
1219 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001220 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 break;
1222
1223 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1224 // fill me in
1225 break;
1226
1227 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1228 // fill me in
1229 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001230 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
1231 for (NetworkStateTracker net : mNetTrackers) {
1232 NetworkInfo i = net.getNetworkInfo();
1233 if (i.isConnected() &&
1234 !mNetAttributes[i.getType()].isDefault()) {
1235 teardown(net);
1236 }
1237 }
1238 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240 }
1241 }
1242}