blob: df685abc17aeb8b51360fd8002279642c46b3e31 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt42acef32009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.EventLog;
42import android.util.Log;
43
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080046import com.android.server.connectivity.Tethering;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Robert Greenwalt42acef32009-08-12 16:08:25 -070050import java.util.ArrayList;
51import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53/**
54 * @hide
55 */
56public class ConnectivityService extends IConnectivityManager.Stub {
57
Robert Greenwaltd8df1492009-10-06 14:12:53 -070058 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private static final String TAG = "ConnectivityService";
60
Robert Greenwalt42acef32009-08-12 16:08:25 -070061 // how long to wait before switching back to a radio's default network
62 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
63 // system property that can override the above value
64 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
65 "android.telephony.apn-restore";
66
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080067
68 private Tethering mTethering;
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 /**
71 * Sometimes we want to refer to the individual network state
72 * trackers separately, and sometimes we just want to treat them
73 * abstractly.
74 */
75 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070076
77 /**
78 * A per Net list of the PID's that requested access to the net
79 * used both as a refcount and for per-PID DNS selection
80 */
81 private List mNetRequestersPids[];
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private WifiWatchdogService mWifiWatchdogService;
84
Robert Greenwalt42acef32009-08-12 16:08:25 -070085 // priority order of the nettrackers
86 // (excluding dynamically set mNetworkPreference)
87 // TODO - move mNetworkTypePreference into this
88 private int[] mPriorityList;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 private Context mContext;
91 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070092 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96 private boolean mTestMode;
97 private static ConnectivityService sServiceInstance;
98
Robert Greenwalt42acef32009-08-12 16:08:25 -070099 private Handler mHandler;
100
101 // list of DeathRecipients used to make sure features are turned off when
102 // a process dies
103 private List mFeatureUsers;
104
Mike Lockwood0f79b542009-08-14 14:18:49 -0400105 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800106 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400107
Robert Greenwalt511288a2009-12-07 11:33:18 -0800108 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700109 /**
110 * Class for holding settings read from resources.
111 */
112 public String mName;
113 public int mType;
114 public int mRadio;
115 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800116 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700117 public NetworkAttributes(String init) {
118 String fragments[] = init.split(",");
119 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700120 mType = Integer.parseInt(fragments[1]);
121 mRadio = Integer.parseInt(fragments[2]);
122 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800123 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700124 }
125 public boolean isDefault() {
126 return (mType == mRadio);
127 }
128 }
129 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700130 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700131
Robert Greenwalt511288a2009-12-07 11:33:18 -0800132 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700133 public int mSimultaneity;
134 public int mType;
135 public RadioAttributes(String init) {
136 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700137 mType = Integer.parseInt(fragments[0]);
138 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700139 }
140 }
141 RadioAttributes[] mRadioAttributes;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private static class ConnectivityThread extends Thread {
144 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private ConnectivityThread(Context context) {
147 super("ConnectivityThread");
148 mContext = context;
149 }
150
151 @Override
152 public void run() {
153 Looper.prepare();
154 synchronized (this) {
155 sServiceInstance = new ConnectivityService(mContext);
156 notifyAll();
157 }
158 Looper.loop();
159 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 public static ConnectivityService getServiceInstance(Context context) {
162 ConnectivityThread thread = new ConnectivityThread(context);
163 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 synchronized (thread) {
166 while (sServiceInstance == null) {
167 try {
168 // Wait until sServiceInstance has been initialized.
169 thread.wait();
170 } catch (InterruptedException ignore) {
171 Log.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700172 "Unexpected InterruptedException while waiting"+
173 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 }
175 }
176 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 return sServiceInstance;
179 }
180 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static ConnectivityService getInstance(Context context) {
183 return ConnectivityThread.getServiceInstance(context);
184 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 private ConnectivityService(Context context) {
187 if (DBG) Log.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800188
189 // setup our unique device name
190 String id = Settings.Secure.getString(context.getContentResolver(),
191 Settings.Secure.ANDROID_ID);
192 if (id != null && id.length() > 0) {
193 String name = new String("android_").concat(id);
194 SystemProperties.set("net.hostname", name);
195 }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700198 mNetTrackers = new NetworkStateTracker[
199 ConnectivityManager.MAX_NETWORK_TYPE+1];
200 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700203
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700204 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
205 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
206
Robert Greenwalt42acef32009-08-12 16:08:25 -0700207 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700208 String[] raStrings = context.getResources().getStringArray(
209 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700210 for (String raString : raStrings) {
211 RadioAttributes r = new RadioAttributes(raString);
212 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
213 Log.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
214 continue;
215 }
216 if (mRadioAttributes[r.mType] != null) {
217 Log.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
218 r.mType);
219 continue;
220 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700221 mRadioAttributes[r.mType] = r;
222 }
223
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700224 String[] naStrings = context.getResources().getStringArray(
225 com.android.internal.R.array.networkAttributes);
226 for (String naString : naStrings) {
227 try {
228 NetworkAttributes n = new NetworkAttributes(naString);
229 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
230 Log.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
231 n.mType);
232 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700233 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700234 if (mNetAttributes[n.mType] != null) {
235 Log.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
236 n.mType);
237 continue;
238 }
239 if (mRadioAttributes[n.mRadio] == null) {
240 Log.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
241 "radio " + n.mRadio + " in network type " + n.mType);
242 continue;
243 }
244 mNetAttributes[n.mType] = n;
245 mNetworksDefined++;
246 } catch(Exception e) {
247 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700248 }
249 }
250
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700251 // high priority first
252 mPriorityList = new int[mNetworksDefined];
253 {
254 int insertionPoint = mNetworksDefined-1;
255 int currentLowest = 0;
256 int nextLowest = 0;
257 while (insertionPoint > -1) {
258 for (NetworkAttributes na : mNetAttributes) {
259 if (na == null) continue;
260 if (na.mPriority < currentLowest) continue;
261 if (na.mPriority > currentLowest) {
262 if (na.mPriority < nextLowest || nextLowest == 0) {
263 nextLowest = na.mPriority;
264 }
265 continue;
266 }
267 mPriorityList[insertionPoint--] = na.mType;
268 }
269 currentLowest = nextLowest;
270 nextLowest = 0;
271 }
272 }
273
274 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
275 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700276 mNetRequestersPids[i] = new ArrayList();
277 }
278
279 mFeatureUsers = new ArrayList();
280
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700281 mNumDnsEntries = 0;
282
283 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
284 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /*
286 * Create the network state trackers for Wi-Fi and mobile
287 * data. Maybe this could be done with a factory class,
288 * but it's not clear that it's worth it, given that
289 * the number of different network types is not going
290 * to change very often.
291 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800292 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700293 for (int netType : mPriorityList) {
294 switch (mNetAttributes[netType].mRadio) {
295 case ConnectivityManager.TYPE_WIFI:
296 if (DBG) Log.v(TAG, "Starting Wifi Service.");
297 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
298 WifiService wifiService = new WifiService(context, wst);
299 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
300 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
301 wst.startMonitoring();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700303 // Constructing this starts it too
304 mWifiWatchdogService = new WifiWatchdogService(context, wst);
305 break;
306 case ConnectivityManager.TYPE_MOBILE:
307 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
308 netType, mNetAttributes[netType].mName);
309 mNetTrackers[netType].startMonitoring();
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800310 if (noMobileData) {
311 if (DBG) Log.d(TAG, "tearing down Mobile networks due to setting");
312 mNetTrackers[netType].teardown();
313 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700314 break;
315 default:
316 Log.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
317 mNetAttributes[netType].mRadio);
318 continue;
319 }
320 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800321
322 mTethering = new Tethering(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700327 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 * @param preference the new preference
329 */
330 public synchronized void setNetworkPreference(int preference) {
331 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700332 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700333 mNetAttributes[preference] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 if (mNetworkPreference != preference) {
336 persistNetworkPreference(preference);
337 mNetworkPreference = preference;
338 enforcePreference();
339 }
340 }
341 }
342
343 public int getNetworkPreference() {
344 enforceAccessPermission();
345 return mNetworkPreference;
346 }
347
348 private void persistNetworkPreference(int networkPreference) {
349 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700350 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
351 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 private int getPersistedNetworkPreference() {
355 final ContentResolver cr = mContext.getContentResolver();
356
357 final int networkPrefSetting = Settings.Secure
358 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
359 if (networkPrefSetting != -1) {
360 return networkPrefSetting;
361 }
362
363 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
364 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700367 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * In this method, we only tear down a non-preferred network. Establishing
369 * a connection to the preferred network is taken care of when we handle
370 * the disconnect event from the non-preferred network
371 * (see {@link #handleDisconnect(NetworkInfo)}).
372 */
373 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700374 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 return;
376
Robert Greenwalt42acef32009-08-12 16:08:25 -0700377 if (!mNetTrackers[mNetworkPreference].isAvailable())
378 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379
Robert Greenwalt42acef32009-08-12 16:08:25 -0700380 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700381 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700382 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700383 if (DBG) {
384 Log.d(TAG, "tearing down " +
385 mNetTrackers[t].getNetworkInfo() +
386 " in enforcePreference");
387 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700388 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 }
391 }
392
393 private boolean teardown(NetworkStateTracker netTracker) {
394 if (netTracker.teardown()) {
395 netTracker.setTeardownRequested(true);
396 return true;
397 } else {
398 return false;
399 }
400 }
401
402 /**
403 * Return NetworkInfo for the active (i.e., connected) network interface.
404 * It is assumed that at most one network is active at a time. If more
405 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700406 * @return the info for the active network, or {@code null} if none is
407 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 */
409 public NetworkInfo getActiveNetworkInfo() {
410 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700411 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700412 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700413 continue;
414 }
415 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 NetworkInfo info = t.getNetworkInfo();
417 if (info.isConnected()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700418 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
419 "connected default network is not " +
420 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 return info;
422 }
423 }
424 return null;
425 }
426
427 public NetworkInfo getNetworkInfo(int networkType) {
428 enforceAccessPermission();
429 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
430 NetworkStateTracker t = mNetTrackers[networkType];
431 if (t != null)
432 return t.getNetworkInfo();
433 }
434 return null;
435 }
436
437 public NetworkInfo[] getAllNetworkInfo() {
438 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700439 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 int i = 0;
441 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700442 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444 return result;
445 }
446
447 public boolean setRadios(boolean turnOn) {
448 boolean result = true;
449 enforceChangePermission();
450 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700451 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453 return result;
454 }
455
456 public boolean setRadio(int netType, boolean turnOn) {
457 enforceChangePermission();
458 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
459 return false;
460 }
461 NetworkStateTracker tracker = mNetTrackers[netType];
462 return tracker != null && tracker.setRadio(turnOn);
463 }
464
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700465 /**
466 * Used to notice when the calling process dies so we can self-expire
467 *
468 * Also used to know if the process has cleaned up after itself when
469 * our auto-expire timer goes off. The timer has a link to an object.
470 *
471 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 private class FeatureUser implements IBinder.DeathRecipient {
473 int mNetworkType;
474 String mFeature;
475 IBinder mBinder;
476 int mPid;
477 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800478 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700479
480 FeatureUser(int type, String feature, IBinder binder) {
481 super();
482 mNetworkType = type;
483 mFeature = feature;
484 mBinder = binder;
485 mPid = getCallingPid();
486 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800487 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700488
Robert Greenwalt42acef32009-08-12 16:08:25 -0700489 try {
490 mBinder.linkToDeath(this, 0);
491 } catch (RemoteException e) {
492 binderDied();
493 }
494 }
495
496 void unlinkDeathRecipient() {
497 mBinder.unlinkToDeath(this, 0);
498 }
499
500 public void binderDied() {
501 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800502 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
503 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700504 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700505 }
506
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700507 public void expire() {
508 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800509 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
510 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700511 stopUsingNetworkFeature(this, false);
512 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800513
514 public String toString() {
515 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
516 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
517 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700518 }
519
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700520 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700521 public int startUsingNetworkFeature(int networkType, String feature,
522 IBinder binder) {
523 if (DBG) {
524 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
525 ": " + feature);
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700528 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
529 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700530 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700533 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534
535 // TODO - move this into the MobileDataStateTracker
536 int usedNetworkType = networkType;
537 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800538 if (!getMobileDataEnabled()) {
539 if (DBG) Log.d(TAG, "requested special network with data disabled - rejected");
540 return Phone.APN_TYPE_NOT_AVAILABLE;
541 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700542 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
543 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
544 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
545 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
546 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
547 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
548 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
549 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
550 }
551 }
552 NetworkStateTracker network = mNetTrackers[usedNetworkType];
553 if (network != null) {
554 if (usedNetworkType != networkType) {
555 Integer currentPid = new Integer(getCallingPid());
556
557 NetworkStateTracker radio = mNetTrackers[networkType];
558 NetworkInfo ni = network.getNetworkInfo();
559
560 if (ni.isAvailable() == false) {
561 if (DBG) Log.d(TAG, "special network not available");
562 return Phone.APN_TYPE_NOT_AVAILABLE;
563 }
564
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700565 synchronized(this) {
566 mFeatureUsers.add(f);
567 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
568 // this gets used for per-pid dns when connected
569 mNetRequestersPids[usedNetworkType].add(currentPid);
570 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700571 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700572 mHandler.sendMessageDelayed(mHandler.obtainMessage(
573 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
574 f), getRestoreDefaultNetworkDelay());
575
Robert Greenwalt42acef32009-08-12 16:08:25 -0700576
Robert Greenwalta64bf832009-08-19 20:19:33 -0700577 if ((ni.isConnectedOrConnecting() == true) &&
578 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700579 if (ni.isConnected() == true) {
580 // add the pid-specific dns
581 handleDnsConfigurationChange();
582 if (DBG) Log.d(TAG, "special network already active");
583 return Phone.APN_ALREADY_ACTIVE;
584 }
585 if (DBG) Log.d(TAG, "special network already connecting");
586 return Phone.APN_REQUEST_STARTED;
587 }
588
589 // check if the radio in play can make another contact
590 // assume if cannot for now
591
Robert Greenwalt42acef32009-08-12 16:08:25 -0700592 if (DBG) Log.d(TAG, "reconnecting to special network");
593 network.reconnect();
594 return Phone.APN_REQUEST_STARTED;
595 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700596 synchronized(this) {
597 mFeatureUsers.add(f);
598 }
599 mHandler.sendMessageDelayed(mHandler.obtainMessage(
600 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
601 f), getRestoreDefaultNetworkDelay());
602
Robert Greenwalt42acef32009-08-12 16:08:25 -0700603 return network.startUsingNetworkFeature(feature,
604 getCallingPid(), getCallingUid());
605 }
606 }
607 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700610 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700612 enforceChangePermission();
613
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700614 int pid = getCallingPid();
615 int uid = getCallingUid();
616
617 FeatureUser u = null;
618 boolean found = false;
619
620 synchronized(this) {
621 for (int i = 0; i < mFeatureUsers.size() ; i++) {
622 u = (FeatureUser)mFeatureUsers.get(i);
623 if (uid == u.mUid && pid == u.mPid &&
624 networkType == u.mNetworkType &&
625 TextUtils.equals(feature, u.mFeature)) {
626 found = true;
627 break;
628 }
629 }
630 }
631 if (found && u != null) {
632 // stop regardless of how many other time this proc had called start
633 return stopUsingNetworkFeature(u, true);
634 } else {
635 // none found!
Robert Greenwaltb9285352009-12-21 18:24:07 -0800636 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700637 return 1;
638 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700639 }
640
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700641 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
642 int networkType = u.mNetworkType;
643 String feature = u.mFeature;
644 int pid = u.mPid;
645 int uid = u.mUid;
646
647 NetworkStateTracker tracker = null;
648 boolean callTeardown = false; // used to carry our decision outside of sync block
649
Robert Greenwalt42acef32009-08-12 16:08:25 -0700650 if (DBG) {
651 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
652 ": " + feature);
653 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
656 return -1;
657 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700658
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700659 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
660 // sync block
661 synchronized(this) {
662 // check if this process still has an outstanding start request
663 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700664 return 1;
665 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700666 u.unlinkDeathRecipient();
667 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
668 // If we care about duplicate requests, check for that here.
669 //
670 // This is done to support the extension of a request - the app
671 // can request we start the network feature again and renew the
672 // auto-shutoff delay. Normal "stop" calls from the app though
673 // do not pay attention to duplicate requests - in effect the
674 // API does not refcount and a single stop will counter multiple starts.
675 if (ignoreDups == false) {
676 for (int i = 0; i < mFeatureUsers.size() ; i++) {
677 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
678 if (x.mUid == u.mUid && x.mPid == u.mPid &&
679 x.mNetworkType == u.mNetworkType &&
680 TextUtils.equals(x.mFeature, u.mFeature)) {
Robert Greenwaltb9285352009-12-21 18:24:07 -0800681 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700682 return 1;
683 }
684 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700685 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700686
687 // TODO - move to MobileDataStateTracker
688 int usedNetworkType = networkType;
689 if (networkType == ConnectivityManager.TYPE_MOBILE) {
690 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
691 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
692 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
693 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
694 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
695 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
696 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
698 }
699 }
700 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700701 if (tracker == null) {
702 return -1;
703 }
704 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700705 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700706 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800707 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700708 if (mNetRequestersPids[usedNetworkType].size() != 0) {
709 if (DBG) Log.d(TAG, "not tearing down special network - " +
710 "others still using it");
711 return 1;
712 }
713 callTeardown = true;
714 }
715 }
716
717 if (callTeardown) {
718 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700719 return 1;
720 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700721 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700722 return tracker.stopUsingNetworkFeature(feature, pid, uid);
723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
725
726 /**
727 * Ensure that a network route exists to deliver traffic to the specified
728 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700729 * @param networkType the type of the network over which traffic to the
730 * specified host is to be routed
731 * @param hostAddress the IP address of the host to which the route is
732 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * @return {@code true} on success, {@code false} on failure
734 */
735 public boolean requestRouteToHost(int networkType, int hostAddress) {
736 enforceChangePermission();
737 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
738 return false;
739 }
740 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700741
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700742 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
743 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700744 if (DBG) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700745 Log.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700746 }
747 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700749 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
752 /**
753 * @see ConnectivityManager#getBackgroundDataSetting()
754 */
755 public boolean getBackgroundDataSetting() {
756 return Settings.Secure.getInt(mContext.getContentResolver(),
757 Settings.Secure.BACKGROUND_DATA, 1) == 1;
758 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 /**
761 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
762 */
763 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
764 mContext.enforceCallingOrSelfPermission(
765 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
766 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
769
770 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700771 Settings.Secure.BACKGROUND_DATA,
772 allowBackgroundDataUsage ? 1 : 0);
773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 Intent broadcast = new Intent(
775 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
776 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700777 }
778
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800779 /**
780 * @see ConnectivityManager#getMobileDataEnabled()
781 */
782 public boolean getMobileDataEnabled() {
783 enforceAccessPermission();
784 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
785 Settings.Secure.MOBILE_DATA, 1) == 1;
786 if (DBG) Log.d(TAG, "getMobileDataEnabled returning " + retVal);
787 return retVal;
788 }
789
790 /**
791 * @see ConnectivityManager#setMobileDataEnabled(boolean)
792 */
793 public synchronized void setMobileDataEnabled(boolean enabled) {
794 enforceChangePermission();
795 if (DBG) Log.d(TAG, "setMobileDataEnabled(" + enabled + ")");
796
797 if (getMobileDataEnabled() == enabled) return;
798
799 Settings.Secure.putInt(mContext.getContentResolver(),
800 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
801
802 if (enabled) {
803 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
804 if (DBG) Log.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
805 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
806 }
807 } else {
808 for (NetworkStateTracker nt : mNetTrackers) {
809 if (nt == null) continue;
810 int netType = nt.getNetworkInfo().getType();
811 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
812 if (DBG) Log.d(TAG, "tearing down " + nt);
813 nt.teardown();
814 }
815 }
816 }
817 }
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 private int getNumConnectedNetworks() {
820 int numConnectedNets = 0;
821
822 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700823 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700824 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 ++numConnectedNets;
826 }
827 }
828 return numConnectedNets;
829 }
830
831 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700832 mContext.enforceCallingOrSelfPermission(
833 android.Manifest.permission.ACCESS_NETWORK_STATE,
834 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836
837 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700838 mContext.enforceCallingOrSelfPermission(
839 android.Manifest.permission.CHANGE_NETWORK_STATE,
840 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800843 // TODO Make this a special check when it goes public
844 private void enforceTetherChangePermission() {
845 mContext.enforceCallingOrSelfPermission(
846 android.Manifest.permission.CHANGE_NETWORK_STATE,
847 "ConnectivityService");
848 }
849
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800850 private void enforceTetherAccessPermission() {
851 mContext.enforceCallingOrSelfPermission(
852 android.Manifest.permission.ACCESS_NETWORK_STATE,
853 "ConnectivityService");
854 }
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700857 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
858 * network, we ignore it. If it is for the active network, we send out a
859 * broadcast. But first, we check whether it might be possible to connect
860 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 * @param info the {@code NetworkInfo} for the network
862 */
863 private void handleDisconnect(NetworkInfo info) {
864
Robert Greenwalt42acef32009-08-12 16:08:25 -0700865 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866
Robert Greenwalt42acef32009-08-12 16:08:25 -0700867 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 /*
869 * If the disconnected network is not the active one, then don't report
870 * this as a loss of connectivity. What probably happened is that we're
871 * getting the disconnect for a network that we explicitly disabled
872 * in accordance with network preference policies.
873 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700874 if (!mNetAttributes[prevNetType].isDefault()) {
875 List pids = mNetRequestersPids[prevNetType];
876 for (int i = 0; i<pids.size(); i++) {
877 Integer pid = (Integer)pids.get(i);
878 // will remove them because the net's no longer connected
879 // need to do this now as only now do we know the pids and
880 // can properly null things that are no longer referenced.
881 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800886 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
888 if (info.isFailover()) {
889 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
890 info.setFailover(false);
891 }
892 if (info.getReason() != null) {
893 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
894 }
895 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700896 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
897 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700899
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800900 NetworkStateTracker newNet = null;
901 if (mNetAttributes[prevNetType].isDefault()) {
902 newNet = tryFailover(prevNetType);
903 if (newNet != null) {
904 NetworkInfo switchTo = newNet.getNetworkInfo();
905 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
906 } else {
907 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
908 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800909 }
910 // do this before we broadcast the change
911 handleConnectivityChange();
912
913 sendStickyBroadcast(intent);
914 /*
915 * If the failover network is already connected, then immediately send
916 * out a followup broadcast indicating successful failover
917 */
918 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
919 sendConnectedBroadcast(newNet.getNetworkInfo());
920 }
921 }
922
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800923 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800924 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700925 /*
926 * If this is a default network, check if other defaults are available
927 * or active
928 */
929 NetworkStateTracker newNet = null;
930 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700931 if (mActiveDefaultNetwork == prevNetType) {
932 mActiveDefaultNetwork = -1;
933 }
934
935 int newType = -1;
936 int newPriority = -1;
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800937 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700938 if (checkType == prevNetType) continue;
939 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700940 if (mNetAttributes[checkType].isDefault()) {
941 /* TODO - if we have multiple nets we could use
942 * we may want to put more thought into which we choose
943 */
944 if (checkType == mNetworkPreference) {
945 newType = checkType;
946 break;
947 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700948 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700949 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700950 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700954
955 if (newType != -1) {
956 newNet = mNetTrackers[newType];
957 /**
958 * See if the other network is available to fail over to.
959 * If is not available, we enable it anyway, so that it
960 * will be able to connect when it does become available,
961 * but we report a total loss of connectivity rather than
962 * report that we are attempting to fail over.
963 */
964 if (newNet.isAvailable()) {
965 NetworkInfo switchTo = newNet.getNetworkInfo();
966 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700967 if (!switchTo.isConnectedOrConnecting() ||
968 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700969 newNet.reconnect();
970 }
971 if (DBG) {
972 if (switchTo.isConnected()) {
973 Log.v(TAG, "Switching to already connected " +
974 switchTo.getTypeName());
975 } else {
976 Log.v(TAG, "Attempting to switch to " +
977 switchTo.getTypeName());
978 }
979 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700980 } else {
981 newNet.reconnect();
982 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700985
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800986 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988
989 private void sendConnectedBroadcast(NetworkInfo info) {
990 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800991 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
993 if (info.isFailover()) {
994 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
995 info.setFailover(false);
996 }
997 if (info.getReason() != null) {
998 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
999 }
1000 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001001 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1002 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001004 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006
1007 /**
1008 * Called when an attempt to fail over to another network has failed.
1009 * @param info the {@link NetworkInfo} for the failed network
1010 */
1011 private void handleConnectionFailure(NetworkInfo info) {
1012 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013
Robert Greenwalt42acef32009-08-12 16:08:25 -07001014 String reason = info.getReason();
1015 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001016
Robert Greenwalt42acef32009-08-12 16:08:25 -07001017 if (DBG) {
1018 String reasonText;
1019 if (reason == null) {
1020 reasonText = ".";
1021 } else {
1022 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001024 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
1025 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001027
1028 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001029 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001030 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1031 if (getActiveNetworkInfo() == null) {
1032 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1033 }
1034 if (reason != null) {
1035 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1036 }
1037 if (extraInfo != null) {
1038 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1039 }
1040 if (info.isFailover()) {
1041 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1042 info.setFailover(false);
1043 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001044
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001045 NetworkStateTracker newNet = null;
1046 if (mNetAttributes[info.getType()].isDefault()) {
1047 newNet = tryFailover(info.getType());
1048 if (newNet != null) {
1049 NetworkInfo switchTo = newNet.getNetworkInfo();
1050 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1051 } else {
1052 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1053 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001054 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001055
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001056 // do this before we broadcast the change
1057 handleConnectivityChange();
1058
Mike Lockwood0f79b542009-08-14 14:18:49 -04001059 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001060 /*
1061 * If the failover network is already connected, then immediately send
1062 * out a followup broadcast indicating successful failover
1063 */
1064 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1065 sendConnectedBroadcast(newNet.getNetworkInfo());
1066 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001067 }
1068
1069 private void sendStickyBroadcast(Intent intent) {
1070 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001071 if (!mSystemReady) {
1072 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001073 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001074 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1075 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001076 }
1077 }
1078
1079 void systemReady() {
1080 synchronized(this) {
1081 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001082 if (mInitialBroadcast != null) {
1083 mContext.sendStickyBroadcast(mInitialBroadcast);
1084 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001085 }
1086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
1089 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001090 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091
1092 // snapshot isFailover, because sendConnectedBroadcast() resets it
1093 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001094 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095
Robert Greenwalt42acef32009-08-12 16:08:25 -07001096 // if this is a default net and other default is running
1097 // kill the one not preferred
1098 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001099 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1100 if ((type != mNetworkPreference &&
1101 mNetAttributes[mActiveDefaultNetwork].mPriority >
1102 mNetAttributes[type].mPriority) ||
1103 mNetworkPreference == mActiveDefaultNetwork) {
1104 // don't accept this one
1105 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
1106 "to torn down network " + info.getTypeName());
1107 teardown(thisNet);
1108 return;
1109 } else {
1110 // tear down the other
1111 NetworkStateTracker otherNet =
1112 mNetTrackers[mActiveDefaultNetwork];
1113 if (DBG) Log.v(TAG, "Policy requires " +
1114 otherNet.getNetworkInfo().getTypeName() +
1115 " teardown");
1116 if (!teardown(otherNet)) {
1117 Log.e(TAG, "Network declined teardown request");
1118 return;
1119 }
1120 if (isFailover) {
1121 otherNet.releaseWakeLock();
1122 }
1123 }
1124 }
1125 mActiveDefaultNetwork = type;
1126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001128 thisNet.updateNetworkSettings();
1129 handleConnectivityChange();
1130 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132
1133 private void handleScanResultsAvailable(NetworkInfo info) {
1134 int networkType = info.getType();
1135 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001136 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1137 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 mNetTrackers[networkType].interpretScanResultsAvailable();
1141 }
1142
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001143 private void handleNotificationChange(boolean visible, int id,
1144 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 NotificationManager notificationManager = (NotificationManager) mContext
1146 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (visible) {
1149 notificationManager.notify(id, notification);
1150 } else {
1151 notificationManager.cancel(id);
1152 }
1153 }
1154
1155 /**
1156 * After any kind of change in the connectivity state of any network,
1157 * make sure that anything that depends on the connectivity state of
1158 * more than one network is set up correctly. We're mainly concerned
1159 * with making sure that the list of DNS servers is set up according
1160 * to which networks are connected, and ensuring that the right routing
1161 * table entries exist.
1162 */
1163 private void handleConnectivityChange() {
1164 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001166 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 * If both mobile and wifi are enabled, add the host routes that
1168 * will allow MMS traffic to pass on the mobile network. But
1169 * remove the default route for the mobile network, so that there
1170 * will be only one default route, to ensure that all traffic
1171 * except MMS will travel via Wi-Fi.
1172 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 handleDnsConfigurationChange();
1174
1175 for (int netType : mPriorityList) {
1176 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1177 if (mNetAttributes[netType].isDefault()) {
1178 mNetTrackers[netType].addDefaultRoute();
1179 } else {
1180 mNetTrackers[netType].addPrivateDnsRoutes();
1181 }
1182 } else {
1183 if (mNetAttributes[netType].isDefault()) {
1184 mNetTrackers[netType].removeDefaultRoute();
1185 } else {
1186 mNetTrackers[netType].removePrivateDnsRoutes();
1187 }
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
1191
Robert Greenwalt42acef32009-08-12 16:08:25 -07001192 /**
1193 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1194 * on the highest priority active net which this process requested.
1195 * If there aren't any, clear it out
1196 */
1197 private void reassessPidDns(int myPid, boolean doBump)
1198 {
1199 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1200 for(int i : mPriorityList) {
1201 if (mNetAttributes[i].isDefault()) {
1202 continue;
1203 }
1204 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001205 if (nt.getNetworkInfo().isConnected() &&
1206 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001207 List pids = mNetRequestersPids[i];
1208 for (int j=0; j<pids.size(); j++) {
1209 Integer pid = (Integer)pids.get(j);
1210 if (pid.intValue() == myPid) {
1211 String[] dnsList = nt.getNameServers();
1212 writePidDns(dnsList, myPid);
1213 if (doBump) {
1214 bumpDns();
1215 }
1216 return;
1217 }
1218 }
1219 }
1220 }
1221 // nothing found - delete
1222 for (int i = 1; ; i++) {
1223 String prop = "net.dns" + i + "." + myPid;
1224 if (SystemProperties.get(prop).length() == 0) {
1225 if (doBump) {
1226 bumpDns();
1227 }
1228 return;
1229 }
1230 SystemProperties.set(prop, "");
1231 }
1232 }
1233
1234 private void writePidDns(String[] dnsList, int pid) {
1235 int j = 1;
1236 for (String dns : dnsList) {
1237 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1238 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1239 }
1240 }
1241 }
1242
1243 private void bumpDns() {
1244 /*
1245 * Bump the property that tells the name resolver library to reread
1246 * the DNS server list from the properties.
1247 */
1248 String propVal = SystemProperties.get("net.dnschange");
1249 int n = 0;
1250 if (propVal.length() != 0) {
1251 try {
1252 n = Integer.parseInt(propVal);
1253 } catch (NumberFormatException e) {}
1254 }
1255 SystemProperties.set("net.dnschange", "" + (n+1));
1256 }
1257
1258 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001259 // add default net's dns entries
1260 for (int x = mPriorityList.length-1; x>= 0; x--) {
1261 int netType = mPriorityList[x];
1262 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001263 if (nt != null && nt.getNetworkInfo().isConnected() &&
1264 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001266 if (mNetAttributes[netType].isDefault()) {
1267 int j = 1;
1268 for (String dns : dnsList) {
1269 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001270 if (DBG) {
1271 Log.d(TAG, "adding dns " + dns + " for " +
1272 nt.getNetworkInfo().getTypeName());
1273 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001274 SystemProperties.set("net.dns" + j++, dns);
1275 }
1276 }
1277 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001278 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1279 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001280 }
1281 mNumDnsEntries = j;
1282 } else {
1283 // set per-pid dns for attached secondary nets
1284 List pids = mNetRequestersPids[netType];
1285 for (int y=0; y< pids.size(); y++) {
1286 Integer pid = (Integer)pids.get(y);
1287 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 }
1290 }
1291 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001292
1293 bumpDns();
1294 }
1295
1296 private int getRestoreDefaultNetworkDelay() {
1297 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1298 NETWORK_RESTORE_DELAY_PROP_NAME);
1299 if(restoreDefaultNetworkDelayStr != null &&
1300 restoreDefaultNetworkDelayStr.length() != 0) {
1301 try {
1302 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1303 } catch (NumberFormatException e) {
1304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001306 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308
1309 @Override
1310 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001311 if (mContext.checkCallingOrSelfPermission(
1312 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001314 pw.println("Permission Denial: can't dump ConnectivityService " +
1315 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1316 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 return;
1318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 pw.println();
1320 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001321 if (nst != null) {
1322 if (nst.getNetworkInfo().isConnected()) {
1323 pw.println("Active network: " + nst.getNetworkInfo().
1324 getTypeName());
1325 }
1326 pw.println(nst.getNetworkInfo());
1327 pw.println(nst);
1328 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001331
1332 pw.println("Network Requester Pids:");
1333 for (int net : mPriorityList) {
1334 String pidString = net + ": ";
1335 for (Object pid : mNetRequestersPids[net]) {
1336 pidString = pidString + pid.toString() + ", ";
1337 }
1338 pw.println(pidString);
1339 }
1340 pw.println();
1341
1342 pw.println("FeatureUsers:");
1343 for (Object requester : mFeatureUsers) {
1344 pw.println(requester.toString());
1345 }
1346 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001347
1348 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350
Robert Greenwalt42acef32009-08-12 16:08:25 -07001351 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 private class MyHandler extends Handler {
1353 @Override
1354 public void handleMessage(Message msg) {
1355 NetworkInfo info;
1356 switch (msg.what) {
1357 case NetworkStateTracker.EVENT_STATE_CHANGED:
1358 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001359 int type = info.getType();
1360 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001361 // only do this optimization for wifi. It going into scan mode for location
1362 // services generates alot of noise. Meanwhile the mms apn won't send out
1363 // subsequent notifications when on default cellular because it never
1364 // disconnects.. so only do this to wifi notifications. Fixed better when the
1365 // APN notifications are standardized.
1366 if (mNetAttributes[type].mLastState == state &&
1367 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001368 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001369 // TODO - remove this after we validate the dropping doesn't break
1370 // anything
Robert Greenwalt511288a2009-12-07 11:33:18 -08001371 Log.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001372 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001373 state + "/" + info.getDetailedState());
1374 }
1375 return;
1376 }
1377 mNetAttributes[type].mLastState = state;
1378
Robert Greenwalt42acef32009-08-12 16:08:25 -07001379 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001380 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001381 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382
1383 // Connectivity state changed:
1384 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001385 // [12-9] Network subtype (for mobile network, as defined
1386 // by TelephonyManager)
1387 // [8-3] Detailed state ordinal (as defined by
1388 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 // [2-0] Network type (as defined by ConnectivityManager)
1390 int eventLogParam = (info.getType() & 0x7) |
1391 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1392 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001394 eventLogParam);
1395
1396 if (info.getDetailedState() ==
1397 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001399 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001401 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001403 // the logic here is, handle SUSPENDED the same as
1404 // DISCONNECTED. The only difference being we are
1405 // broadcasting an intent with NetworkInfo that's
1406 // suspended. This allows the applications an
1407 // opportunity to handle DISCONNECTED and SUSPENDED
1408 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001410 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 handleConnect(info);
1412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 break;
1414
1415 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1416 info = (NetworkInfo) msg.obj;
1417 handleScanResultsAvailable(info);
1418 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001421 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1422 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423
1424 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001425 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 break;
1427
1428 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1429 // fill me in
1430 break;
1431
1432 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1433 // fill me in
1434 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001435 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001436 FeatureUser u = (FeatureUser)msg.obj;
1437 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001438 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 }
1441 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001442
1443 // javadoc from interface
1444 public boolean tether(String iface) {
1445 enforceTetherChangePermission();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001446 return isTetheringSupported() && mTethering.tether(iface);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001447 }
1448
1449 // javadoc from interface
1450 public boolean untether(String iface) {
1451 enforceTetherChangePermission();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001452 return isTetheringSupported() && mTethering.untether(iface);
1453 }
1454
1455 // TODO - proper iface API for selection by property, inspection, etc
1456 public String[] getTetherableUsbRegexs() {
1457 enforceTetherAccessPermission();
1458 if (isTetheringSupported()) {
1459 return mTethering.getTetherableUsbRegexs();
1460 } else {
1461 return new String[0];
1462 }
1463 }
1464
1465 public String[] getTetherableWifiRegexs() {
1466 enforceTetherAccessPermission();
1467 if (isTetheringSupported()) {
1468 return mTethering.getTetherableWifiRegexs();
1469 } else {
1470 return new String[0];
1471 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001472 }
1473
1474 // TODO - move iface listing, queries, etc to new module
1475 // javadoc from interface
1476 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001477 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001478 return mTethering.getTetherableIfaces();
1479 }
1480
1481 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001482 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001483 return mTethering.getTetheredIfaces();
1484 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001485
1486 // if ro.tether.denied = true we default to no tethering
1487 // gservices could set the secure setting to 1 though to enable it on a build where it
1488 // had previously been turned off.
1489 public boolean isTetheringSupported() {
1490 enforceTetherAccessPermission();
1491 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
1492 return ((Settings.Secure.getInt(mContext.getContentResolver(),
1493 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0) &&
1494 (mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null));
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496}