blob: 5c0f35210e71667ae94a33eac50e021c4a9059c8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt42acef32009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.io.FileDescriptor;
47import java.io.PrintWriter;
Robert Greenwalt42acef32009-08-12 16:08:25 -070048import java.util.ArrayList;
49import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51/**
52 * @hide
53 */
54public class ConnectivityService extends IConnectivityManager.Stub {
55
Robert Greenwaltd8df1492009-10-06 14:12:53 -070056 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 private static final String TAG = "ConnectivityService";
58
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 Greenwaltb097fbb2009-12-07 15:20:50 -0800106 private class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700107 /**
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;
Robert Greenwalt1193ae42010-01-13 09:36:31 -0800114 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700115 public NetworkAttributes(String init) {
116 String fragments[] = init.split(",");
117 mName = fragments[0].toLowerCase();
118 if (fragments[1].toLowerCase().equals("wifi")) {
119 mRadio = ConnectivityManager.TYPE_WIFI;
120 } else {
121 mRadio = ConnectivityManager.TYPE_MOBILE;
122 }
123 if (mName.equals("default")) {
124 mType = mRadio;
125 } else if (mName.equals("mms")) {
126 mType = ConnectivityManager.TYPE_MOBILE_MMS;
127 } else if (mName.equals("supl")) {
128 mType = ConnectivityManager.TYPE_MOBILE_SUPL;
129 } else if (mName.equals("dun")) {
130 mType = ConnectivityManager.TYPE_MOBILE_DUN;
131 } else if (mName.equals("hipri")) {
132 mType = ConnectivityManager.TYPE_MOBILE_HIPRI;
133 }
134 mPriority = Integer.parseInt(fragments[2]);
Robert Greenwalt1193ae42010-01-13 09:36:31 -0800135 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700136 }
137 public boolean isDefault() {
138 return (mType == mRadio);
139 }
140 }
141 NetworkAttributes[] mNetAttributes;
142
Robert Greenwaltb097fbb2009-12-07 15:20:50 -0800143 private class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700144 public String mName;
145 public int mPriority;
146 public int mSimultaneity;
147 public int mType;
148 public RadioAttributes(String init) {
149 String fragments[] = init.split(",");
150 mName = fragments[0].toLowerCase();
151 mPriority = Integer.parseInt(fragments[1]);
152 mSimultaneity = Integer.parseInt(fragments[2]);
153 if (mName.equals("wifi")) {
154 mType = ConnectivityManager.TYPE_WIFI;
155 } else {
156 mType = ConnectivityManager.TYPE_MOBILE;
157 }
158 }
159 }
160 RadioAttributes[] mRadioAttributes;
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private static class ConnectivityThread extends Thread {
163 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private ConnectivityThread(Context context) {
166 super("ConnectivityThread");
167 mContext = context;
168 }
169
170 @Override
171 public void run() {
172 Looper.prepare();
173 synchronized (this) {
174 sServiceInstance = new ConnectivityService(mContext);
175 notifyAll();
176 }
177 Looper.loop();
178 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static ConnectivityService getServiceInstance(Context context) {
181 ConnectivityThread thread = new ConnectivityThread(context);
182 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 synchronized (thread) {
185 while (sServiceInstance == null) {
186 try {
187 // Wait until sServiceInstance has been initialized.
188 thread.wait();
189 } catch (InterruptedException ignore) {
190 Log.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700191 "Unexpected InterruptedException while waiting"+
192 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
194 }
195 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 return sServiceInstance;
198 }
199 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static ConnectivityService getInstance(Context context) {
202 return ConnectivityThread.getServiceInstance(context);
203 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 private ConnectivityService(Context context) {
206 if (DBG) Log.v(TAG, "ConnectivityService starting up");
207 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700208 mNetTrackers = new NetworkStateTracker[
209 ConnectivityManager.MAX_NETWORK_TYPE+1];
210 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700213
Robert Greenwalt42acef32009-08-12 16:08:25 -0700214 // Load device network attributes from resources
215 mNetAttributes = new NetworkAttributes[
216 ConnectivityManager.MAX_NETWORK_TYPE+1];
217 mRadioAttributes = new RadioAttributes[
218 ConnectivityManager.MAX_RADIO_TYPE+1];
219 String[] naStrings = context.getResources().getStringArray(
220 com.android.internal.R.array.networkAttributes);
221 // TODO - what if the setting has gaps/unknown types?
222 for (String a : naStrings) {
223 NetworkAttributes n = new NetworkAttributes(a);
224 mNetAttributes[n.mType] = n;
225 }
226 String[] raStrings = context.getResources().getStringArray(
227 com.android.internal.R.array.radioAttributes);
228 for (String a : raStrings) {
229 RadioAttributes r = new RadioAttributes(a);
230 mRadioAttributes[r.mType] = r;
231 }
232
233 // high priority first
234 mPriorityList = new int[naStrings.length];
235 {
236 int priority = 0; //lowest
237 int nextPos = naStrings.length-1;
238 while (nextPos>-1) {
239 for (int i = 0; i < mNetAttributes.length; i++) {
240 if(mNetAttributes[i].mPriority == priority) {
241 mPriorityList[nextPos--] = i;
242 }
243 }
244 priority++;
245 }
246 }
247
248 mNetRequestersPids =
249 new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
250 for (int i=0; i<=ConnectivityManager.MAX_NETWORK_TYPE; i++) {
251 mNetRequestersPids[i] = new ArrayList();
252 }
253
254 mFeatureUsers = new ArrayList();
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /*
257 * Create the network state trackers for Wi-Fi and mobile
258 * data. Maybe this could be done with a factory class,
259 * but it's not clear that it's worth it, given that
260 * the number of different network types is not going
261 * to change very often.
262 */
263 if (DBG) Log.v(TAG, "Starting Wifi Service.");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700264 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
265 WifiService wifiService = new WifiService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700267 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Robert Greenwalt42acef32009-08-12 16:08:25 -0700269 mNetTrackers[ConnectivityManager.TYPE_MOBILE] =
270 new MobileDataStateTracker(context, mHandler,
271 ConnectivityManager.TYPE_MOBILE, Phone.APN_TYPE_DEFAULT,
272 "MOBILE");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700273
Robert Greenwalt42acef32009-08-12 16:08:25 -0700274 mNetTrackers[ConnectivityManager.TYPE_MOBILE_MMS] =
275 new MobileDataStateTracker(context, mHandler,
276 ConnectivityManager.TYPE_MOBILE_MMS, Phone.APN_TYPE_MMS,
277 "MOBILE_MMS");
278
279 mNetTrackers[ConnectivityManager.TYPE_MOBILE_SUPL] =
280 new MobileDataStateTracker(context, mHandler,
281 ConnectivityManager.TYPE_MOBILE_SUPL, Phone.APN_TYPE_SUPL,
282 "MOBILE_SUPL");
283
284 mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] =
285 new MobileDataStateTracker(context, mHandler,
286 ConnectivityManager.TYPE_MOBILE_DUN, Phone.APN_TYPE_DUN,
287 "MOBILE_DUN");
288
289 mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI] =
290 new MobileDataStateTracker(context, mHandler,
291 ConnectivityManager.TYPE_MOBILE_HIPRI, Phone.APN_TYPE_HIPRI,
292 "MOBILE_HIPRI");
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 mNumDnsEntries = 0;
295
296 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
297 && SystemProperties.get("ro.build.type").equals("eng");
298
299 for (NetworkStateTracker t : mNetTrackers)
300 t.startMonitoring();
301
302 // Constructing this starts it too
Robert Greenwalt42acef32009-08-12 16:08:25 -0700303 mWifiWatchdogService = new WifiWatchdogService(context, wst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305
306 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700307 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 * @param preference the new preference
309 */
310 public synchronized void setNetworkPreference(int preference) {
311 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700312 if (ConnectivityManager.isNetworkTypeValid(preference) &&
313 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 if (mNetworkPreference != preference) {
315 persistNetworkPreference(preference);
316 mNetworkPreference = preference;
317 enforcePreference();
318 }
319 }
320 }
321
322 public int getNetworkPreference() {
323 enforceAccessPermission();
324 return mNetworkPreference;
325 }
326
327 private void persistNetworkPreference(int networkPreference) {
328 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700329 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
330 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 private int getPersistedNetworkPreference() {
334 final ContentResolver cr = mContext.getContentResolver();
335
336 final int networkPrefSetting = Settings.Secure
337 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
338 if (networkPrefSetting != -1) {
339 return networkPrefSetting;
340 }
341
342 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
343 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700346 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * In this method, we only tear down a non-preferred network. Establishing
348 * a connection to the preferred network is taken care of when we handle
349 * the disconnect event from the non-preferred network
350 * (see {@link #handleDisconnect(NetworkInfo)}).
351 */
352 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700353 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 return;
355
Robert Greenwalt42acef32009-08-12 16:08:25 -0700356 if (!mNetTrackers[mNetworkPreference].isAvailable())
357 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358
Robert Greenwalt42acef32009-08-12 16:08:25 -0700359 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
360 if (t != mNetworkPreference &&
361 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700362 if (DBG) {
363 Log.d(TAG, "tearing down " +
364 mNetTrackers[t].getNetworkInfo() +
365 " in enforcePreference");
366 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700367 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369 }
370 }
371
372 private boolean teardown(NetworkStateTracker netTracker) {
373 if (netTracker.teardown()) {
374 netTracker.setTeardownRequested(true);
375 return true;
376 } else {
377 return false;
378 }
379 }
380
381 /**
382 * Return NetworkInfo for the active (i.e., connected) network interface.
383 * It is assumed that at most one network is active at a time. If more
384 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700385 * @return the info for the active network, or {@code null} if none is
386 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 */
388 public NetworkInfo getActiveNetworkInfo() {
389 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700390 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
391 if (!mNetAttributes[type].isDefault()) {
392 continue;
393 }
394 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 NetworkInfo info = t.getNetworkInfo();
396 if (info.isConnected()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700397 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
398 "connected default network is not " +
399 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 return info;
401 }
402 }
403 return null;
404 }
405
406 public NetworkInfo getNetworkInfo(int networkType) {
407 enforceAccessPermission();
408 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
409 NetworkStateTracker t = mNetTrackers[networkType];
410 if (t != null)
411 return t.getNetworkInfo();
412 }
413 return null;
414 }
415
416 public NetworkInfo[] getAllNetworkInfo() {
417 enforceAccessPermission();
418 NetworkInfo[] result = new NetworkInfo[mNetTrackers.length];
419 int i = 0;
420 for (NetworkStateTracker t : mNetTrackers) {
421 result[i++] = t.getNetworkInfo();
422 }
423 return result;
424 }
425
426 public boolean setRadios(boolean turnOn) {
427 boolean result = true;
428 enforceChangePermission();
429 for (NetworkStateTracker t : mNetTrackers) {
430 result = t.setRadio(turnOn) && result;
431 }
432 return result;
433 }
434
435 public boolean setRadio(int netType, boolean turnOn) {
436 enforceChangePermission();
437 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
438 return false;
439 }
440 NetworkStateTracker tracker = mNetTrackers[netType];
441 return tracker != null && tracker.setRadio(turnOn);
442 }
443
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700444 /**
445 * Used to notice when the calling process dies so we can self-expire
446 *
447 * Also used to know if the process has cleaned up after itself when
448 * our auto-expire timer goes off. The timer has a link to an object.
449 *
450 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700451 private class FeatureUser implements IBinder.DeathRecipient {
452 int mNetworkType;
453 String mFeature;
454 IBinder mBinder;
455 int mPid;
456 int mUid;
457
458 FeatureUser(int type, String feature, IBinder binder) {
459 super();
460 mNetworkType = type;
461 mFeature = feature;
462 mBinder = binder;
463 mPid = getCallingPid();
464 mUid = getCallingUid();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700465
Robert Greenwalt42acef32009-08-12 16:08:25 -0700466 try {
467 mBinder.linkToDeath(this, 0);
468 } catch (RemoteException e) {
469 binderDied();
470 }
471 }
472
473 void unlinkDeathRecipient() {
474 mBinder.unlinkToDeath(this, 0);
475 }
476
477 public void binderDied() {
478 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
479 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700480 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700481 }
482
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700483 public void expire() {
484 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
485 mNetworkType + ", " + mFeature + ", " + mBinder);
486 stopUsingNetworkFeature(this, false);
487 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700488 }
489
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700490 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 public int startUsingNetworkFeature(int networkType, String feature,
492 IBinder binder) {
493 if (DBG) {
494 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
495 ": " + feature);
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 enforceChangePermission();
498 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700502 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503
504 // TODO - move this into the MobileDataStateTracker
505 int usedNetworkType = networkType;
506 if(networkType == ConnectivityManager.TYPE_MOBILE) {
507 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
508 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
509 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
510 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
511 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
512 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
513 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
514 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
515 }
516 }
517 NetworkStateTracker network = mNetTrackers[usedNetworkType];
518 if (network != null) {
519 if (usedNetworkType != networkType) {
520 Integer currentPid = new Integer(getCallingPid());
521
522 NetworkStateTracker radio = mNetTrackers[networkType];
523 NetworkInfo ni = network.getNetworkInfo();
524
525 if (ni.isAvailable() == false) {
526 if (DBG) Log.d(TAG, "special network not available");
527 return Phone.APN_TYPE_NOT_AVAILABLE;
528 }
529
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700530 synchronized(this) {
531 mFeatureUsers.add(f);
532 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
533 // this gets used for per-pid dns when connected
534 mNetRequestersPids[usedNetworkType].add(currentPid);
535 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700537 mHandler.sendMessageDelayed(mHandler.obtainMessage(
538 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
539 f), getRestoreDefaultNetworkDelay());
540
Robert Greenwalt42acef32009-08-12 16:08:25 -0700541
Robert Greenwalta64bf832009-08-19 20:19:33 -0700542 if ((ni.isConnectedOrConnecting() == true) &&
543 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700544 if (ni.isConnected() == true) {
545 // add the pid-specific dns
546 handleDnsConfigurationChange();
547 if (DBG) Log.d(TAG, "special network already active");
548 return Phone.APN_ALREADY_ACTIVE;
549 }
550 if (DBG) Log.d(TAG, "special network already connecting");
551 return Phone.APN_REQUEST_STARTED;
552 }
553
554 // check if the radio in play can make another contact
555 // assume if cannot for now
556
Robert Greenwalt42acef32009-08-12 16:08:25 -0700557 if (DBG) Log.d(TAG, "reconnecting to special network");
558 network.reconnect();
559 return Phone.APN_REQUEST_STARTED;
560 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700561 synchronized(this) {
562 mFeatureUsers.add(f);
563 }
564 mHandler.sendMessageDelayed(mHandler.obtainMessage(
565 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
566 f), getRestoreDefaultNetworkDelay());
567
Robert Greenwalt42acef32009-08-12 16:08:25 -0700568 return network.startUsingNetworkFeature(feature,
569 getCallingPid(), getCallingUid());
570 }
571 }
572 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700575 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700577 int pid = getCallingPid();
578 int uid = getCallingUid();
579
580 FeatureUser u = null;
581 boolean found = false;
582
583 synchronized(this) {
584 for (int i = 0; i < mFeatureUsers.size() ; i++) {
585 u = (FeatureUser)mFeatureUsers.get(i);
586 if (uid == u.mUid && pid == u.mPid &&
587 networkType == u.mNetworkType &&
588 TextUtils.equals(feature, u.mFeature)) {
589 found = true;
590 break;
591 }
592 }
593 }
594 if (found && u != null) {
595 // stop regardless of how many other time this proc had called start
596 return stopUsingNetworkFeature(u, true);
597 } else {
598 // none found!
599 return 1;
600 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700601 }
602
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700603 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
604 int networkType = u.mNetworkType;
605 String feature = u.mFeature;
606 int pid = u.mPid;
607 int uid = u.mUid;
608
609 NetworkStateTracker tracker = null;
610 boolean callTeardown = false; // used to carry our decision outside of sync block
611
Robert Greenwalt42acef32009-08-12 16:08:25 -0700612 if (DBG) {
613 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
614 ": " + feature);
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 enforceChangePermission();
617 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
618 return -1;
619 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700620
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700621 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
622 // sync block
623 synchronized(this) {
624 // check if this process still has an outstanding start request
625 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700626 return 1;
627 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700628 u.unlinkDeathRecipient();
629 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
630 // If we care about duplicate requests, check for that here.
631 //
632 // This is done to support the extension of a request - the app
633 // can request we start the network feature again and renew the
634 // auto-shutoff delay. Normal "stop" calls from the app though
635 // do not pay attention to duplicate requests - in effect the
636 // API does not refcount and a single stop will counter multiple starts.
637 if (ignoreDups == false) {
638 for (int i = 0; i < mFeatureUsers.size() ; i++) {
639 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
640 if (x.mUid == u.mUid && x.mPid == u.mPid &&
641 x.mNetworkType == u.mNetworkType &&
642 TextUtils.equals(x.mFeature, u.mFeature)) {
643 return 1;
644 }
645 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700647
648 // TODO - move to MobileDataStateTracker
649 int usedNetworkType = networkType;
650 if (networkType == ConnectivityManager.TYPE_MOBILE) {
651 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
652 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
653 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
654 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
655 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
656 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
657 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
658 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
659 }
660 }
661 tracker = mNetTrackers[usedNetworkType];
662 if(usedNetworkType != networkType) {
663 Integer currentPid = new Integer(pid);
664 reassessPidDns(pid, true);
665 mNetRequestersPids[usedNetworkType].remove(currentPid);
666 if (mNetRequestersPids[usedNetworkType].size() != 0) {
667 if (DBG) Log.d(TAG, "not tearing down special network - " +
668 "others still using it");
669 return 1;
670 }
671 callTeardown = true;
672 }
673 }
674
675 if (callTeardown) {
676 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700677 return 1;
678 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700679 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700680 return tracker.stopUsingNetworkFeature(feature, pid, uid);
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683
684 /**
685 * Ensure that a network route exists to deliver traffic to the specified
686 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700687 * @param networkType the type of the network over which traffic to the
688 * specified host is to be routed
689 * @param hostAddress the IP address of the host to which the route is
690 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 * @return {@code true} on success, {@code false} on failure
692 */
693 public boolean requestRouteToHost(int networkType, int hostAddress) {
694 enforceChangePermission();
695 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
696 return false;
697 }
698 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700699
700 if (!tracker.getNetworkInfo().isConnected() || tracker.isTeardownRequested()) {
701 if (DBG) {
702 Log.d(TAG, "requestRouteToHost on down network (" + networkType + " - dropped");
703 }
704 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700706 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
709 /**
710 * @see ConnectivityManager#getBackgroundDataSetting()
711 */
712 public boolean getBackgroundDataSetting() {
713 return Settings.Secure.getInt(mContext.getContentResolver(),
714 Settings.Secure.BACKGROUND_DATA, 1) == 1;
715 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 /**
718 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
719 */
720 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
721 mContext.enforceCallingOrSelfPermission(
722 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
723 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
726
727 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700728 Settings.Secure.BACKGROUND_DATA,
729 allowBackgroundDataUsage ? 1 : 0);
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 Intent broadcast = new Intent(
732 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
733 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700734 }
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 private int getNumConnectedNetworks() {
737 int numConnectedNets = 0;
738
739 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700740 if (nt.getNetworkInfo().isConnected() &&
741 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 ++numConnectedNets;
743 }
744 }
745 return numConnectedNets;
746 }
747
748 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700749 mContext.enforceCallingOrSelfPermission(
750 android.Manifest.permission.ACCESS_NETWORK_STATE,
751 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
754 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700755 mContext.enforceCallingOrSelfPermission(
756 android.Manifest.permission.CHANGE_NETWORK_STATE,
757 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700761 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
762 * network, we ignore it. If it is for the active network, we send out a
763 * broadcast. But first, we check whether it might be possible to connect
764 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 * @param info the {@code NetworkInfo} for the network
766 */
767 private void handleDisconnect(NetworkInfo info) {
768
Robert Greenwalt42acef32009-08-12 16:08:25 -0700769 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
Robert Greenwalt42acef32009-08-12 16:08:25 -0700771 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 /*
773 * If the disconnected network is not the active one, then don't report
774 * this as a loss of connectivity. What probably happened is that we're
775 * getting the disconnect for a network that we explicitly disabled
776 * in accordance with network preference policies.
777 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700778 if (!mNetAttributes[prevNetType].isDefault()) {
779 List pids = mNetRequestersPids[prevNetType];
780 for (int i = 0; i<pids.size(); i++) {
781 Integer pid = (Integer)pids.get(i);
782 // will remove them because the net's no longer connected
783 // need to do this now as only now do we know the pids and
784 // can properly null things that are no longer referenced.
785 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
790 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
791 if (info.isFailover()) {
792 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
793 info.setFailover(false);
794 }
795 if (info.getReason() != null) {
796 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
797 }
798 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700799 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
800 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700802
803 /*
804 * If this is a default network, check if other defaults are available
805 * or active
806 */
807 NetworkStateTracker newNet = null;
808 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700809 if (mActiveDefaultNetwork == prevNetType) {
810 mActiveDefaultNetwork = -1;
811 }
812
813 int newType = -1;
814 int newPriority = -1;
815 for (int checkType=0; checkType <=
816 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
817 if (checkType == prevNetType) {
818 continue;
819 }
820 if (mNetAttributes[checkType].isDefault()) {
821 /* TODO - if we have multiple nets we could use
822 * we may want to put more thought into which we choose
823 */
824 if (checkType == mNetworkPreference) {
825 newType = checkType;
826 break;
827 }
828 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
829 mPriority > newPriority) {
830 newType = checkType;
831 newPriority = mRadioAttributes[mNetAttributes[newType].
832 mRadio].mPriority;
833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700836
837 if (newType != -1) {
838 newNet = mNetTrackers[newType];
839 /**
840 * See if the other network is available to fail over to.
841 * If is not available, we enable it anyway, so that it
842 * will be able to connect when it does become available,
843 * but we report a total loss of connectivity rather than
844 * report that we are attempting to fail over.
845 */
846 if (newNet.isAvailable()) {
847 NetworkInfo switchTo = newNet.getNetworkInfo();
848 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700849 if (!switchTo.isConnectedOrConnecting() ||
850 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700851 newNet.reconnect();
852 }
853 if (DBG) {
854 if (switchTo.isConnected()) {
855 Log.v(TAG, "Switching to already connected " +
856 switchTo.getTypeName());
857 } else {
858 Log.v(TAG, "Attempting to switch to " +
859 switchTo.getTypeName());
860 }
861 }
862 intent.putExtra(ConnectivityManager.
863 EXTRA_OTHER_NETWORK_INFO, switchTo);
864 } else {
Robert Greenwaltc7d25302009-09-17 14:58:16 -0700865 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
866 true);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867 newNet.reconnect();
868 }
869 } else {
870 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
871 true);
872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700874
875 // do this before we broadcast the change
876 handleConnectivityChange();
877
Mike Lockwood0f79b542009-08-14 14:18:49 -0400878 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 /*
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700880 * If the failover network is already connected, then immediately send
881 * out a followup broadcast indicating successful failover
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700883 if (newNet != null && newNet.getNetworkInfo().isConnected())
884 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
886
887 private void sendConnectedBroadcast(NetworkInfo info) {
888 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
889 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
890 if (info.isFailover()) {
891 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
892 info.setFailover(false);
893 }
894 if (info.getReason() != null) {
895 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
896 }
897 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700898 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
899 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400901 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903
904 /**
905 * Called when an attempt to fail over to another network has failed.
906 * @param info the {@link NetworkInfo} for the failed network
907 */
908 private void handleConnectionFailure(NetworkInfo info) {
909 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910
Robert Greenwalt42acef32009-08-12 16:08:25 -0700911 String reason = info.getReason();
912 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700913
Robert Greenwalt42acef32009-08-12 16:08:25 -0700914 if (DBG) {
915 String reasonText;
916 if (reason == null) {
917 reasonText = ".";
918 } else {
919 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700921 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
922 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700924
925 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
926 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
927 if (getActiveNetworkInfo() == null) {
928 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
929 }
930 if (reason != null) {
931 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
932 }
933 if (extraInfo != null) {
934 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
935 }
936 if (info.isFailover()) {
937 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
938 info.setFailover(false);
939 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400940 sendStickyBroadcast(intent);
941 }
942
943 private void sendStickyBroadcast(Intent intent) {
944 synchronized(this) {
945 if (mSystemReady) {
946 mContext.sendStickyBroadcast(intent);
947 } else {
948 if (mDeferredBroadcasts == null) {
949 mDeferredBroadcasts = new ArrayList<Intent>();
950 }
951 mDeferredBroadcasts.add(intent);
952 }
953 }
954 }
955
956 void systemReady() {
957 synchronized(this) {
958 mSystemReady = true;
959 if (mDeferredBroadcasts != null) {
960 int count = mDeferredBroadcasts.size();
961 for (int i = 0; i < count; i++) {
962 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
963 }
964 mDeferredBroadcasts = null;
965 }
966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968
969 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700970 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971
972 // snapshot isFailover, because sendConnectedBroadcast() resets it
973 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700974 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975
Robert Greenwalt42acef32009-08-12 16:08:25 -0700976 // if this is a default net and other default is running
977 // kill the one not preferred
978 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700979 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
980 if ((type != mNetworkPreference &&
981 mNetAttributes[mActiveDefaultNetwork].mPriority >
982 mNetAttributes[type].mPriority) ||
983 mNetworkPreference == mActiveDefaultNetwork) {
984 // don't accept this one
985 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
986 "to torn down network " + info.getTypeName());
987 teardown(thisNet);
988 return;
989 } else {
990 // tear down the other
991 NetworkStateTracker otherNet =
992 mNetTrackers[mActiveDefaultNetwork];
993 if (DBG) Log.v(TAG, "Policy requires " +
994 otherNet.getNetworkInfo().getTypeName() +
995 " teardown");
996 if (!teardown(otherNet)) {
997 Log.e(TAG, "Network declined teardown request");
998 return;
999 }
1000 if (isFailover) {
1001 otherNet.releaseWakeLock();
1002 }
1003 }
1004 }
1005 mActiveDefaultNetwork = type;
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001008 thisNet.updateNetworkSettings();
1009 handleConnectivityChange();
1010 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012
1013 private void handleScanResultsAvailable(NetworkInfo info) {
1014 int networkType = info.getType();
1015 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001016 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1017 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 mNetTrackers[networkType].interpretScanResultsAvailable();
1021 }
1022
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001023 private void handleNotificationChange(boolean visible, int id,
1024 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 NotificationManager notificationManager = (NotificationManager) mContext
1026 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 if (visible) {
1029 notificationManager.notify(id, notification);
1030 } else {
1031 notificationManager.cancel(id);
1032 }
1033 }
1034
1035 /**
1036 * After any kind of change in the connectivity state of any network,
1037 * make sure that anything that depends on the connectivity state of
1038 * more than one network is set up correctly. We're mainly concerned
1039 * with making sure that the list of DNS servers is set up according
1040 * to which networks are connected, and ensuring that the right routing
1041 * table entries exist.
1042 */
1043 private void handleConnectivityChange() {
1044 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001045 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001046 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 * If both mobile and wifi are enabled, add the host routes that
1048 * will allow MMS traffic to pass on the mobile network. But
1049 * remove the default route for the mobile network, so that there
1050 * will be only one default route, to ensure that all traffic
1051 * except MMS will travel via Wi-Fi.
1052 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001053 handleDnsConfigurationChange();
1054
1055 for (int netType : mPriorityList) {
1056 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1057 if (mNetAttributes[netType].isDefault()) {
1058 mNetTrackers[netType].addDefaultRoute();
1059 } else {
1060 mNetTrackers[netType].addPrivateDnsRoutes();
1061 }
1062 } else {
1063 if (mNetAttributes[netType].isDefault()) {
1064 mNetTrackers[netType].removeDefaultRoute();
1065 } else {
1066 mNetTrackers[netType].removePrivateDnsRoutes();
1067 }
1068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
1071
Robert Greenwalt42acef32009-08-12 16:08:25 -07001072 /**
1073 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1074 * on the highest priority active net which this process requested.
1075 * If there aren't any, clear it out
1076 */
1077 private void reassessPidDns(int myPid, boolean doBump)
1078 {
1079 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1080 for(int i : mPriorityList) {
1081 if (mNetAttributes[i].isDefault()) {
1082 continue;
1083 }
1084 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001085 if (nt.getNetworkInfo().isConnected() &&
1086 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 List pids = mNetRequestersPids[i];
1088 for (int j=0; j<pids.size(); j++) {
1089 Integer pid = (Integer)pids.get(j);
1090 if (pid.intValue() == myPid) {
1091 String[] dnsList = nt.getNameServers();
1092 writePidDns(dnsList, myPid);
1093 if (doBump) {
1094 bumpDns();
1095 }
1096 return;
1097 }
1098 }
1099 }
1100 }
1101 // nothing found - delete
1102 for (int i = 1; ; i++) {
1103 String prop = "net.dns" + i + "." + myPid;
1104 if (SystemProperties.get(prop).length() == 0) {
1105 if (doBump) {
1106 bumpDns();
1107 }
1108 return;
1109 }
1110 SystemProperties.set(prop, "");
1111 }
1112 }
1113
1114 private void writePidDns(String[] dnsList, int pid) {
1115 int j = 1;
1116 for (String dns : dnsList) {
1117 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1118 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1119 }
1120 }
1121 }
1122
1123 private void bumpDns() {
1124 /*
1125 * Bump the property that tells the name resolver library to reread
1126 * the DNS server list from the properties.
1127 */
1128 String propVal = SystemProperties.get("net.dnschange");
1129 int n = 0;
1130 if (propVal.length() != 0) {
1131 try {
1132 n = Integer.parseInt(propVal);
1133 } catch (NumberFormatException e) {}
1134 }
1135 SystemProperties.set("net.dnschange", "" + (n+1));
1136 }
1137
1138 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001139 // add default net's dns entries
1140 for (int x = mPriorityList.length-1; x>= 0; x--) {
1141 int netType = mPriorityList[x];
1142 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001143 if (nt != null && nt.getNetworkInfo().isConnected() &&
1144 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001146 if (mNetAttributes[netType].isDefault()) {
1147 int j = 1;
1148 for (String dns : dnsList) {
1149 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001150 if (DBG) {
1151 Log.d(TAG, "adding dns " + dns + " for " +
1152 nt.getNetworkInfo().getTypeName());
1153 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001154 SystemProperties.set("net.dns" + j++, dns);
1155 }
1156 }
1157 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001158 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1159 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001160 }
1161 mNumDnsEntries = j;
1162 } else {
1163 // set per-pid dns for attached secondary nets
1164 List pids = mNetRequestersPids[netType];
1165 for (int y=0; y< pids.size(); y++) {
1166 Integer pid = (Integer)pids.get(y);
1167 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 }
1170 }
1171 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172
1173 bumpDns();
1174 }
1175
1176 private int getRestoreDefaultNetworkDelay() {
1177 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1178 NETWORK_RESTORE_DELAY_PROP_NAME);
1179 if(restoreDefaultNetworkDelayStr != null &&
1180 restoreDefaultNetworkDelayStr.length() != 0) {
1181 try {
1182 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1183 } catch (NumberFormatException e) {
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001186 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188
1189 @Override
1190 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001191 if (mContext.checkCallingOrSelfPermission(
1192 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001194 pw.println("Permission Denial: can't dump ConnectivityService " +
1195 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1196 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 return;
1198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 pw.println();
1200 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001201 if (nst.getNetworkInfo().isConnected()) {
1202 pw.println("Active network: " + nst.getNetworkInfo().
1203 getTypeName());
1204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 pw.println(nst.getNetworkInfo());
1206 pw.println(nst);
1207 pw.println();
1208 }
1209 }
1210
Robert Greenwalt42acef32009-08-12 16:08:25 -07001211 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 private class MyHandler extends Handler {
1213 @Override
1214 public void handleMessage(Message msg) {
1215 NetworkInfo info;
1216 switch (msg.what) {
1217 case NetworkStateTracker.EVENT_STATE_CHANGED:
1218 info = (NetworkInfo) msg.obj;
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001219 int type = info.getType();
1220 NetworkInfo.State state = info.getState();
1221 if (mNetAttributes[type].mLastState == state) {
1222 if (DBG) {
1223 // TODO - remove this after we validate the dropping doesn't break anything
1224 Log.d(TAG, "Dropping ConnectivityChange for " +
1225 info.getTypeName() + ": " +
1226 state + "/" + info.getDetailedState());
1227 }
1228 return;
1229 }
1230 mNetAttributes[type].mLastState = state;
1231
Robert Greenwalt42acef32009-08-12 16:08:25 -07001232 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001233 info.getTypeName() + ": " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001234 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235
1236 // Connectivity state changed:
1237 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001238 // [12-9] Network subtype (for mobile network, as defined
1239 // by TelephonyManager)
1240 // [8-3] Detailed state ordinal (as defined by
1241 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 // [2-0] Network type (as defined by ConnectivityManager)
1243 int eventLogParam = (info.getType() & 0x7) |
1244 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1245 (info.getSubtype() << 9);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001246 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1247 eventLogParam);
1248
1249 if (info.getDetailedState() ==
1250 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 handleConnectionFailure(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001252 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 handleDisconnect(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001254 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 // 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);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001263 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 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}