blob: 9c64709c90f69c226c33f434d0889143d063e7e1 [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()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700360 if (DBG) {
361 Log.d(TAG, "tearing down " +
362 mNetTrackers[t].getNetworkInfo() +
363 " in enforcePreference");
364 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700365 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 }
367 }
368 }
369
370 private boolean teardown(NetworkStateTracker netTracker) {
371 if (netTracker.teardown()) {
372 netTracker.setTeardownRequested(true);
373 return true;
374 } else {
375 return false;
376 }
377 }
378
379 /**
380 * Return NetworkInfo for the active (i.e., connected) network interface.
381 * It is assumed that at most one network is active at a time. If more
382 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700383 * @return the info for the active network, or {@code null} if none is
384 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 */
386 public NetworkInfo getActiveNetworkInfo() {
387 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700388 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
389 if (!mNetAttributes[type].isDefault()) {
390 continue;
391 }
392 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 NetworkInfo info = t.getNetworkInfo();
394 if (info.isConnected()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700395 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
396 "connected default network is not " +
397 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 return info;
399 }
400 }
401 return null;
402 }
403
404 public NetworkInfo getNetworkInfo(int networkType) {
405 enforceAccessPermission();
406 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
407 NetworkStateTracker t = mNetTrackers[networkType];
408 if (t != null)
409 return t.getNetworkInfo();
410 }
411 return null;
412 }
413
414 public NetworkInfo[] getAllNetworkInfo() {
415 enforceAccessPermission();
416 NetworkInfo[] result = new NetworkInfo[mNetTrackers.length];
417 int i = 0;
418 for (NetworkStateTracker t : mNetTrackers) {
419 result[i++] = t.getNetworkInfo();
420 }
421 return result;
422 }
423
424 public boolean setRadios(boolean turnOn) {
425 boolean result = true;
426 enforceChangePermission();
427 for (NetworkStateTracker t : mNetTrackers) {
428 result = t.setRadio(turnOn) && result;
429 }
430 return result;
431 }
432
433 public boolean setRadio(int netType, boolean turnOn) {
434 enforceChangePermission();
435 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
436 return false;
437 }
438 NetworkStateTracker tracker = mNetTrackers[netType];
439 return tracker != null && tracker.setRadio(turnOn);
440 }
441
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700442 /**
443 * Used to notice when the calling process dies so we can self-expire
444 *
445 * Also used to know if the process has cleaned up after itself when
446 * our auto-expire timer goes off. The timer has a link to an object.
447 *
448 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700449 private class FeatureUser implements IBinder.DeathRecipient {
450 int mNetworkType;
451 String mFeature;
452 IBinder mBinder;
453 int mPid;
454 int mUid;
455
456 FeatureUser(int type, String feature, IBinder binder) {
457 super();
458 mNetworkType = type;
459 mFeature = feature;
460 mBinder = binder;
461 mPid = getCallingPid();
462 mUid = getCallingUid();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700463
Robert Greenwalt42acef32009-08-12 16:08:25 -0700464 try {
465 mBinder.linkToDeath(this, 0);
466 } catch (RemoteException e) {
467 binderDied();
468 }
469 }
470
471 void unlinkDeathRecipient() {
472 mBinder.unlinkToDeath(this, 0);
473 }
474
475 public void binderDied() {
476 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
477 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700478 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700479 }
480
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700481 public void expire() {
482 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
483 mNetworkType + ", " + mFeature + ", " + mBinder);
484 stopUsingNetworkFeature(this, false);
485 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700486 }
487
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700488 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700489 public int startUsingNetworkFeature(int networkType, String feature,
490 IBinder binder) {
491 if (DBG) {
492 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
493 ": " + feature);
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 enforceChangePermission();
496 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700500 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501
502 // TODO - move this into the MobileDataStateTracker
503 int usedNetworkType = networkType;
504 if(networkType == ConnectivityManager.TYPE_MOBILE) {
505 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
506 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
507 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
508 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
509 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
510 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
511 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
512 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
513 }
514 }
515 NetworkStateTracker network = mNetTrackers[usedNetworkType];
516 if (network != null) {
517 if (usedNetworkType != networkType) {
518 Integer currentPid = new Integer(getCallingPid());
519
520 NetworkStateTracker radio = mNetTrackers[networkType];
521 NetworkInfo ni = network.getNetworkInfo();
522
523 if (ni.isAvailable() == false) {
524 if (DBG) Log.d(TAG, "special network not available");
525 return Phone.APN_TYPE_NOT_AVAILABLE;
526 }
527
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700528 synchronized(this) {
529 mFeatureUsers.add(f);
530 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
531 // this gets used for per-pid dns when connected
532 mNetRequestersPids[usedNetworkType].add(currentPid);
533 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700535 mHandler.sendMessageDelayed(mHandler.obtainMessage(
536 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
537 f), getRestoreDefaultNetworkDelay());
538
Robert Greenwalt42acef32009-08-12 16:08:25 -0700539
Robert Greenwalta64bf832009-08-19 20:19:33 -0700540 if ((ni.isConnectedOrConnecting() == true) &&
541 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700542 if (ni.isConnected() == true) {
543 // add the pid-specific dns
544 handleDnsConfigurationChange();
545 if (DBG) Log.d(TAG, "special network already active");
546 return Phone.APN_ALREADY_ACTIVE;
547 }
548 if (DBG) Log.d(TAG, "special network already connecting");
549 return Phone.APN_REQUEST_STARTED;
550 }
551
552 // check if the radio in play can make another contact
553 // assume if cannot for now
554
Robert Greenwalt42acef32009-08-12 16:08:25 -0700555 if (DBG) Log.d(TAG, "reconnecting to special network");
556 network.reconnect();
557 return Phone.APN_REQUEST_STARTED;
558 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700559 synchronized(this) {
560 mFeatureUsers.add(f);
561 }
562 mHandler.sendMessageDelayed(mHandler.obtainMessage(
563 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
564 f), getRestoreDefaultNetworkDelay());
565
Robert Greenwalt42acef32009-08-12 16:08:25 -0700566 return network.startUsingNetworkFeature(feature,
567 getCallingPid(), getCallingUid());
568 }
569 }
570 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700573 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700575 int pid = getCallingPid();
576 int uid = getCallingUid();
577
578 FeatureUser u = null;
579 boolean found = false;
580
581 synchronized(this) {
582 for (int i = 0; i < mFeatureUsers.size() ; i++) {
583 u = (FeatureUser)mFeatureUsers.get(i);
584 if (uid == u.mUid && pid == u.mPid &&
585 networkType == u.mNetworkType &&
586 TextUtils.equals(feature, u.mFeature)) {
587 found = true;
588 break;
589 }
590 }
591 }
592 if (found && u != null) {
593 // stop regardless of how many other time this proc had called start
594 return stopUsingNetworkFeature(u, true);
595 } else {
596 // none found!
597 return 1;
598 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700599 }
600
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700601 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
602 int networkType = u.mNetworkType;
603 String feature = u.mFeature;
604 int pid = u.mPid;
605 int uid = u.mUid;
606
607 NetworkStateTracker tracker = null;
608 boolean callTeardown = false; // used to carry our decision outside of sync block
609
Robert Greenwalt42acef32009-08-12 16:08:25 -0700610 if (DBG) {
611 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
612 ": " + feature);
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 enforceChangePermission();
615 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
616 return -1;
617 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700618
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700619 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
620 // sync block
621 synchronized(this) {
622 // check if this process still has an outstanding start request
623 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700624 return 1;
625 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700626 u.unlinkDeathRecipient();
627 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
628 // If we care about duplicate requests, check for that here.
629 //
630 // This is done to support the extension of a request - the app
631 // can request we start the network feature again and renew the
632 // auto-shutoff delay. Normal "stop" calls from the app though
633 // do not pay attention to duplicate requests - in effect the
634 // API does not refcount and a single stop will counter multiple starts.
635 if (ignoreDups == false) {
636 for (int i = 0; i < mFeatureUsers.size() ; i++) {
637 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
638 if (x.mUid == u.mUid && x.mPid == u.mPid &&
639 x.mNetworkType == u.mNetworkType &&
640 TextUtils.equals(x.mFeature, u.mFeature)) {
641 return 1;
642 }
643 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700644 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700645
646 // TODO - move to MobileDataStateTracker
647 int usedNetworkType = networkType;
648 if (networkType == ConnectivityManager.TYPE_MOBILE) {
649 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
650 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
651 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
652 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
653 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
654 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
655 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
656 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
657 }
658 }
659 tracker = mNetTrackers[usedNetworkType];
660 if(usedNetworkType != networkType) {
661 Integer currentPid = new Integer(pid);
662 reassessPidDns(pid, true);
663 mNetRequestersPids[usedNetworkType].remove(currentPid);
664 if (mNetRequestersPids[usedNetworkType].size() != 0) {
665 if (DBG) Log.d(TAG, "not tearing down special network - " +
666 "others still using it");
667 return 1;
668 }
669 callTeardown = true;
670 }
671 }
672
673 if (callTeardown) {
674 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700675 return 1;
676 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700677 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700678 return tracker.stopUsingNetworkFeature(feature, pid, uid);
679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 }
681
682 /**
683 * Ensure that a network route exists to deliver traffic to the specified
684 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700685 * @param networkType the type of the network over which traffic to the
686 * specified host is to be routed
687 * @param hostAddress the IP address of the host to which the route is
688 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 * @return {@code true} on success, {@code false} on failure
690 */
691 public boolean requestRouteToHost(int networkType, int hostAddress) {
692 enforceChangePermission();
693 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
694 return false;
695 }
696 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700697
698 if (!tracker.getNetworkInfo().isConnected() || tracker.isTeardownRequested()) {
699 if (DBG) {
700 Log.d(TAG, "requestRouteToHost on down network (" + networkType + " - dropped");
701 }
702 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700704 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706
707 /**
708 * @see ConnectivityManager#getBackgroundDataSetting()
709 */
710 public boolean getBackgroundDataSetting() {
711 return Settings.Secure.getInt(mContext.getContentResolver(),
712 Settings.Secure.BACKGROUND_DATA, 1) == 1;
713 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 /**
716 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
717 */
718 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
719 mContext.enforceCallingOrSelfPermission(
720 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
721 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
724
725 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700726 Settings.Secure.BACKGROUND_DATA,
727 allowBackgroundDataUsage ? 1 : 0);
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 Intent broadcast = new Intent(
730 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
731 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700732 }
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 private int getNumConnectedNetworks() {
735 int numConnectedNets = 0;
736
737 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700738 if (nt.getNetworkInfo().isConnected() &&
739 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 ++numConnectedNets;
741 }
742 }
Robert Greenwalta64bf832009-08-19 20:19:33 -0700743 if (DBG) Log.d(TAG, "numConnectedNets returning "+numConnectedNets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 return numConnectedNets;
745 }
746
747 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700748 mContext.enforceCallingOrSelfPermission(
749 android.Manifest.permission.ACCESS_NETWORK_STATE,
750 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
753 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700754 mContext.enforceCallingOrSelfPermission(
755 android.Manifest.permission.CHANGE_NETWORK_STATE,
756 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
759 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700760 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
761 * network, we ignore it. If it is for the active network, we send out a
762 * broadcast. But first, we check whether it might be possible to connect
763 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 * @param info the {@code NetworkInfo} for the network
765 */
766 private void handleDisconnect(NetworkInfo info) {
767
Robert Greenwalt42acef32009-08-12 16:08:25 -0700768 int prevNetType = info.getType();
Robert Greenwalt1ef95f92009-09-30 21:01:30 -0700769 if (DBG) {
770 Log.v(TAG, "Handle DISCONNECT for " + info.getTypeName() +
771 (mNetAttributes[prevNetType].isDefault() ? ", a default network" : ""));
772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773
Robert Greenwalt42acef32009-08-12 16:08:25 -0700774 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 /*
776 * If the disconnected network is not the active one, then don't report
777 * this as a loss of connectivity. What probably happened is that we're
778 * getting the disconnect for a network that we explicitly disabled
779 * in accordance with network preference policies.
780 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700781 if (!mNetAttributes[prevNetType].isDefault()) {
782 List pids = mNetRequestersPids[prevNetType];
783 for (int i = 0; i<pids.size(); i++) {
784 Integer pid = (Integer)pids.get(i);
785 // will remove them because the net's no longer connected
786 // need to do this now as only now do we know the pids and
787 // can properly null things that are no longer referenced.
788 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
793 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
794 if (info.isFailover()) {
795 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
796 info.setFailover(false);
797 }
798 if (info.getReason() != null) {
799 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
800 }
801 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700802 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
803 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700805
806 /*
807 * If this is a default network, check if other defaults are available
808 * or active
809 */
810 NetworkStateTracker newNet = null;
811 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700812 if (mActiveDefaultNetwork == prevNetType) {
813 mActiveDefaultNetwork = -1;
814 }
815
816 int newType = -1;
817 int newPriority = -1;
818 for (int checkType=0; checkType <=
819 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
820 if (checkType == prevNetType) {
821 continue;
822 }
823 if (mNetAttributes[checkType].isDefault()) {
824 /* TODO - if we have multiple nets we could use
825 * we may want to put more thought into which we choose
826 */
827 if (checkType == mNetworkPreference) {
828 newType = checkType;
829 break;
830 }
831 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
832 mPriority > newPriority) {
833 newType = checkType;
834 newPriority = mRadioAttributes[mNetAttributes[newType].
835 mRadio].mPriority;
836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700839
840 if (newType != -1) {
841 newNet = mNetTrackers[newType];
842 /**
843 * See if the other network is available to fail over to.
844 * If is not available, we enable it anyway, so that it
845 * will be able to connect when it does become available,
846 * but we report a total loss of connectivity rather than
847 * report that we are attempting to fail over.
848 */
849 if (newNet.isAvailable()) {
850 NetworkInfo switchTo = newNet.getNetworkInfo();
851 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700852 if (!switchTo.isConnectedOrConnecting() ||
853 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700854 newNet.reconnect();
855 }
856 if (DBG) {
857 if (switchTo.isConnected()) {
858 Log.v(TAG, "Switching to already connected " +
859 switchTo.getTypeName());
860 } else {
861 Log.v(TAG, "Attempting to switch to " +
862 switchTo.getTypeName());
863 }
864 }
865 intent.putExtra(ConnectivityManager.
866 EXTRA_OTHER_NETWORK_INFO, switchTo);
867 } else {
Robert Greenwaltc7d25302009-09-17 14:58:16 -0700868 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
869 true);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700870 newNet.reconnect();
871 }
872 } else {
873 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
874 true);
875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700877
878 // do this before we broadcast the change
879 handleConnectivityChange();
880
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700881 if (DBG) Log.v(TAG, "Sending DISCONNECT bcast for " +
882 info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700883 (newNet == null || !newNet.isAvailable() ? "" : " other=" +
884 newNet.getNetworkInfo().getTypeName()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885
Mike Lockwood0f79b542009-08-14 14:18:49 -0400886 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 /*
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700888 * If the failover network is already connected, then immediately send
889 * out a followup broadcast indicating successful failover
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700891 if (newNet != null && newNet.getNetworkInfo().isConnected())
892 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 private void sendConnectedBroadcast(NetworkInfo info) {
896 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
897 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
898 if (info.isFailover()) {
899 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
900 info.setFailover(false);
901 }
902 if (info.getReason() != null) {
903 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
904 }
905 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700906 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
907 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400909 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
912 /**
913 * Called when an attempt to fail over to another network has failed.
914 * @param info the {@link NetworkInfo} for the failed network
915 */
916 private void handleConnectionFailure(NetworkInfo info) {
917 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
Robert Greenwalt42acef32009-08-12 16:08:25 -0700919 String reason = info.getReason();
920 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700921
Robert Greenwalt42acef32009-08-12 16:08:25 -0700922 if (DBG) {
923 String reasonText;
924 if (reason == null) {
925 reasonText = ".";
926 } else {
927 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700929 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
930 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700932
933 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
934 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
935 if (getActiveNetworkInfo() == null) {
936 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
937 }
938 if (reason != null) {
939 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
940 }
941 if (extraInfo != null) {
942 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
943 }
944 if (info.isFailover()) {
945 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
946 info.setFailover(false);
947 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400948 sendStickyBroadcast(intent);
949 }
950
951 private void sendStickyBroadcast(Intent intent) {
952 synchronized(this) {
953 if (mSystemReady) {
954 mContext.sendStickyBroadcast(intent);
955 } else {
956 if (mDeferredBroadcasts == null) {
957 mDeferredBroadcasts = new ArrayList<Intent>();
958 }
959 mDeferredBroadcasts.add(intent);
960 }
961 }
962 }
963
964 void systemReady() {
965 synchronized(this) {
966 mSystemReady = true;
967 if (mDeferredBroadcasts != null) {
968 int count = mDeferredBroadcasts.size();
969 for (int i = 0; i < count; i++) {
970 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
971 }
972 mDeferredBroadcasts = null;
973 }
974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976
977 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700978 if (DBG) Log.d(TAG, "Handle CONNECT for " + info.getTypeName());
979
980 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981
982 // snapshot isFailover, because sendConnectedBroadcast() resets it
983 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700984 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
Robert Greenwalt42acef32009-08-12 16:08:25 -0700986 // if this is a default net and other default is running
987 // kill the one not preferred
988 if (mNetAttributes[type].isDefault()) {
989 if (DBG) Log.d(TAG, "connecting to a default network");
990 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
991 if ((type != mNetworkPreference &&
992 mNetAttributes[mActiveDefaultNetwork].mPriority >
993 mNetAttributes[type].mPriority) ||
994 mNetworkPreference == mActiveDefaultNetwork) {
995 // don't accept this one
996 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
997 "to torn down network " + info.getTypeName());
998 teardown(thisNet);
999 return;
1000 } else {
1001 // tear down the other
1002 NetworkStateTracker otherNet =
1003 mNetTrackers[mActiveDefaultNetwork];
1004 if (DBG) Log.v(TAG, "Policy requires " +
1005 otherNet.getNetworkInfo().getTypeName() +
1006 " teardown");
1007 if (!teardown(otherNet)) {
1008 Log.e(TAG, "Network declined teardown request");
1009 return;
1010 }
1011 if (isFailover) {
1012 otherNet.releaseWakeLock();
1013 }
1014 }
1015 }
1016 mActiveDefaultNetwork = type;
1017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001019 if (DBG) Log.d(TAG, "Sending CONNECT bcast for " + info.getTypeName());
1020 thisNet.updateNetworkSettings();
1021 handleConnectivityChange();
1022 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024
1025 private void handleScanResultsAvailable(NetworkInfo info) {
1026 int networkType = info.getType();
1027 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001028 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1029 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 mNetTrackers[networkType].interpretScanResultsAvailable();
1033 }
1034
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001035 private void handleNotificationChange(boolean visible, int id,
1036 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 NotificationManager notificationManager = (NotificationManager) mContext
1038 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 if (visible) {
1041 notificationManager.notify(id, notification);
1042 } else {
1043 notificationManager.cancel(id);
1044 }
1045 }
1046
1047 /**
1048 * After any kind of change in the connectivity state of any network,
1049 * make sure that anything that depends on the connectivity state of
1050 * more than one network is set up correctly. We're mainly concerned
1051 * with making sure that the list of DNS servers is set up according
1052 * to which networks are connected, and ensuring that the right routing
1053 * table entries exist.
1054 */
1055 private void handleConnectivityChange() {
1056 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001057 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001058 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 * If both mobile and wifi are enabled, add the host routes that
1060 * will allow MMS traffic to pass on the mobile network. But
1061 * remove the default route for the mobile network, so that there
1062 * will be only one default route, to ensure that all traffic
1063 * except MMS will travel via Wi-Fi.
1064 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001065 handleDnsConfigurationChange();
1066
1067 for (int netType : mPriorityList) {
1068 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1069 if (mNetAttributes[netType].isDefault()) {
1070 mNetTrackers[netType].addDefaultRoute();
1071 } else {
1072 mNetTrackers[netType].addPrivateDnsRoutes();
1073 }
1074 } else {
1075 if (mNetAttributes[netType].isDefault()) {
1076 mNetTrackers[netType].removeDefaultRoute();
1077 } else {
1078 mNetTrackers[netType].removePrivateDnsRoutes();
1079 }
1080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 }
1083
Robert Greenwalt42acef32009-08-12 16:08:25 -07001084 /**
1085 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1086 * on the highest priority active net which this process requested.
1087 * If there aren't any, clear it out
1088 */
1089 private void reassessPidDns(int myPid, boolean doBump)
1090 {
1091 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1092 for(int i : mPriorityList) {
1093 if (mNetAttributes[i].isDefault()) {
1094 continue;
1095 }
1096 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001097 if (nt.getNetworkInfo().isConnected() &&
1098 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001099 List pids = mNetRequestersPids[i];
1100 for (int j=0; j<pids.size(); j++) {
1101 Integer pid = (Integer)pids.get(j);
1102 if (pid.intValue() == myPid) {
1103 String[] dnsList = nt.getNameServers();
1104 writePidDns(dnsList, myPid);
1105 if (doBump) {
1106 bumpDns();
1107 }
1108 return;
1109 }
1110 }
1111 }
1112 }
1113 // nothing found - delete
1114 for (int i = 1; ; i++) {
1115 String prop = "net.dns" + i + "." + myPid;
1116 if (SystemProperties.get(prop).length() == 0) {
1117 if (doBump) {
1118 bumpDns();
1119 }
1120 return;
1121 }
1122 SystemProperties.set(prop, "");
1123 }
1124 }
1125
1126 private void writePidDns(String[] dnsList, int pid) {
1127 int j = 1;
1128 for (String dns : dnsList) {
1129 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1130 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1131 }
1132 }
1133 }
1134
1135 private void bumpDns() {
1136 /*
1137 * Bump the property that tells the name resolver library to reread
1138 * the DNS server list from the properties.
1139 */
1140 String propVal = SystemProperties.get("net.dnschange");
1141 int n = 0;
1142 if (propVal.length() != 0) {
1143 try {
1144 n = Integer.parseInt(propVal);
1145 } catch (NumberFormatException e) {}
1146 }
1147 SystemProperties.set("net.dnschange", "" + (n+1));
1148 }
1149
1150 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001151 // add default net's dns entries
1152 for (int x = mPriorityList.length-1; x>= 0; x--) {
1153 int netType = mPriorityList[x];
1154 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001155 if (nt != null && nt.getNetworkInfo().isConnected() &&
1156 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001158 if (mNetAttributes[netType].isDefault()) {
1159 int j = 1;
1160 for (String dns : dnsList) {
1161 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001162 if (DBG) {
1163 Log.d(TAG, "adding dns " + dns + " for " +
1164 nt.getNetworkInfo().getTypeName());
1165 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001166 SystemProperties.set("net.dns" + j++, dns);
1167 }
1168 }
1169 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001170 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1171 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 }
1173 mNumDnsEntries = j;
1174 } else {
1175 // set per-pid dns for attached secondary nets
1176 List pids = mNetRequestersPids[netType];
1177 for (int y=0; y< pids.size(); y++) {
1178 Integer pid = (Integer)pids.get(y);
1179 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182 }
1183 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001184
1185 bumpDns();
1186 }
1187
1188 private int getRestoreDefaultNetworkDelay() {
1189 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1190 NETWORK_RESTORE_DELAY_PROP_NAME);
1191 if(restoreDefaultNetworkDelayStr != null &&
1192 restoreDefaultNetworkDelayStr.length() != 0) {
1193 try {
1194 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1195 } catch (NumberFormatException e) {
1196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001198 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200
1201 @Override
1202 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001203 if (mContext.checkCallingOrSelfPermission(
1204 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001206 pw.println("Permission Denial: can't dump ConnectivityService " +
1207 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1208 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 return;
1210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 pw.println();
1212 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001213 if (nst.getNetworkInfo().isConnected()) {
1214 pw.println("Active network: " + nst.getNetworkInfo().
1215 getTypeName());
1216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 pw.println(nst.getNetworkInfo());
1218 pw.println(nst);
1219 pw.println();
1220 }
1221 }
1222
Robert Greenwalt42acef32009-08-12 16:08:25 -07001223 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 private class MyHandler extends Handler {
1225 @Override
1226 public void handleMessage(Message msg) {
1227 NetworkInfo info;
1228 switch (msg.what) {
1229 case NetworkStateTracker.EVENT_STATE_CHANGED:
1230 info = (NetworkInfo) msg.obj;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001231 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001232 info.getTypeName() + ": " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 info.getState() + "/" + info.getDetailedState());
1234
1235 // Connectivity state changed:
1236 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001237 // [12-9] Network subtype (for mobile network, as defined
1238 // by TelephonyManager)
1239 // [8-3] Detailed state ordinal (as defined by
1240 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 // [2-0] Network type (as defined by ConnectivityManager)
1242 int eventLogParam = (info.getType() & 0x7) |
1243 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1244 (info.getSubtype() << 9);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001245 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1246 eventLogParam);
1247
1248 if (info.getDetailedState() ==
1249 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 handleConnectionFailure(info);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001251 } else if (info.getState() ==
1252 NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 handleDisconnect(info);
1254 } else if (info.getState() == NetworkInfo.State.SUSPENDED) {
1255 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001256 // the logic here is, handle SUSPENDED the same as
1257 // DISCONNECTED. The only difference being we are
1258 // broadcasting an intent with NetworkInfo that's
1259 // suspended. This allows the applications an
1260 // opportunity to handle DISCONNECTED and SUSPENDED
1261 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 handleDisconnect(info);
1263 } else if (info.getState() == NetworkInfo.State.CONNECTED) {
1264 handleConnect(info);
1265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 break;
1267
1268 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1269 info = (NetworkInfo) msg.obj;
1270 handleScanResultsAvailable(info);
1271 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001274 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1275 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276
1277 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001278 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 break;
1280
1281 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1282 // fill me in
1283 break;
1284
1285 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1286 // fill me in
1287 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001288 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001289 FeatureUser u = (FeatureUser)msg.obj;
1290 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293 }
1294 }
1295}