blob: 08eceac4b37c835bbcbec7371dcd7c8f86432da4 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import java.io.FileDescriptor;
47import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import java.util.ArrayList;
49import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050
51/**
52 * @hide
53 */
54public class ConnectivityService extends IConnectivityManager.Stub {
55
Robert Greenwalta25fd712009-10-06 14:12:53 -070056 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057 private static final String TAG = "ConnectivityService";
58
59 // Event log tags (must be in sync with event-log-tags)
60 private static final int EVENTLOG_CONNECTIVITY_STATE_CHANGED = 50020;
61
Robert Greenwalt2034b912009-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 Project28527d22009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -080081 private WifiWatchdogService mWifiWatchdogService;
82
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -080088 private Context mContext;
89 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070090 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080091
92 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080093
94 private boolean mTestMode;
95 private static ConnectivityService sServiceInstance;
96
Robert Greenwalt2034b912009-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 Lockwoodfde2b762009-08-14 14:18:49 -0400103 private boolean mSystemReady;
104 private ArrayList<Intent> mDeferredBroadcasts;
105
Robert Greenwalt6d7aa192009-12-07 15:20:50 -0800106 private class NetworkAttributes {
Robert Greenwalt2034b912009-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 Greenwalt2adbc7f2010-01-13 09:36:31 -0800114 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-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 Greenwalt2adbc7f2010-01-13 09:36:31 -0800135 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700136 }
137 public boolean isDefault() {
138 return (mType == mRadio);
139 }
140 }
141 NetworkAttributes[] mNetAttributes;
142
Robert Greenwalt6d7aa192009-12-07 15:20:50 -0800143 private class RadioAttributes {
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800162 private static class ConnectivityThread extends Thread {
163 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700164
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700179
The Android Open Source Project28527d22009-03-03 19:31:44 -0800180 public static ConnectivityService getServiceInstance(Context context) {
181 ConnectivityThread thread = new ConnectivityThread(context);
182 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700183
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700191 "Unexpected InterruptedException while waiting"+
192 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800193 }
194 }
195 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700196
The Android Open Source Project28527d22009-03-03 19:31:44 -0800197 return sServiceInstance;
198 }
199 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700200
The Android Open Source Project28527d22009-03-03 19:31:44 -0800201 public static ConnectivityService getInstance(Context context) {
202 return ConnectivityThread.getServiceInstance(context);
203 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700204
The Android Open Source Project28527d22009-03-03 19:31:44 -0800205 private ConnectivityService(Context context) {
206 if (DBG) Log.v(TAG, "ConnectivityService starting up");
207 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700208 mNetTrackers = new NetworkStateTracker[
209 ConnectivityManager.MAX_NETWORK_TYPE+1];
210 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700211
The Android Open Source Project28527d22009-03-03 19:31:44 -0800212 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700213
Robert Greenwalt2034b912009-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 Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700264 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
265 WifiService wifiService = new WifiService(context, wst);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800266 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700267 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800268
Robert Greenwalt2034b912009-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 Greenwalt0659da32009-07-16 17:21:39 -0700273
Robert Greenwalt2034b912009-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 Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700303 mWifiWatchdogService = new WifiWatchdogService(context, wst);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800304 }
305
306 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700307 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800308 * @param preference the new preference
309 */
310 public synchronized void setNetworkPreference(int preference) {
311 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700312 if (ConnectivityManager.isNetworkTypeValid(preference) &&
313 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700329 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
330 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800331 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700332
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700344
The Android Open Source Project28527d22009-03-03 19:31:44 -0800345 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700346 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700353 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800354 return;
355
Robert Greenwalt2034b912009-08-12 16:08:25 -0700356 if (!mNetTrackers[mNetworkPreference].isAvailable())
357 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800358
Robert Greenwalt2034b912009-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 Greenwaltf3f045b2009-08-20 15:25:14 -0700362 if (DBG) {
363 Log.d(TAG, "tearing down " +
364 mNetTrackers[t].getNetworkInfo() +
365 " in enforcePreference");
366 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700367 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800387 */
388 public NetworkInfo getActiveNetworkInfo() {
389 enforceAccessPermission();
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800395 NetworkInfo info = t.getNetworkInfo();
396 if (info.isConnected()) {
Robert Greenwalt2034b912009-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 Project28527d22009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-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 Greenwaltbfb89af2009-12-21 18:24:07 -0800457 long mCreateTime;
Robert Greenwalt2034b912009-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 Greenwaltbfb89af2009-12-21 18:24:07 -0800466 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700467
Robert Greenwalt2034b912009-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 Greenwaltbfb89af2009-12-21 18:24:07 -0800481 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
482 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700483 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700484 }
485
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700486 public void expire() {
487 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltbfb89af2009-12-21 18:24:07 -0800488 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
489 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700490 stopUsingNetworkFeature(this, false);
491 }
Robert Greenwaltbfb89af2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700497 }
498
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700499 // javadoc from interface
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800506 enforceChangePermission();
507 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800509 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700510
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700511 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700545 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700546 mHandler.sendMessageDelayed(mHandler.obtainMessage(
547 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
548 f), getRestoreDefaultNetworkDelay());
549
Robert Greenwalt2034b912009-08-12 16:08:25 -0700550
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700551 if ((ni.isConnectedOrConnecting() == true) &&
552 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-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 Greenwalt2034b912009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800582 }
583
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700584 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800585 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltaffc3a12009-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 Greenwaltbfb89af2009-12-21 18:24:07 -0800608 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700609 return 1;
610 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700611 }
612
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700622 if (DBG) {
623 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
624 ": " + feature);
625 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800626 enforceChangePermission();
627 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
628 return -1;
629 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700630
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700636 return 1;
637 }
Robert Greenwaltaffc3a12009-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 Greenwaltbfb89af2009-12-21 18:24:07 -0800653 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 return 1;
655 }
656 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700657 }
Robert Greenwaltaffc3a12009-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 Greenwaltaffc3a12009-09-27 17:27:04 -0700675 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt52855a12009-12-17 14:54:59 -0800676 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700688 return 1;
689 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700690 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700691 return tracker.stopUsingNetworkFeature(feature, pid, uid);
692 }
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-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 Greenwalt4666ed02009-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 Project28527d22009-03-03 19:31:44 -0800716 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700717 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700727
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700735
The Android Open Source Project28527d22009-03-03 19:31:44 -0800736 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
737
738 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700739 Settings.Secure.BACKGROUND_DATA,
740 allowBackgroundDataUsage ? 1 : 0);
741
The Android Open Source Project28527d22009-03-03 19:31:44 -0800742 Intent broadcast = new Intent(
743 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
744 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700745 }
746
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 private int getNumConnectedNetworks() {
748 int numConnectedNets = 0;
749
750 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700751 if (nt.getNetworkInfo().isConnected() &&
752 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800753 ++numConnectedNets;
754 }
755 }
756 return numConnectedNets;
757 }
758
759 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700760 mContext.enforceCallingOrSelfPermission(
761 android.Manifest.permission.ACCESS_NETWORK_STATE,
762 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800763 }
764
765 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700766 mContext.enforceCallingOrSelfPermission(
767 android.Manifest.permission.CHANGE_NETWORK_STATE,
768 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800769 }
770
771 /**
Robert Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800776 * @param info the {@code NetworkInfo} for the network
777 */
778 private void handleDisconnect(NetworkInfo info) {
779
Robert Greenwalt2034b912009-08-12 16:08:25 -0700780 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800781
Robert Greenwalt2034b912009-08-12 16:08:25 -0700782 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800797 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800798 }
799
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700810 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
811 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800812 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700813
814 /*
815 * If this is a default network, check if other defaults are available
816 * or active
817 */
818 NetworkStateTracker newNet = null;
819 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700820 if (mActiveDefaultNetwork == prevNetType) {
821 mActiveDefaultNetwork = -1;
822 }
823
824 int newType = -1;
825 int newPriority = -1;
826 for (int checkType=0; checkType <=
827 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
828 if (checkType == prevNetType) {
829 continue;
830 }
831 if (mNetAttributes[checkType].isDefault()) {
832 /* TODO - if we have multiple nets we could use
833 * we may want to put more thought into which we choose
834 */
835 if (checkType == mNetworkPreference) {
836 newType = checkType;
837 break;
838 }
839 if (mRadioAttributes[mNetAttributes[checkType].mRadio].
840 mPriority > newPriority) {
841 newType = checkType;
842 newPriority = mRadioAttributes[mNetAttributes[newType].
843 mRadio].mPriority;
844 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800845 }
846 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700847
848 if (newType != -1) {
849 newNet = mNetTrackers[newType];
850 /**
851 * See if the other network is available to fail over to.
852 * If is not available, we enable it anyway, so that it
853 * will be able to connect when it does become available,
854 * but we report a total loss of connectivity rather than
855 * report that we are attempting to fail over.
856 */
857 if (newNet.isAvailable()) {
858 NetworkInfo switchTo = newNet.getNetworkInfo();
859 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700860 if (!switchTo.isConnectedOrConnecting() ||
861 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700862 newNet.reconnect();
863 }
864 if (DBG) {
865 if (switchTo.isConnected()) {
866 Log.v(TAG, "Switching to already connected " +
867 switchTo.getTypeName());
868 } else {
869 Log.v(TAG, "Attempting to switch to " +
870 switchTo.getTypeName());
871 }
872 }
873 intent.putExtra(ConnectivityManager.
874 EXTRA_OTHER_NETWORK_INFO, switchTo);
875 } else {
Robert Greenwalt149d5ac2009-09-17 14:58:16 -0700876 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
877 true);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700878 newNet.reconnect();
879 }
880 } else {
881 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
882 true);
883 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800884 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700885
886 // do this before we broadcast the change
887 handleConnectivityChange();
888
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400889 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800890 /*
Robert Greenwalt0659da32009-07-16 17:21:39 -0700891 * If the failover network is already connected, then immediately send
892 * out a followup broadcast indicating successful failover
The Android Open Source Project28527d22009-03-03 19:31:44 -0800893 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700894 if (newNet != null && newNet.getNetworkInfo().isConnected())
895 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800896 }
897
898 private void sendConnectedBroadcast(NetworkInfo info) {
899 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
900 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
901 if (info.isFailover()) {
902 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
903 info.setFailover(false);
904 }
905 if (info.getReason() != null) {
906 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
907 }
908 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700909 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
910 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800911 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400912 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800913 }
914
915 /**
916 * Called when an attempt to fail over to another network has failed.
917 * @param info the {@link NetworkInfo} for the failed network
918 */
919 private void handleConnectionFailure(NetworkInfo info) {
920 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800921
Robert Greenwalt2034b912009-08-12 16:08:25 -0700922 String reason = info.getReason();
923 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700924
Robert Greenwalt2034b912009-08-12 16:08:25 -0700925 if (DBG) {
926 String reasonText;
927 if (reason == null) {
928 reasonText = ".";
929 } else {
930 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800931 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700932 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
933 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800934 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700935
936 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
937 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
938 if (getActiveNetworkInfo() == null) {
939 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
940 }
941 if (reason != null) {
942 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
943 }
944 if (extraInfo != null) {
945 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
946 }
947 if (info.isFailover()) {
948 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
949 info.setFailover(false);
950 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400951 sendStickyBroadcast(intent);
952 }
953
954 private void sendStickyBroadcast(Intent intent) {
955 synchronized(this) {
956 if (mSystemReady) {
957 mContext.sendStickyBroadcast(intent);
958 } else {
959 if (mDeferredBroadcasts == null) {
960 mDeferredBroadcasts = new ArrayList<Intent>();
961 }
962 mDeferredBroadcasts.add(intent);
963 }
964 }
965 }
966
967 void systemReady() {
968 synchronized(this) {
969 mSystemReady = true;
970 if (mDeferredBroadcasts != null) {
971 int count = mDeferredBroadcasts.size();
972 for (int i = 0; i < count; i++) {
973 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
974 }
975 mDeferredBroadcasts = null;
976 }
977 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800978 }
979
980 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700981 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800982
983 // snapshot isFailover, because sendConnectedBroadcast() resets it
984 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700985 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800986
Robert Greenwalt2034b912009-08-12 16:08:25 -0700987 // if this is a default net and other default is running
988 // kill the one not preferred
989 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700990 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
991 if ((type != mNetworkPreference &&
992 mNetAttributes[mActiveDefaultNetwork].mPriority >
993 mNetAttributes[type].mPriority) ||
994 mNetworkPreference == mActiveDefaultNetwork) {
995 // don't accept this one
996 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
997 "to torn down network " + info.getTypeName());
998 teardown(thisNet);
999 return;
1000 } else {
1001 // tear down the other
1002 NetworkStateTracker otherNet =
1003 mNetTrackers[mActiveDefaultNetwork];
1004 if (DBG) Log.v(TAG, "Policy requires " +
1005 otherNet.getNetworkInfo().getTypeName() +
1006 " teardown");
1007 if (!teardown(otherNet)) {
1008 Log.e(TAG, "Network declined teardown request");
1009 return;
1010 }
1011 if (isFailover) {
1012 otherNet.releaseWakeLock();
1013 }
1014 }
1015 }
1016 mActiveDefaultNetwork = type;
1017 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001018 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 thisNet.updateNetworkSettings();
1020 handleConnectivityChange();
1021 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001022 }
1023
1024 private void handleScanResultsAvailable(NetworkInfo info) {
1025 int networkType = info.getType();
1026 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001027 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1028 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001029 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001030
The Android Open Source Project28527d22009-03-03 19:31:44 -08001031 mNetTrackers[networkType].interpretScanResultsAvailable();
1032 }
1033
Robert Greenwalt0659da32009-07-16 17:21:39 -07001034 private void handleNotificationChange(boolean visible, int id,
1035 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001036 NotificationManager notificationManager = (NotificationManager) mContext
1037 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001038
The Android Open Source Project28527d22009-03-03 19:31:44 -08001039 if (visible) {
1040 notificationManager.notify(id, notification);
1041 } else {
1042 notificationManager.cancel(id);
1043 }
1044 }
1045
1046 /**
1047 * After any kind of change in the connectivity state of any network,
1048 * make sure that anything that depends on the connectivity state of
1049 * more than one network is set up correctly. We're mainly concerned
1050 * with making sure that the list of DNS servers is set up according
1051 * to which networks are connected, and ensuring that the right routing
1052 * table entries exist.
1053 */
1054 private void handleConnectivityChange() {
1055 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001056 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001057 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058 * If both mobile and wifi are enabled, add the host routes that
1059 * will allow MMS traffic to pass on the mobile network. But
1060 * remove the default route for the mobile network, so that there
1061 * will be only one default route, to ensure that all traffic
1062 * except MMS will travel via Wi-Fi.
1063 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001064 handleDnsConfigurationChange();
1065
1066 for (int netType : mPriorityList) {
1067 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1068 if (mNetAttributes[netType].isDefault()) {
1069 mNetTrackers[netType].addDefaultRoute();
1070 } else {
1071 mNetTrackers[netType].addPrivateDnsRoutes();
1072 }
1073 } else {
1074 if (mNetAttributes[netType].isDefault()) {
1075 mNetTrackers[netType].removeDefaultRoute();
1076 } else {
1077 mNetTrackers[netType].removePrivateDnsRoutes();
1078 }
1079 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001080 }
1081 }
1082
Robert Greenwalt2034b912009-08-12 16:08:25 -07001083 /**
1084 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1085 * on the highest priority active net which this process requested.
1086 * If there aren't any, clear it out
1087 */
1088 private void reassessPidDns(int myPid, boolean doBump)
1089 {
1090 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1091 for(int i : mPriorityList) {
1092 if (mNetAttributes[i].isDefault()) {
1093 continue;
1094 }
1095 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001096 if (nt.getNetworkInfo().isConnected() &&
1097 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001098 List pids = mNetRequestersPids[i];
1099 for (int j=0; j<pids.size(); j++) {
1100 Integer pid = (Integer)pids.get(j);
1101 if (pid.intValue() == myPid) {
1102 String[] dnsList = nt.getNameServers();
1103 writePidDns(dnsList, myPid);
1104 if (doBump) {
1105 bumpDns();
1106 }
1107 return;
1108 }
1109 }
1110 }
1111 }
1112 // nothing found - delete
1113 for (int i = 1; ; i++) {
1114 String prop = "net.dns" + i + "." + myPid;
1115 if (SystemProperties.get(prop).length() == 0) {
1116 if (doBump) {
1117 bumpDns();
1118 }
1119 return;
1120 }
1121 SystemProperties.set(prop, "");
1122 }
1123 }
1124
1125 private void writePidDns(String[] dnsList, int pid) {
1126 int j = 1;
1127 for (String dns : dnsList) {
1128 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1129 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1130 }
1131 }
1132 }
1133
1134 private void bumpDns() {
1135 /*
1136 * Bump the property that tells the name resolver library to reread
1137 * the DNS server list from the properties.
1138 */
1139 String propVal = SystemProperties.get("net.dnschange");
1140 int n = 0;
1141 if (propVal.length() != 0) {
1142 try {
1143 n = Integer.parseInt(propVal);
1144 } catch (NumberFormatException e) {}
1145 }
1146 SystemProperties.set("net.dnschange", "" + (n+1));
1147 }
1148
1149 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001150 // add default net's dns entries
1151 for (int x = mPriorityList.length-1; x>= 0; x--) {
1152 int netType = mPriorityList[x];
1153 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001154 if (nt != null && nt.getNetworkInfo().isConnected() &&
1155 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001156 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001157 if (mNetAttributes[netType].isDefault()) {
1158 int j = 1;
1159 for (String dns : dnsList) {
1160 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001161 if (DBG) {
1162 Log.d(TAG, "adding dns " + dns + " for " +
1163 nt.getNetworkInfo().getTypeName());
1164 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001165 SystemProperties.set("net.dns" + j++, dns);
1166 }
1167 }
1168 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001169 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1170 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001171 }
1172 mNumDnsEntries = j;
1173 } else {
1174 // set per-pid dns for attached secondary nets
1175 List pids = mNetRequestersPids[netType];
1176 for (int y=0; y< pids.size(); y++) {
1177 Integer pid = (Integer)pids.get(y);
1178 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001179 }
1180 }
1181 }
1182 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183
1184 bumpDns();
1185 }
1186
1187 private int getRestoreDefaultNetworkDelay() {
1188 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1189 NETWORK_RESTORE_DELAY_PROP_NAME);
1190 if(restoreDefaultNetworkDelayStr != null &&
1191 restoreDefaultNetworkDelayStr.length() != 0) {
1192 try {
1193 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1194 } catch (NumberFormatException e) {
1195 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001196 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001197 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 }
1199
1200 @Override
1201 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001202 if (mContext.checkCallingOrSelfPermission(
1203 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001205 pw.println("Permission Denial: can't dump ConnectivityService " +
1206 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1207 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001208 return;
1209 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 pw.println();
1211 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltbfb89af2009-12-21 18:24:07 -08001212 if (nst != null) {
1213 if (nst.getNetworkInfo().isConnected()) {
1214 pw.println("Active network: " + nst.getNetworkInfo().
1215 getTypeName());
1216 }
1217 pw.println(nst.getNetworkInfo());
1218 pw.println(nst);
1219 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001220 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 }
Robert Greenwaltbfb89af2009-12-21 18:24:07 -08001222
1223 pw.println("Network Requester Pids:");
1224 for (int net : mPriorityList) {
1225 String pidString = net + ": ";
1226 for (Object pid : mNetRequestersPids[net]) {
1227 pidString = pidString + pid.toString() + ", ";
1228 }
1229 pw.println(pidString);
1230 }
1231 pw.println();
1232
1233 pw.println("FeatureUsers:");
1234 for (Object requester : mFeatureUsers) {
1235 pw.println(requester.toString());
1236 }
1237 pw.println();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238 }
1239
Robert Greenwalt2034b912009-08-12 16:08:25 -07001240 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241 private class MyHandler extends Handler {
1242 @Override
1243 public void handleMessage(Message msg) {
1244 NetworkInfo info;
1245 switch (msg.what) {
1246 case NetworkStateTracker.EVENT_STATE_CHANGED:
1247 info = (NetworkInfo) msg.obj;
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001248 int type = info.getType();
1249 NetworkInfo.State state = info.getState();
1250 if (mNetAttributes[type].mLastState == state) {
1251 if (DBG) {
1252 // TODO - remove this after we validate the dropping doesn't break anything
1253 Log.d(TAG, "Dropping ConnectivityChange for " +
1254 info.getTypeName() + ": " +
1255 state + "/" + info.getDetailedState());
1256 }
1257 return;
1258 }
1259 mNetAttributes[type].mLastState = state;
1260
Robert Greenwalt2034b912009-08-12 16:08:25 -07001261 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001262 info.getTypeName() + ": " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001263 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001264
1265 // Connectivity state changed:
1266 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001267 // [12-9] Network subtype (for mobile network, as defined
1268 // by TelephonyManager)
1269 // [8-3] Detailed state ordinal (as defined by
1270 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001271 // [2-0] Network type (as defined by ConnectivityManager)
1272 int eventLogParam = (info.getType() & 0x7) |
1273 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1274 (info.getSubtype() << 9);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001275 EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED,
1276 eventLogParam);
1277
1278 if (info.getDetailedState() ==
1279 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001280 handleConnectionFailure(info);
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001281 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001282 handleDisconnect(info);
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001283 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001284 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001285 // the logic here is, handle SUSPENDED the same as
1286 // DISCONNECTED. The only difference being we are
1287 // broadcasting an intent with NetworkInfo that's
1288 // suspended. This allows the applications an
1289 // opportunity to handle DISCONNECTED and SUSPENDED
1290 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001291 handleDisconnect(info);
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001292 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001293 handleConnect(info);
1294 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001295 break;
1296
1297 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1298 info = (NetworkInfo) msg.obj;
1299 handleScanResultsAvailable(info);
1300 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001301
The Android Open Source Project28527d22009-03-03 19:31:44 -08001302 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001303 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1304 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001305
1306 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001307 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001308 break;
1309
1310 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1311 // fill me in
1312 break;
1313
1314 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1315 // fill me in
1316 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001318 FeatureUser u = (FeatureUser)msg.obj;
1319 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001321 }
1322 }
1323 }
1324}