blob: 3d7025d131ddafe9ba9e3d574ac0e5ba451d695c [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;
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800457 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700458
459 FeatureUser(int type, String feature, IBinder binder) {
460 super();
461 mNetworkType = type;
462 mFeature = feature;
463 mBinder = binder;
464 mPid = getCallingPid();
465 mUid = getCallingUid();
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800466 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700467
Robert Greenwalt42acef32009-08-12 16:08:25 -0700468 try {
469 mBinder.linkToDeath(this, 0);
470 } catch (RemoteException e) {
471 binderDied();
472 }
473 }
474
475 void unlinkDeathRecipient() {
476 mBinder.unlinkToDeath(this, 0);
477 }
478
479 public void binderDied() {
480 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800481 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
482 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700483 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700484 }
485
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700486 public void expire() {
487 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800488 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
489 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700490 stopUsingNetworkFeature(this, false);
491 }
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800492
493 public String toString() {
494 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
495 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
496 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 }
498
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700499 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 public int startUsingNetworkFeature(int networkType, String feature,
501 IBinder binder) {
502 if (DBG) {
503 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
504 ": " + feature);
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 enforceChangePermission();
507 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700511 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512
513 // TODO - move this into the MobileDataStateTracker
514 int usedNetworkType = networkType;
515 if(networkType == ConnectivityManager.TYPE_MOBILE) {
516 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
517 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
518 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
519 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
520 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
521 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
522 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
523 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
524 }
525 }
526 NetworkStateTracker network = mNetTrackers[usedNetworkType];
527 if (network != null) {
528 if (usedNetworkType != networkType) {
529 Integer currentPid = new Integer(getCallingPid());
530
531 NetworkStateTracker radio = mNetTrackers[networkType];
532 NetworkInfo ni = network.getNetworkInfo();
533
534 if (ni.isAvailable() == false) {
535 if (DBG) Log.d(TAG, "special network not available");
536 return Phone.APN_TYPE_NOT_AVAILABLE;
537 }
538
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700539 synchronized(this) {
540 mFeatureUsers.add(f);
541 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
542 // this gets used for per-pid dns when connected
543 mNetRequestersPids[usedNetworkType].add(currentPid);
544 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700545 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700546 mHandler.sendMessageDelayed(mHandler.obtainMessage(
547 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
548 f), getRestoreDefaultNetworkDelay());
549
Robert Greenwalt42acef32009-08-12 16:08:25 -0700550
Robert Greenwalta64bf832009-08-19 20:19:33 -0700551 if ((ni.isConnectedOrConnecting() == true) &&
552 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700553 if (ni.isConnected() == true) {
554 // add the pid-specific dns
555 handleDnsConfigurationChange();
556 if (DBG) Log.d(TAG, "special network already active");
557 return Phone.APN_ALREADY_ACTIVE;
558 }
559 if (DBG) Log.d(TAG, "special network already connecting");
560 return Phone.APN_REQUEST_STARTED;
561 }
562
563 // check if the radio in play can make another contact
564 // assume if cannot for now
565
Robert Greenwalt42acef32009-08-12 16:08:25 -0700566 if (DBG) Log.d(TAG, "reconnecting to special network");
567 network.reconnect();
568 return Phone.APN_REQUEST_STARTED;
569 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700570 synchronized(this) {
571 mFeatureUsers.add(f);
572 }
573 mHandler.sendMessageDelayed(mHandler.obtainMessage(
574 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
575 f), getRestoreDefaultNetworkDelay());
576
Robert Greenwalt42acef32009-08-12 16:08:25 -0700577 return network.startUsingNetworkFeature(feature,
578 getCallingPid(), getCallingUid());
579 }
580 }
581 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700584 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700586 int pid = getCallingPid();
587 int uid = getCallingUid();
588
589 FeatureUser u = null;
590 boolean found = false;
591
592 synchronized(this) {
593 for (int i = 0; i < mFeatureUsers.size() ; i++) {
594 u = (FeatureUser)mFeatureUsers.get(i);
595 if (uid == u.mUid && pid == u.mPid &&
596 networkType == u.mNetworkType &&
597 TextUtils.equals(feature, u.mFeature)) {
598 found = true;
599 break;
600 }
601 }
602 }
603 if (found && u != null) {
604 // stop regardless of how many other time this proc had called start
605 return stopUsingNetworkFeature(u, true);
606 } else {
607 // none found!
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800608 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700609 return 1;
610 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700611 }
612
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700613 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
614 int networkType = u.mNetworkType;
615 String feature = u.mFeature;
616 int pid = u.mPid;
617 int uid = u.mUid;
618
619 NetworkStateTracker tracker = null;
620 boolean callTeardown = false; // used to carry our decision outside of sync block
621
Robert Greenwalt42acef32009-08-12 16:08:25 -0700622 if (DBG) {
623 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
624 ": " + feature);
625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 enforceChangePermission();
627 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
628 return -1;
629 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700630
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700631 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
632 // sync block
633 synchronized(this) {
634 // check if this process still has an outstanding start request
635 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700636 return 1;
637 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700638 u.unlinkDeathRecipient();
639 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
640 // If we care about duplicate requests, check for that here.
641 //
642 // This is done to support the extension of a request - the app
643 // can request we start the network feature again and renew the
644 // auto-shutoff delay. Normal "stop" calls from the app though
645 // do not pay attention to duplicate requests - in effect the
646 // API does not refcount and a single stop will counter multiple starts.
647 if (ignoreDups == false) {
648 for (int i = 0; i < mFeatureUsers.size() ; i++) {
649 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
650 if (x.mUid == u.mUid && x.mPid == u.mPid &&
651 x.mNetworkType == u.mNetworkType &&
652 TextUtils.equals(x.mFeature, u.mFeature)) {
Robert Greenwalt73912ce2009-12-21 18:24:07 -0800653 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700654 return 1;
655 }
656 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700657 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700658
659 // TODO - move to MobileDataStateTracker
660 int usedNetworkType = networkType;
661 if (networkType == ConnectivityManager.TYPE_MOBILE) {
662 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
663 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
664 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
665 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
666 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
667 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
668 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
669 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
670 }
671 }
672 tracker = mNetTrackers[usedNetworkType];
673 if(usedNetworkType != networkType) {
674 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700675 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt15abc902009-12-17 14:54:59 -0800676 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700677 if (mNetRequestersPids[usedNetworkType].size() != 0) {
678 if (DBG) Log.d(TAG, "not tearing down special network - " +
679 "others still using it");
680 return 1;
681 }
682 callTeardown = true;
683 }
684 }
685
686 if (callTeardown) {
687 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700688 return 1;
689 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700690 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700691 return tracker.stopUsingNetworkFeature(feature, pid, uid);
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694
695 /**
696 * Ensure that a network route exists to deliver traffic to the specified
697 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700698 * @param networkType the type of the network over which traffic to the
699 * specified host is to be routed
700 * @param hostAddress the IP address of the host to which the route is
701 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 * @return {@code true} on success, {@code false} on failure
703 */
704 public boolean requestRouteToHost(int networkType, int hostAddress) {
705 enforceChangePermission();
706 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
707 return false;
708 }
709 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700710
711 if (!tracker.getNetworkInfo().isConnected() || tracker.isTeardownRequested()) {
712 if (DBG) {
713 Log.d(TAG, "requestRouteToHost on down network (" + networkType + " - dropped");
714 }
715 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700717 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719
720 /**
721 * @see ConnectivityManager#getBackgroundDataSetting()
722 */
723 public boolean getBackgroundDataSetting() {
724 return Settings.Secure.getInt(mContext.getContentResolver(),
725 Settings.Secure.BACKGROUND_DATA, 1) == 1;
726 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 /**
729 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
730 */
731 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
732 mContext.enforceCallingOrSelfPermission(
733 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
734 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
737
738 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700739 Settings.Secure.BACKGROUND_DATA,
740 allowBackgroundDataUsage ? 1 : 0);
741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 Intent broadcast = new Intent(
743 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
744 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700745 }
746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 private int getNumConnectedNetworks() {
748 int numConnectedNets = 0;
749
750 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700751 if (nt.getNetworkInfo().isConnected() &&
752 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 ++numConnectedNets;
754 }
755 }
756 return numConnectedNets;
757 }
758
759 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700760 mContext.enforceCallingOrSelfPermission(
761 android.Manifest.permission.ACCESS_NETWORK_STATE,
762 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700766 mContext.enforceCallingOrSelfPermission(
767 android.Manifest.permission.CHANGE_NETWORK_STATE,
768 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770
771 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700772 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
773 * network, we ignore it. If it is for the active network, we send out a
774 * broadcast. But first, we check whether it might be possible to connect
775 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * @param info the {@code NetworkInfo} for the network
777 */
778 private void handleDisconnect(NetworkInfo info) {
779
Robert Greenwalt42acef32009-08-12 16:08:25 -0700780 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781
Robert Greenwalt42acef32009-08-12 16:08:25 -0700782 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 /*
784 * If the disconnected network is not the active one, then don't report
785 * this as a loss of connectivity. What probably happened is that we're
786 * getting the disconnect for a network that we explicitly disabled
787 * in accordance with network preference policies.
788 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700789 if (!mNetAttributes[prevNetType].isDefault()) {
790 List pids = mNetRequestersPids[prevNetType];
791 for (int i = 0; i<pids.size(); i++) {
792 Integer pid = (Integer)pids.get(i);
793 // will remove them because the net's no longer connected
794 // need to do this now as only now do we know the pids and
795 // can properly null things that are no longer referenced.
796 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
801 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
802 if (info.isFailover()) {
803 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
804 info.setFailover(false);
805 }
806 if (info.getReason() != null) {
807 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
808 }
809 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700810 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
811 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700813
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800814 NetworkStateTracker newNet = null;
815 if (mNetAttributes[prevNetType].isDefault()) {
816 newNet = tryFailover(prevNetType);
817 if (newNet != null) {
818 NetworkInfo switchTo = newNet.getNetworkInfo();
819 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
820 } else {
821 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
822 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800823 }
824 // do this before we broadcast the change
825 handleConnectivityChange();
826
827 sendStickyBroadcast(intent);
828 /*
829 * If the failover network is already connected, then immediately send
830 * out a followup broadcast indicating successful failover
831 */
832 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
833 sendConnectedBroadcast(newNet.getNetworkInfo());
834 }
835 }
836
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800837 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800838 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700839 /*
840 * If this is a default network, check if other defaults are available
841 * or active
842 */
843 NetworkStateTracker newNet = null;
844 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700845 if (mActiveDefaultNetwork == prevNetType) {
846 mActiveDefaultNetwork = -1;
847 }
848
849 int newType = -1;
850 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800851 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700852 if (checkType == prevNetType) {
853 continue;
854 }
855 if (mNetAttributes[checkType].isDefault()) {
856 /* TODO - if we have multiple nets we could use
857 * we may want to put more thought into which we choose
858 */
859 if (checkType == mNetworkPreference) {
860 newType = checkType;
861 break;
862 }
863 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
864 mPriority > newPriority) {
865 newType = checkType;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800866 newPriority = mRadioAttributes[mNetAttributes[newType].mRadio].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 }
869 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700870
871 if (newType != -1) {
872 newNet = mNetTrackers[newType];
873 /**
874 * See if the other network is available to fail over to.
875 * If is not available, we enable it anyway, so that it
876 * will be able to connect when it does become available,
877 * but we report a total loss of connectivity rather than
878 * report that we are attempting to fail over.
879 */
880 if (newNet.isAvailable()) {
881 NetworkInfo switchTo = newNet.getNetworkInfo();
882 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700883 if (!switchTo.isConnectedOrConnecting() ||
884 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700885 newNet.reconnect();
886 }
887 if (DBG) {
888 if (switchTo.isConnected()) {
889 Log.v(TAG, "Switching to already connected " +
890 switchTo.getTypeName());
891 } else {
892 Log.v(TAG, "Attempting to switch to " +
893 switchTo.getTypeName());
894 }
895 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700896 } else {
897 newNet.reconnect();
898 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700901
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800902 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
905 private void sendConnectedBroadcast(NetworkInfo info) {
906 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
907 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
908 if (info.isFailover()) {
909 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
910 info.setFailover(false);
911 }
912 if (info.getReason() != null) {
913 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
914 }
915 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700916 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
917 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400919 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
921
922 /**
923 * Called when an attempt to fail over to another network has failed.
924 * @param info the {@link NetworkInfo} for the failed network
925 */
926 private void handleConnectionFailure(NetworkInfo info) {
927 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928
Robert Greenwalt42acef32009-08-12 16:08:25 -0700929 String reason = info.getReason();
930 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700931
Robert Greenwalt42acef32009-08-12 16:08:25 -0700932 if (DBG) {
933 String reasonText;
934 if (reason == null) {
935 reasonText = ".";
936 } else {
937 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700939 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
940 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700942
943 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
944 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
945 if (getActiveNetworkInfo() == null) {
946 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
947 }
948 if (reason != null) {
949 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
950 }
951 if (extraInfo != null) {
952 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
953 }
954 if (info.isFailover()) {
955 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
956 info.setFailover(false);
957 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800958
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800959 NetworkStateTracker newNet = null;
960 if (mNetAttributes[info.getType()].isDefault()) {
961 newNet = tryFailover(info.getType());
962 if (newNet != null) {
963 NetworkInfo switchTo = newNet.getNetworkInfo();
964 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
965 } else {
966 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
967 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800968 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800969
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800970 // do this before we broadcast the change
971 handleConnectivityChange();
972
Mike Lockwood0f79b542009-08-14 14:18:49 -0400973 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800974 /*
975 * If the failover network is already connected, then immediately send
976 * out a followup broadcast indicating successful failover
977 */
978 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
979 sendConnectedBroadcast(newNet.getNetworkInfo());
980 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400981 }
982
983 private void sendStickyBroadcast(Intent intent) {
984 synchronized(this) {
985 if (mSystemReady) {
986 mContext.sendStickyBroadcast(intent);
987 } else {
988 if (mDeferredBroadcasts == null) {
989 mDeferredBroadcasts = new ArrayList<Intent>();
990 }
991 mDeferredBroadcasts.add(intent);
992 }
993 }
994 }
995
996 void systemReady() {
997 synchronized(this) {
998 mSystemReady = true;
999 if (mDeferredBroadcasts != null) {
1000 int count = mDeferredBroadcasts.size();
1001 for (int i = 0; i < count; i++) {
1002 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
1003 }
1004 mDeferredBroadcasts = null;
1005 }
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008
1009 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001010 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011
1012 // snapshot isFailover, because sendConnectedBroadcast() resets it
1013 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001014 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
Robert Greenwalt42acef32009-08-12 16:08:25 -07001016 // if this is a default net and other default is running
1017 // kill the one not preferred
1018 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001019 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1020 if ((type != mNetworkPreference &&
1021 mNetAttributes[mActiveDefaultNetwork].mPriority >
1022 mNetAttributes[type].mPriority) ||
1023 mNetworkPreference == mActiveDefaultNetwork) {
1024 // don't accept this one
1025 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
1026 "to torn down network " + info.getTypeName());
1027 teardown(thisNet);
1028 return;
1029 } else {
1030 // tear down the other
1031 NetworkStateTracker otherNet =
1032 mNetTrackers[mActiveDefaultNetwork];
1033 if (DBG) Log.v(TAG, "Policy requires " +
1034 otherNet.getNetworkInfo().getTypeName() +
1035 " teardown");
1036 if (!teardown(otherNet)) {
1037 Log.e(TAG, "Network declined teardown request");
1038 return;
1039 }
1040 if (isFailover) {
1041 otherNet.releaseWakeLock();
1042 }
1043 }
1044 }
1045 mActiveDefaultNetwork = type;
1046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001048 thisNet.updateNetworkSettings();
1049 handleConnectivityChange();
1050 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
1053 private void handleScanResultsAvailable(NetworkInfo info) {
1054 int networkType = info.getType();
1055 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001056 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1057 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mNetTrackers[networkType].interpretScanResultsAvailable();
1061 }
1062
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001063 private void handleNotificationChange(boolean visible, int id,
1064 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 NotificationManager notificationManager = (NotificationManager) mContext
1066 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 if (visible) {
1069 notificationManager.notify(id, notification);
1070 } else {
1071 notificationManager.cancel(id);
1072 }
1073 }
1074
1075 /**
1076 * After any kind of change in the connectivity state of any network,
1077 * make sure that anything that depends on the connectivity state of
1078 * more than one network is set up correctly. We're mainly concerned
1079 * with making sure that the list of DNS servers is set up according
1080 * to which networks are connected, and ensuring that the right routing
1081 * table entries exist.
1082 */
1083 private void handleConnectivityChange() {
1084 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001085 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001086 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 * If both mobile and wifi are enabled, add the host routes that
1088 * will allow MMS traffic to pass on the mobile network. But
1089 * remove the default route for the mobile network, so that there
1090 * will be only one default route, to ensure that all traffic
1091 * except MMS will travel via Wi-Fi.
1092 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001093 handleDnsConfigurationChange();
1094
1095 for (int netType : mPriorityList) {
1096 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1097 if (mNetAttributes[netType].isDefault()) {
1098 mNetTrackers[netType].addDefaultRoute();
1099 } else {
1100 mNetTrackers[netType].addPrivateDnsRoutes();
1101 }
1102 } else {
1103 if (mNetAttributes[netType].isDefault()) {
1104 mNetTrackers[netType].removeDefaultRoute();
1105 } else {
1106 mNetTrackers[netType].removePrivateDnsRoutes();
1107 }
1108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110 }
1111
Robert Greenwalt42acef32009-08-12 16:08:25 -07001112 /**
1113 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1114 * on the highest priority active net which this process requested.
1115 * If there aren't any, clear it out
1116 */
1117 private void reassessPidDns(int myPid, boolean doBump)
1118 {
1119 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1120 for(int i : mPriorityList) {
1121 if (mNetAttributes[i].isDefault()) {
1122 continue;
1123 }
1124 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001125 if (nt.getNetworkInfo().isConnected() &&
1126 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001127 List pids = mNetRequestersPids[i];
1128 for (int j=0; j<pids.size(); j++) {
1129 Integer pid = (Integer)pids.get(j);
1130 if (pid.intValue() == myPid) {
1131 String[] dnsList = nt.getNameServers();
1132 writePidDns(dnsList, myPid);
1133 if (doBump) {
1134 bumpDns();
1135 }
1136 return;
1137 }
1138 }
1139 }
1140 }
1141 // nothing found - delete
1142 for (int i = 1; ; i++) {
1143 String prop = "net.dns" + i + "." + myPid;
1144 if (SystemProperties.get(prop).length() == 0) {
1145 if (doBump) {
1146 bumpDns();
1147 }
1148 return;
1149 }
1150 SystemProperties.set(prop, "");
1151 }
1152 }
1153
1154 private void writePidDns(String[] dnsList, int pid) {
1155 int j = 1;
1156 for (String dns : dnsList) {
1157 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1158 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1159 }
1160 }
1161 }
1162
1163 private void bumpDns() {
1164 /*
1165 * Bump the property that tells the name resolver library to reread
1166 * the DNS server list from the properties.
1167 */
1168 String propVal = SystemProperties.get("net.dnschange");
1169 int n = 0;
1170 if (propVal.length() != 0) {
1171 try {
1172 n = Integer.parseInt(propVal);
1173 } catch (NumberFormatException e) {}
1174 }
1175 SystemProperties.set("net.dnschange", "" + (n+1));
1176 }
1177
1178 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001179 // add default net's dns entries
1180 for (int x = mPriorityList.length-1; x>= 0; x--) {
1181 int netType = mPriorityList[x];
1182 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001183 if (nt != null && nt.getNetworkInfo().isConnected() &&
1184 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001186 if (mNetAttributes[netType].isDefault()) {
1187 int j = 1;
1188 for (String dns : dnsList) {
1189 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001190 if (DBG) {
1191 Log.d(TAG, "adding dns " + dns + " for " +
1192 nt.getNetworkInfo().getTypeName());
1193 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001194 SystemProperties.set("net.dns" + j++, dns);
1195 }
1196 }
1197 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001198 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1199 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001200 }
1201 mNumDnsEntries = j;
1202 } else {
1203 // set per-pid dns for attached secondary nets
1204 List pids = mNetRequestersPids[netType];
1205 for (int y=0; y< pids.size(); y++) {
1206 Integer pid = (Integer)pids.get(y);
1207 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209 }
1210 }
1211 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001212
1213 bumpDns();
1214 }
1215
1216 private int getRestoreDefaultNetworkDelay() {
1217 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1218 NETWORK_RESTORE_DELAY_PROP_NAME);
1219 if(restoreDefaultNetworkDelayStr != null &&
1220 restoreDefaultNetworkDelayStr.length() != 0) {
1221 try {
1222 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1223 } catch (NumberFormatException e) {
1224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001226 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228
1229 @Override
1230 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001231 if (mContext.checkCallingOrSelfPermission(
1232 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001234 pw.println("Permission Denial: can't dump ConnectivityService " +
1235 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1236 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 return;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 pw.println();
1240 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt73912ce2009-12-21 18:24:07 -08001241 if (nst != null) {
1242 if (nst.getNetworkInfo().isConnected()) {
1243 pw.println("Active network: " + nst.getNetworkInfo().
1244 getTypeName());
1245 }
1246 pw.println(nst.getNetworkInfo());
1247 pw.println(nst);
1248 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
Robert Greenwalt73912ce2009-12-21 18:24:07 -08001251
1252 pw.println("Network Requester Pids:");
1253 for (int net : mPriorityList) {
1254 String pidString = net + ": ";
1255 for (Object pid : mNetRequestersPids[net]) {
1256 pidString = pidString + pid.toString() + ", ";
1257 }
1258 pw.println(pidString);
1259 }
1260 pw.println();
1261
1262 pw.println("FeatureUsers:");
1263 for (Object requester : mFeatureUsers) {
1264 pw.println(requester.toString());
1265 }
1266 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268
Robert Greenwalt42acef32009-08-12 16:08:25 -07001269 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 private class MyHandler extends Handler {
1271 @Override
1272 public void handleMessage(Message msg) {
1273 NetworkInfo info;
1274 switch (msg.what) {
1275 case NetworkStateTracker.EVENT_STATE_CHANGED:
1276 info = (NetworkInfo) msg.obj;
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001277 int type = info.getType();
1278 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001279 // only do this optimization for wifi. It going into scan mode for location
1280 // services generates alot of noise. Meanwhile the mms apn won't send out
1281 // subsequent notifications when on default cellular because it never
1282 // disconnects.. so only do this to wifi notifications. Fixed better when the
1283 // APN notifications are standardized.
1284 if (mNetAttributes[type].mLastState == state &&
1285 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001286 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001287 // TODO - remove this after we validate the dropping doesn't break
1288 // anything
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001289 Log.d(TAG, "Dropping ConnectivityChange for " +
1290 info.getTypeName() + ": " +
1291 state + "/" + info.getDetailedState());
1292 }
1293 return;
1294 }
1295 mNetAttributes[type].mLastState = state;
1296
Robert Greenwalt42acef32009-08-12 16:08:25 -07001297 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001298 info.getTypeName() + ": " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001299 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300
1301 // Connectivity state changed:
1302 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001303 // [12-9] Network subtype (for mobile network, as defined
1304 // by TelephonyManager)
1305 // [8-3] Detailed state ordinal (as defined by
1306 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 // [2-0] Network type (as defined by ConnectivityManager)
1308 int eventLogParam = (info.getType() & 0x7) |
1309 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1310 (info.getSubtype() << 9);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001311 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1312 eventLogParam);
1313
1314 if (info.getDetailedState() ==
1315 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 handleConnectionFailure(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001317 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 handleDisconnect(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001319 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001321 // the logic here is, handle SUSPENDED the same as
1322 // DISCONNECTED. The only difference being we are
1323 // broadcasting an intent with NetworkInfo that's
1324 // suspended. This allows the applications an
1325 // opportunity to handle DISCONNECTED and SUSPENDED
1326 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 handleDisconnect(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001328 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 handleConnect(info);
1330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 break;
1332
1333 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1334 info = (NetworkInfo) msg.obj;
1335 handleScanResultsAvailable(info);
1336 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001339 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1340 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001343 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 break;
1345
1346 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1347 // fill me in
1348 break;
1349
1350 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1351 // fill me in
1352 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001353 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001354 FeatureUser u = (FeatureUser)msg.obj;
1355 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001356 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358 }
1359 }
1360}