blob: 509554b6173b411155fff002ca1eb029028ae182 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
The Android Open Source Project28527d22009-03-03 19:31:44 -080046import java.io.FileDescriptor;
47import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070048import java.util.ArrayList;
49import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050
51/**
52 * @hide
53 */
54public class ConnectivityService extends IConnectivityManager.Stub {
55
Robert Greenwalta25fd712009-10-06 14:12:53 -070056 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057 private static final String TAG = "ConnectivityService";
58
Robert Greenwalt2034b912009-08-12 16:08:25 -070059 // how long to wait before switching back to a radio's default network
60 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
61 // system property that can override the above value
62 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
63 "android.telephony.apn-restore";
64
The Android Open Source Project28527d22009-03-03 19:31:44 -080065 /**
66 * Sometimes we want to refer to the individual network state
67 * trackers separately, and sometimes we just want to treat them
68 * abstractly.
69 */
70 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070071
72 /**
73 * A per Net list of the PID's that requested access to the net
74 * used both as a refcount and for per-PID DNS selection
75 */
76 private List mNetRequestersPids[];
77
The Android Open Source Project28527d22009-03-03 19:31:44 -080078 private WifiWatchdogService mWifiWatchdogService;
79
Robert Greenwalt2034b912009-08-12 16:08:25 -070080 // priority order of the nettrackers
81 // (excluding dynamically set mNetworkPreference)
82 // TODO - move mNetworkTypePreference into this
83 private int[] mPriorityList;
84
The Android Open Source Project28527d22009-03-03 19:31:44 -080085 private Context mContext;
86 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070087 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080088
89 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080090
91 private boolean mTestMode;
92 private static ConnectivityService sServiceInstance;
93
Robert Greenwalt2034b912009-08-12 16:08:25 -070094 private Handler mHandler;
95
96 // list of DeathRecipients used to make sure features are turned off when
97 // a process dies
98 private List mFeatureUsers;
99
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400100 private boolean mSystemReady;
101 private ArrayList<Intent> mDeferredBroadcasts;
102
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private class NetworkAttributes {
104 /**
105 * Class for holding settings read from resources.
106 */
107 public String mName;
108 public int mType;
109 public int mRadio;
110 public int mPriority;
111 public NetworkAttributes(String init) {
112 String fragments[] = init.split(",");
113 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700114 mType = Integer.parseInt(fragments[1]);
115 mRadio = Integer.parseInt(fragments[2]);
116 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700117 }
118 public boolean isDefault() {
119 return (mType == mRadio);
120 }
121 }
122 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700123 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700124
125 private class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700126 public int mSimultaneity;
127 public int mType;
128 public RadioAttributes(String init) {
129 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700130 mType = Integer.parseInt(fragments[0]);
131 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700132 }
133 }
134 RadioAttributes[] mRadioAttributes;
135
The Android Open Source Project28527d22009-03-03 19:31:44 -0800136 private static class ConnectivityThread extends Thread {
137 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700138
The Android Open Source Project28527d22009-03-03 19:31:44 -0800139 private ConnectivityThread(Context context) {
140 super("ConnectivityThread");
141 mContext = context;
142 }
143
144 @Override
145 public void run() {
146 Looper.prepare();
147 synchronized (this) {
148 sServiceInstance = new ConnectivityService(mContext);
149 notifyAll();
150 }
151 Looper.loop();
152 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700153
The Android Open Source Project28527d22009-03-03 19:31:44 -0800154 public static ConnectivityService getServiceInstance(Context context) {
155 ConnectivityThread thread = new ConnectivityThread(context);
156 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700157
The Android Open Source Project28527d22009-03-03 19:31:44 -0800158 synchronized (thread) {
159 while (sServiceInstance == null) {
160 try {
161 // Wait until sServiceInstance has been initialized.
162 thread.wait();
163 } catch (InterruptedException ignore) {
164 Log.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700165 "Unexpected InterruptedException while waiting"+
166 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800167 }
168 }
169 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700170
The Android Open Source Project28527d22009-03-03 19:31:44 -0800171 return sServiceInstance;
172 }
173 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700174
The Android Open Source Project28527d22009-03-03 19:31:44 -0800175 public static ConnectivityService getInstance(Context context) {
176 return ConnectivityThread.getServiceInstance(context);
177 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700178
The Android Open Source Project28527d22009-03-03 19:31:44 -0800179 private ConnectivityService(Context context) {
180 if (DBG) Log.v(TAG, "ConnectivityService starting up");
181 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700182 mNetTrackers = new NetworkStateTracker[
183 ConnectivityManager.MAX_NETWORK_TYPE+1];
184 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700185
The Android Open Source Project28527d22009-03-03 19:31:44 -0800186 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700187
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700188 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
189 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
190
Robert Greenwalt2034b912009-08-12 16:08:25 -0700191 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700192 String[] raStrings = context.getResources().getStringArray(
193 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700194 for (String raString : raStrings) {
195 RadioAttributes r = new RadioAttributes(raString);
196 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
197 Log.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
198 continue;
199 }
200 if (mRadioAttributes[r.mType] != null) {
201 Log.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
202 r.mType);
203 continue;
204 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700205 mRadioAttributes[r.mType] = r;
206 }
207
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700208 String[] naStrings = context.getResources().getStringArray(
209 com.android.internal.R.array.networkAttributes);
210 for (String naString : naStrings) {
211 try {
212 NetworkAttributes n = new NetworkAttributes(naString);
213 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
214 Log.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
215 n.mType);
216 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700217 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700218 if (mNetAttributes[n.mType] != null) {
219 Log.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
220 n.mType);
221 continue;
222 }
223 if (mRadioAttributes[n.mRadio] == null) {
224 Log.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
225 "radio " + n.mRadio + " in network type " + n.mType);
226 continue;
227 }
228 mNetAttributes[n.mType] = n;
229 mNetworksDefined++;
230 } catch(Exception e) {
231 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700232 }
233 }
234
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700235 // high priority first
236 mPriorityList = new int[mNetworksDefined];
237 {
238 int insertionPoint = mNetworksDefined-1;
239 int currentLowest = 0;
240 int nextLowest = 0;
241 while (insertionPoint > -1) {
242 for (NetworkAttributes na : mNetAttributes) {
243 if (na == null) continue;
244 if (na.mPriority < currentLowest) continue;
245 if (na.mPriority > currentLowest) {
246 if (na.mPriority < nextLowest || nextLowest == 0) {
247 nextLowest = na.mPriority;
248 }
249 continue;
250 }
251 mPriorityList[insertionPoint--] = na.mType;
252 }
253 currentLowest = nextLowest;
254 nextLowest = 0;
255 }
256 }
257
258 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
259 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700260 mNetRequestersPids[i] = new ArrayList();
261 }
262
263 mFeatureUsers = new ArrayList();
264
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700265 mNumDnsEntries = 0;
266
267 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
268 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800269 /*
270 * Create the network state trackers for Wi-Fi and mobile
271 * data. Maybe this could be done with a factory class,
272 * but it's not clear that it's worth it, given that
273 * the number of different network types is not going
274 * to change very often.
275 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700276 for (int netType : mPriorityList) {
277 switch (mNetAttributes[netType].mRadio) {
278 case ConnectivityManager.TYPE_WIFI:
279 if (DBG) Log.v(TAG, "Starting Wifi Service.");
280 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
281 WifiService wifiService = new WifiService(context, wst);
282 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
283 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
284 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800285
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700286 // Constructing this starts it too
287 mWifiWatchdogService = new WifiWatchdogService(context, wst);
288 break;
289 case ConnectivityManager.TYPE_MOBILE:
290 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
291 netType, mNetAttributes[netType].mName);
292 mNetTrackers[netType].startMonitoring();
293 break;
294 default:
295 Log.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
296 mNetAttributes[netType].mRadio);
297 continue;
298 }
299 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800300 }
301
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302
The Android Open Source Project28527d22009-03-03 19:31:44 -0800303 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700304 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800305 * @param preference the new preference
306 */
307 public synchronized void setNetworkPreference(int preference) {
308 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700309 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700310 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800312 if (mNetworkPreference != preference) {
313 persistNetworkPreference(preference);
314 mNetworkPreference = preference;
315 enforcePreference();
316 }
317 }
318 }
319
320 public int getNetworkPreference() {
321 enforceAccessPermission();
322 return mNetworkPreference;
323 }
324
325 private void persistNetworkPreference(int networkPreference) {
326 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700327 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
328 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800329 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700330
The Android Open Source Project28527d22009-03-03 19:31:44 -0800331 private int getPersistedNetworkPreference() {
332 final ContentResolver cr = mContext.getContentResolver();
333
334 final int networkPrefSetting = Settings.Secure
335 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
336 if (networkPrefSetting != -1) {
337 return networkPrefSetting;
338 }
339
340 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
341 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700342
The Android Open Source Project28527d22009-03-03 19:31:44 -0800343 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700344 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800345 * In this method, we only tear down a non-preferred network. Establishing
346 * a connection to the preferred network is taken care of when we handle
347 * the disconnect event from the non-preferred network
348 * (see {@link #handleDisconnect(NetworkInfo)}).
349 */
350 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700351 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800352 return;
353
Robert Greenwalt2034b912009-08-12 16:08:25 -0700354 if (!mNetTrackers[mNetworkPreference].isAvailable())
355 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800356
Robert Greenwalt2034b912009-08-12 16:08:25 -0700357 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700358 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700359 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700360 if (DBG) {
361 Log.d(TAG, "tearing down " +
362 mNetTrackers[t].getNetworkInfo() +
363 " in enforcePreference");
364 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700365 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800366 }
367 }
368 }
369
370 private boolean teardown(NetworkStateTracker netTracker) {
371 if (netTracker.teardown()) {
372 netTracker.setTeardownRequested(true);
373 return true;
374 } else {
375 return false;
376 }
377 }
378
379 /**
380 * Return NetworkInfo for the active (i.e., connected) network interface.
381 * It is assumed that at most one network is active at a time. If more
382 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700383 * @return the info for the active network, or {@code null} if none is
384 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800385 */
386 public NetworkInfo getActiveNetworkInfo() {
387 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700388 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700389 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700390 continue;
391 }
392 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800393 NetworkInfo info = t.getNetworkInfo();
394 if (info.isConnected()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700395 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
396 "connected default network is not " +
397 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398 return info;
399 }
400 }
401 return null;
402 }
403
404 public NetworkInfo getNetworkInfo(int networkType) {
405 enforceAccessPermission();
406 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
407 NetworkStateTracker t = mNetTrackers[networkType];
408 if (t != null)
409 return t.getNetworkInfo();
410 }
411 return null;
412 }
413
414 public NetworkInfo[] getAllNetworkInfo() {
415 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700416 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800417 int i = 0;
418 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800420 }
421 return result;
422 }
423
424 public boolean setRadios(boolean turnOn) {
425 boolean result = true;
426 enforceChangePermission();
427 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700428 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800429 }
430 return result;
431 }
432
433 public boolean setRadio(int netType, boolean turnOn) {
434 enforceChangePermission();
435 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
436 return false;
437 }
438 NetworkStateTracker tracker = mNetTrackers[netType];
439 return tracker != null && tracker.setRadio(turnOn);
440 }
441
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700442 /**
443 * Used to notice when the calling process dies so we can self-expire
444 *
445 * Also used to know if the process has cleaned up after itself when
446 * our auto-expire timer goes off. The timer has a link to an object.
447 *
448 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700449 private class FeatureUser implements IBinder.DeathRecipient {
450 int mNetworkType;
451 String mFeature;
452 IBinder mBinder;
453 int mPid;
454 int mUid;
455
456 FeatureUser(int type, String feature, IBinder binder) {
457 super();
458 mNetworkType = type;
459 mFeature = feature;
460 mBinder = binder;
461 mPid = getCallingPid();
462 mUid = getCallingUid();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700463
Robert Greenwalt2034b912009-08-12 16:08:25 -0700464 try {
465 mBinder.linkToDeath(this, 0);
466 } catch (RemoteException e) {
467 binderDied();
468 }
469 }
470
471 void unlinkDeathRecipient() {
472 mBinder.unlinkToDeath(this, 0);
473 }
474
475 public void binderDied() {
476 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
477 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700478 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700479 }
480
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700481 public void expire() {
482 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
483 mNetworkType + ", " + mFeature + ", " + mBinder);
484 stopUsingNetworkFeature(this, false);
485 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700486 }
487
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700488 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700489 public int startUsingNetworkFeature(int networkType, String feature,
490 IBinder binder) {
491 if (DBG) {
492 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
493 ": " + feature);
494 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800495 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700496 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
497 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800499 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700501 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700502
503 // TODO - move this into the MobileDataStateTracker
504 int usedNetworkType = networkType;
505 if(networkType == ConnectivityManager.TYPE_MOBILE) {
506 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
507 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
508 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
509 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
510 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
511 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
512 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
513 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
514 }
515 }
516 NetworkStateTracker network = mNetTrackers[usedNetworkType];
517 if (network != null) {
518 if (usedNetworkType != networkType) {
519 Integer currentPid = new Integer(getCallingPid());
520
521 NetworkStateTracker radio = mNetTrackers[networkType];
522 NetworkInfo ni = network.getNetworkInfo();
523
524 if (ni.isAvailable() == false) {
525 if (DBG) Log.d(TAG, "special network not available");
526 return Phone.APN_TYPE_NOT_AVAILABLE;
527 }
528
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700529 synchronized(this) {
530 mFeatureUsers.add(f);
531 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
532 // this gets used for per-pid dns when connected
533 mNetRequestersPids[usedNetworkType].add(currentPid);
534 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700535 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700536 mHandler.sendMessageDelayed(mHandler.obtainMessage(
537 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
538 f), getRestoreDefaultNetworkDelay());
539
Robert Greenwalt2034b912009-08-12 16:08:25 -0700540
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700541 if ((ni.isConnectedOrConnecting() == true) &&
542 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700543 if (ni.isConnected() == true) {
544 // add the pid-specific dns
545 handleDnsConfigurationChange();
546 if (DBG) Log.d(TAG, "special network already active");
547 return Phone.APN_ALREADY_ACTIVE;
548 }
549 if (DBG) Log.d(TAG, "special network already connecting");
550 return Phone.APN_REQUEST_STARTED;
551 }
552
553 // check if the radio in play can make another contact
554 // assume if cannot for now
555
Robert Greenwalt2034b912009-08-12 16:08:25 -0700556 if (DBG) Log.d(TAG, "reconnecting to special network");
557 network.reconnect();
558 return Phone.APN_REQUEST_STARTED;
559 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700560 synchronized(this) {
561 mFeatureUsers.add(f);
562 }
563 mHandler.sendMessageDelayed(mHandler.obtainMessage(
564 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
565 f), getRestoreDefaultNetworkDelay());
566
Robert Greenwalt2034b912009-08-12 16:08:25 -0700567 return network.startUsingNetworkFeature(feature,
568 getCallingPid(), getCallingUid());
569 }
570 }
571 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800572 }
573
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700574 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800575 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700576 enforceChangePermission();
577
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700578 int pid = getCallingPid();
579 int uid = getCallingUid();
580
581 FeatureUser u = null;
582 boolean found = false;
583
584 synchronized(this) {
585 for (int i = 0; i < mFeatureUsers.size() ; i++) {
586 u = (FeatureUser)mFeatureUsers.get(i);
587 if (uid == u.mUid && pid == u.mPid &&
588 networkType == u.mNetworkType &&
589 TextUtils.equals(feature, u.mFeature)) {
590 found = true;
591 break;
592 }
593 }
594 }
595 if (found && u != null) {
596 // stop regardless of how many other time this proc had called start
597 return stopUsingNetworkFeature(u, true);
598 } else {
599 // none found!
600 return 1;
601 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 }
603
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700604 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
605 int networkType = u.mNetworkType;
606 String feature = u.mFeature;
607 int pid = u.mPid;
608 int uid = u.mUid;
609
610 NetworkStateTracker tracker = null;
611 boolean callTeardown = false; // used to carry our decision outside of sync block
612
Robert Greenwalt2034b912009-08-12 16:08:25 -0700613 if (DBG) {
614 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
615 ": " + feature);
616 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700617
The Android Open Source Project28527d22009-03-03 19:31:44 -0800618 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
619 return -1;
620 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700621
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700622 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
623 // sync block
624 synchronized(this) {
625 // check if this process still has an outstanding start request
626 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700627 return 1;
628 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700629 u.unlinkDeathRecipient();
630 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
631 // If we care about duplicate requests, check for that here.
632 //
633 // This is done to support the extension of a request - the app
634 // can request we start the network feature again and renew the
635 // auto-shutoff delay. Normal "stop" calls from the app though
636 // do not pay attention to duplicate requests - in effect the
637 // API does not refcount and a single stop will counter multiple starts.
638 if (ignoreDups == false) {
639 for (int i = 0; i < mFeatureUsers.size() ; i++) {
640 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
641 if (x.mUid == u.mUid && x.mPid == u.mPid &&
642 x.mNetworkType == u.mNetworkType &&
643 TextUtils.equals(x.mFeature, u.mFeature)) {
644 return 1;
645 }
646 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700647 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700648
649 // TODO - move to MobileDataStateTracker
650 int usedNetworkType = networkType;
651 if (networkType == ConnectivityManager.TYPE_MOBILE) {
652 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
653 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
654 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
655 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
656 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
657 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
658 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
659 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
660 }
661 }
662 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700663 if (tracker == null) {
664 return -1;
665 }
666 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700667 Integer currentPid = new Integer(pid);
668 reassessPidDns(pid, true);
669 mNetRequestersPids[usedNetworkType].remove(currentPid);
670 if (mNetRequestersPids[usedNetworkType].size() != 0) {
671 if (DBG) Log.d(TAG, "not tearing down special network - " +
672 "others still using it");
673 return 1;
674 }
675 callTeardown = true;
676 }
677 }
678
679 if (callTeardown) {
680 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700681 return 1;
682 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700683 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684 return tracker.stopUsingNetworkFeature(feature, pid, uid);
685 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800686 }
687
688 /**
689 * Ensure that a network route exists to deliver traffic to the specified
690 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700691 * @param networkType the type of the network over which traffic to the
692 * specified host is to be routed
693 * @param hostAddress the IP address of the host to which the route is
694 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800695 * @return {@code true} on success, {@code false} on failure
696 */
697 public boolean requestRouteToHost(int networkType, int hostAddress) {
698 enforceChangePermission();
699 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
700 return false;
701 }
702 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700703
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700704 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
705 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700706 if (DBG) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700707 Log.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700708 }
709 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800710 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700711 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800712 }
713
714 /**
715 * @see ConnectivityManager#getBackgroundDataSetting()
716 */
717 public boolean getBackgroundDataSetting() {
718 return Settings.Secure.getInt(mContext.getContentResolver(),
719 Settings.Secure.BACKGROUND_DATA, 1) == 1;
720 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700721
The Android Open Source Project28527d22009-03-03 19:31:44 -0800722 /**
723 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
724 */
725 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
726 mContext.enforceCallingOrSelfPermission(
727 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
728 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700729
The Android Open Source Project28527d22009-03-03 19:31:44 -0800730 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
731
732 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700733 Settings.Secure.BACKGROUND_DATA,
734 allowBackgroundDataUsage ? 1 : 0);
735
The Android Open Source Project28527d22009-03-03 19:31:44 -0800736 Intent broadcast = new Intent(
737 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
738 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700739 }
740
The Android Open Source Project28527d22009-03-03 19:31:44 -0800741 private int getNumConnectedNetworks() {
742 int numConnectedNets = 0;
743
744 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700745 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700746 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 ++numConnectedNets;
748 }
749 }
750 return numConnectedNets;
751 }
752
753 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700754 mContext.enforceCallingOrSelfPermission(
755 android.Manifest.permission.ACCESS_NETWORK_STATE,
756 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800757 }
758
759 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700760 mContext.enforceCallingOrSelfPermission(
761 android.Manifest.permission.CHANGE_NETWORK_STATE,
762 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800763 }
764
765 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700766 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
767 * network, we ignore it. If it is for the active network, we send out a
768 * broadcast. But first, we check whether it might be possible to connect
769 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800770 * @param info the {@code NetworkInfo} for the network
771 */
772 private void handleDisconnect(NetworkInfo info) {
773
Robert Greenwalt2034b912009-08-12 16:08:25 -0700774 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800775
Robert Greenwalt2034b912009-08-12 16:08:25 -0700776 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800777 /*
778 * If the disconnected network is not the active one, then don't report
779 * this as a loss of connectivity. What probably happened is that we're
780 * getting the disconnect for a network that we explicitly disabled
781 * in accordance with network preference policies.
782 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700783 if (!mNetAttributes[prevNetType].isDefault()) {
784 List pids = mNetRequestersPids[prevNetType];
785 for (int i = 0; i<pids.size(); i++) {
786 Integer pid = (Integer)pids.get(i);
787 // will remove them because the net's no longer connected
788 // need to do this now as only now do we know the pids and
789 // can properly null things that are no longer referenced.
790 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800791 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800792 }
793
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
795 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
796 if (info.isFailover()) {
797 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
798 info.setFailover(false);
799 }
800 if (info.getReason() != null) {
801 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
802 }
803 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700804 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
805 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800806 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700807
808 /*
809 * If this is a default network, check if other defaults are available
810 * or active
811 */
812 NetworkStateTracker newNet = null;
813 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700814 if (mActiveDefaultNetwork == prevNetType) {
815 mActiveDefaultNetwork = -1;
816 }
817
818 int newType = -1;
819 int newPriority = -1;
820 for (int checkType=0; checkType <=
821 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700822 if (checkType == prevNetType) continue;
823 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700824 if (mNetAttributes[checkType].isDefault()) {
825 /* TODO - if we have multiple nets we could use
826 * we may want to put more thought into which we choose
827 */
828 if (checkType == mNetworkPreference) {
829 newType = checkType;
830 break;
831 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700832 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700833 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700834 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700835 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800836 }
837 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700838
839 if (newType != -1) {
840 newNet = mNetTrackers[newType];
841 /**
842 * See if the other network is available to fail over to.
843 * If is not available, we enable it anyway, so that it
844 * will be able to connect when it does become available,
845 * but we report a total loss of connectivity rather than
846 * report that we are attempting to fail over.
847 */
848 if (newNet.isAvailable()) {
849 NetworkInfo switchTo = newNet.getNetworkInfo();
850 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700851 if (!switchTo.isConnectedOrConnecting() ||
852 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700853 newNet.reconnect();
854 }
855 if (DBG) {
856 if (switchTo.isConnected()) {
857 Log.v(TAG, "Switching to already connected " +
858 switchTo.getTypeName());
859 } else {
860 Log.v(TAG, "Attempting to switch to " +
861 switchTo.getTypeName());
862 }
863 }
864 intent.putExtra(ConnectivityManager.
865 EXTRA_OTHER_NETWORK_INFO, switchTo);
866 } else {
Robert Greenwalt149d5ac2009-09-17 14:58:16 -0700867 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
868 true);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700869 newNet.reconnect();
870 }
871 } else {
872 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
873 true);
874 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800875 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700876
877 // do this before we broadcast the change
878 handleConnectivityChange();
879
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400880 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800881 /*
Robert Greenwalt0659da32009-07-16 17:21:39 -0700882 * If the failover network is already connected, then immediately send
883 * out a followup broadcast indicating successful failover
The Android Open Source Project28527d22009-03-03 19:31:44 -0800884 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700885 if (newNet != null && newNet.getNetworkInfo().isConnected())
886 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800887 }
888
889 private void sendConnectedBroadcast(NetworkInfo info) {
890 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
891 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
892 if (info.isFailover()) {
893 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
894 info.setFailover(false);
895 }
896 if (info.getReason() != null) {
897 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
898 }
899 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700900 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
901 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800902 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400903 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800904 }
905
906 /**
907 * Called when an attempt to fail over to another network has failed.
908 * @param info the {@link NetworkInfo} for the failed network
909 */
910 private void handleConnectionFailure(NetworkInfo info) {
911 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800912
Robert Greenwalt2034b912009-08-12 16:08:25 -0700913 String reason = info.getReason();
914 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700915
Robert Greenwalt2034b912009-08-12 16:08:25 -0700916 if (DBG) {
917 String reasonText;
918 if (reason == null) {
919 reasonText = ".";
920 } else {
921 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800922 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700923 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
924 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800925 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700926
927 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
928 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
929 if (getActiveNetworkInfo() == null) {
930 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
931 }
932 if (reason != null) {
933 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
934 }
935 if (extraInfo != null) {
936 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
937 }
938 if (info.isFailover()) {
939 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
940 info.setFailover(false);
941 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400942 sendStickyBroadcast(intent);
943 }
944
945 private void sendStickyBroadcast(Intent intent) {
946 synchronized(this) {
947 if (mSystemReady) {
948 mContext.sendStickyBroadcast(intent);
949 } else {
950 if (mDeferredBroadcasts == null) {
951 mDeferredBroadcasts = new ArrayList<Intent>();
952 }
953 mDeferredBroadcasts.add(intent);
954 }
955 }
956 }
957
958 void systemReady() {
959 synchronized(this) {
960 mSystemReady = true;
961 if (mDeferredBroadcasts != null) {
962 int count = mDeferredBroadcasts.size();
963 for (int i = 0; i < count; i++) {
964 mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i));
965 }
966 mDeferredBroadcasts = null;
967 }
968 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800969 }
970
971 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700972 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800973
974 // snapshot isFailover, because sendConnectedBroadcast() resets it
975 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700976 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800977
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978 // if this is a default net and other default is running
979 // kill the one not preferred
980 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700981 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
982 if ((type != mNetworkPreference &&
983 mNetAttributes[mActiveDefaultNetwork].mPriority >
984 mNetAttributes[type].mPriority) ||
985 mNetworkPreference == mActiveDefaultNetwork) {
986 // don't accept this one
987 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
988 "to torn down network " + info.getTypeName());
989 teardown(thisNet);
990 return;
991 } else {
992 // tear down the other
993 NetworkStateTracker otherNet =
994 mNetTrackers[mActiveDefaultNetwork];
995 if (DBG) Log.v(TAG, "Policy requires " +
996 otherNet.getNetworkInfo().getTypeName() +
997 " teardown");
998 if (!teardown(otherNet)) {
999 Log.e(TAG, "Network declined teardown request");
1000 return;
1001 }
1002 if (isFailover) {
1003 otherNet.releaseWakeLock();
1004 }
1005 }
1006 }
1007 mActiveDefaultNetwork = type;
1008 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001009 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001010 thisNet.updateNetworkSettings();
1011 handleConnectivityChange();
1012 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001013 }
1014
1015 private void handleScanResultsAvailable(NetworkInfo info) {
1016 int networkType = info.getType();
1017 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001018 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1019 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001021
The Android Open Source Project28527d22009-03-03 19:31:44 -08001022 mNetTrackers[networkType].interpretScanResultsAvailable();
1023 }
1024
Robert Greenwalt0659da32009-07-16 17:21:39 -07001025 private void handleNotificationChange(boolean visible, int id,
1026 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001027 NotificationManager notificationManager = (NotificationManager) mContext
1028 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001029
The Android Open Source Project28527d22009-03-03 19:31:44 -08001030 if (visible) {
1031 notificationManager.notify(id, notification);
1032 } else {
1033 notificationManager.cancel(id);
1034 }
1035 }
1036
1037 /**
1038 * After any kind of change in the connectivity state of any network,
1039 * make sure that anything that depends on the connectivity state of
1040 * more than one network is set up correctly. We're mainly concerned
1041 * with making sure that the list of DNS servers is set up according
1042 * to which networks are connected, and ensuring that the right routing
1043 * table entries exist.
1044 */
1045 private void handleConnectivityChange() {
1046 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001047 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001048 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001049 * If both mobile and wifi are enabled, add the host routes that
1050 * will allow MMS traffic to pass on the mobile network. But
1051 * remove the default route for the mobile network, so that there
1052 * will be only one default route, to ensure that all traffic
1053 * except MMS will travel via Wi-Fi.
1054 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001055 handleDnsConfigurationChange();
1056
1057 for (int netType : mPriorityList) {
1058 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1059 if (mNetAttributes[netType].isDefault()) {
1060 mNetTrackers[netType].addDefaultRoute();
1061 } else {
1062 mNetTrackers[netType].addPrivateDnsRoutes();
1063 }
1064 } else {
1065 if (mNetAttributes[netType].isDefault()) {
1066 mNetTrackers[netType].removeDefaultRoute();
1067 } else {
1068 mNetTrackers[netType].removePrivateDnsRoutes();
1069 }
1070 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001071 }
1072 }
1073
Robert Greenwalt2034b912009-08-12 16:08:25 -07001074 /**
1075 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1076 * on the highest priority active net which this process requested.
1077 * If there aren't any, clear it out
1078 */
1079 private void reassessPidDns(int myPid, boolean doBump)
1080 {
1081 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1082 for(int i : mPriorityList) {
1083 if (mNetAttributes[i].isDefault()) {
1084 continue;
1085 }
1086 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001087 if (nt.getNetworkInfo().isConnected() &&
1088 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001089 List pids = mNetRequestersPids[i];
1090 for (int j=0; j<pids.size(); j++) {
1091 Integer pid = (Integer)pids.get(j);
1092 if (pid.intValue() == myPid) {
1093 String[] dnsList = nt.getNameServers();
1094 writePidDns(dnsList, myPid);
1095 if (doBump) {
1096 bumpDns();
1097 }
1098 return;
1099 }
1100 }
1101 }
1102 }
1103 // nothing found - delete
1104 for (int i = 1; ; i++) {
1105 String prop = "net.dns" + i + "." + myPid;
1106 if (SystemProperties.get(prop).length() == 0) {
1107 if (doBump) {
1108 bumpDns();
1109 }
1110 return;
1111 }
1112 SystemProperties.set(prop, "");
1113 }
1114 }
1115
1116 private void writePidDns(String[] dnsList, int pid) {
1117 int j = 1;
1118 for (String dns : dnsList) {
1119 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1120 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1121 }
1122 }
1123 }
1124
1125 private void bumpDns() {
1126 /*
1127 * Bump the property that tells the name resolver library to reread
1128 * the DNS server list from the properties.
1129 */
1130 String propVal = SystemProperties.get("net.dnschange");
1131 int n = 0;
1132 if (propVal.length() != 0) {
1133 try {
1134 n = Integer.parseInt(propVal);
1135 } catch (NumberFormatException e) {}
1136 }
1137 SystemProperties.set("net.dnschange", "" + (n+1));
1138 }
1139
1140 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001141 // add default net's dns entries
1142 for (int x = mPriorityList.length-1; x>= 0; x--) {
1143 int netType = mPriorityList[x];
1144 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001145 if (nt != null && nt.getNetworkInfo().isConnected() &&
1146 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001147 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001148 if (mNetAttributes[netType].isDefault()) {
1149 int j = 1;
1150 for (String dns : dnsList) {
1151 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001152 if (DBG) {
1153 Log.d(TAG, "adding dns " + dns + " for " +
1154 nt.getNetworkInfo().getTypeName());
1155 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001156 SystemProperties.set("net.dns" + j++, dns);
1157 }
1158 }
1159 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001160 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1161 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 }
1163 mNumDnsEntries = j;
1164 } else {
1165 // set per-pid dns for attached secondary nets
1166 List pids = mNetRequestersPids[netType];
1167 for (int y=0; y< pids.size(); y++) {
1168 Integer pid = (Integer)pids.get(y);
1169 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001170 }
1171 }
1172 }
1173 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174
1175 bumpDns();
1176 }
1177
1178 private int getRestoreDefaultNetworkDelay() {
1179 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1180 NETWORK_RESTORE_DELAY_PROP_NAME);
1181 if(restoreDefaultNetworkDelayStr != null &&
1182 restoreDefaultNetworkDelayStr.length() != 0) {
1183 try {
1184 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1185 } catch (NumberFormatException e) {
1186 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 }
1190
1191 @Override
1192 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001193 if (mContext.checkCallingOrSelfPermission(
1194 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001195 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001196 pw.println("Permission Denial: can't dump ConnectivityService " +
1197 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1198 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001199 return;
1200 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001201 pw.println();
1202 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001203 if (nst != null && nst.getNetworkInfo().isConnected()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 pw.println("Active network: " + nst.getNetworkInfo().
1205 getTypeName());
1206 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001207 pw.println(nst.getNetworkInfo());
1208 pw.println(nst);
1209 pw.println();
1210 }
1211 }
1212
Robert Greenwalt2034b912009-08-12 16:08:25 -07001213 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001214 private class MyHandler extends Handler {
1215 @Override
1216 public void handleMessage(Message msg) {
1217 NetworkInfo info;
1218 switch (msg.what) {
1219 case NetworkStateTracker.EVENT_STATE_CHANGED:
1220 info = (NetworkInfo) msg.obj;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001222 info.getTypeName() + ": " +
The Android Open Source Project28527d22009-03-03 19:31:44 -08001223 info.getState() + "/" + info.getDetailedState());
1224
1225 // Connectivity state changed:
1226 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001227 // [12-9] Network subtype (for mobile network, as defined
1228 // by TelephonyManager)
1229 // [8-3] Detailed state ordinal (as defined by
1230 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001231 // [2-0] Network type (as defined by ConnectivityManager)
1232 int eventLogParam = (info.getType() & 0x7) |
1233 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1234 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001235 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001236 eventLogParam);
1237
1238 if (info.getDetailedState() ==
1239 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240 handleConnectionFailure(info);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001241 } else if (info.getState() ==
1242 NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 handleDisconnect(info);
1244 } else if (info.getState() == NetworkInfo.State.SUSPENDED) {
1245 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001246 // the logic here is, handle SUSPENDED the same as
1247 // DISCONNECTED. The only difference being we are
1248 // broadcasting an intent with NetworkInfo that's
1249 // suspended. This allows the applications an
1250 // opportunity to handle DISCONNECTED and SUSPENDED
1251 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001252 handleDisconnect(info);
1253 } else if (info.getState() == NetworkInfo.State.CONNECTED) {
1254 handleConnect(info);
1255 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 break;
1257
1258 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1259 info = (NetworkInfo) msg.obj;
1260 handleScanResultsAvailable(info);
1261 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001262
The Android Open Source Project28527d22009-03-03 19:31:44 -08001263 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001264 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1265 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001266
1267 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001269 break;
1270
1271 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1272 // fill me in
1273 break;
1274
1275 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1276 // fill me in
1277 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001278 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001279 FeatureUser u = (FeatureUser)msg.obj;
1280 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001281 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001282 }
1283 }
1284 }
1285}