blob: 27b631e4929bab3e088cf86653f6fd89c7dcf574 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
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 Greenwalt2034b912009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import java.io.FileDescriptor;
47import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import java.util.ArrayList;
49import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050
51/**
52 * @hide
53 */
54public class ConnectivityService extends IConnectivityManager.Stub {
55
Robert Greenwalta25fd712009-10-06 14:12:53 -070056 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057 private static final String TAG = "ConnectivityService";
58
Robert Greenwalt2034b912009-08-12 16:08:25 -070059 // how long to wait before switching back to a radio's default network
60 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
61 // system property that can override the above value
62 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
63 "android.telephony.apn-restore";
64
The Android Open Source Project28527d22009-03-03 19:31:44 -080065 /**
66 * Sometimes we want to refer to the individual network state
67 * trackers separately, and sometimes we just want to treat them
68 * abstractly.
69 */
70 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070071
72 /**
73 * A per Net list of the PID's that requested access to the net
74 * used both as a refcount and for per-PID DNS selection
75 */
76 private List mNetRequestersPids[];
77
The Android Open Source Project28527d22009-03-03 19:31:44 -080078 private WifiWatchdogService mWifiWatchdogService;
79
Robert Greenwalt2034b912009-08-12 16:08:25 -070080 // priority order of the nettrackers
81 // (excluding dynamically set mNetworkPreference)
82 // TODO - move mNetworkTypePreference into this
83 private int[] mPriorityList;
84
The Android Open Source Project28527d22009-03-03 19:31:44 -080085 private Context mContext;
86 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070087 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080088
89 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080090
91 private boolean mTestMode;
92 private static ConnectivityService sServiceInstance;
93
Robert Greenwalt2034b912009-08-12 16:08:25 -070094 private Handler mHandler;
95
96 // list of DeathRecipients used to make sure features are turned off when
97 // a process dies
98 private List mFeatureUsers;
99
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400100 private boolean mSystemReady;
101 private ArrayList<Intent> mDeferredBroadcasts;
102
Robert Greenwalt12c44552009-12-07 11:33:18 -0800103 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700104 /**
105 * Class for holding settings read from resources.
106 */
107 public String mName;
108 public int mType;
109 public int mRadio;
110 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800111 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700112 public NetworkAttributes(String init) {
113 String fragments[] = init.split(",");
114 mName = fragments[0].toLowerCase();
115 if (fragments[1].toLowerCase().equals("wifi")) {
116 mRadio = ConnectivityManager.TYPE_WIFI;
117 } else {
118 mRadio = ConnectivityManager.TYPE_MOBILE;
119 }
120 if (mName.equals("default")) {
121 mType = mRadio;
122 } else if (mName.equals("mms")) {
123 mType = ConnectivityManager.TYPE_MOBILE_MMS;
124 } else if (mName.equals("supl")) {
125 mType = ConnectivityManager.TYPE_MOBILE_SUPL;
126 } else if (mName.equals("dun")) {
127 mType = ConnectivityManager.TYPE_MOBILE_DUN;
128 } else if (mName.equals("hipri")) {
129 mType = ConnectivityManager.TYPE_MOBILE_HIPRI;
130 }
131 mPriority = Integer.parseInt(fragments[2]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 }
134 public boolean isDefault() {
135 return (mType == mRadio);
136 }
137 }
138 NetworkAttributes[] mNetAttributes;
139
Robert Greenwalt12c44552009-12-07 11:33:18 -0800140 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700141 public String mName;
142 public int mPriority;
143 public int mSimultaneity;
144 public int mType;
145 public RadioAttributes(String init) {
146 String fragments[] = init.split(",");
147 mName = fragments[0].toLowerCase();
148 mPriority = Integer.parseInt(fragments[1]);
149 mSimultaneity = Integer.parseInt(fragments[2]);
150 if (mName.equals("wifi")) {
151 mType = ConnectivityManager.TYPE_WIFI;
152 } else {
153 mType = ConnectivityManager.TYPE_MOBILE;
154 }
155 }
156 }
157 RadioAttributes[] mRadioAttributes;
158
The Android Open Source Project28527d22009-03-03 19:31:44 -0800159 private static class ConnectivityThread extends Thread {
160 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700161
The Android Open Source Project28527d22009-03-03 19:31:44 -0800162 private ConnectivityThread(Context context) {
163 super("ConnectivityThread");
164 mContext = context;
165 }
166
167 @Override
168 public void run() {
169 Looper.prepare();
170 synchronized (this) {
171 sServiceInstance = new ConnectivityService(mContext);
172 notifyAll();
173 }
174 Looper.loop();
175 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176
The Android Open Source Project28527d22009-03-03 19:31:44 -0800177 public static ConnectivityService getServiceInstance(Context context) {
178 ConnectivityThread thread = new ConnectivityThread(context);
179 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700180
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 synchronized (thread) {
182 while (sServiceInstance == null) {
183 try {
184 // Wait until sServiceInstance has been initialized.
185 thread.wait();
186 } catch (InterruptedException ignore) {
187 Log.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700188 "Unexpected InterruptedException while waiting"+
189 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800190 }
191 }
192 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700193
The Android Open Source Project28527d22009-03-03 19:31:44 -0800194 return sServiceInstance;
195 }
196 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700197
The Android Open Source Project28527d22009-03-03 19:31:44 -0800198 public static ConnectivityService getInstance(Context context) {
199 return ConnectivityThread.getServiceInstance(context);
200 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700201
The Android Open Source Project28527d22009-03-03 19:31:44 -0800202 private ConnectivityService(Context context) {
203 if (DBG) Log.v(TAG, "ConnectivityService starting up");
204 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700205 mNetTrackers = new NetworkStateTracker[
206 ConnectivityManager.MAX_NETWORK_TYPE+1];
207 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700208
The Android Open Source Project28527d22009-03-03 19:31:44 -0800209 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700210
Robert Greenwalt2034b912009-08-12 16:08:25 -0700211 // Load device network attributes from resources
212 mNetAttributes = new NetworkAttributes[
213 ConnectivityManager.MAX_NETWORK_TYPE+1];
214 mRadioAttributes = new RadioAttributes[
215 ConnectivityManager.MAX_RADIO_TYPE+1];
216 String[] naStrings = context.getResources().getStringArray(
217 com.android.internal.R.array.networkAttributes);
218 // TODO - what if the setting has gaps/unknown types?
219 for (String a : naStrings) {
220 NetworkAttributes n = new NetworkAttributes(a);
221 mNetAttributes[n.mType] = n;
222 }
223 String[] raStrings = context.getResources().getStringArray(
224 com.android.internal.R.array.radioAttributes);
225 for (String a : raStrings) {
226 RadioAttributes r = new RadioAttributes(a);
227 mRadioAttributes[r.mType] = r;
228 }
229
230 // high priority first
231 mPriorityList = new int[naStrings.length];
232 {
233 int priority = 0; //lowest
234 int nextPos = naStrings.length-1;
235 while (nextPos>-1) {
236 for (int i = 0; i < mNetAttributes.length; i++) {
237 if(mNetAttributes[i].mPriority == priority) {
238 mPriorityList[nextPos--] = i;
239 }
240 }
241 priority++;
242 }
243 }
244
245 mNetRequestersPids =
246 new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
247 for (int i=0; i<=ConnectivityManager.MAX_NETWORK_TYPE; i++) {
248 mNetRequestersPids[i] = new ArrayList();
249 }
250
251 mFeatureUsers = new ArrayList();
252
The Android Open Source Project28527d22009-03-03 19:31:44 -0800253 /*
254 * Create the network state trackers for Wi-Fi and mobile
255 * data. Maybe this could be done with a factory class,
256 * but it's not clear that it's worth it, given that
257 * the number of different network types is not going
258 * to change very often.
259 */
260 if (DBG) Log.v(TAG, "Starting Wifi Service.");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700261 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
262 WifiService wifiService = new WifiService(context, wst);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800263 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700264 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800265
Robert Greenwalt2034b912009-08-12 16:08:25 -0700266 mNetTrackers[ConnectivityManager.TYPE_MOBILE] =
267 new MobileDataStateTracker(context, mHandler,
268 ConnectivityManager.TYPE_MOBILE, Phone.APN_TYPE_DEFAULT,
269 "MOBILE");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700270
Robert Greenwalt2034b912009-08-12 16:08:25 -0700271 mNetTrackers[ConnectivityManager.TYPE_MOBILE_MMS] =
272 new MobileDataStateTracker(context, mHandler,
273 ConnectivityManager.TYPE_MOBILE_MMS, Phone.APN_TYPE_MMS,
274 "MOBILE_MMS");
275
276 mNetTrackers[ConnectivityManager.TYPE_MOBILE_SUPL] =
277 new MobileDataStateTracker(context, mHandler,
278 ConnectivityManager.TYPE_MOBILE_SUPL, Phone.APN_TYPE_SUPL,
279 "MOBILE_SUPL");
280
281 mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] =
282 new MobileDataStateTracker(context, mHandler,
283 ConnectivityManager.TYPE_MOBILE_DUN, Phone.APN_TYPE_DUN,
284 "MOBILE_DUN");
285
286 mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI] =
287 new MobileDataStateTracker(context, mHandler,
288 ConnectivityManager.TYPE_MOBILE_HIPRI, Phone.APN_TYPE_HIPRI,
289 "MOBILE_HIPRI");
290
The Android Open Source Project28527d22009-03-03 19:31:44 -0800291 mNumDnsEntries = 0;
292
293 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
294 && SystemProperties.get("ro.build.type").equals("eng");
295
296 for (NetworkStateTracker t : mNetTrackers)
297 t.startMonitoring();
298
299 // Constructing this starts it too
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 mWifiWatchdogService = new WifiWatchdogService(context, wst);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800301 }
302
303 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700304 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800305 * @param preference the new preference
306 */
307 public synchronized void setNetworkPreference(int preference) {
308 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700309 if (ConnectivityManager.isNetworkTypeValid(preference) &&
310 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800311 if (mNetworkPreference != preference) {
312 persistNetworkPreference(preference);
313 mNetworkPreference = preference;
314 enforcePreference();
315 }
316 }
317 }
318
319 public int getNetworkPreference() {
320 enforceAccessPermission();
321 return mNetworkPreference;
322 }
323
324 private void persistNetworkPreference(int networkPreference) {
325 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700326 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
327 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800328 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700329
The Android Open Source Project28527d22009-03-03 19:31:44 -0800330 private int getPersistedNetworkPreference() {
331 final ContentResolver cr = mContext.getContentResolver();
332
333 final int networkPrefSetting = Settings.Secure
334 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
335 if (networkPrefSetting != -1) {
336 return networkPrefSetting;
337 }
338
339 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
340 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700341
The Android Open Source Project28527d22009-03-03 19:31:44 -0800342 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700343 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800344 * In this method, we only tear down a non-preferred network. Establishing
345 * a connection to the preferred network is taken care of when we handle
346 * the disconnect event from the non-preferred network
347 * (see {@link #handleDisconnect(NetworkInfo)}).
348 */
349 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700350 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800351 return;
352
Robert Greenwalt2034b912009-08-12 16:08:25 -0700353 if (!mNetTrackers[mNetworkPreference].isAvailable())
354 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800355
Robert Greenwalt2034b912009-08-12 16:08:25 -0700356 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
357 if (t != mNetworkPreference &&
358 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700359 if (DBG) {
360 Log.d(TAG, "tearing down " +
361 mNetTrackers[t].getNetworkInfo() +
362 " in enforcePreference");
363 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700364 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800365 }
366 }
367 }
368
369 private boolean teardown(NetworkStateTracker netTracker) {
370 if (netTracker.teardown()) {
371 netTracker.setTeardownRequested(true);
372 return true;
373 } else {
374 return false;
375 }
376 }
377
378 /**
379 * Return NetworkInfo for the active (i.e., connected) network interface.
380 * It is assumed that at most one network is active at a time. If more
381 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700382 * @return the info for the active network, or {@code null} if none is
383 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800384 */
385 public NetworkInfo getActiveNetworkInfo() {
386 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700387 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
388 if (!mNetAttributes[type].isDefault()) {
389 continue;
390 }
391 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800392 NetworkInfo info = t.getNetworkInfo();
393 if (info.isConnected()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700394 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
395 "connected default network is not " +
396 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800397 return info;
398 }
399 }
400 return null;
401 }
402
403 public NetworkInfo getNetworkInfo(int networkType) {
404 enforceAccessPermission();
405 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
406 NetworkStateTracker t = mNetTrackers[networkType];
407 if (t != null)
408 return t.getNetworkInfo();
409 }
410 return null;
411 }
412
413 public NetworkInfo[] getAllNetworkInfo() {
414 enforceAccessPermission();
415 NetworkInfo[] result = new NetworkInfo[mNetTrackers.length];
416 int i = 0;
417 for (NetworkStateTracker t : mNetTrackers) {
418 result[i++] = t.getNetworkInfo();
419 }
420 return result;
421 }
422
423 public boolean setRadios(boolean turnOn) {
424 boolean result = true;
425 enforceChangePermission();
426 for (NetworkStateTracker t : mNetTrackers) {
427 result = t.setRadio(turnOn) && result;
428 }
429 return result;
430 }
431
432 public boolean setRadio(int netType, boolean turnOn) {
433 enforceChangePermission();
434 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
435 return false;
436 }
437 NetworkStateTracker tracker = mNetTrackers[netType];
438 return tracker != null && tracker.setRadio(turnOn);
439 }
440
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700441 /**
442 * Used to notice when the calling process dies so we can self-expire
443 *
444 * Also used to know if the process has cleaned up after itself when
445 * our auto-expire timer goes off. The timer has a link to an object.
446 *
447 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700448 private class FeatureUser implements IBinder.DeathRecipient {
449 int mNetworkType;
450 String mFeature;
451 IBinder mBinder;
452 int mPid;
453 int mUid;
454
455 FeatureUser(int type, String feature, IBinder binder) {
456 super();
457 mNetworkType = type;
458 mFeature = feature;
459 mBinder = binder;
460 mPid = getCallingPid();
461 mUid = getCallingUid();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700462
Robert Greenwalt2034b912009-08-12 16:08:25 -0700463 try {
464 mBinder.linkToDeath(this, 0);
465 } catch (RemoteException e) {
466 binderDied();
467 }
468 }
469
470 void unlinkDeathRecipient() {
471 mBinder.unlinkToDeath(this, 0);
472 }
473
474 public void binderDied() {
475 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
476 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700477 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700478 }
479
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700480 public void expire() {
481 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
482 mNetworkType + ", " + mFeature + ", " + mBinder);
483 stopUsingNetworkFeature(this, false);
484 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700485 }
486
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700487 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700488 public int startUsingNetworkFeature(int networkType, String feature,
489 IBinder binder) {
490 if (DBG) {
491 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
492 ": " + feature);
493 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800494 enforceChangePermission();
495 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700496 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700499 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500
501 // TODO - move this into the MobileDataStateTracker
502 int usedNetworkType = networkType;
503 if(networkType == ConnectivityManager.TYPE_MOBILE) {
504 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
505 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
506 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
507 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
508 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
509 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
510 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
511 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
512 }
513 }
514 NetworkStateTracker network = mNetTrackers[usedNetworkType];
515 if (network != null) {
516 if (usedNetworkType != networkType) {
517 Integer currentPid = new Integer(getCallingPid());
518
519 NetworkStateTracker radio = mNetTrackers[networkType];
520 NetworkInfo ni = network.getNetworkInfo();
521
522 if (ni.isAvailable() == false) {
523 if (DBG) Log.d(TAG, "special network not available");
524 return Phone.APN_TYPE_NOT_AVAILABLE;
525 }
526
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700527 synchronized(this) {
528 mFeatureUsers.add(f);
529 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
530 // this gets used for per-pid dns when connected
531 mNetRequestersPids[usedNetworkType].add(currentPid);
532 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700534 mHandler.sendMessageDelayed(mHandler.obtainMessage(
535 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
536 f), getRestoreDefaultNetworkDelay());
537
Robert Greenwalt2034b912009-08-12 16:08:25 -0700538
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700539 if ((ni.isConnectedOrConnecting() == true) &&
540 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700541 if (ni.isConnected() == true) {
542 // add the pid-specific dns
543 handleDnsConfigurationChange();
544 if (DBG) Log.d(TAG, "special network already active");
545 return Phone.APN_ALREADY_ACTIVE;
546 }
547 if (DBG) Log.d(TAG, "special network already connecting");
548 return Phone.APN_REQUEST_STARTED;
549 }
550
551 // check if the radio in play can make another contact
552 // assume if cannot for now
553
Robert Greenwalt2034b912009-08-12 16:08:25 -0700554 if (DBG) Log.d(TAG, "reconnecting to special network");
555 network.reconnect();
556 return Phone.APN_REQUEST_STARTED;
557 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700558 synchronized(this) {
559 mFeatureUsers.add(f);
560 }
561 mHandler.sendMessageDelayed(mHandler.obtainMessage(
562 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
563 f), getRestoreDefaultNetworkDelay());
564
Robert Greenwalt2034b912009-08-12 16:08:25 -0700565 return network.startUsingNetworkFeature(feature,
566 getCallingPid(), getCallingUid());
567 }
568 }
569 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800570 }
571
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700572 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800573 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700574 enforceChangePermission();
575
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700576 int pid = getCallingPid();
577 int uid = getCallingUid();
578
579 FeatureUser u = null;
580 boolean found = false;
581
582 synchronized(this) {
583 for (int i = 0; i < mFeatureUsers.size() ; i++) {
584 u = (FeatureUser)mFeatureUsers.get(i);
585 if (uid == u.mUid && pid == u.mPid &&
586 networkType == u.mNetworkType &&
587 TextUtils.equals(feature, u.mFeature)) {
588 found = true;
589 break;
590 }
591 }
592 }
593 if (found && u != null) {
594 // stop regardless of how many other time this proc had called start
595 return stopUsingNetworkFeature(u, true);
596 } else {
597 // none found!
598 return 1;
599 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700600 }
601
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700602 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
603 int networkType = u.mNetworkType;
604 String feature = u.mFeature;
605 int pid = u.mPid;
606 int uid = u.mUid;
607
608 NetworkStateTracker tracker = null;
609 boolean callTeardown = false; // used to carry our decision outside of sync block
610
Robert Greenwalt2034b912009-08-12 16:08:25 -0700611 if (DBG) {
612 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
613 ": " + feature);
614 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700615
The Android Open Source Project28527d22009-03-03 19:31:44 -0800616 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
617 return -1;
618 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700619
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700620 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
621 // sync block
622 synchronized(this) {
623 // check if this process still has an outstanding start request
624 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700625 return 1;
626 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700627 u.unlinkDeathRecipient();
628 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
629 // If we care about duplicate requests, check for that here.
630 //
631 // This is done to support the extension of a request - the app
632 // can request we start the network feature again and renew the
633 // auto-shutoff delay. Normal "stop" calls from the app though
634 // do not pay attention to duplicate requests - in effect the
635 // API does not refcount and a single stop will counter multiple starts.
636 if (ignoreDups == false) {
637 for (int i = 0; i < mFeatureUsers.size() ; i++) {
638 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
639 if (x.mUid == u.mUid && x.mPid == u.mPid &&
640 x.mNetworkType == u.mNetworkType &&
641 TextUtils.equals(x.mFeature, u.mFeature)) {
642 return 1;
643 }
644 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700645 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700646
647 // TODO - move to MobileDataStateTracker
648 int usedNetworkType = networkType;
649 if (networkType == ConnectivityManager.TYPE_MOBILE) {
650 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
651 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
652 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
653 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
654 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
655 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
656 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
657 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
658 }
659 }
660 tracker = mNetTrackers[usedNetworkType];
661 if(usedNetworkType != networkType) {
662 Integer currentPid = new Integer(pid);
663 reassessPidDns(pid, true);
664 mNetRequestersPids[usedNetworkType].remove(currentPid);
665 if (mNetRequestersPids[usedNetworkType].size() != 0) {
666 if (DBG) Log.d(TAG, "not tearing down special network - " +
667 "others still using it");
668 return 1;
669 }
670 callTeardown = true;
671 }
672 }
673
674 if (callTeardown) {
675 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 return 1;
677 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700678 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700679 return tracker.stopUsingNetworkFeature(feature, pid, uid);
680 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800681 }
682
683 /**
684 * Ensure that a network route exists to deliver traffic to the specified
685 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700686 * @param networkType the type of the network over which traffic to the
687 * specified host is to be routed
688 * @param hostAddress the IP address of the host to which the route is
689 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800690 * @return {@code true} on success, {@code false} on failure
691 */
692 public boolean requestRouteToHost(int networkType, int hostAddress) {
693 enforceChangePermission();
694 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
695 return false;
696 }
697 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700698
699 if (!tracker.getNetworkInfo().isConnected() || tracker.isTeardownRequested()) {
700 if (DBG) {
701 Log.d(TAG, "requestRouteToHost on down network (" + networkType + " - dropped");
702 }
703 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800704 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700705 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800706 }
707
708 /**
709 * @see ConnectivityManager#getBackgroundDataSetting()
710 */
711 public boolean getBackgroundDataSetting() {
712 return Settings.Secure.getInt(mContext.getContentResolver(),
713 Settings.Secure.BACKGROUND_DATA, 1) == 1;
714 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700715
The Android Open Source Project28527d22009-03-03 19:31:44 -0800716 /**
717 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
718 */
719 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
720 mContext.enforceCallingOrSelfPermission(
721 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
722 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700723
The Android Open Source Project28527d22009-03-03 19:31:44 -0800724 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
725
726 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700727 Settings.Secure.BACKGROUND_DATA,
728 allowBackgroundDataUsage ? 1 : 0);
729
The Android Open Source Project28527d22009-03-03 19:31:44 -0800730 Intent broadcast = new Intent(
731 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
732 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700733 }
734
The Android Open Source Project28527d22009-03-03 19:31:44 -0800735 private int getNumConnectedNetworks() {
736 int numConnectedNets = 0;
737
738 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700739 if (nt.getNetworkInfo().isConnected() &&
740 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800741 ++numConnectedNets;
742 }
743 }
744 return numConnectedNets;
745 }
746
747 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700748 mContext.enforceCallingOrSelfPermission(
749 android.Manifest.permission.ACCESS_NETWORK_STATE,
750 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800751 }
752
753 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700754 mContext.enforceCallingOrSelfPermission(
755 android.Manifest.permission.CHANGE_NETWORK_STATE,
756 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800757 }
758
759 /**
Robert Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800764 * @param info the {@code NetworkInfo} for the network
765 */
766 private void handleDisconnect(NetworkInfo info) {
767
Robert Greenwalt2034b912009-08-12 16:08:25 -0700768 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800769
Robert Greenwalt2034b912009-08-12 16:08:25 -0700770 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800771 /*
772 * If the disconnected network is not the active one, then don't report
773 * this as a loss of connectivity. What probably happened is that we're
774 * getting the disconnect for a network that we explicitly disabled
775 * in accordance with network preference policies.
776 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700777 if (!mNetAttributes[prevNetType].isDefault()) {
778 List pids = mNetRequestersPids[prevNetType];
779 for (int i = 0; i<pids.size(); i++) {
780 Integer pid = (Integer)pids.get(i);
781 // will remove them because the net's no longer connected
782 // need to do this now as only now do we know the pids and
783 // can properly null things that are no longer referenced.
784 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800785 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800786 }
787
The Android Open Source Project28527d22009-03-03 19:31:44 -0800788 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
789 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
790 if (info.isFailover()) {
791 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
792 info.setFailover(false);
793 }
794 if (info.getReason() != null) {
795 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
796 }
797 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700798 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
799 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800800 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700801
802 /*
803 * If this is a default network, check if other defaults are available
804 * or active
805 */
806 NetworkStateTracker newNet = null;
807 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700808 if (mActiveDefaultNetwork == prevNetType) {
809 mActiveDefaultNetwork = -1;
810 }
811
812 int newType = -1;
813 int newPriority = -1;
814 for (int checkType=0; checkType <=
815 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
816 if (checkType == prevNetType) {
817 continue;
818 }
819 if (mNetAttributes[checkType].isDefault()) {
820 /* TODO - if we have multiple nets we could use
821 * we may want to put more thought into which we choose
822 */
823 if (checkType == mNetworkPreference) {
824 newType = checkType;
825 break;
826 }
827 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
828 mPriority > newPriority) {
829 newType = checkType;
830 newPriority = mRadioAttributes[mNetAttributes[newType].
831 mRadio].mPriority;
832 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800833 }
834 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700835
836 if (newType != -1) {
837 newNet = mNetTrackers[newType];
838 /**
839 * See if the other network is available to fail over to.
840 * If is not available, we enable it anyway, so that it
841 * will be able to connect when it does become available,
842 * but we report a total loss of connectivity rather than
843 * report that we are attempting to fail over.
844 */
845 if (newNet.isAvailable()) {
846 NetworkInfo switchTo = newNet.getNetworkInfo();
847 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700848 if (!switchTo.isConnectedOrConnecting() ||
849 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700850 newNet.reconnect();
851 }
852 if (DBG) {
853 if (switchTo.isConnected()) {
854 Log.v(TAG, "Switching to already connected " +
855 switchTo.getTypeName());
856 } else {
857 Log.v(TAG, "Attempting to switch to " +
858 switchTo.getTypeName());
859 }
860 }
861 intent.putExtra(ConnectivityManager.
862 EXTRA_OTHER_NETWORK_INFO, switchTo);
863 } else {
Robert Greenwalt149d5ac2009-09-17 14:58:16 -0700864 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
865 true);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700866 newNet.reconnect();
867 }
868 } else {
869 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
870 true);
871 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700873
874 // do this before we broadcast the change
875 handleConnectivityChange();
876
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400877 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800878 /*
Robert Greenwalt0659da32009-07-16 17:21:39 -0700879 * If the failover network is already connected, then immediately send
880 * out a followup broadcast indicating successful failover
The Android Open Source Project28527d22009-03-03 19:31:44 -0800881 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700882 if (newNet != null && newNet.getNetworkInfo().isConnected())
883 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800884 }
885
886 private void sendConnectedBroadcast(NetworkInfo info) {
887 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
888 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
889 if (info.isFailover()) {
890 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
891 info.setFailover(false);
892 }
893 if (info.getReason() != null) {
894 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
895 }
896 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700897 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
898 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800899 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400900 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800901 }
902
903 /**
904 * Called when an attempt to fail over to another network has failed.
905 * @param info the {@link NetworkInfo} for the failed network
906 */
907 private void handleConnectionFailure(NetworkInfo info) {
908 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800909
Robert Greenwalt2034b912009-08-12 16:08:25 -0700910 String reason = info.getReason();
911 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700912
Robert Greenwalt2034b912009-08-12 16:08:25 -0700913 if (DBG) {
914 String reasonText;
915 if (reason == null) {
916 reasonText = ".";
917 } else {
918 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800919 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700920 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
921 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800922 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700923
924 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
925 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
926 if (getActiveNetworkInfo() == null) {
927 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
928 }
929 if (reason != null) {
930 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
931 }
932 if (extraInfo != null) {
933 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
934 }
935 if (info.isFailover()) {
936 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
937 info.setFailover(false);
938 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400939 sendStickyBroadcast(intent);
940 }
941
942 private void sendStickyBroadcast(Intent intent) {
943 synchronized(this) {
944 if (mSystemReady) {
945 mContext.sendStickyBroadcast(intent);
946 } else {
947 if (mDeferredBroadcasts == null) {
948 mDeferredBroadcasts = new ArrayList<Intent>();
949 }
950 mDeferredBroadcasts.add(intent);
951 }
952 }
953 }
954
955 void systemReady() {
956 synchronized(this) {
957 mSystemReady = true;
958 if (mDeferredBroadcasts != null) {
959 int count = mDeferredBroadcasts.size();
960 for (int i = 0; i < count; i++) {
961 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
962 }
963 mDeferredBroadcasts = null;
964 }
965 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 }
967
968 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700969 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800970
971 // snapshot isFailover, because sendConnectedBroadcast() resets it
972 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700973 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800974
Robert Greenwalt2034b912009-08-12 16:08:25 -0700975 // if this is a default net and other default is running
976 // kill the one not preferred
977 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
979 if ((type != mNetworkPreference &&
980 mNetAttributes[mActiveDefaultNetwork].mPriority >
981 mNetAttributes[type].mPriority) ||
982 mNetworkPreference == mActiveDefaultNetwork) {
983 // don't accept this one
984 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
985 "to torn down network " + info.getTypeName());
986 teardown(thisNet);
987 return;
988 } else {
989 // tear down the other
990 NetworkStateTracker otherNet =
991 mNetTrackers[mActiveDefaultNetwork];
992 if (DBG) Log.v(TAG, "Policy requires " +
993 otherNet.getNetworkInfo().getTypeName() +
994 " teardown");
995 if (!teardown(otherNet)) {
996 Log.e(TAG, "Network declined teardown request");
997 return;
998 }
999 if (isFailover) {
1000 otherNet.releaseWakeLock();
1001 }
1002 }
1003 }
1004 mActiveDefaultNetwork = type;
1005 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001006 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001007 thisNet.updateNetworkSettings();
1008 handleConnectivityChange();
1009 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001010 }
1011
1012 private void handleScanResultsAvailable(NetworkInfo info) {
1013 int networkType = info.getType();
1014 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001015 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1016 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001017 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001018
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 mNetTrackers[networkType].interpretScanResultsAvailable();
1020 }
1021
Robert Greenwalt0659da32009-07-16 17:21:39 -07001022 private void handleNotificationChange(boolean visible, int id,
1023 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001024 NotificationManager notificationManager = (NotificationManager) mContext
1025 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001026
The Android Open Source Project28527d22009-03-03 19:31:44 -08001027 if (visible) {
1028 notificationManager.notify(id, notification);
1029 } else {
1030 notificationManager.cancel(id);
1031 }
1032 }
1033
1034 /**
1035 * After any kind of change in the connectivity state of any network,
1036 * make sure that anything that depends on the connectivity state of
1037 * more than one network is set up correctly. We're mainly concerned
1038 * with making sure that the list of DNS servers is set up according
1039 * to which networks are connected, and ensuring that the right routing
1040 * table entries exist.
1041 */
1042 private void handleConnectivityChange() {
1043 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001044 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001045 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001046 * If both mobile and wifi are enabled, add the host routes that
1047 * will allow MMS traffic to pass on the mobile network. But
1048 * remove the default route for the mobile network, so that there
1049 * will be only one default route, to ensure that all traffic
1050 * except MMS will travel via Wi-Fi.
1051 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001052 handleDnsConfigurationChange();
1053
1054 for (int netType : mPriorityList) {
1055 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1056 if (mNetAttributes[netType].isDefault()) {
1057 mNetTrackers[netType].addDefaultRoute();
1058 } else {
1059 mNetTrackers[netType].addPrivateDnsRoutes();
1060 }
1061 } else {
1062 if (mNetAttributes[netType].isDefault()) {
1063 mNetTrackers[netType].removeDefaultRoute();
1064 } else {
1065 mNetTrackers[netType].removePrivateDnsRoutes();
1066 }
1067 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001068 }
1069 }
1070
Robert Greenwalt2034b912009-08-12 16:08:25 -07001071 /**
1072 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1073 * on the highest priority active net which this process requested.
1074 * If there aren't any, clear it out
1075 */
1076 private void reassessPidDns(int myPid, boolean doBump)
1077 {
1078 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1079 for(int i : mPriorityList) {
1080 if (mNetAttributes[i].isDefault()) {
1081 continue;
1082 }
1083 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001084 if (nt.getNetworkInfo().isConnected() &&
1085 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 List pids = mNetRequestersPids[i];
1087 for (int j=0; j<pids.size(); j++) {
1088 Integer pid = (Integer)pids.get(j);
1089 if (pid.intValue() == myPid) {
1090 String[] dnsList = nt.getNameServers();
1091 writePidDns(dnsList, myPid);
1092 if (doBump) {
1093 bumpDns();
1094 }
1095 return;
1096 }
1097 }
1098 }
1099 }
1100 // nothing found - delete
1101 for (int i = 1; ; i++) {
1102 String prop = "net.dns" + i + "." + myPid;
1103 if (SystemProperties.get(prop).length() == 0) {
1104 if (doBump) {
1105 bumpDns();
1106 }
1107 return;
1108 }
1109 SystemProperties.set(prop, "");
1110 }
1111 }
1112
1113 private void writePidDns(String[] dnsList, int pid) {
1114 int j = 1;
1115 for (String dns : dnsList) {
1116 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1117 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1118 }
1119 }
1120 }
1121
1122 private void bumpDns() {
1123 /*
1124 * Bump the property that tells the name resolver library to reread
1125 * the DNS server list from the properties.
1126 */
1127 String propVal = SystemProperties.get("net.dnschange");
1128 int n = 0;
1129 if (propVal.length() != 0) {
1130 try {
1131 n = Integer.parseInt(propVal);
1132 } catch (NumberFormatException e) {}
1133 }
1134 SystemProperties.set("net.dnschange", "" + (n+1));
1135 }
1136
1137 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001138 // add default net's dns entries
1139 for (int x = mPriorityList.length-1; x>= 0; x--) {
1140 int netType = mPriorityList[x];
1141 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001142 if (nt != null && nt.getNetworkInfo().isConnected() &&
1143 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001144 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001145 if (mNetAttributes[netType].isDefault()) {
1146 int j = 1;
1147 for (String dns : dnsList) {
1148 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001149 if (DBG) {
1150 Log.d(TAG, "adding dns " + dns + " for " +
1151 nt.getNetworkInfo().getTypeName());
1152 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001153 SystemProperties.set("net.dns" + j++, dns);
1154 }
1155 }
1156 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001157 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1158 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001159 }
1160 mNumDnsEntries = j;
1161 } else {
1162 // set per-pid dns for attached secondary nets
1163 List pids = mNetRequestersPids[netType];
1164 for (int y=0; y< pids.size(); y++) {
1165 Integer pid = (Integer)pids.get(y);
1166 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001167 }
1168 }
1169 }
1170 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001171
1172 bumpDns();
1173 }
1174
1175 private int getRestoreDefaultNetworkDelay() {
1176 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1177 NETWORK_RESTORE_DELAY_PROP_NAME);
1178 if(restoreDefaultNetworkDelayStr != null &&
1179 restoreDefaultNetworkDelayStr.length() != 0) {
1180 try {
1181 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1182 } catch (NumberFormatException e) {
1183 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001185 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001186 }
1187
1188 @Override
1189 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001190 if (mContext.checkCallingOrSelfPermission(
1191 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001192 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001193 pw.println("Permission Denial: can't dump ConnectivityService " +
1194 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1195 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001196 return;
1197 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 pw.println();
1199 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200 if (nst.getNetworkInfo().isConnected()) {
1201 pw.println("Active network: " + nst.getNetworkInfo().
1202 getTypeName());
1203 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 pw.println(nst.getNetworkInfo());
1205 pw.println(nst);
1206 pw.println();
1207 }
1208 }
1209
Robert Greenwalt2034b912009-08-12 16:08:25 -07001210 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001211 private class MyHandler extends Handler {
1212 @Override
1213 public void handleMessage(Message msg) {
1214 NetworkInfo info;
1215 switch (msg.what) {
1216 case NetworkStateTracker.EVENT_STATE_CHANGED:
1217 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001218 int type = info.getType();
1219 NetworkInfo.State state = info.getState();
1220 if(mNetAttributes[type].mLastState == state) {
1221 if (DBG) {
1222 // TODO - remove this after we validate the dropping doesn't break anything
1223 Log.d(TAG, "Dropping ConnectivityChange for " +
1224 info.getTypeName() +": " +
1225 state + "/" + info.getDetailedState());
1226 }
1227 return;
1228 }
1229 mNetAttributes[type].mLastState = state;
1230
Robert Greenwalt2034b912009-08-12 16:08:25 -07001231 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001232 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001233 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001234
1235 // Connectivity state changed:
1236 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-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 Project28527d22009-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);
Doug Zongker2fc96232009-12-04 10:31:43 -08001245 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001246 eventLogParam);
1247
1248 if (info.getDetailedState() ==
1249 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001250 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001251 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001252 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001253 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001254 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001255 // the logic here is, handle SUSPENDED the same as
1256 // DISCONNECTED. The only difference being we are
1257 // broadcasting an intent with NetworkInfo that's
1258 // suspended. This allows the applications an
1259 // opportunity to handle DISCONNECTED and SUSPENDED
1260 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001261 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001262 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001263 handleConnect(info);
1264 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001265 break;
1266
1267 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1268 info = (NetworkInfo) msg.obj;
1269 handleScanResultsAvailable(info);
1270 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001271
The Android Open Source Project28527d22009-03-03 19:31:44 -08001272 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001273 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1274 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001275
1276 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001277 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001278 break;
1279
1280 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1281 // fill me in
1282 break;
1283
1284 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1285 // fill me in
1286 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001287 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001288 FeatureUser u = (FeatureUser)msg.obj;
1289 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001291 }
1292 }
1293 }
1294}