blob: 45eb6721092e276233cab1f3fa5abc276df2fe24 [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 Greenwaltda03c4e2010-01-20 19:29:41 -0800814 NetworkStateTracker newNet = tryFailover(prevNetType);
815 if (newNet != null) {
816 NetworkInfo switchTo = newNet.getNetworkInfo();
817 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
818 } else {
819 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
820 }
821 // do this before we broadcast the change
822 handleConnectivityChange();
823
824 sendStickyBroadcast(intent);
825 /*
826 * If the failover network is already connected, then immediately send
827 * out a followup broadcast indicating successful failover
828 */
829 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
830 sendConnectedBroadcast(newNet.getNetworkInfo());
831 }
832 }
833
834 // returns -1 if no failover available
835 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700836 /*
837 * If this is a default network, check if other defaults are available
838 * or active
839 */
840 NetworkStateTracker newNet = null;
841 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700842 if (mActiveDefaultNetwork == prevNetType) {
843 mActiveDefaultNetwork = -1;
844 }
845
846 int newType = -1;
847 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800848 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700849 if (checkType == prevNetType) {
850 continue;
851 }
852 if (mNetAttributes[checkType].isDefault()) {
853 /* TODO - if we have multiple nets we could use
854 * we may want to put more thought into which we choose
855 */
856 if (checkType == mNetworkPreference) {
857 newType = checkType;
858 break;
859 }
860 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
861 mPriority > newPriority) {
862 newType = checkType;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800863 newPriority = mRadioAttributes[mNetAttributes[newType].mRadio].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867
868 if (newType != -1) {
869 newNet = mNetTrackers[newType];
870 /**
871 * See if the other network is available to fail over to.
872 * If is not available, we enable it anyway, so that it
873 * will be able to connect when it does become available,
874 * but we report a total loss of connectivity rather than
875 * report that we are attempting to fail over.
876 */
877 if (newNet.isAvailable()) {
878 NetworkInfo switchTo = newNet.getNetworkInfo();
879 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700880 if (!switchTo.isConnectedOrConnecting() ||
881 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700882 newNet.reconnect();
883 }
884 if (DBG) {
885 if (switchTo.isConnected()) {
886 Log.v(TAG, "Switching to already connected " +
887 switchTo.getTypeName());
888 } else {
889 Log.v(TAG, "Attempting to switch to " +
890 switchTo.getTypeName());
891 }
892 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700893 } else {
894 newNet.reconnect();
895 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700898
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800899 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
901
902 private void sendConnectedBroadcast(NetworkInfo info) {
903 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
904 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
905 if (info.isFailover()) {
906 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
907 info.setFailover(false);
908 }
909 if (info.getReason() != null) {
910 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
911 }
912 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700913 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
914 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400916 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918
919 /**
920 * Called when an attempt to fail over to another network has failed.
921 * @param info the {@link NetworkInfo} for the failed network
922 */
923 private void handleConnectionFailure(NetworkInfo info) {
924 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
Robert Greenwalt42acef32009-08-12 16:08:25 -0700926 String reason = info.getReason();
927 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700928
Robert Greenwalt42acef32009-08-12 16:08:25 -0700929 if (DBG) {
930 String reasonText;
931 if (reason == null) {
932 reasonText = ".";
933 } else {
934 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700936 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
937 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700939
940 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
941 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
942 if (getActiveNetworkInfo() == null) {
943 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
944 }
945 if (reason != null) {
946 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
947 }
948 if (extraInfo != null) {
949 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
950 }
951 if (info.isFailover()) {
952 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
953 info.setFailover(false);
954 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800955
956 NetworkStateTracker newNet = tryFailover(info.getType());
957 if (newNet != null) {
958 NetworkInfo switchTo = newNet.getNetworkInfo();
959 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
960 } else {
961 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
962 }
963 // do this before we broadcast the change
964 handleConnectivityChange();
965
Mike Lockwood0f79b542009-08-14 14:18:49 -0400966 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800967 /*
968 * If the failover network is already connected, then immediately send
969 * out a followup broadcast indicating successful failover
970 */
971 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
972 sendConnectedBroadcast(newNet.getNetworkInfo());
973 }
Mike Lockwood0f79b542009-08-14 14:18:49 -0400974 }
975
976 private void sendStickyBroadcast(Intent intent) {
977 synchronized(this) {
978 if (mSystemReady) {
979 mContext.sendStickyBroadcast(intent);
980 } else {
981 if (mDeferredBroadcasts == null) {
982 mDeferredBroadcasts = new ArrayList<Intent>();
983 }
984 mDeferredBroadcasts.add(intent);
985 }
986 }
987 }
988
989 void systemReady() {
990 synchronized(this) {
991 mSystemReady = true;
992 if (mDeferredBroadcasts != null) {
993 int count = mDeferredBroadcasts.size();
994 for (int i = 0; i < count; i++) {
995 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
996 }
997 mDeferredBroadcasts = null;
998 }
999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001003 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004
1005 // snapshot isFailover, because sendConnectedBroadcast() resets it
1006 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001007 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008
Robert Greenwalt42acef32009-08-12 16:08:25 -07001009 // if this is a default net and other default is running
1010 // kill the one not preferred
1011 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001012 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1013 if ((type != mNetworkPreference &&
1014 mNetAttributes[mActiveDefaultNetwork].mPriority >
1015 mNetAttributes[type].mPriority) ||
1016 mNetworkPreference == mActiveDefaultNetwork) {
1017 // don't accept this one
1018 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
1019 "to torn down network " + info.getTypeName());
1020 teardown(thisNet);
1021 return;
1022 } else {
1023 // tear down the other
1024 NetworkStateTracker otherNet =
1025 mNetTrackers[mActiveDefaultNetwork];
1026 if (DBG) Log.v(TAG, "Policy requires " +
1027 otherNet.getNetworkInfo().getTypeName() +
1028 " teardown");
1029 if (!teardown(otherNet)) {
1030 Log.e(TAG, "Network declined teardown request");
1031 return;
1032 }
1033 if (isFailover) {
1034 otherNet.releaseWakeLock();
1035 }
1036 }
1037 }
1038 mActiveDefaultNetwork = type;
1039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001041 thisNet.updateNetworkSettings();
1042 handleConnectivityChange();
1043 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045
1046 private void handleScanResultsAvailable(NetworkInfo info) {
1047 int networkType = info.getType();
1048 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001049 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1050 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 mNetTrackers[networkType].interpretScanResultsAvailable();
1054 }
1055
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001056 private void handleNotificationChange(boolean visible, int id,
1057 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 NotificationManager notificationManager = (NotificationManager) mContext
1059 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 if (visible) {
1062 notificationManager.notify(id, notification);
1063 } else {
1064 notificationManager.cancel(id);
1065 }
1066 }
1067
1068 /**
1069 * After any kind of change in the connectivity state of any network,
1070 * make sure that anything that depends on the connectivity state of
1071 * more than one network is set up correctly. We're mainly concerned
1072 * with making sure that the list of DNS servers is set up according
1073 * to which networks are connected, and ensuring that the right routing
1074 * table entries exist.
1075 */
1076 private void handleConnectivityChange() {
1077 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001078 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001079 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 * If both mobile and wifi are enabled, add the host routes that
1081 * will allow MMS traffic to pass on the mobile network. But
1082 * remove the default route for the mobile network, so that there
1083 * will be only one default route, to ensure that all traffic
1084 * except MMS will travel via Wi-Fi.
1085 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001086 handleDnsConfigurationChange();
1087
1088 for (int netType : mPriorityList) {
1089 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1090 if (mNetAttributes[netType].isDefault()) {
1091 mNetTrackers[netType].addDefaultRoute();
1092 } else {
1093 mNetTrackers[netType].addPrivateDnsRoutes();
1094 }
1095 } else {
1096 if (mNetAttributes[netType].isDefault()) {
1097 mNetTrackers[netType].removeDefaultRoute();
1098 } else {
1099 mNetTrackers[netType].removePrivateDnsRoutes();
1100 }
1101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 }
1104
Robert Greenwalt42acef32009-08-12 16:08:25 -07001105 /**
1106 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1107 * on the highest priority active net which this process requested.
1108 * If there aren't any, clear it out
1109 */
1110 private void reassessPidDns(int myPid, boolean doBump)
1111 {
1112 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1113 for(int i : mPriorityList) {
1114 if (mNetAttributes[i].isDefault()) {
1115 continue;
1116 }
1117 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001118 if (nt.getNetworkInfo().isConnected() &&
1119 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001120 List pids = mNetRequestersPids[i];
1121 for (int j=0; j<pids.size(); j++) {
1122 Integer pid = (Integer)pids.get(j);
1123 if (pid.intValue() == myPid) {
1124 String[] dnsList = nt.getNameServers();
1125 writePidDns(dnsList, myPid);
1126 if (doBump) {
1127 bumpDns();
1128 }
1129 return;
1130 }
1131 }
1132 }
1133 }
1134 // nothing found - delete
1135 for (int i = 1; ; i++) {
1136 String prop = "net.dns" + i + "." + myPid;
1137 if (SystemProperties.get(prop).length() == 0) {
1138 if (doBump) {
1139 bumpDns();
1140 }
1141 return;
1142 }
1143 SystemProperties.set(prop, "");
1144 }
1145 }
1146
1147 private void writePidDns(String[] dnsList, int pid) {
1148 int j = 1;
1149 for (String dns : dnsList) {
1150 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1151 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1152 }
1153 }
1154 }
1155
1156 private void bumpDns() {
1157 /*
1158 * Bump the property that tells the name resolver library to reread
1159 * the DNS server list from the properties.
1160 */
1161 String propVal = SystemProperties.get("net.dnschange");
1162 int n = 0;
1163 if (propVal.length() != 0) {
1164 try {
1165 n = Integer.parseInt(propVal);
1166 } catch (NumberFormatException e) {}
1167 }
1168 SystemProperties.set("net.dnschange", "" + (n+1));
1169 }
1170
1171 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001172 // add default net's dns entries
1173 for (int x = mPriorityList.length-1; x>= 0; x--) {
1174 int netType = mPriorityList[x];
1175 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001176 if (nt != null && nt.getNetworkInfo().isConnected() &&
1177 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001179 if (mNetAttributes[netType].isDefault()) {
1180 int j = 1;
1181 for (String dns : dnsList) {
1182 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001183 if (DBG) {
1184 Log.d(TAG, "adding dns " + dns + " for " +
1185 nt.getNetworkInfo().getTypeName());
1186 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001187 SystemProperties.set("net.dns" + j++, dns);
1188 }
1189 }
1190 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001191 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1192 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001193 }
1194 mNumDnsEntries = j;
1195 } else {
1196 // set per-pid dns for attached secondary nets
1197 List pids = mNetRequestersPids[netType];
1198 for (int y=0; y< pids.size(); y++) {
1199 Integer pid = (Integer)pids.get(y);
1200 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 }
1203 }
1204 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001205
1206 bumpDns();
1207 }
1208
1209 private int getRestoreDefaultNetworkDelay() {
1210 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1211 NETWORK_RESTORE_DELAY_PROP_NAME);
1212 if(restoreDefaultNetworkDelayStr != null &&
1213 restoreDefaultNetworkDelayStr.length() != 0) {
1214 try {
1215 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1216 } catch (NumberFormatException e) {
1217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001219 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221
1222 @Override
1223 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001224 if (mContext.checkCallingOrSelfPermission(
1225 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001227 pw.println("Permission Denial: can't dump ConnectivityService " +
1228 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1229 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 return;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 pw.println();
1233 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt73912ce2009-12-21 18:24:07 -08001234 if (nst != null) {
1235 if (nst.getNetworkInfo().isConnected()) {
1236 pw.println("Active network: " + nst.getNetworkInfo().
1237 getTypeName());
1238 }
1239 pw.println(nst.getNetworkInfo());
1240 pw.println(nst);
1241 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
Robert Greenwalt73912ce2009-12-21 18:24:07 -08001244
1245 pw.println("Network Requester Pids:");
1246 for (int net : mPriorityList) {
1247 String pidString = net + ": ";
1248 for (Object pid : mNetRequestersPids[net]) {
1249 pidString = pidString + pid.toString() + ", ";
1250 }
1251 pw.println(pidString);
1252 }
1253 pw.println();
1254
1255 pw.println("FeatureUsers:");
1256 for (Object requester : mFeatureUsers) {
1257 pw.println(requester.toString());
1258 }
1259 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261
Robert Greenwalt42acef32009-08-12 16:08:25 -07001262 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 private class MyHandler extends Handler {
1264 @Override
1265 public void handleMessage(Message msg) {
1266 NetworkInfo info;
1267 switch (msg.what) {
1268 case NetworkStateTracker.EVENT_STATE_CHANGED:
1269 info = (NetworkInfo) msg.obj;
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001270 int type = info.getType();
1271 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001272 // only do this optimization for wifi. It going into scan mode for location
1273 // services generates alot of noise. Meanwhile the mms apn won't send out
1274 // subsequent notifications when on default cellular because it never
1275 // disconnects.. so only do this to wifi notifications. Fixed better when the
1276 // APN notifications are standardized.
1277 if (mNetAttributes[type].mLastState == state &&
1278 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001279 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001280 // TODO - remove this after we validate the dropping doesn't break
1281 // anything
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001282 Log.d(TAG, "Dropping ConnectivityChange for " +
1283 info.getTypeName() + ": " +
1284 state + "/" + info.getDetailedState());
1285 }
1286 return;
1287 }
1288 mNetAttributes[type].mLastState = state;
1289
Robert Greenwalt42acef32009-08-12 16:08:25 -07001290 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001291 info.getTypeName() + ": " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001292 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293
1294 // Connectivity state changed:
1295 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001296 // [12-9] Network subtype (for mobile network, as defined
1297 // by TelephonyManager)
1298 // [8-3] Detailed state ordinal (as defined by
1299 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 // [2-0] Network type (as defined by ConnectivityManager)
1301 int eventLogParam = (info.getType() & 0x7) |
1302 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1303 (info.getSubtype() << 9);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001304 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1305 eventLogParam);
1306
1307 if (info.getDetailedState() ==
1308 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 handleConnectionFailure(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001310 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 handleDisconnect(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001312 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001314 // the logic here is, handle SUSPENDED the same as
1315 // DISCONNECTED. The only difference being we are
1316 // broadcasting an intent with NetworkInfo that's
1317 // suspended. This allows the applications an
1318 // opportunity to handle DISCONNECTED and SUSPENDED
1319 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 handleDisconnect(info);
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001321 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 handleConnect(info);
1323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 break;
1325
1326 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1327 info = (NetworkInfo) msg.obj;
1328 handleScanResultsAvailable(info);
1329 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001332 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1333 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334
1335 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001336 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 break;
1338
1339 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1340 // fill me in
1341 break;
1342
1343 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1344 // fill me in
1345 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001347 FeatureUser u = (FeatureUser)msg.obj;
1348 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001349 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
1352 }
1353}