blob: 108246daa1b910cabb7e0a36ac9cacdaeecc6a98 [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
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080046import com.android.server.connectivity.Tethering;
47
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import java.util.ArrayList;
51import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052
53/**
54 * @hide
55 */
56public class ConnectivityService extends IConnectivityManager.Stub {
57
Robert Greenwalta25fd712009-10-06 14:12:53 -070058 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059 private static final String TAG = "ConnectivityService";
60
Robert Greenwalt2034b912009-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 Greenwalt0c4828c2010-01-26 11:40:34 -080067
68 private Tethering mTethering;
69
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -080083 private WifiWatchdogService mWifiWatchdogService;
84
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -080090 private Context mContext;
91 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070092 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080093
94 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080095
96 private boolean mTestMode;
97 private static ConnectivityService sServiceInstance;
98
Robert Greenwalt2034b912009-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 Lockwoodfde2b762009-08-14 14:18:49 -0400105 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800106 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400107
Robert Greenwalt12c44552009-12-07 11:33:18 -0800108 private static class NetworkAttributes {
Robert Greenwalt2034b912009-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 Greenwalt12c44552009-12-07 11:33:18 -0800116 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700117 public NetworkAttributes(String init) {
118 String fragments[] = init.split(",");
119 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700120 mType = Integer.parseInt(fragments[1]);
121 mRadio = Integer.parseInt(fragments[2]);
122 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800123 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700124 }
125 public boolean isDefault() {
126 return (mType == mRadio);
127 }
128 }
129 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700130 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700131
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 public int mSimultaneity;
134 public int mType;
135 public RadioAttributes(String init) {
136 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700137 mType = Integer.parseInt(fragments[0]);
138 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700139 }
140 }
141 RadioAttributes[] mRadioAttributes;
142
The Android Open Source Project28527d22009-03-03 19:31:44 -0800143 private static class ConnectivityThread extends Thread {
144 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700145
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700160
The Android Open Source Project28527d22009-03-03 19:31:44 -0800161 public static ConnectivityService getServiceInstance(Context context) {
162 ConnectivityThread thread = new ConnectivityThread(context);
163 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700164
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700172 "Unexpected InterruptedException while waiting"+
173 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800174 }
175 }
176 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700177
The Android Open Source Project28527d22009-03-03 19:31:44 -0800178 return sServiceInstance;
179 }
180 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700181
The Android Open Source Project28527d22009-03-03 19:31:44 -0800182 public static ConnectivityService getInstance(Context context) {
183 return ConnectivityThread.getServiceInstance(context);
184 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700185
The Android Open Source Project28527d22009-03-03 19:31:44 -0800186 private ConnectivityService(Context context) {
187 if (DBG) Log.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-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 Project28527d22009-03-03 19:31:44 -0800197 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700198 mNetTrackers = new NetworkStateTracker[
199 ConnectivityManager.MAX_NETWORK_TYPE+1];
200 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700201
The Android Open Source Project28527d22009-03-03 19:31:44 -0800202 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700203
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700207 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700208 String[] raStrings = context.getResources().getStringArray(
209 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700221 mRadioAttributes[r.mType] = r;
222 }
223
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700233 }
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700248 }
249 }
250
Robert Greenwaltec05b3c2009-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 Greenwalt2034b912009-08-12 16:08:25 -0700276 mNetRequestersPids[i] = new ArrayList();
277 }
278
279 mFeatureUsers = new ArrayList();
280
Robert Greenwaltec05b3c2009-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 Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700292 for (int netType : mPriorityList) {
293 switch (mNetAttributes[netType].mRadio) {
294 case ConnectivityManager.TYPE_WIFI:
295 if (DBG) Log.v(TAG, "Starting Wifi Service.");
296 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
297 WifiService wifiService = new WifiService(context, wst);
298 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
299 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
300 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800301
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302 // Constructing this starts it too
303 mWifiWatchdogService = new WifiWatchdogService(context, wst);
304 break;
305 case ConnectivityManager.TYPE_MOBILE:
306 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
307 netType, mNetAttributes[netType].mName);
308 mNetTrackers[netType].startMonitoring();
309 break;
310 default:
311 Log.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
312 mNetAttributes[netType].mRadio);
313 continue;
314 }
315 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800316
317 mTethering = new Tethering(mContext);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800318 }
319
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320
The Android Open Source Project28527d22009-03-03 19:31:44 -0800321 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700322 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800323 * @param preference the new preference
324 */
325 public synchronized void setNetworkPreference(int preference) {
326 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700327 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700328 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700329 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800330 if (mNetworkPreference != preference) {
331 persistNetworkPreference(preference);
332 mNetworkPreference = preference;
333 enforcePreference();
334 }
335 }
336 }
337
338 public int getNetworkPreference() {
339 enforceAccessPermission();
340 return mNetworkPreference;
341 }
342
343 private void persistNetworkPreference(int networkPreference) {
344 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700345 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
346 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800347 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700348
The Android Open Source Project28527d22009-03-03 19:31:44 -0800349 private int getPersistedNetworkPreference() {
350 final ContentResolver cr = mContext.getContentResolver();
351
352 final int networkPrefSetting = Settings.Secure
353 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
354 if (networkPrefSetting != -1) {
355 return networkPrefSetting;
356 }
357
358 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
359 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700360
The Android Open Source Project28527d22009-03-03 19:31:44 -0800361 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700362 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800363 * In this method, we only tear down a non-preferred network. Establishing
364 * a connection to the preferred network is taken care of when we handle
365 * the disconnect event from the non-preferred network
366 * (see {@link #handleDisconnect(NetworkInfo)}).
367 */
368 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700369 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800370 return;
371
Robert Greenwalt2034b912009-08-12 16:08:25 -0700372 if (!mNetTrackers[mNetworkPreference].isAvailable())
373 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800374
Robert Greenwalt2034b912009-08-12 16:08:25 -0700375 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700376 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700378 if (DBG) {
379 Log.d(TAG, "tearing down " +
380 mNetTrackers[t].getNetworkInfo() +
381 " in enforcePreference");
382 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700383 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800384 }
385 }
386 }
387
388 private boolean teardown(NetworkStateTracker netTracker) {
389 if (netTracker.teardown()) {
390 netTracker.setTeardownRequested(true);
391 return true;
392 } else {
393 return false;
394 }
395 }
396
397 /**
398 * Return NetworkInfo for the active (i.e., connected) network interface.
399 * It is assumed that at most one network is active at a time. If more
400 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700401 * @return the info for the active network, or {@code null} if none is
402 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800403 */
404 public NetworkInfo getActiveNetworkInfo() {
405 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700406 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700407 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700408 continue;
409 }
410 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800411 NetworkInfo info = t.getNetworkInfo();
412 if (info.isConnected()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700413 if (DBG && type != mActiveDefaultNetwork) Log.e(TAG,
414 "connected default network is not " +
415 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800416 return info;
417 }
418 }
419 return null;
420 }
421
422 public NetworkInfo getNetworkInfo(int networkType) {
423 enforceAccessPermission();
424 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
425 NetworkStateTracker t = mNetTrackers[networkType];
426 if (t != null)
427 return t.getNetworkInfo();
428 }
429 return null;
430 }
431
432 public NetworkInfo[] getAllNetworkInfo() {
433 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700434 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435 int i = 0;
436 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700437 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 }
439 return result;
440 }
441
442 public boolean setRadios(boolean turnOn) {
443 boolean result = true;
444 enforceChangePermission();
445 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700446 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 }
448 return result;
449 }
450
451 public boolean setRadio(int netType, boolean turnOn) {
452 enforceChangePermission();
453 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
454 return false;
455 }
456 NetworkStateTracker tracker = mNetTrackers[netType];
457 return tracker != null && tracker.setRadio(turnOn);
458 }
459
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700460 /**
461 * Used to notice when the calling process dies so we can self-expire
462 *
463 * Also used to know if the process has cleaned up after itself when
464 * our auto-expire timer goes off. The timer has a link to an object.
465 *
466 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700467 private class FeatureUser implements IBinder.DeathRecipient {
468 int mNetworkType;
469 String mFeature;
470 IBinder mBinder;
471 int mPid;
472 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800473 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700474
475 FeatureUser(int type, String feature, IBinder binder) {
476 super();
477 mNetworkType = type;
478 mFeature = feature;
479 mBinder = binder;
480 mPid = getCallingPid();
481 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800482 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700483
Robert Greenwalt2034b912009-08-12 16:08:25 -0700484 try {
485 mBinder.linkToDeath(this, 0);
486 } catch (RemoteException e) {
487 binderDied();
488 }
489 }
490
491 void unlinkDeathRecipient() {
492 mBinder.unlinkToDeath(this, 0);
493 }
494
495 public void binderDied() {
496 Log.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800497 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
498 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700499 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 }
501
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700502 public void expire() {
503 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800504 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
505 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700506 stopUsingNetworkFeature(this, false);
507 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800508
509 public String toString() {
510 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
511 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
512 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700513 }
514
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700515 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700516 public int startUsingNetworkFeature(int networkType, String feature,
517 IBinder binder) {
518 if (DBG) {
519 Log.d(TAG, "startUsingNetworkFeature for net " + networkType +
520 ": " + feature);
521 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800522 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700523 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
524 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700525 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800526 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700527
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700528 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700529
530 // TODO - move this into the MobileDataStateTracker
531 int usedNetworkType = networkType;
532 if(networkType == ConnectivityManager.TYPE_MOBILE) {
533 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
534 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
535 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
536 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
537 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
538 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
539 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
540 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
541 }
542 }
543 NetworkStateTracker network = mNetTrackers[usedNetworkType];
544 if (network != null) {
545 if (usedNetworkType != networkType) {
546 Integer currentPid = new Integer(getCallingPid());
547
548 NetworkStateTracker radio = mNetTrackers[networkType];
549 NetworkInfo ni = network.getNetworkInfo();
550
551 if (ni.isAvailable() == false) {
552 if (DBG) Log.d(TAG, "special network not available");
553 return Phone.APN_TYPE_NOT_AVAILABLE;
554 }
555
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700556 synchronized(this) {
557 mFeatureUsers.add(f);
558 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
559 // this gets used for per-pid dns when connected
560 mNetRequestersPids[usedNetworkType].add(currentPid);
561 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700562 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700563 mHandler.sendMessageDelayed(mHandler.obtainMessage(
564 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
565 f), getRestoreDefaultNetworkDelay());
566
Robert Greenwalt2034b912009-08-12 16:08:25 -0700567
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700568 if ((ni.isConnectedOrConnecting() == true) &&
569 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700570 if (ni.isConnected() == true) {
571 // add the pid-specific dns
572 handleDnsConfigurationChange();
573 if (DBG) Log.d(TAG, "special network already active");
574 return Phone.APN_ALREADY_ACTIVE;
575 }
576 if (DBG) Log.d(TAG, "special network already connecting");
577 return Phone.APN_REQUEST_STARTED;
578 }
579
580 // check if the radio in play can make another contact
581 // assume if cannot for now
582
Robert Greenwalt2034b912009-08-12 16:08:25 -0700583 if (DBG) Log.d(TAG, "reconnecting to special network");
584 network.reconnect();
585 return Phone.APN_REQUEST_STARTED;
586 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700587 synchronized(this) {
588 mFeatureUsers.add(f);
589 }
590 mHandler.sendMessageDelayed(mHandler.obtainMessage(
591 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
592 f), getRestoreDefaultNetworkDelay());
593
Robert Greenwalt2034b912009-08-12 16:08:25 -0700594 return network.startUsingNetworkFeature(feature,
595 getCallingPid(), getCallingUid());
596 }
597 }
598 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800599 }
600
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700601 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800602 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700603 enforceChangePermission();
604
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700605 int pid = getCallingPid();
606 int uid = getCallingUid();
607
608 FeatureUser u = null;
609 boolean found = false;
610
611 synchronized(this) {
612 for (int i = 0; i < mFeatureUsers.size() ; i++) {
613 u = (FeatureUser)mFeatureUsers.get(i);
614 if (uid == u.mUid && pid == u.mPid &&
615 networkType == u.mNetworkType &&
616 TextUtils.equals(feature, u.mFeature)) {
617 found = true;
618 break;
619 }
620 }
621 }
622 if (found && u != null) {
623 // stop regardless of how many other time this proc had called start
624 return stopUsingNetworkFeature(u, true);
625 } else {
626 // none found!
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800627 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700628 return 1;
629 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700630 }
631
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700632 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
633 int networkType = u.mNetworkType;
634 String feature = u.mFeature;
635 int pid = u.mPid;
636 int uid = u.mUid;
637
638 NetworkStateTracker tracker = null;
639 boolean callTeardown = false; // used to carry our decision outside of sync block
640
Robert Greenwalt2034b912009-08-12 16:08:25 -0700641 if (DBG) {
642 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
643 ": " + feature);
644 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700645
The Android Open Source Project28527d22009-03-03 19:31:44 -0800646 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
647 return -1;
648 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700649
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
651 // sync block
652 synchronized(this) {
653 // check if this process still has an outstanding start request
654 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700655 return 1;
656 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 u.unlinkDeathRecipient();
658 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
659 // If we care about duplicate requests, check for that here.
660 //
661 // This is done to support the extension of a request - the app
662 // can request we start the network feature again and renew the
663 // auto-shutoff delay. Normal "stop" calls from the app though
664 // do not pay attention to duplicate requests - in effect the
665 // API does not refcount and a single stop will counter multiple starts.
666 if (ignoreDups == false) {
667 for (int i = 0; i < mFeatureUsers.size() ; i++) {
668 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
669 if (x.mUid == u.mUid && x.mPid == u.mPid &&
670 x.mNetworkType == u.mNetworkType &&
671 TextUtils.equals(x.mFeature, u.mFeature)) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800672 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700673 return 1;
674 }
675 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700677
678 // TODO - move to MobileDataStateTracker
679 int usedNetworkType = networkType;
680 if (networkType == ConnectivityManager.TYPE_MOBILE) {
681 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
682 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
683 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
684 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
685 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
686 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
687 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
688 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
689 }
690 }
691 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700692 if (tracker == null) {
693 return -1;
694 }
695 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700696 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700697 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800698 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700699 if (mNetRequestersPids[usedNetworkType].size() != 0) {
700 if (DBG) Log.d(TAG, "not tearing down special network - " +
701 "others still using it");
702 return 1;
703 }
704 callTeardown = true;
705 }
706 }
707
708 if (callTeardown) {
709 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700710 return 1;
711 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700712 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700713 return tracker.stopUsingNetworkFeature(feature, pid, uid);
714 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800715 }
716
717 /**
718 * Ensure that a network route exists to deliver traffic to the specified
719 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700720 * @param networkType the type of the network over which traffic to the
721 * specified host is to be routed
722 * @param hostAddress the IP address of the host to which the route is
723 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800724 * @return {@code true} on success, {@code false} on failure
725 */
726 public boolean requestRouteToHost(int networkType, int hostAddress) {
727 enforceChangePermission();
728 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
729 return false;
730 }
731 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700732
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700733 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
734 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700735 if (DBG) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700736 Log.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700737 }
738 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800739 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700740 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800741 }
742
743 /**
744 * @see ConnectivityManager#getBackgroundDataSetting()
745 */
746 public boolean getBackgroundDataSetting() {
747 return Settings.Secure.getInt(mContext.getContentResolver(),
748 Settings.Secure.BACKGROUND_DATA, 1) == 1;
749 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700750
The Android Open Source Project28527d22009-03-03 19:31:44 -0800751 /**
752 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
753 */
754 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
755 mContext.enforceCallingOrSelfPermission(
756 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
757 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700758
The Android Open Source Project28527d22009-03-03 19:31:44 -0800759 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
760
761 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700762 Settings.Secure.BACKGROUND_DATA,
763 allowBackgroundDataUsage ? 1 : 0);
764
The Android Open Source Project28527d22009-03-03 19:31:44 -0800765 Intent broadcast = new Intent(
766 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
767 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700768 }
769
The Android Open Source Project28527d22009-03-03 19:31:44 -0800770 private int getNumConnectedNetworks() {
771 int numConnectedNets = 0;
772
773 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700774 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700775 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800776 ++numConnectedNets;
777 }
778 }
779 return numConnectedNets;
780 }
781
782 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700783 mContext.enforceCallingOrSelfPermission(
784 android.Manifest.permission.ACCESS_NETWORK_STATE,
785 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800786 }
787
788 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700789 mContext.enforceCallingOrSelfPermission(
790 android.Manifest.permission.CHANGE_NETWORK_STATE,
791 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800792 }
793
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800794 // TODO Make this a special check when it goes public
795 private void enforceTetherChangePermission() {
796 mContext.enforceCallingOrSelfPermission(
797 android.Manifest.permission.CHANGE_NETWORK_STATE,
798 "ConnectivityService");
799 }
800
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800801 private void enforceTetherAccessPermission() {
802 mContext.enforceCallingOrSelfPermission(
803 android.Manifest.permission.ACCESS_NETWORK_STATE,
804 "ConnectivityService");
805 }
806
The Android Open Source Project28527d22009-03-03 19:31:44 -0800807 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700808 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
809 * network, we ignore it. If it is for the active network, we send out a
810 * broadcast. But first, we check whether it might be possible to connect
811 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800812 * @param info the {@code NetworkInfo} for the network
813 */
814 private void handleDisconnect(NetworkInfo info) {
815
Robert Greenwalt2034b912009-08-12 16:08:25 -0700816 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800817
Robert Greenwalt2034b912009-08-12 16:08:25 -0700818 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800819 /*
820 * If the disconnected network is not the active one, then don't report
821 * this as a loss of connectivity. What probably happened is that we're
822 * getting the disconnect for a network that we explicitly disabled
823 * in accordance with network preference policies.
824 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700825 if (!mNetAttributes[prevNetType].isDefault()) {
826 List pids = mNetRequestersPids[prevNetType];
827 for (int i = 0; i<pids.size(); i++) {
828 Integer pid = (Integer)pids.get(i);
829 // will remove them because the net's no longer connected
830 // need to do this now as only now do we know the pids and
831 // can properly null things that are no longer referenced.
832 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800833 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800834 }
835
The Android Open Source Project28527d22009-03-03 19:31:44 -0800836 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800837 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800838 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
839 if (info.isFailover()) {
840 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
841 info.setFailover(false);
842 }
843 if (info.getReason() != null) {
844 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
845 }
846 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700847 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
848 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800849 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700850
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800851 NetworkStateTracker newNet = null;
852 if (mNetAttributes[prevNetType].isDefault()) {
853 newNet = tryFailover(prevNetType);
854 if (newNet != null) {
855 NetworkInfo switchTo = newNet.getNetworkInfo();
856 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
857 } else {
858 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
859 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800860 }
861 // do this before we broadcast the change
862 handleConnectivityChange();
863
864 sendStickyBroadcast(intent);
865 /*
866 * If the failover network is already connected, then immediately send
867 * out a followup broadcast indicating successful failover
868 */
869 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
870 sendConnectedBroadcast(newNet.getNetworkInfo());
871 }
872 }
873
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800874 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800875 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700876 /*
877 * If this is a default network, check if other defaults are available
878 * or active
879 */
880 NetworkStateTracker newNet = null;
881 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700882 if (mActiveDefaultNetwork == prevNetType) {
883 mActiveDefaultNetwork = -1;
884 }
885
886 int newType = -1;
887 int newPriority = -1;
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800888 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700889 if (checkType == prevNetType) continue;
890 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700891 if (mNetAttributes[checkType].isDefault()) {
892 /* TODO - if we have multiple nets we could use
893 * we may want to put more thought into which we choose
894 */
895 if (checkType == mNetworkPreference) {
896 newType = checkType;
897 break;
898 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700899 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700900 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700901 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700902 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903 }
904 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700905
906 if (newType != -1) {
907 newNet = mNetTrackers[newType];
908 /**
909 * See if the other network is available to fail over to.
910 * If is not available, we enable it anyway, so that it
911 * will be able to connect when it does become available,
912 * but we report a total loss of connectivity rather than
913 * report that we are attempting to fail over.
914 */
915 if (newNet.isAvailable()) {
916 NetworkInfo switchTo = newNet.getNetworkInfo();
917 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700918 if (!switchTo.isConnectedOrConnecting() ||
919 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700920 newNet.reconnect();
921 }
922 if (DBG) {
923 if (switchTo.isConnected()) {
924 Log.v(TAG, "Switching to already connected " +
925 switchTo.getTypeName());
926 } else {
927 Log.v(TAG, "Attempting to switch to " +
928 switchTo.getTypeName());
929 }
930 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700931 } else {
932 newNet.reconnect();
933 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700934 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800935 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700936
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800937 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800938 }
939
940 private void sendConnectedBroadcast(NetworkInfo info) {
941 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800942 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800943 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
944 if (info.isFailover()) {
945 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
946 info.setFailover(false);
947 }
948 if (info.getReason() != null) {
949 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
950 }
951 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700952 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
953 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400955 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800956 }
957
958 /**
959 * Called when an attempt to fail over to another network has failed.
960 * @param info the {@link NetworkInfo} for the failed network
961 */
962 private void handleConnectionFailure(NetworkInfo info) {
963 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800964
Robert Greenwalt2034b912009-08-12 16:08:25 -0700965 String reason = info.getReason();
966 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700967
Robert Greenwalt2034b912009-08-12 16:08:25 -0700968 if (DBG) {
969 String reasonText;
970 if (reason == null) {
971 reasonText = ".";
972 } else {
973 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -0800974 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700975 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
976 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800977 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978
979 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800980 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700981 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
982 if (getActiveNetworkInfo() == null) {
983 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
984 }
985 if (reason != null) {
986 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
987 }
988 if (extraInfo != null) {
989 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
990 }
991 if (info.isFailover()) {
992 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
993 info.setFailover(false);
994 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800995
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800996 NetworkStateTracker newNet = null;
997 if (mNetAttributes[info.getType()].isDefault()) {
998 newNet = tryFailover(info.getType());
999 if (newNet != null) {
1000 NetworkInfo switchTo = newNet.getNetworkInfo();
1001 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1002 } else {
1003 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1004 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001005 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001006
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001007 // do this before we broadcast the change
1008 handleConnectivityChange();
1009
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001010 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001011 /*
1012 * If the failover network is already connected, then immediately send
1013 * out a followup broadcast indicating successful failover
1014 */
1015 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1016 sendConnectedBroadcast(newNet.getNetworkInfo());
1017 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001018 }
1019
1020 private void sendStickyBroadcast(Intent intent) {
1021 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001022 if (!mSystemReady) {
1023 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001024 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001025 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1026 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001027 }
1028 }
1029
1030 void systemReady() {
1031 synchronized(this) {
1032 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001033 if (mInitialBroadcast != null) {
1034 mContext.sendStickyBroadcast(mInitialBroadcast);
1035 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001036 }
1037 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001038 }
1039
1040 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001041 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001042
1043 // snapshot isFailover, because sendConnectedBroadcast() resets it
1044 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001045 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001046
Robert Greenwalt2034b912009-08-12 16:08:25 -07001047 // if this is a default net and other default is running
1048 // kill the one not preferred
1049 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001050 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1051 if ((type != mNetworkPreference &&
1052 mNetAttributes[mActiveDefaultNetwork].mPriority >
1053 mNetAttributes[type].mPriority) ||
1054 mNetworkPreference == mActiveDefaultNetwork) {
1055 // don't accept this one
1056 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
1057 "to torn down network " + info.getTypeName());
1058 teardown(thisNet);
1059 return;
1060 } else {
1061 // tear down the other
1062 NetworkStateTracker otherNet =
1063 mNetTrackers[mActiveDefaultNetwork];
1064 if (DBG) Log.v(TAG, "Policy requires " +
1065 otherNet.getNetworkInfo().getTypeName() +
1066 " teardown");
1067 if (!teardown(otherNet)) {
1068 Log.e(TAG, "Network declined teardown request");
1069 return;
1070 }
1071 if (isFailover) {
1072 otherNet.releaseWakeLock();
1073 }
1074 }
1075 }
1076 mActiveDefaultNetwork = type;
1077 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001079 thisNet.updateNetworkSettings();
1080 handleConnectivityChange();
1081 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082 }
1083
1084 private void handleScanResultsAvailable(NetworkInfo info) {
1085 int networkType = info.getType();
1086 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001087 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1088 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001089 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001090
The Android Open Source Project28527d22009-03-03 19:31:44 -08001091 mNetTrackers[networkType].interpretScanResultsAvailable();
1092 }
1093
Robert Greenwalt0659da32009-07-16 17:21:39 -07001094 private void handleNotificationChange(boolean visible, int id,
1095 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 NotificationManager notificationManager = (NotificationManager) mContext
1097 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001098
The Android Open Source Project28527d22009-03-03 19:31:44 -08001099 if (visible) {
1100 notificationManager.notify(id, notification);
1101 } else {
1102 notificationManager.cancel(id);
1103 }
1104 }
1105
1106 /**
1107 * After any kind of change in the connectivity state of any network,
1108 * make sure that anything that depends on the connectivity state of
1109 * more than one network is set up correctly. We're mainly concerned
1110 * with making sure that the list of DNS servers is set up according
1111 * to which networks are connected, and ensuring that the right routing
1112 * table entries exist.
1113 */
1114 private void handleConnectivityChange() {
1115 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001116 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001117 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001118 * If both mobile and wifi are enabled, add the host routes that
1119 * will allow MMS traffic to pass on the mobile network. But
1120 * remove the default route for the mobile network, so that there
1121 * will be only one default route, to ensure that all traffic
1122 * except MMS will travel via Wi-Fi.
1123 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001124 handleDnsConfigurationChange();
1125
1126 for (int netType : mPriorityList) {
1127 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1128 if (mNetAttributes[netType].isDefault()) {
1129 mNetTrackers[netType].addDefaultRoute();
1130 } else {
1131 mNetTrackers[netType].addPrivateDnsRoutes();
1132 }
1133 } else {
1134 if (mNetAttributes[netType].isDefault()) {
1135 mNetTrackers[netType].removeDefaultRoute();
1136 } else {
1137 mNetTrackers[netType].removePrivateDnsRoutes();
1138 }
1139 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001140 }
1141 }
1142
Robert Greenwalt2034b912009-08-12 16:08:25 -07001143 /**
1144 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1145 * on the highest priority active net which this process requested.
1146 * If there aren't any, clear it out
1147 */
1148 private void reassessPidDns(int myPid, boolean doBump)
1149 {
1150 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1151 for(int i : mPriorityList) {
1152 if (mNetAttributes[i].isDefault()) {
1153 continue;
1154 }
1155 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001156 if (nt.getNetworkInfo().isConnected() &&
1157 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001158 List pids = mNetRequestersPids[i];
1159 for (int j=0; j<pids.size(); j++) {
1160 Integer pid = (Integer)pids.get(j);
1161 if (pid.intValue() == myPid) {
1162 String[] dnsList = nt.getNameServers();
1163 writePidDns(dnsList, myPid);
1164 if (doBump) {
1165 bumpDns();
1166 }
1167 return;
1168 }
1169 }
1170 }
1171 }
1172 // nothing found - delete
1173 for (int i = 1; ; i++) {
1174 String prop = "net.dns" + i + "." + myPid;
1175 if (SystemProperties.get(prop).length() == 0) {
1176 if (doBump) {
1177 bumpDns();
1178 }
1179 return;
1180 }
1181 SystemProperties.set(prop, "");
1182 }
1183 }
1184
1185 private void writePidDns(String[] dnsList, int pid) {
1186 int j = 1;
1187 for (String dns : dnsList) {
1188 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1189 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1190 }
1191 }
1192 }
1193
1194 private void bumpDns() {
1195 /*
1196 * Bump the property that tells the name resolver library to reread
1197 * the DNS server list from the properties.
1198 */
1199 String propVal = SystemProperties.get("net.dnschange");
1200 int n = 0;
1201 if (propVal.length() != 0) {
1202 try {
1203 n = Integer.parseInt(propVal);
1204 } catch (NumberFormatException e) {}
1205 }
1206 SystemProperties.set("net.dnschange", "" + (n+1));
1207 }
1208
1209 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001210 // add default net's dns entries
1211 for (int x = mPriorityList.length-1; x>= 0; x--) {
1212 int netType = mPriorityList[x];
1213 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001214 if (nt != null && nt.getNetworkInfo().isConnected() &&
1215 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001217 if (mNetAttributes[netType].isDefault()) {
1218 int j = 1;
1219 for (String dns : dnsList) {
1220 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001221 if (DBG) {
1222 Log.d(TAG, "adding dns " + dns + " for " +
1223 nt.getNetworkInfo().getTypeName());
1224 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001225 SystemProperties.set("net.dns" + j++, dns);
1226 }
1227 }
1228 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001229 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1230 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001231 }
1232 mNumDnsEntries = j;
1233 } else {
1234 // set per-pid dns for attached secondary nets
1235 List pids = mNetRequestersPids[netType];
1236 for (int y=0; y< pids.size(); y++) {
1237 Integer pid = (Integer)pids.get(y);
1238 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001239 }
1240 }
1241 }
1242 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001243
1244 bumpDns();
1245 }
1246
1247 private int getRestoreDefaultNetworkDelay() {
1248 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1249 NETWORK_RESTORE_DELAY_PROP_NAME);
1250 if(restoreDefaultNetworkDelayStr != null &&
1251 restoreDefaultNetworkDelayStr.length() != 0) {
1252 try {
1253 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1254 } catch (NumberFormatException e) {
1255 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001257 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 }
1259
1260 @Override
1261 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001262 if (mContext.checkCallingOrSelfPermission(
1263 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001264 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001265 pw.println("Permission Denial: can't dump ConnectivityService " +
1266 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1267 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001268 return;
1269 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001270 pw.println();
1271 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001272 if (nst != null) {
1273 if (nst.getNetworkInfo().isConnected()) {
1274 pw.println("Active network: " + nst.getNetworkInfo().
1275 getTypeName());
1276 }
1277 pw.println(nst.getNetworkInfo());
1278 pw.println(nst);
1279 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001280 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001281 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001282
1283 pw.println("Network Requester Pids:");
1284 for (int net : mPriorityList) {
1285 String pidString = net + ": ";
1286 for (Object pid : mNetRequestersPids[net]) {
1287 pidString = pidString + pid.toString() + ", ";
1288 }
1289 pw.println(pidString);
1290 }
1291 pw.println();
1292
1293 pw.println("FeatureUsers:");
1294 for (Object requester : mFeatureUsers) {
1295 pw.println(requester.toString());
1296 }
1297 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001298
1299 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001300 }
1301
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001303 private class MyHandler extends Handler {
1304 @Override
1305 public void handleMessage(Message msg) {
1306 NetworkInfo info;
1307 switch (msg.what) {
1308 case NetworkStateTracker.EVENT_STATE_CHANGED:
1309 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001310 int type = info.getType();
1311 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001312 // only do this optimization for wifi. It going into scan mode for location
1313 // services generates alot of noise. Meanwhile the mms apn won't send out
1314 // subsequent notifications when on default cellular because it never
1315 // disconnects.. so only do this to wifi notifications. Fixed better when the
1316 // APN notifications are standardized.
1317 if (mNetAttributes[type].mLastState == state &&
1318 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001319 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001320 // TODO - remove this after we validate the dropping doesn't break
1321 // anything
Robert Greenwalt12c44552009-12-07 11:33:18 -08001322 Log.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001323 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001324 state + "/" + info.getDetailedState());
1325 }
1326 return;
1327 }
1328 mNetAttributes[type].mLastState = state;
1329
Robert Greenwalt2034b912009-08-12 16:08:25 -07001330 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001331 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001332 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333
1334 // Connectivity state changed:
1335 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001336 // [12-9] Network subtype (for mobile network, as defined
1337 // by TelephonyManager)
1338 // [8-3] Detailed state ordinal (as defined by
1339 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001340 // [2-0] Network type (as defined by ConnectivityManager)
1341 int eventLogParam = (info.getType() & 0x7) |
1342 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1343 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001344 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001345 eventLogParam);
1346
1347 if (info.getDetailedState() ==
1348 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001349 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001350 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001351 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001352 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001353 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001354 // the logic here is, handle SUSPENDED the same as
1355 // DISCONNECTED. The only difference being we are
1356 // broadcasting an intent with NetworkInfo that's
1357 // suspended. This allows the applications an
1358 // opportunity to handle DISCONNECTED and SUSPENDED
1359 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001360 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001361 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001362 handleConnect(info);
1363 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001364 break;
1365
1366 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1367 info = (NetworkInfo) msg.obj;
1368 handleScanResultsAvailable(info);
1369 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001370
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001372 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1373 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001374
1375 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001376 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001377 break;
1378
1379 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1380 // fill me in
1381 break;
1382
1383 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1384 // fill me in
1385 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001386 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001387 FeatureUser u = (FeatureUser)msg.obj;
1388 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001389 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001390 }
1391 }
1392 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001393
1394 // javadoc from interface
1395 public boolean tether(String iface) {
1396 enforceTetherChangePermission();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001397 return isTetheringSupported() && mTethering.tether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001398 }
1399
1400 // javadoc from interface
1401 public boolean untether(String iface) {
1402 enforceTetherChangePermission();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001403 return isTetheringSupported() && mTethering.untether(iface);
1404 }
1405
1406 // TODO - proper iface API for selection by property, inspection, etc
1407 public String[] getTetherableUsbRegexs() {
1408 enforceTetherAccessPermission();
1409 if (isTetheringSupported()) {
1410 return mTethering.getTetherableUsbRegexs();
1411 } else {
1412 return new String[0];
1413 }
1414 }
1415
1416 public String[] getTetherableWifiRegexs() {
1417 enforceTetherAccessPermission();
1418 if (isTetheringSupported()) {
1419 return mTethering.getTetherableWifiRegexs();
1420 } else {
1421 return new String[0];
1422 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001423 }
1424
1425 // TODO - move iface listing, queries, etc to new module
1426 // javadoc from interface
1427 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001428 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001429 return mTethering.getTetherableIfaces();
1430 }
1431
1432 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001433 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001434 return mTethering.getTetheredIfaces();
1435 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001436
1437 // if ro.tether.denied = true we default to no tethering
1438 // gservices could set the secure setting to 1 though to enable it on a build where it
1439 // had previously been turned off.
1440 public boolean isTetheringSupported() {
1441 enforceTetherAccessPermission();
1442 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
1443 return ((Settings.Secure.getInt(mContext.getContentResolver(),
1444 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0) &&
1445 (mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null));
1446 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001447}