blob: 0f5330b8a550dc616aa6a7bf543ee6850f92a790 [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;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800101 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400102
Robert Greenwalt12c44552009-12-07 11:33:18 -0800103 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700104 /**
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;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800111 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700112 public NetworkAttributes(String init) {
113 String fragments[] = init.split(",");
114 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700115 mType = Integer.parseInt(fragments[1]);
116 mRadio = Integer.parseInt(fragments[2]);
117 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800118 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700119 }
120 public boolean isDefault() {
121 return (mType == mRadio);
122 }
123 }
124 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700125 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700126
Robert Greenwalt12c44552009-12-07 11:33:18 -0800127 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700128 public int mSimultaneity;
129 public int mType;
130 public RadioAttributes(String init) {
131 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700132 mType = Integer.parseInt(fragments[0]);
133 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700134 }
135 }
136 RadioAttributes[] mRadioAttributes;
137
The Android Open Source Project28527d22009-03-03 19:31:44 -0800138 private static class ConnectivityThread extends Thread {
139 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700140
The Android Open Source Project28527d22009-03-03 19:31:44 -0800141 private ConnectivityThread(Context context) {
142 super("ConnectivityThread");
143 mContext = context;
144 }
145
146 @Override
147 public void run() {
148 Looper.prepare();
149 synchronized (this) {
150 sServiceInstance = new ConnectivityService(mContext);
151 notifyAll();
152 }
153 Looper.loop();
154 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700155
The Android Open Source Project28527d22009-03-03 19:31:44 -0800156 public static ConnectivityService getServiceInstance(Context context) {
157 ConnectivityThread thread = new ConnectivityThread(context);
158 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700159
The Android Open Source Project28527d22009-03-03 19:31:44 -0800160 synchronized (thread) {
161 while (sServiceInstance == null) {
162 try {
163 // Wait until sServiceInstance has been initialized.
164 thread.wait();
165 } catch (InterruptedException ignore) {
166 Log.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700167 "Unexpected InterruptedException while waiting"+
168 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800169 }
170 }
171 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700172
The Android Open Source Project28527d22009-03-03 19:31:44 -0800173 return sServiceInstance;
174 }
175 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176
The Android Open Source Project28527d22009-03-03 19:31:44 -0800177 public static ConnectivityService getInstance(Context context) {
178 return ConnectivityThread.getServiceInstance(context);
179 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700180
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 private ConnectivityService(Context context) {
182 if (DBG) Log.v(TAG, "ConnectivityService starting up");
183 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700184 mNetTrackers = new NetworkStateTracker[
185 ConnectivityManager.MAX_NETWORK_TYPE+1];
186 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700187
The Android Open Source Project28527d22009-03-03 19:31:44 -0800188 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700189
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700190 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
191 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
192
Robert Greenwalt2034b912009-08-12 16:08:25 -0700193 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700194 String[] raStrings = context.getResources().getStringArray(
195 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700196 for (String raString : raStrings) {
197 RadioAttributes r = new RadioAttributes(raString);
198 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
199 Log.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
200 continue;
201 }
202 if (mRadioAttributes[r.mType] != null) {
203 Log.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
204 r.mType);
205 continue;
206 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700207 mRadioAttributes[r.mType] = r;
208 }
209
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700210 String[] naStrings = context.getResources().getStringArray(
211 com.android.internal.R.array.networkAttributes);
212 for (String naString : naStrings) {
213 try {
214 NetworkAttributes n = new NetworkAttributes(naString);
215 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
216 Log.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
217 n.mType);
218 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700219 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700220 if (mNetAttributes[n.mType] != null) {
221 Log.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
222 n.mType);
223 continue;
224 }
225 if (mRadioAttributes[n.mRadio] == null) {
226 Log.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
227 "radio " + n.mRadio + " in network type " + n.mType);
228 continue;
229 }
230 mNetAttributes[n.mType] = n;
231 mNetworksDefined++;
232 } catch(Exception e) {
233 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700234 }
235 }
236
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700237 // high priority first
238 mPriorityList = new int[mNetworksDefined];
239 {
240 int insertionPoint = mNetworksDefined-1;
241 int currentLowest = 0;
242 int nextLowest = 0;
243 while (insertionPoint > -1) {
244 for (NetworkAttributes na : mNetAttributes) {
245 if (na == null) continue;
246 if (na.mPriority < currentLowest) continue;
247 if (na.mPriority > currentLowest) {
248 if (na.mPriority < nextLowest || nextLowest == 0) {
249 nextLowest = na.mPriority;
250 }
251 continue;
252 }
253 mPriorityList[insertionPoint--] = na.mType;
254 }
255 currentLowest = nextLowest;
256 nextLowest = 0;
257 }
258 }
259
260 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
261 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700262 mNetRequestersPids[i] = new ArrayList();
263 }
264
265 mFeatureUsers = new ArrayList();
266
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700267 mNumDnsEntries = 0;
268
269 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
270 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800271 /*
272 * Create the network state trackers for Wi-Fi and mobile
273 * data. Maybe this could be done with a factory class,
274 * but it's not clear that it's worth it, given that
275 * the number of different network types is not going
276 * to change very often.
277 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700278 for (int netType : mPriorityList) {
279 switch (mNetAttributes[netType].mRadio) {
280 case ConnectivityManager.TYPE_WIFI:
281 if (DBG) Log.v(TAG, "Starting Wifi Service.");
282 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
283 WifiService wifiService = new WifiService(context, wst);
284 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
285 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
286 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800287
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700288 // Constructing this starts it too
289 mWifiWatchdogService = new WifiWatchdogService(context, wst);
290 break;
291 case ConnectivityManager.TYPE_MOBILE:
292 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
293 netType, mNetAttributes[netType].mName);
294 mNetTrackers[netType].startMonitoring();
295 break;
296 default:
297 Log.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
298 mNetAttributes[netType].mRadio);
299 continue;
300 }
301 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800302 }
303
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700304
The Android Open Source Project28527d22009-03-03 19:31:44 -0800305 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700306 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800307 * @param preference the new preference
308 */
309 public synchronized void setNetworkPreference(int preference) {
310 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700311 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700312 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700313 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++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700360 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700361 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++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700391 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700392 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();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700418 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800419 int i = 0;
420 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700421 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800422 }
423 return result;
424 }
425
426 public boolean setRadios(boolean turnOn) {
427 boolean result = true;
428 enforceChangePermission();
429 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700430 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800431 }
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;
457
458 FeatureUser(int type, String feature, IBinder binder) {
459 super();
460 mNetworkType = type;
461 mFeature = feature;
462 mBinder = binder;
463 mPid = getCallingPid();
464 mUid = getCallingUid();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700465
Robert Greenwalt2034b912009-08-12 16:08:25 -0700466 try {
467 mBinder.linkToDeath(this, 0);
468 } catch (RemoteException e) {
469 binderDied();
470 }
471 }
472
473 void unlinkDeathRecipient() {
474 mBinder.unlinkToDeath(this, 0);
475 }
476
477 public void binderDied() {
478 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
479 mNetworkType + ", " + mFeature + ", " + mBinder);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700480 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700481 }
482
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700483 public void expire() {
484 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
485 mNetworkType + ", " + mFeature + ", " + mBinder);
486 stopUsingNetworkFeature(this, false);
487 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700488 }
489
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700490 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700491 public int startUsingNetworkFeature(int networkType, String feature,
492 IBinder binder) {
493 if (DBG) {
494 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
495 ": " + feature);
496 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700498 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
499 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800501 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700502
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700503 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700504
505 // TODO - move this into the MobileDataStateTracker
506 int usedNetworkType = networkType;
507 if(networkType == ConnectivityManager.TYPE_MOBILE) {
508 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
509 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
510 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
511 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
512 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
513 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
514 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
515 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
516 }
517 }
518 NetworkStateTracker network = mNetTrackers[usedNetworkType];
519 if (network != null) {
520 if (usedNetworkType != networkType) {
521 Integer currentPid = new Integer(getCallingPid());
522
523 NetworkStateTracker radio = mNetTrackers[networkType];
524 NetworkInfo ni = network.getNetworkInfo();
525
526 if (ni.isAvailable() == false) {
527 if (DBG) Log.d(TAG, "special network not available");
528 return Phone.APN_TYPE_NOT_AVAILABLE;
529 }
530
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700531 synchronized(this) {
532 mFeatureUsers.add(f);
533 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
534 // this gets used for per-pid dns when connected
535 mNetRequestersPids[usedNetworkType].add(currentPid);
536 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700537 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700538 mHandler.sendMessageDelayed(mHandler.obtainMessage(
539 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
540 f), getRestoreDefaultNetworkDelay());
541
Robert Greenwalt2034b912009-08-12 16:08:25 -0700542
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700543 if ((ni.isConnectedOrConnecting() == true) &&
544 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700545 if (ni.isConnected() == true) {
546 // add the pid-specific dns
547 handleDnsConfigurationChange();
548 if (DBG) Log.d(TAG, "special network already active");
549 return Phone.APN_ALREADY_ACTIVE;
550 }
551 if (DBG) Log.d(TAG, "special network already connecting");
552 return Phone.APN_REQUEST_STARTED;
553 }
554
555 // check if the radio in play can make another contact
556 // assume if cannot for now
557
Robert Greenwalt2034b912009-08-12 16:08:25 -0700558 if (DBG) Log.d(TAG, "reconnecting to special network");
559 network.reconnect();
560 return Phone.APN_REQUEST_STARTED;
561 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700562 synchronized(this) {
563 mFeatureUsers.add(f);
564 }
565 mHandler.sendMessageDelayed(mHandler.obtainMessage(
566 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
567 f), getRestoreDefaultNetworkDelay());
568
Robert Greenwalt2034b912009-08-12 16:08:25 -0700569 return network.startUsingNetworkFeature(feature,
570 getCallingPid(), getCallingUid());
571 }
572 }
573 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800574 }
575
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700576 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800577 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700578 enforceChangePermission();
579
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700580 int pid = getCallingPid();
581 int uid = getCallingUid();
582
583 FeatureUser u = null;
584 boolean found = false;
585
586 synchronized(this) {
587 for (int i = 0; i < mFeatureUsers.size() ; i++) {
588 u = (FeatureUser)mFeatureUsers.get(i);
589 if (uid == u.mUid && pid == u.mPid &&
590 networkType == u.mNetworkType &&
591 TextUtils.equals(feature, u.mFeature)) {
592 found = true;
593 break;
594 }
595 }
596 }
597 if (found && u != null) {
598 // stop regardless of how many other time this proc had called start
599 return stopUsingNetworkFeature(u, true);
600 } else {
601 // none found!
602 return 1;
603 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700604 }
605
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700606 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
607 int networkType = u.mNetworkType;
608 String feature = u.mFeature;
609 int pid = u.mPid;
610 int uid = u.mUid;
611
612 NetworkStateTracker tracker = null;
613 boolean callTeardown = false; // used to carry our decision outside of sync block
614
Robert Greenwalt2034b912009-08-12 16:08:25 -0700615 if (DBG) {
616 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
617 ": " + feature);
618 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700619
The Android Open Source Project28527d22009-03-03 19:31:44 -0800620 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
621 return -1;
622 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700623
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700624 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
625 // sync block
626 synchronized(this) {
627 // check if this process still has an outstanding start request
628 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700629 return 1;
630 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700631 u.unlinkDeathRecipient();
632 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
633 // If we care about duplicate requests, check for that here.
634 //
635 // This is done to support the extension of a request - the app
636 // can request we start the network feature again and renew the
637 // auto-shutoff delay. Normal "stop" calls from the app though
638 // do not pay attention to duplicate requests - in effect the
639 // API does not refcount and a single stop will counter multiple starts.
640 if (ignoreDups == false) {
641 for (int i = 0; i < mFeatureUsers.size() ; i++) {
642 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
643 if (x.mUid == u.mUid && x.mPid == u.mPid &&
644 x.mNetworkType == u.mNetworkType &&
645 TextUtils.equals(x.mFeature, u.mFeature)) {
646 return 1;
647 }
648 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700649 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650
651 // TODO - move to MobileDataStateTracker
652 int usedNetworkType = networkType;
653 if (networkType == ConnectivityManager.TYPE_MOBILE) {
654 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
655 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
656 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
657 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
658 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
659 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
660 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
661 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
662 }
663 }
664 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700665 if (tracker == null) {
666 return -1;
667 }
668 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700669 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700670 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800671 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700672 if (mNetRequestersPids[usedNetworkType].size() != 0) {
673 if (DBG) Log.d(TAG, "not tearing down special network - " +
674 "others still using it");
675 return 1;
676 }
677 callTeardown = true;
678 }
679 }
680
681 if (callTeardown) {
682 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700683 return 1;
684 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700685 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700686 return tracker.stopUsingNetworkFeature(feature, pid, uid);
687 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800688 }
689
690 /**
691 * Ensure that a network route exists to deliver traffic to the specified
692 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700693 * @param networkType the type of the network over which traffic to the
694 * specified host is to be routed
695 * @param hostAddress the IP address of the host to which the route is
696 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800697 * @return {@code true} on success, {@code false} on failure
698 */
699 public boolean requestRouteToHost(int networkType, int hostAddress) {
700 enforceChangePermission();
701 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
702 return false;
703 }
704 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700705
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700706 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
707 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700708 if (DBG) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700709 Log.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700710 }
711 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800712 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700713 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800714 }
715
716 /**
717 * @see ConnectivityManager#getBackgroundDataSetting()
718 */
719 public boolean getBackgroundDataSetting() {
720 return Settings.Secure.getInt(mContext.getContentResolver(),
721 Settings.Secure.BACKGROUND_DATA, 1) == 1;
722 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700723
The Android Open Source Project28527d22009-03-03 19:31:44 -0800724 /**
725 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
726 */
727 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
728 mContext.enforceCallingOrSelfPermission(
729 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
730 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700731
The Android Open Source Project28527d22009-03-03 19:31:44 -0800732 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
733
734 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700735 Settings.Secure.BACKGROUND_DATA,
736 allowBackgroundDataUsage ? 1 : 0);
737
The Android Open Source Project28527d22009-03-03 19:31:44 -0800738 Intent broadcast = new Intent(
739 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
740 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700741 }
742
The Android Open Source Project28527d22009-03-03 19:31:44 -0800743 private int getNumConnectedNetworks() {
744 int numConnectedNets = 0;
745
746 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700747 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700748 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800749 ++numConnectedNets;
750 }
751 }
752 return numConnectedNets;
753 }
754
755 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700756 mContext.enforceCallingOrSelfPermission(
757 android.Manifest.permission.ACCESS_NETWORK_STATE,
758 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800759 }
760
761 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700762 mContext.enforceCallingOrSelfPermission(
763 android.Manifest.permission.CHANGE_NETWORK_STATE,
764 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800765 }
766
767 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700768 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
769 * network, we ignore it. If it is for the active network, we send out a
770 * broadcast. But first, we check whether it might be possible to connect
771 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800772 * @param info the {@code NetworkInfo} for the network
773 */
774 private void handleDisconnect(NetworkInfo info) {
775
Robert Greenwalt2034b912009-08-12 16:08:25 -0700776 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800777
Robert Greenwalt2034b912009-08-12 16:08:25 -0700778 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 /*
780 * If the disconnected network is not the active one, then don't report
781 * this as a loss of connectivity. What probably happened is that we're
782 * getting the disconnect for a network that we explicitly disabled
783 * in accordance with network preference policies.
784 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700785 if (!mNetAttributes[prevNetType].isDefault()) {
786 List pids = mNetRequestersPids[prevNetType];
787 for (int i = 0; i<pids.size(); i++) {
788 Integer pid = (Integer)pids.get(i);
789 // will remove them because the net's no longer connected
790 // need to do this now as only now do we know the pids and
791 // can properly null things that are no longer referenced.
792 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800793 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 }
795
The Android Open Source Project28527d22009-03-03 19:31:44 -0800796 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800797 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800798 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
799 if (info.isFailover()) {
800 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
801 info.setFailover(false);
802 }
803 if (info.getReason() != null) {
804 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
805 }
806 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700807 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
808 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800809 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700810
811 /*
812 * If this is a default network, check if other defaults are available
813 * or active
814 */
815 NetworkStateTracker newNet = null;
816 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700817 if (mActiveDefaultNetwork == prevNetType) {
818 mActiveDefaultNetwork = -1;
819 }
820
821 int newType = -1;
822 int newPriority = -1;
823 for (int checkType=0; checkType <=
824 ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700825 if (checkType == prevNetType) continue;
826 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700827 if (mNetAttributes[checkType].isDefault()) {
828 /* TODO - if we have multiple nets we could use
829 * we may want to put more thought into which we choose
830 */
831 if (checkType == mNetworkPreference) {
832 newType = checkType;
833 break;
834 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700835 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700836 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700837 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700838 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800839 }
840 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700841
842 if (newType != -1) {
843 newNet = mNetTrackers[newType];
844 /**
845 * See if the other network is available to fail over to.
846 * If is not available, we enable it anyway, so that it
847 * will be able to connect when it does become available,
848 * but we report a total loss of connectivity rather than
849 * report that we are attempting to fail over.
850 */
851 if (newNet.isAvailable()) {
852 NetworkInfo switchTo = newNet.getNetworkInfo();
853 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700854 if (!switchTo.isConnectedOrConnecting() ||
855 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700856 newNet.reconnect();
857 }
858 if (DBG) {
859 if (switchTo.isConnected()) {
860 Log.v(TAG, "Switching to already connected " +
861 switchTo.getTypeName());
862 } else {
863 Log.v(TAG, "Attempting to switch to " +
864 switchTo.getTypeName());
865 }
866 }
867 intent.putExtra(ConnectivityManager.
868 EXTRA_OTHER_NETWORK_INFO, switchTo);
869 } else {
Robert Greenwalt149d5ac2009-09-17 14:58:16 -0700870 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
871 true);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700872 newNet.reconnect();
873 }
874 } else {
875 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
876 true);
877 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800878 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700879
880 // do this before we broadcast the change
881 handleConnectivityChange();
882
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400883 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800884 /*
Robert Greenwalt0659da32009-07-16 17:21:39 -0700885 * If the failover network is already connected, then immediately send
886 * out a followup broadcast indicating successful failover
The Android Open Source Project28527d22009-03-03 19:31:44 -0800887 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700888 if (newNet != null && newNet.getNetworkInfo().isConnected())
889 sendConnectedBroadcast(newNet.getNetworkInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800890 }
891
892 private void sendConnectedBroadcast(NetworkInfo info) {
893 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800894 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800895 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
896 if (info.isFailover()) {
897 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
898 info.setFailover(false);
899 }
900 if (info.getReason() != null) {
901 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
902 }
903 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700904 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
905 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800906 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400907 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800908 }
909
910 /**
911 * Called when an attempt to fail over to another network has failed.
912 * @param info the {@link NetworkInfo} for the failed network
913 */
914 private void handleConnectionFailure(NetworkInfo info) {
915 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916
Robert Greenwalt2034b912009-08-12 16:08:25 -0700917 String reason = info.getReason();
918 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700919
Robert Greenwalt2034b912009-08-12 16:08:25 -0700920 if (DBG) {
921 String reasonText;
922 if (reason == null) {
923 reasonText = ".";
924 } else {
925 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800926 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700927 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
928 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800929 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700930
931 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800932 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700933 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
934 if (getActiveNetworkInfo() == null) {
935 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
936 }
937 if (reason != null) {
938 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
939 }
940 if (extraInfo != null) {
941 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
942 }
943 if (info.isFailover()) {
944 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
945 info.setFailover(false);
946 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400947 sendStickyBroadcast(intent);
948 }
949
950 private void sendStickyBroadcast(Intent intent) {
951 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -0800952 if (!mSystemReady) {
953 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400954 }
Dianne Hackborna417ff82009-12-08 19:45:14 -0800955 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
956 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400957 }
958 }
959
960 void systemReady() {
961 synchronized(this) {
962 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800963 if (mInitialBroadcast != null) {
964 mContext.sendStickyBroadcast(mInitialBroadcast);
965 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400966 }
967 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800968 }
969
970 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700971 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800972
973 // snapshot isFailover, because sendConnectedBroadcast() resets it
974 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700975 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800976
Robert Greenwalt2034b912009-08-12 16:08:25 -0700977 // if this is a default net and other default is running
978 // kill the one not preferred
979 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700980 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
981 if ((type != mNetworkPreference &&
982 mNetAttributes[mActiveDefaultNetwork].mPriority >
983 mNetAttributes[type].mPriority) ||
984 mNetworkPreference == mActiveDefaultNetwork) {
985 // don't accept this one
986 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
987 "to torn down network " + info.getTypeName());
988 teardown(thisNet);
989 return;
990 } else {
991 // tear down the other
992 NetworkStateTracker otherNet =
993 mNetTrackers[mActiveDefaultNetwork];
994 if (DBG) Log.v(TAG, "Policy requires " +
995 otherNet.getNetworkInfo().getTypeName() +
996 " teardown");
997 if (!teardown(otherNet)) {
998 Log.e(TAG, "Network declined teardown request");
999 return;
1000 }
1001 if (isFailover) {
1002 otherNet.releaseWakeLock();
1003 }
1004 }
1005 }
1006 mActiveDefaultNetwork = type;
1007 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001008 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 thisNet.updateNetworkSettings();
1010 handleConnectivityChange();
1011 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001012 }
1013
1014 private void handleScanResultsAvailable(NetworkInfo info) {
1015 int networkType = info.getType();
1016 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001017 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1018 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001020
The Android Open Source Project28527d22009-03-03 19:31:44 -08001021 mNetTrackers[networkType].interpretScanResultsAvailable();
1022 }
1023
Robert Greenwalt0659da32009-07-16 17:21:39 -07001024 private void handleNotificationChange(boolean visible, int id,
1025 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001026 NotificationManager notificationManager = (NotificationManager) mContext
1027 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001028
The Android Open Source Project28527d22009-03-03 19:31:44 -08001029 if (visible) {
1030 notificationManager.notify(id, notification);
1031 } else {
1032 notificationManager.cancel(id);
1033 }
1034 }
1035
1036 /**
1037 * After any kind of change in the connectivity state of any network,
1038 * make sure that anything that depends on the connectivity state of
1039 * more than one network is set up correctly. We're mainly concerned
1040 * with making sure that the list of DNS servers is set up according
1041 * to which networks are connected, and ensuring that the right routing
1042 * table entries exist.
1043 */
1044 private void handleConnectivityChange() {
1045 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001047 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001048 * If both mobile and wifi are enabled, add the host routes that
1049 * will allow MMS traffic to pass on the mobile network. But
1050 * remove the default route for the mobile network, so that there
1051 * will be only one default route, to ensure that all traffic
1052 * except MMS will travel via Wi-Fi.
1053 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 handleDnsConfigurationChange();
1055
1056 for (int netType : mPriorityList) {
1057 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1058 if (mNetAttributes[netType].isDefault()) {
1059 mNetTrackers[netType].addDefaultRoute();
1060 } else {
1061 mNetTrackers[netType].addPrivateDnsRoutes();
1062 }
1063 } else {
1064 if (mNetAttributes[netType].isDefault()) {
1065 mNetTrackers[netType].removeDefaultRoute();
1066 } else {
1067 mNetTrackers[netType].removePrivateDnsRoutes();
1068 }
1069 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001070 }
1071 }
1072
Robert Greenwalt2034b912009-08-12 16:08:25 -07001073 /**
1074 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1075 * on the highest priority active net which this process requested.
1076 * If there aren't any, clear it out
1077 */
1078 private void reassessPidDns(int myPid, boolean doBump)
1079 {
1080 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1081 for(int i : mPriorityList) {
1082 if (mNetAttributes[i].isDefault()) {
1083 continue;
1084 }
1085 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001086 if (nt.getNetworkInfo().isConnected() &&
1087 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001088 List pids = mNetRequestersPids[i];
1089 for (int j=0; j<pids.size(); j++) {
1090 Integer pid = (Integer)pids.get(j);
1091 if (pid.intValue() == myPid) {
1092 String[] dnsList = nt.getNameServers();
1093 writePidDns(dnsList, myPid);
1094 if (doBump) {
1095 bumpDns();
1096 }
1097 return;
1098 }
1099 }
1100 }
1101 }
1102 // nothing found - delete
1103 for (int i = 1; ; i++) {
1104 String prop = "net.dns" + i + "." + myPid;
1105 if (SystemProperties.get(prop).length() == 0) {
1106 if (doBump) {
1107 bumpDns();
1108 }
1109 return;
1110 }
1111 SystemProperties.set(prop, "");
1112 }
1113 }
1114
1115 private void writePidDns(String[] dnsList, int pid) {
1116 int j = 1;
1117 for (String dns : dnsList) {
1118 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1119 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1120 }
1121 }
1122 }
1123
1124 private void bumpDns() {
1125 /*
1126 * Bump the property that tells the name resolver library to reread
1127 * the DNS server list from the properties.
1128 */
1129 String propVal = SystemProperties.get("net.dnschange");
1130 int n = 0;
1131 if (propVal.length() != 0) {
1132 try {
1133 n = Integer.parseInt(propVal);
1134 } catch (NumberFormatException e) {}
1135 }
1136 SystemProperties.set("net.dnschange", "" + (n+1));
1137 }
1138
1139 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001140 // add default net's dns entries
1141 for (int x = mPriorityList.length-1; x>= 0; x--) {
1142 int netType = mPriorityList[x];
1143 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001144 if (nt != null && nt.getNetworkInfo().isConnected() &&
1145 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001146 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001147 if (mNetAttributes[netType].isDefault()) {
1148 int j = 1;
1149 for (String dns : dnsList) {
1150 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001151 if (DBG) {
1152 Log.d(TAG, "adding dns " + dns + " for " +
1153 nt.getNetworkInfo().getTypeName());
1154 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 SystemProperties.set("net.dns" + j++, dns);
1156 }
1157 }
1158 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001159 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1160 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 }
1162 mNumDnsEntries = j;
1163 } else {
1164 // set per-pid dns for attached secondary nets
1165 List pids = mNetRequestersPids[netType];
1166 for (int y=0; y< pids.size(); y++) {
1167 Integer pid = (Integer)pids.get(y);
1168 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001169 }
1170 }
1171 }
1172 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001173
1174 bumpDns();
1175 }
1176
1177 private int getRestoreDefaultNetworkDelay() {
1178 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1179 NETWORK_RESTORE_DELAY_PROP_NAME);
1180 if(restoreDefaultNetworkDelayStr != null &&
1181 restoreDefaultNetworkDelayStr.length() != 0) {
1182 try {
1183 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1184 } catch (NumberFormatException e) {
1185 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001186 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001187 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001188 }
1189
1190 @Override
1191 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001192 if (mContext.checkCallingOrSelfPermission(
1193 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001194 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001195 pw.println("Permission Denial: can't dump ConnectivityService " +
1196 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1197 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 return;
1199 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001200 pw.println();
1201 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001202 if (nst != null && nst.getNetworkInfo().isConnected()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001203 pw.println("Active network: " + nst.getNetworkInfo().
1204 getTypeName());
1205 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001206 pw.println(nst.getNetworkInfo());
1207 pw.println(nst);
1208 pw.println();
1209 }
1210 }
1211
Robert Greenwalt2034b912009-08-12 16:08:25 -07001212 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 private class MyHandler extends Handler {
1214 @Override
1215 public void handleMessage(Message msg) {
1216 NetworkInfo info;
1217 switch (msg.what) {
1218 case NetworkStateTracker.EVENT_STATE_CHANGED:
1219 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001220 int type = info.getType();
1221 NetworkInfo.State state = info.getState();
1222 if(mNetAttributes[type].mLastState == state) {
1223 if (DBG) {
1224 // TODO - remove this after we validate the dropping doesn't break anything
1225 Log.d(TAG, "Dropping ConnectivityChange for " +
1226 info.getTypeName() +": " +
1227 state + "/" + info.getDetailedState());
1228 }
1229 return;
1230 }
1231 mNetAttributes[type].mLastState = state;
1232
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001234 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001235 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236
1237 // Connectivity state changed:
1238 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001239 // [12-9] Network subtype (for mobile network, as defined
1240 // by TelephonyManager)
1241 // [8-3] Detailed state ordinal (as defined by
1242 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001243 // [2-0] Network type (as defined by ConnectivityManager)
1244 int eventLogParam = (info.getType() & 0x7) |
1245 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1246 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001247 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001248 eventLogParam);
1249
1250 if (info.getDetailedState() ==
1251 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001252 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001253 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001254 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001255 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001257 // the logic here is, handle SUSPENDED the same as
1258 // DISCONNECTED. The only difference being we are
1259 // broadcasting an intent with NetworkInfo that's
1260 // suspended. This allows the applications an
1261 // opportunity to handle DISCONNECTED and SUSPENDED
1262 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001263 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001264 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001265 handleConnect(info);
1266 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001267 break;
1268
1269 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1270 info = (NetworkInfo) msg.obj;
1271 handleScanResultsAvailable(info);
1272 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001273
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001275 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1276 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001277
1278 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001279 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001280 break;
1281
1282 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1283 // fill me in
1284 break;
1285
1286 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1287 // fill me in
1288 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001289 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001290 FeatureUser u = (FeatureUser)msg.obj;
1291 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001292 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001293 }
1294 }
1295 }
1296}