blob: 19f4b8ae1f7418384c9f0009420f37a1379892c0 [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 Greenwalt1b21f6c2010-02-23 18:58:05 -0800292 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-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 Project28527d22009-03-03 19:31:44 -0800302
Robert Greenwaltec05b3c2009-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 Greenwalt1b21f6c2010-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 Greenwaltec05b3c2009-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 Greenwalt0c4828c2010-01-26 11:40:34 -0800321
322 mTethering = new Tethering(mContext);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800323 }
324
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700325
The Android Open Source Project28527d22009-03-03 19:31:44 -0800326 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700327 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800328 * @param preference the new preference
329 */
330 public synchronized void setNetworkPreference(int preference) {
331 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700332 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700333 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700350 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
351 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800352 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700353
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700365
The Android Open Source Project28527d22009-03-03 19:31:44 -0800366 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700367 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-08-12 16:08:25 -0700374 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800375 return;
376
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 if (!mNetTrackers[mNetworkPreference].isAvailable())
378 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800379
Robert Greenwalt2034b912009-08-12 16:08:25 -0700380 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700381 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700382 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700383 if (DBG) {
384 Log.d(TAG, "tearing down " +
385 mNetTrackers[t].getNetworkInfo() +
386 " in enforcePreference");
387 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700388 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800408 */
409 public NetworkInfo getActiveNetworkInfo() {
410 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700411 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700412 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700413 continue;
414 }
415 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800416 NetworkInfo info = t.getNetworkInfo();
417 if (info.isConnected()) {
Robert Greenwalt2034b912009-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 Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700439 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800440 int i = 0;
441 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700442 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700451 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800478 long mCreateTime;
Robert Greenwalt2034b912009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800487 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700488
Robert Greenwalt2034b912009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800502 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
503 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700504 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700505 }
506
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700507 public void expire() {
508 Log.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800509 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
510 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700511 stopUsingNetworkFeature(this, false);
512 }
Robert Greenwalt3eeb6032009-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 Greenwalt2034b912009-08-12 16:08:25 -0700518 }
519
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700520 // javadoc from interface
Robert Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800527 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700528 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
529 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700532
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700533 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534
535 // TODO - move this into the MobileDataStateTracker
536 int usedNetworkType = networkType;
537 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-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 Greenwalt2034b912009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700571 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700572 mHandler.sendMessageDelayed(mHandler.obtainMessage(
573 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
574 f), getRestoreDefaultNetworkDelay());
575
Robert Greenwalt2034b912009-08-12 16:08:25 -0700576
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700577 if ((ni.isConnectedOrConnecting() == true) &&
578 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-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 Greenwalt2034b912009-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 Greenwaltaffc3a12009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800608 }
609
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700610 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800611 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700612 enforceChangePermission();
613
Robert Greenwaltaffc3a12009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800636 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700637 return 1;
638 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700639 }
640
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700650 if (DBG) {
651 Log.d(TAG, "stopUsingNetworkFeature for net " + networkType +
652 ": " + feature);
653 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700654
The Android Open Source Project28527d22009-03-03 19:31:44 -0800655 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
656 return -1;
657 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700658
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700664 return 1;
665 }
Robert Greenwaltaffc3a12009-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 Greenwalt3eeb6032009-12-21 18:24:07 -0800681 if (DBG) Log.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700682 return 1;
683 }
684 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685 }
Robert Greenwaltaffc3a12009-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 Greenwaltec05b3c2009-10-30 14:17:42 -0700701 if (tracker == null) {
702 return -1;
703 }
704 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700705 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700706 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800707 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-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 Greenwalt2034b912009-08-12 16:08:25 -0700719 return 1;
720 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700722 return tracker.stopUsingNetworkFeature(feature, pid, uid);
723 }
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-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 Project28527d22009-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 Greenwalt4666ed02009-09-10 15:06:20 -0700741
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700742 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
743 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700744 if (DBG) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700745 Log.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700746 }
747 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800748 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700749 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700759
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700767
The Android Open Source Project28527d22009-03-03 19:31:44 -0800768 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
769
770 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700771 Settings.Secure.BACKGROUND_DATA,
772 allowBackgroundDataUsage ? 1 : 0);
773
The Android Open Source Project28527d22009-03-03 19:31:44 -0800774 Intent broadcast = new Intent(
775 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
776 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700777 }
778
Robert Greenwalt1b21f6c2010-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 Project28527d22009-03-03 19:31:44 -0800819 private int getNumConnectedNetworks() {
820 int numConnectedNets = 0;
821
822 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700823 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700824 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800825 ++numConnectedNets;
826 }
827 }
828 return numConnectedNets;
829 }
830
831 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700832 mContext.enforceCallingOrSelfPermission(
833 android.Manifest.permission.ACCESS_NETWORK_STATE,
834 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800835 }
836
837 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700838 mContext.enforceCallingOrSelfPermission(
839 android.Manifest.permission.CHANGE_NETWORK_STATE,
840 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800841 }
842
Robert Greenwalt0c4828c2010-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 Greenwalt8e87f122010-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 Project28527d22009-03-03 19:31:44 -0800856 /**
Robert Greenwalt0659da32009-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 Project28527d22009-03-03 19:31:44 -0800861 * @param info the {@code NetworkInfo} for the network
862 */
863 private void handleDisconnect(NetworkInfo info) {
864
Robert Greenwalt2034b912009-08-12 16:08:25 -0700865 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800866
Robert Greenwalt2034b912009-08-12 16:08:25 -0700867 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-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 Greenwalt2034b912009-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 Project28527d22009-03-03 19:31:44 -0800882 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800883 }
884
The Android Open Source Project28527d22009-03-03 19:31:44 -0800885 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -0800886 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-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 Greenwalt0659da32009-07-16 17:21:39 -0700896 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
897 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800898 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700899
Robert Greenwalt3cc68d32010-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 Greenwaltf55ced92010-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 Greenwalt3cc68d32010-01-25 17:54:29 -0800923 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800924 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-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 Greenwalt2034b912009-08-12 16:08:25 -0700931 if (mActiveDefaultNetwork == prevNetType) {
932 mActiveDefaultNetwork = -1;
933 }
934
935 int newType = -1;
936 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800937 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800938 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700939 if (checkType == prevNetType) continue;
940 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800941 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
942 noMobileData) {
943 if (DBG) {
944 Log.d(TAG, "not failing over to mobile type " + checkType +
945 " because Mobile Data Disabled");
946 }
947 continue;
948 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700949 if (mNetAttributes[checkType].isDefault()) {
950 /* TODO - if we have multiple nets we could use
951 * we may want to put more thought into which we choose
952 */
953 if (checkType == mNetworkPreference) {
954 newType = checkType;
955 break;
956 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700957 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700958 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700959 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700960 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800961 }
962 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700963
964 if (newType != -1) {
965 newNet = mNetTrackers[newType];
966 /**
967 * See if the other network is available to fail over to.
968 * If is not available, we enable it anyway, so that it
969 * will be able to connect when it does become available,
970 * but we report a total loss of connectivity rather than
971 * report that we are attempting to fail over.
972 */
973 if (newNet.isAvailable()) {
974 NetworkInfo switchTo = newNet.getNetworkInfo();
975 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700976 if (!switchTo.isConnectedOrConnecting() ||
977 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978 newNet.reconnect();
979 }
980 if (DBG) {
981 if (switchTo.isConnected()) {
982 Log.v(TAG, "Switching to already connected " +
983 switchTo.getTypeName());
984 } else {
985 Log.v(TAG, "Attempting to switch to " +
986 switchTo.getTypeName());
987 }
988 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700989 } else {
990 newNet.reconnect();
991 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700992 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800993 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700994
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800995 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800996 }
997
998 private void sendConnectedBroadcast(NetworkInfo info) {
999 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -08001000 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001001 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1002 if (info.isFailover()) {
1003 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1004 info.setFailover(false);
1005 }
1006 if (info.getReason() != null) {
1007 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1008 }
1009 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001010 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1011 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001012 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001013 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001014 }
1015
1016 /**
1017 * Called when an attempt to fail over to another network has failed.
1018 * @param info the {@link NetworkInfo} for the failed network
1019 */
1020 private void handleConnectionFailure(NetworkInfo info) {
1021 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001022
Robert Greenwalt2034b912009-08-12 16:08:25 -07001023 String reason = info.getReason();
1024 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001025
Robert Greenwalt2034b912009-08-12 16:08:25 -07001026 if (DBG) {
1027 String reasonText;
1028 if (reason == null) {
1029 reasonText = ".";
1030 } else {
1031 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001032 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001033 Log.v(TAG, "Attempt to connect to " + info.getTypeName() +
1034 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001035 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001036
1037 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborna417ff82009-12-08 19:45:14 -08001038 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001039 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1040 if (getActiveNetworkInfo() == null) {
1041 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1042 }
1043 if (reason != null) {
1044 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1045 }
1046 if (extraInfo != null) {
1047 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1048 }
1049 if (info.isFailover()) {
1050 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1051 info.setFailover(false);
1052 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001053
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001054 NetworkStateTracker newNet = null;
1055 if (mNetAttributes[info.getType()].isDefault()) {
1056 newNet = tryFailover(info.getType());
1057 if (newNet != null) {
1058 NetworkInfo switchTo = newNet.getNetworkInfo();
1059 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1060 } else {
1061 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1062 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001063 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001064
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001065 // do this before we broadcast the change
1066 handleConnectivityChange();
1067
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001068 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001069 /*
1070 * If the failover network is already connected, then immediately send
1071 * out a followup broadcast indicating successful failover
1072 */
1073 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1074 sendConnectedBroadcast(newNet.getNetworkInfo());
1075 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001076 }
1077
1078 private void sendStickyBroadcast(Intent intent) {
1079 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001080 if (!mSystemReady) {
1081 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001082 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001083 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1084 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001085 }
1086 }
1087
1088 void systemReady() {
1089 synchronized(this) {
1090 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001091 if (mInitialBroadcast != null) {
1092 mContext.sendStickyBroadcast(mInitialBroadcast);
1093 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001094 }
1095 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 }
1097
1098 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001099 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001100
1101 // snapshot isFailover, because sendConnectedBroadcast() resets it
1102 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001103 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001104
Robert Greenwalt2034b912009-08-12 16:08:25 -07001105 // if this is a default net and other default is running
1106 // kill the one not preferred
1107 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001108 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1109 if ((type != mNetworkPreference &&
1110 mNetAttributes[mActiveDefaultNetwork].mPriority >
1111 mNetAttributes[type].mPriority) ||
1112 mNetworkPreference == mActiveDefaultNetwork) {
1113 // don't accept this one
1114 if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " +
1115 "to torn down network " + info.getTypeName());
1116 teardown(thisNet);
1117 return;
1118 } else {
1119 // tear down the other
1120 NetworkStateTracker otherNet =
1121 mNetTrackers[mActiveDefaultNetwork];
1122 if (DBG) Log.v(TAG, "Policy requires " +
1123 otherNet.getNetworkInfo().getTypeName() +
1124 " teardown");
1125 if (!teardown(otherNet)) {
1126 Log.e(TAG, "Network declined teardown request");
1127 return;
1128 }
1129 if (isFailover) {
1130 otherNet.releaseWakeLock();
1131 }
1132 }
1133 }
1134 mActiveDefaultNetwork = type;
1135 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001136 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001137 thisNet.updateNetworkSettings();
1138 handleConnectivityChange();
1139 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001140 }
1141
1142 private void handleScanResultsAvailable(NetworkInfo info) {
1143 int networkType = info.getType();
1144 if (networkType != ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001145 if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " +
1146 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001147 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001148
The Android Open Source Project28527d22009-03-03 19:31:44 -08001149 mNetTrackers[networkType].interpretScanResultsAvailable();
1150 }
1151
Robert Greenwalt0659da32009-07-16 17:21:39 -07001152 private void handleNotificationChange(boolean visible, int id,
1153 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001154 NotificationManager notificationManager = (NotificationManager) mContext
1155 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001156
The Android Open Source Project28527d22009-03-03 19:31:44 -08001157 if (visible) {
1158 notificationManager.notify(id, notification);
1159 } else {
1160 notificationManager.cancel(id);
1161 }
1162 }
1163
1164 /**
1165 * After any kind of change in the connectivity state of any network,
1166 * make sure that anything that depends on the connectivity state of
1167 * more than one network is set up correctly. We're mainly concerned
1168 * with making sure that the list of DNS servers is set up according
1169 * to which networks are connected, and ensuring that the right routing
1170 * table entries exist.
1171 */
1172 private void handleConnectivityChange() {
1173 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 * If a non-default network is enabled, add the host routes that
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001175 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project28527d22009-03-03 19:31:44 -08001176 * If both mobile and wifi are enabled, add the host routes that
1177 * will allow MMS traffic to pass on the mobile network. But
1178 * remove the default route for the mobile network, so that there
1179 * will be only one default route, to ensure that all traffic
1180 * except MMS will travel via Wi-Fi.
1181 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 handleDnsConfigurationChange();
1183
1184 for (int netType : mPriorityList) {
1185 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1186 if (mNetAttributes[netType].isDefault()) {
1187 mNetTrackers[netType].addDefaultRoute();
1188 } else {
1189 mNetTrackers[netType].addPrivateDnsRoutes();
1190 }
1191 } else {
1192 if (mNetAttributes[netType].isDefault()) {
1193 mNetTrackers[netType].removeDefaultRoute();
1194 } else {
1195 mNetTrackers[netType].removePrivateDnsRoutes();
1196 }
1197 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 }
1199 }
1200
Robert Greenwalt2034b912009-08-12 16:08:25 -07001201 /**
1202 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1203 * on the highest priority active net which this process requested.
1204 * If there aren't any, clear it out
1205 */
1206 private void reassessPidDns(int myPid, boolean doBump)
1207 {
1208 if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid);
1209 for(int i : mPriorityList) {
1210 if (mNetAttributes[i].isDefault()) {
1211 continue;
1212 }
1213 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001214 if (nt.getNetworkInfo().isConnected() &&
1215 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001216 List pids = mNetRequestersPids[i];
1217 for (int j=0; j<pids.size(); j++) {
1218 Integer pid = (Integer)pids.get(j);
1219 if (pid.intValue() == myPid) {
1220 String[] dnsList = nt.getNameServers();
1221 writePidDns(dnsList, myPid);
1222 if (doBump) {
1223 bumpDns();
1224 }
1225 return;
1226 }
1227 }
1228 }
1229 }
1230 // nothing found - delete
1231 for (int i = 1; ; i++) {
1232 String prop = "net.dns" + i + "." + myPid;
1233 if (SystemProperties.get(prop).length() == 0) {
1234 if (doBump) {
1235 bumpDns();
1236 }
1237 return;
1238 }
1239 SystemProperties.set(prop, "");
1240 }
1241 }
1242
1243 private void writePidDns(String[] dnsList, int pid) {
1244 int j = 1;
1245 for (String dns : dnsList) {
1246 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1247 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1248 }
1249 }
1250 }
1251
1252 private void bumpDns() {
1253 /*
1254 * Bump the property that tells the name resolver library to reread
1255 * the DNS server list from the properties.
1256 */
1257 String propVal = SystemProperties.get("net.dnschange");
1258 int n = 0;
1259 if (propVal.length() != 0) {
1260 try {
1261 n = Integer.parseInt(propVal);
1262 } catch (NumberFormatException e) {}
1263 }
1264 SystemProperties.set("net.dnschange", "" + (n+1));
1265 }
1266
1267 private void handleDnsConfigurationChange() {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 // add default net's dns entries
1269 for (int x = mPriorityList.length-1; x>= 0; x--) {
1270 int netType = mPriorityList[x];
1271 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt2034b912009-08-12 16:08:25 -07001272 if (nt != null && nt.getNetworkInfo().isConnected() &&
1273 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001274 String[] dnsList = nt.getNameServers();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001275 if (mNetAttributes[netType].isDefault()) {
1276 int j = 1;
1277 for (String dns : dnsList) {
1278 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt423dbbc2009-09-30 21:01:30 -07001279 if (DBG) {
1280 Log.d(TAG, "adding dns " + dns + " for " +
1281 nt.getNetworkInfo().getTypeName());
1282 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001283 SystemProperties.set("net.dns" + j++, dns);
1284 }
1285 }
1286 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8e5b8532009-08-25 14:00:10 -07001287 if (DBG) Log.d(TAG, "erasing net.dns" + k);
1288 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001289 }
1290 mNumDnsEntries = j;
1291 } else {
1292 // set per-pid dns for attached secondary nets
1293 List pids = mNetRequestersPids[netType];
1294 for (int y=0; y< pids.size(); y++) {
1295 Integer pid = (Integer)pids.get(y);
1296 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001297 }
1298 }
1299 }
1300 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001301
1302 bumpDns();
1303 }
1304
1305 private int getRestoreDefaultNetworkDelay() {
1306 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1307 NETWORK_RESTORE_DELAY_PROP_NAME);
1308 if(restoreDefaultNetworkDelayStr != null &&
1309 restoreDefaultNetworkDelayStr.length() != 0) {
1310 try {
1311 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1312 } catch (NumberFormatException e) {
1313 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001314 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001315 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001316 }
1317
1318 @Override
1319 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001320 if (mContext.checkCallingOrSelfPermission(
1321 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001322 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001323 pw.println("Permission Denial: can't dump ConnectivityService " +
1324 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1325 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001326 return;
1327 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001328 pw.println();
1329 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001330 if (nst != null) {
1331 if (nst.getNetworkInfo().isConnected()) {
1332 pw.println("Active network: " + nst.getNetworkInfo().
1333 getTypeName());
1334 }
1335 pw.println(nst.getNetworkInfo());
1336 pw.println(nst);
1337 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001338 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001339 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001340
1341 pw.println("Network Requester Pids:");
1342 for (int net : mPriorityList) {
1343 String pidString = net + ": ";
1344 for (Object pid : mNetRequestersPids[net]) {
1345 pidString = pidString + pid.toString() + ", ";
1346 }
1347 pw.println(pidString);
1348 }
1349 pw.println();
1350
1351 pw.println("FeatureUsers:");
1352 for (Object requester : mFeatureUsers) {
1353 pw.println(requester.toString());
1354 }
1355 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001356
1357 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001358 }
1359
Robert Greenwalt2034b912009-08-12 16:08:25 -07001360 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001361 private class MyHandler extends Handler {
1362 @Override
1363 public void handleMessage(Message msg) {
1364 NetworkInfo info;
1365 switch (msg.what) {
1366 case NetworkStateTracker.EVENT_STATE_CHANGED:
1367 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001368 int type = info.getType();
1369 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001370 // only do this optimization for wifi. It going into scan mode for location
1371 // services generates alot of noise. Meanwhile the mms apn won't send out
1372 // subsequent notifications when on default cellular because it never
1373 // disconnects.. so only do this to wifi notifications. Fixed better when the
1374 // APN notifications are standardized.
1375 if (mNetAttributes[type].mLastState == state &&
1376 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001377 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001378 // TODO - remove this after we validate the dropping doesn't break
1379 // anything
Robert Greenwalt12c44552009-12-07 11:33:18 -08001380 Log.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001381 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001382 state + "/" + info.getDetailedState());
1383 }
1384 return;
1385 }
1386 mNetAttributes[type].mLastState = state;
1387
Robert Greenwalt2034b912009-08-12 16:08:25 -07001388 if (DBG) Log.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001389 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001390 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001391
1392 // Connectivity state changed:
1393 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001394 // [12-9] Network subtype (for mobile network, as defined
1395 // by TelephonyManager)
1396 // [8-3] Detailed state ordinal (as defined by
1397 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001398 // [2-0] Network type (as defined by ConnectivityManager)
1399 int eventLogParam = (info.getType() & 0x7) |
1400 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1401 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001402 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001403 eventLogParam);
1404
1405 if (info.getDetailedState() ==
1406 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001407 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001408 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001409 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001410 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001411 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001412 // the logic here is, handle SUSPENDED the same as
1413 // DISCONNECTED. The only difference being we are
1414 // broadcasting an intent with NetworkInfo that's
1415 // suspended. This allows the applications an
1416 // opportunity to handle DISCONNECTED and SUSPENDED
1417 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001418 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001419 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001420 handleConnect(info);
1421 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001422 break;
1423
1424 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1425 info = (NetworkInfo) msg.obj;
1426 handleScanResultsAvailable(info);
1427 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001428
The Android Open Source Project28527d22009-03-03 19:31:44 -08001429 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001430 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1431 (Notification) msg.obj);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001432
1433 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt2034b912009-08-12 16:08:25 -07001434 handleDnsConfigurationChange();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001435 break;
1436
1437 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1438 // fill me in
1439 break;
1440
1441 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1442 // fill me in
1443 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001444 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001445 FeatureUser u = (FeatureUser)msg.obj;
1446 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001447 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001448 }
1449 }
1450 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001451
1452 // javadoc from interface
1453 public boolean tether(String iface) {
1454 enforceTetherChangePermission();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001455 return isTetheringSupported() && mTethering.tether(iface);
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001456 }
1457
1458 // javadoc from interface
1459 public boolean untether(String iface) {
1460 enforceTetherChangePermission();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001461 return isTetheringSupported() && mTethering.untether(iface);
1462 }
1463
1464 // TODO - proper iface API for selection by property, inspection, etc
1465 public String[] getTetherableUsbRegexs() {
1466 enforceTetherAccessPermission();
1467 if (isTetheringSupported()) {
1468 return mTethering.getTetherableUsbRegexs();
1469 } else {
1470 return new String[0];
1471 }
1472 }
1473
1474 public String[] getTetherableWifiRegexs() {
1475 enforceTetherAccessPermission();
1476 if (isTetheringSupported()) {
1477 return mTethering.getTetherableWifiRegexs();
1478 } else {
1479 return new String[0];
1480 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001481 }
1482
1483 // TODO - move iface listing, queries, etc to new module
1484 // javadoc from interface
1485 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001486 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001487 return mTethering.getTetherableIfaces();
1488 }
1489
1490 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001491 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001492 return mTethering.getTetheredIfaces();
1493 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001494
1495 // if ro.tether.denied = true we default to no tethering
1496 // gservices could set the secure setting to 1 though to enable it on a build where it
1497 // had previously been turned off.
1498 public boolean isTetheringSupported() {
1499 enforceTetherAccessPermission();
1500 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
1501 return ((Settings.Secure.getInt(mContext.getContentResolver(),
1502 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0) &&
1503 (mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null));
1504 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001505}