blob: 81b8d40b0a37de4fe89f60f05a839a075f3aa511 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt42acef32009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Robert Greenwalt42acef32009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080046import com.android.server.connectivity.Tethering;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Robert Greenwalt42acef32009-08-12 16:08:25 -070050import java.util.ArrayList;
51import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53/**
54 * @hide
55 */
56public class ConnectivityService extends IConnectivityManager.Stub {
57
Robert Greenwaltd8df1492009-10-06 14:12:53 -070058 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private static final String TAG = "ConnectivityService";
60
Robert Greenwalt42acef32009-08-12 16:08:25 -070061 // how long to wait before switching back to a radio's default network
62 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
63 // system property that can override the above value
64 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
65 "android.telephony.apn-restore";
66
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080067
68 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080069 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
72 * Sometimes we want to refer to the individual network state
73 * trackers separately, and sometimes we just want to treat them
74 * abstractly.
75 */
76 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt42acef32009-08-12 16:08:25 -070077
78 /**
79 * A per Net list of the PID's that requested access to the net
80 * used both as a refcount and for per-PID DNS selection
81 */
82 private List mNetRequestersPids[];
83
Robert Greenwalt42acef32009-08-12 16:08:25 -070084 // priority order of the nettrackers
85 // (excluding dynamically set mNetworkPreference)
86 // TODO - move mNetworkTypePreference into this
87 private int[] mPriorityList;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private Context mContext;
90 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070091 private int mActiveDefaultNetwork = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 private boolean mTestMode;
96 private static ConnectivityService sServiceInstance;
97
Robert Greenwalt42acef32009-08-12 16:08:25 -070098 private Handler mHandler;
99
100 // list of DeathRecipients used to make sure features are turned off when
101 // a process dies
102 private List mFeatureUsers;
103
Mike Lockwood0f79b542009-08-14 14:18:49 -0400104 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800105 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400106
Robert Greenwalt511288a2009-12-07 11:33:18 -0800107 private static class NetworkAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700108 /**
109 * Class for holding settings read from resources.
110 */
111 public String mName;
112 public int mType;
113 public int mRadio;
114 public int mPriority;
Robert Greenwalt511288a2009-12-07 11:33:18 -0800115 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700116 public NetworkAttributes(String init) {
117 String fragments[] = init.split(",");
118 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700119 mType = Integer.parseInt(fragments[1]);
120 mRadio = Integer.parseInt(fragments[2]);
121 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800122 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700123 }
124 public boolean isDefault() {
125 return (mType == mRadio);
126 }
127 }
128 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700129 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700130
Robert Greenwalt511288a2009-12-07 11:33:18 -0800131 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700132 public int mSimultaneity;
133 public int mType;
134 public RadioAttributes(String init) {
135 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700136 mType = Integer.parseInt(fragments[0]);
137 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700138 }
139 }
140 RadioAttributes[] mRadioAttributes;
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private static class ConnectivityThread extends Thread {
143 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private ConnectivityThread(Context context) {
146 super("ConnectivityThread");
147 mContext = context;
148 }
149
150 @Override
151 public void run() {
152 Looper.prepare();
153 synchronized (this) {
154 sServiceInstance = new ConnectivityService(mContext);
155 notifyAll();
156 }
157 Looper.loop();
158 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 public static ConnectivityService getServiceInstance(Context context) {
161 ConnectivityThread thread = new ConnectivityThread(context);
162 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 synchronized (thread) {
165 while (sServiceInstance == null) {
166 try {
167 // Wait until sServiceInstance has been initialized.
168 thread.wait();
169 } catch (InterruptedException ignore) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800170 Slog.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700171 "Unexpected InterruptedException while waiting"+
172 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
174 }
175 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return sServiceInstance;
178 }
179 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static ConnectivityService getInstance(Context context) {
182 return ConnectivityThread.getServiceInstance(context);
183 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 private ConnectivityService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800186 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-01-14 17:47:58 -0800187
188 // setup our unique device name
189 String id = Settings.Secure.getString(context.getContentResolver(),
190 Settings.Secure.ANDROID_ID);
191 if (id != null && id.length() > 0) {
192 String name = new String("android_").concat(id);
193 SystemProperties.set("net.hostname", name);
194 }
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700197 mNetTrackers = new NetworkStateTracker[
198 ConnectivityManager.MAX_NETWORK_TYPE+1];
199 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700202
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700203 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
204 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
205
Robert Greenwalt42acef32009-08-12 16:08:25 -0700206 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700207 String[] raStrings = context.getResources().getStringArray(
208 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700209 for (String raString : raStrings) {
210 RadioAttributes r = new RadioAttributes(raString);
211 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800212 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700213 continue;
214 }
215 if (mRadioAttributes[r.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800216 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700217 r.mType);
218 continue;
219 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700220 mRadioAttributes[r.mType] = r;
221 }
222
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700223 String[] naStrings = context.getResources().getStringArray(
224 com.android.internal.R.array.networkAttributes);
225 for (String naString : naStrings) {
226 try {
227 NetworkAttributes n = new NetworkAttributes(naString);
228 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800229 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700230 n.mType);
231 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700232 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700233 if (mNetAttributes[n.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800234 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700235 n.mType);
236 continue;
237 }
238 if (mRadioAttributes[n.mRadio] == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800239 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700240 "radio " + n.mRadio + " in network type " + n.mType);
241 continue;
242 }
243 mNetAttributes[n.mType] = n;
244 mNetworksDefined++;
245 } catch(Exception e) {
246 // ignore it - leave the entry null
Robert Greenwalt42acef32009-08-12 16:08:25 -0700247 }
248 }
249
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700250 // high priority first
251 mPriorityList = new int[mNetworksDefined];
252 {
253 int insertionPoint = mNetworksDefined-1;
254 int currentLowest = 0;
255 int nextLowest = 0;
256 while (insertionPoint > -1) {
257 for (NetworkAttributes na : mNetAttributes) {
258 if (na == null) continue;
259 if (na.mPriority < currentLowest) continue;
260 if (na.mPriority > currentLowest) {
261 if (na.mPriority < nextLowest || nextLowest == 0) {
262 nextLowest = na.mPriority;
263 }
264 continue;
265 }
266 mPriorityList[insertionPoint--] = na.mType;
267 }
268 currentLowest = nextLowest;
269 nextLowest = 0;
270 }
271 }
272
273 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
274 for (int i : mPriorityList) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700275 mNetRequestersPids[i] = new ArrayList();
276 }
277
278 mFeatureUsers = new ArrayList();
279
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700280 mNumDnsEntries = 0;
281
282 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
283 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /*
285 * Create the network state trackers for Wi-Fi and mobile
286 * data. Maybe this could be done with a factory class,
287 * but it's not clear that it's worth it, given that
288 * the number of different network types is not going
289 * to change very often.
290 */
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800291 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700292 for (int netType : mPriorityList) {
293 switch (mNetAttributes[netType].mRadio) {
294 case ConnectivityManager.TYPE_WIFI:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800295 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700296 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
297 WifiService wifiService = new WifiService(context, wst);
298 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff7b009782010-03-11 16:37:45 -0800299 wifiService.startWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700300 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
301 wst.startMonitoring();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700303 break;
304 case ConnectivityManager.TYPE_MOBILE:
305 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
306 netType, mNetAttributes[netType].mName);
307 mNetTrackers[netType].startMonitoring();
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800308 if (noMobileData) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800309 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800310 mNetTrackers[netType].teardown();
311 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700312 break;
313 default:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800314 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700315 mNetAttributes[netType].mRadio);
316 continue;
317 }
318 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800319
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800320 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -0800321 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
322 !mTethering.isDunRequired()) &&
323 (mTethering.getTetherableUsbRegexs().length != 0 ||
324 mTethering.getTetherableWifiRegexs().length != 0) &&
325 mTethering.getUpstreamIfaceRegexs().length != 0);
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700331 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 * @param preference the new preference
333 */
334 public synchronized void setNetworkPreference(int preference) {
335 enforceChangePermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700336 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700337 mNetAttributes[preference] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700338 mNetAttributes[preference].isDefault()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 if (mNetworkPreference != preference) {
340 persistNetworkPreference(preference);
341 mNetworkPreference = preference;
342 enforcePreference();
343 }
344 }
345 }
346
347 public int getNetworkPreference() {
348 enforceAccessPermission();
349 return mNetworkPreference;
350 }
351
352 private void persistNetworkPreference(int networkPreference) {
353 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700354 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
355 networkPreference);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 private int getPersistedNetworkPreference() {
359 final ContentResolver cr = mContext.getContentResolver();
360
361 final int networkPrefSetting = Settings.Secure
362 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
363 if (networkPrefSetting != -1) {
364 return networkPrefSetting;
365 }
366
367 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
368 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700371 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 * In this method, we only tear down a non-preferred network. Establishing
373 * a connection to the preferred network is taken care of when we handle
374 * the disconnect event from the non-preferred network
375 * (see {@link #handleDisconnect(NetworkInfo)}).
376 */
377 private void enforcePreference() {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700378 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 return;
380
Robert Greenwalt42acef32009-08-12 16:08:25 -0700381 if (!mNetTrackers[mNetworkPreference].isAvailable())
382 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383
Robert Greenwalt42acef32009-08-12 16:08:25 -0700384 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700385 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700386 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700387 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800388 Slog.d(TAG, "tearing down " +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700389 mNetTrackers[t].getNetworkInfo() +
390 " in enforcePreference");
391 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700392 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 }
395 }
396
397 private boolean teardown(NetworkStateTracker netTracker) {
398 if (netTracker.teardown()) {
399 netTracker.setTeardownRequested(true);
400 return true;
401 } else {
402 return false;
403 }
404 }
405
406 /**
407 * Return NetworkInfo for the active (i.e., connected) network interface.
408 * It is assumed that at most one network is active at a time. If more
409 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700410 * @return the info for the active network, or {@code null} if none is
411 * active
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
413 public NetworkInfo getActiveNetworkInfo() {
414 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700415 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700416 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700417 continue;
418 }
419 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 NetworkInfo info = t.getNetworkInfo();
421 if (info.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800422 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt42acef32009-08-12 16:08:25 -0700423 "connected default network is not " +
424 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 return info;
426 }
427 }
428 return null;
429 }
430
431 public NetworkInfo getNetworkInfo(int networkType) {
432 enforceAccessPermission();
433 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
434 NetworkStateTracker t = mNetTrackers[networkType];
435 if (t != null)
436 return t.getNetworkInfo();
437 }
438 return null;
439 }
440
441 public NetworkInfo[] getAllNetworkInfo() {
442 enforceAccessPermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700443 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 int i = 0;
445 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700446 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448 return result;
449 }
450
451 public boolean setRadios(boolean turnOn) {
452 boolean result = true;
453 enforceChangePermission();
454 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700455 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 return result;
458 }
459
460 public boolean setRadio(int netType, boolean turnOn) {
461 enforceChangePermission();
462 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
463 return false;
464 }
465 NetworkStateTracker tracker = mNetTrackers[netType];
466 return tracker != null && tracker.setRadio(turnOn);
467 }
468
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700469 /**
470 * Used to notice when the calling process dies so we can self-expire
471 *
472 * Also used to know if the process has cleaned up after itself when
473 * our auto-expire timer goes off. The timer has a link to an object.
474 *
475 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700476 private class FeatureUser implements IBinder.DeathRecipient {
477 int mNetworkType;
478 String mFeature;
479 IBinder mBinder;
480 int mPid;
481 int mUid;
Robert Greenwaltb9285352009-12-21 18:24:07 -0800482 long mCreateTime;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700483
484 FeatureUser(int type, String feature, IBinder binder) {
485 super();
486 mNetworkType = type;
487 mFeature = feature;
488 mBinder = binder;
489 mPid = getCallingPid();
490 mUid = getCallingUid();
Robert Greenwaltb9285352009-12-21 18:24:07 -0800491 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700492
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493 try {
494 mBinder.linkToDeath(this, 0);
495 } catch (RemoteException e) {
496 binderDied();
497 }
498 }
499
500 void unlinkDeathRecipient() {
501 mBinder.unlinkToDeath(this, 0);
502 }
503
504 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800505 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800506 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
507 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700508 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700509 }
510
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700511 public void expire() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800512 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800513 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
514 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700515 stopUsingNetworkFeature(this, false);
516 }
Robert Greenwaltb9285352009-12-21 18:24:07 -0800517
518 public String toString() {
519 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
520 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
521 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700522 }
523
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700524 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700525 public int startUsingNetworkFeature(int networkType, String feature,
526 IBinder binder) {
527 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700529 ": " + feature);
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700532 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
533 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700537 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538
539 // TODO - move this into the MobileDataStateTracker
540 int usedNetworkType = networkType;
541 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800542 if (!getMobileDataEnabled()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800543 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800544 return Phone.APN_TYPE_NOT_AVAILABLE;
545 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700546 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
547 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
548 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
549 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
550 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
551 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
552 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
553 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
554 }
555 }
556 NetworkStateTracker network = mNetTrackers[usedNetworkType];
557 if (network != null) {
558 if (usedNetworkType != networkType) {
559 Integer currentPid = new Integer(getCallingPid());
560
561 NetworkStateTracker radio = mNetTrackers[networkType];
562 NetworkInfo ni = network.getNetworkInfo();
563
564 if (ni.isAvailable() == false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700566 return Phone.APN_TYPE_NOT_AVAILABLE;
567 }
568
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700569 synchronized(this) {
570 mFeatureUsers.add(f);
571 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
572 // this gets used for per-pid dns when connected
573 mNetRequestersPids[usedNetworkType].add(currentPid);
574 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700575 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700576 mHandler.sendMessageDelayed(mHandler.obtainMessage(
577 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
578 f), getRestoreDefaultNetworkDelay());
579
Robert Greenwalt42acef32009-08-12 16:08:25 -0700580
Robert Greenwalta64bf832009-08-19 20:19:33 -0700581 if ((ni.isConnectedOrConnecting() == true) &&
582 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700583 if (ni.isConnected() == true) {
584 // add the pid-specific dns
585 handleDnsConfigurationChange();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700587 return Phone.APN_ALREADY_ACTIVE;
588 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700590 return Phone.APN_REQUEST_STARTED;
591 }
592
593 // check if the radio in play can make another contact
594 // assume if cannot for now
595
Joe Onorato8a9b2202010-02-26 18:56:32 -0800596 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700597 network.reconnect();
598 return Phone.APN_REQUEST_STARTED;
599 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700600 synchronized(this) {
601 mFeatureUsers.add(f);
602 }
603 mHandler.sendMessageDelayed(mHandler.obtainMessage(
604 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
605 f), getRestoreDefaultNetworkDelay());
606
Robert Greenwalt42acef32009-08-12 16:08:25 -0700607 return network.startUsingNetworkFeature(feature,
608 getCallingPid(), getCallingUid());
609 }
610 }
611 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 }
613
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700614 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700616 enforceChangePermission();
617
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700618 int pid = getCallingPid();
619 int uid = getCallingUid();
620
621 FeatureUser u = null;
622 boolean found = false;
623
624 synchronized(this) {
625 for (int i = 0; i < mFeatureUsers.size() ; i++) {
626 u = (FeatureUser)mFeatureUsers.get(i);
627 if (uid == u.mUid && pid == u.mPid &&
628 networkType == u.mNetworkType &&
629 TextUtils.equals(feature, u.mFeature)) {
630 found = true;
631 break;
632 }
633 }
634 }
635 if (found && u != null) {
636 // stop regardless of how many other time this proc had called start
637 return stopUsingNetworkFeature(u, true);
638 } else {
639 // none found!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800640 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700641 return 1;
642 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700643 }
644
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700645 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
646 int networkType = u.mNetworkType;
647 String feature = u.mFeature;
648 int pid = u.mPid;
649 int uid = u.mUid;
650
651 NetworkStateTracker tracker = null;
652 boolean callTeardown = false; // used to carry our decision outside of sync block
653
Robert Greenwalt42acef32009-08-12 16:08:25 -0700654 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800655 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700656 ": " + feature);
657 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
660 return -1;
661 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700662
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700663 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
664 // sync block
665 synchronized(this) {
666 // check if this process still has an outstanding start request
667 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800668 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700669 return 1;
670 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700671 u.unlinkDeathRecipient();
672 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
673 // If we care about duplicate requests, check for that here.
674 //
675 // This is done to support the extension of a request - the app
676 // can request we start the network feature again and renew the
677 // auto-shutoff delay. Normal "stop" calls from the app though
678 // do not pay attention to duplicate requests - in effect the
679 // API does not refcount and a single stop will counter multiple starts.
680 if (ignoreDups == false) {
681 for (int i = 0; i < mFeatureUsers.size() ; i++) {
682 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
683 if (x.mUid == u.mUid && x.mPid == u.mPid &&
684 x.mNetworkType == u.mNetworkType &&
685 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700687 return 1;
688 }
689 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700690 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700691
692 // TODO - move to MobileDataStateTracker
693 int usedNetworkType = networkType;
694 if (networkType == ConnectivityManager.TYPE_MOBILE) {
695 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
696 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
697 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
698 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
699 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
700 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
701 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
703 }
704 }
705 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700706 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800707 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700708 return -1;
709 }
710 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700711 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700712 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800713 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700714 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800715 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700716 "others still using it");
717 return 1;
718 }
719 callTeardown = true;
720 }
721 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800722 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700723 if (callTeardown) {
724 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700725 return 1;
726 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700727 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700728 return tracker.stopUsingNetworkFeature(feature, pid, uid);
729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 /**
733 * Ensure that a network route exists to deliver traffic to the specified
734 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700735 * @param networkType the type of the network over which traffic to the
736 * specified host is to be routed
737 * @param hostAddress the IP address of the host to which the route is
738 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 * @return {@code true} on success, {@code false} on failure
740 */
741 public boolean requestRouteToHost(int networkType, int hostAddress) {
742 enforceChangePermission();
743 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
744 return false;
745 }
746 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700747
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700748 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
749 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700750 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800751 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700752 }
753 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700755 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 /**
759 * @see ConnectivityManager#getBackgroundDataSetting()
760 */
761 public boolean getBackgroundDataSetting() {
762 return Settings.Secure.getInt(mContext.getContentResolver(),
763 Settings.Secure.BACKGROUND_DATA, 1) == 1;
764 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 /**
767 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
768 */
769 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
770 mContext.enforceCallingOrSelfPermission(
771 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
772 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
775
776 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700777 Settings.Secure.BACKGROUND_DATA,
778 allowBackgroundDataUsage ? 1 : 0);
779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 Intent broadcast = new Intent(
781 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
782 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700783 }
784
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800785 /**
786 * @see ConnectivityManager#getMobileDataEnabled()
787 */
788 public boolean getMobileDataEnabled() {
789 enforceAccessPermission();
790 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
791 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800793 return retVal;
794 }
795
796 /**
797 * @see ConnectivityManager#setMobileDataEnabled(boolean)
798 */
799 public synchronized void setMobileDataEnabled(boolean enabled) {
800 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800802
803 if (getMobileDataEnabled() == enabled) return;
804
805 Settings.Secure.putInt(mContext.getContentResolver(),
806 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
807
808 if (enabled) {
809 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800810 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800811 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
812 }
813 } else {
814 for (NetworkStateTracker nt : mNetTrackers) {
815 if (nt == null) continue;
816 int netType = nt.getNetworkInfo().getType();
817 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800819 nt.teardown();
820 }
821 }
822 }
823 }
824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 private int getNumConnectedNetworks() {
826 int numConnectedNets = 0;
827
828 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700829 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700830 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 ++numConnectedNets;
832 }
833 }
834 return numConnectedNets;
835 }
836
837 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700838 mContext.enforceCallingOrSelfPermission(
839 android.Manifest.permission.ACCESS_NETWORK_STATE,
840 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
843 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700844 mContext.enforceCallingOrSelfPermission(
845 android.Manifest.permission.CHANGE_NETWORK_STATE,
846 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
848
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800849 // TODO Make this a special check when it goes public
850 private void enforceTetherChangePermission() {
851 mContext.enforceCallingOrSelfPermission(
852 android.Manifest.permission.CHANGE_NETWORK_STATE,
853 "ConnectivityService");
854 }
855
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800856 private void enforceTetherAccessPermission() {
857 mContext.enforceCallingOrSelfPermission(
858 android.Manifest.permission.ACCESS_NETWORK_STATE,
859 "ConnectivityService");
860 }
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700863 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
864 * network, we ignore it. If it is for the active network, we send out a
865 * broadcast. But first, we check whether it might be possible to connect
866 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 * @param info the {@code NetworkInfo} for the network
868 */
869 private void handleDisconnect(NetworkInfo info) {
870
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872
Robert Greenwalt42acef32009-08-12 16:08:25 -0700873 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 /*
875 * If the disconnected network is not the active one, then don't report
876 * this as a loss of connectivity. What probably happened is that we're
877 * getting the disconnect for a network that we explicitly disabled
878 * in accordance with network preference policies.
879 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700880 if (!mNetAttributes[prevNetType].isDefault()) {
881 List pids = mNetRequestersPids[prevNetType];
882 for (int i = 0; i<pids.size(); i++) {
883 Integer pid = (Integer)pids.get(i);
884 // will remove them because the net's no longer connected
885 // need to do this now as only now do we know the pids and
886 // can properly null things that are no longer referenced.
887 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 }
890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800892 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
894 if (info.isFailover()) {
895 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
896 info.setFailover(false);
897 }
898 if (info.getReason() != null) {
899 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
900 }
901 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700902 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
903 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700905
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800906 NetworkStateTracker newNet = null;
907 if (mNetAttributes[prevNetType].isDefault()) {
908 newNet = tryFailover(prevNetType);
909 if (newNet != null) {
910 NetworkInfo switchTo = newNet.getNetworkInfo();
911 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
912 } else {
913 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
914 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800915 }
916 // do this before we broadcast the change
917 handleConnectivityChange();
918
919 sendStickyBroadcast(intent);
920 /*
921 * If the failover network is already connected, then immediately send
922 * out a followup broadcast indicating successful failover
923 */
924 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
925 sendConnectedBroadcast(newNet.getNetworkInfo());
926 }
927 }
928
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800929 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800930 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700931 /*
932 * If this is a default network, check if other defaults are available
933 * or active
934 */
935 NetworkStateTracker newNet = null;
936 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700937 if (mActiveDefaultNetwork == prevNetType) {
938 mActiveDefaultNetwork = -1;
939 }
940
941 int newType = -1;
942 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -0800943 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800944 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700945 if (checkType == prevNetType) continue;
946 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -0800947 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
948 noMobileData) {
949 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800950 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -0800951 " because Mobile Data Disabled");
952 }
953 continue;
954 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700955 if (mNetAttributes[checkType].isDefault()) {
956 /* TODO - if we have multiple nets we could use
957 * we may want to put more thought into which we choose
958 */
959 if (checkType == mNetworkPreference) {
960 newType = checkType;
961 break;
962 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700963 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700964 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700965 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700969
970 if (newType != -1) {
971 newNet = mNetTrackers[newType];
972 /**
973 * See if the other network is available to fail over to.
974 * If is not available, we enable it anyway, so that it
975 * will be able to connect when it does become available,
976 * but we report a total loss of connectivity rather than
977 * report that we are attempting to fail over.
978 */
979 if (newNet.isAvailable()) {
980 NetworkInfo switchTo = newNet.getNetworkInfo();
981 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700982 if (!switchTo.isConnectedOrConnecting() ||
983 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700984 newNet.reconnect();
985 }
986 if (DBG) {
987 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800988 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700989 switchTo.getTypeName());
990 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800991 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700992 switchTo.getTypeName());
993 }
994 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700995 } else {
996 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -0800997 newNet = null; // not officially avail.. try anyway, but
998 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -0700999 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001002
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001003 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
1006 private void sendConnectedBroadcast(NetworkInfo info) {
1007 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001008 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1010 if (info.isFailover()) {
1011 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1012 info.setFailover(false);
1013 }
1014 if (info.getReason() != null) {
1015 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1016 }
1017 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001018 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1019 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001021 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
1024 /**
1025 * Called when an attempt to fail over to another network has failed.
1026 * @param info the {@link NetworkInfo} for the failed network
1027 */
1028 private void handleConnectionFailure(NetworkInfo info) {
1029 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030
Robert Greenwalt42acef32009-08-12 16:08:25 -07001031 String reason = info.getReason();
1032 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001033
Robert Greenwalt42acef32009-08-12 16:08:25 -07001034 if (DBG) {
1035 String reasonText;
1036 if (reason == null) {
1037 reasonText = ".";
1038 } else {
1039 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001041 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001042 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001044
1045 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001046 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001047 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1048 if (getActiveNetworkInfo() == null) {
1049 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1050 }
1051 if (reason != null) {
1052 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1053 }
1054 if (extraInfo != null) {
1055 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1056 }
1057 if (info.isFailover()) {
1058 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1059 info.setFailover(false);
1060 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001061
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001062 NetworkStateTracker newNet = null;
1063 if (mNetAttributes[info.getType()].isDefault()) {
1064 newNet = tryFailover(info.getType());
1065 if (newNet != null) {
1066 NetworkInfo switchTo = newNet.getNetworkInfo();
1067 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1068 } else {
1069 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1070 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001071 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001072
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001073 // do this before we broadcast the change
1074 handleConnectivityChange();
1075
Mike Lockwood0f79b542009-08-14 14:18:49 -04001076 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001077 /*
1078 * If the failover network is already connected, then immediately send
1079 * out a followup broadcast indicating successful failover
1080 */
1081 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1082 sendConnectedBroadcast(newNet.getNetworkInfo());
1083 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001084 }
1085
1086 private void sendStickyBroadcast(Intent intent) {
1087 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001088 if (!mSystemReady) {
1089 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001090 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001091 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1092 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001093 }
1094 }
1095
1096 void systemReady() {
1097 synchronized(this) {
1098 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001099 if (mInitialBroadcast != null) {
1100 mContext.sendStickyBroadcast(mInitialBroadcast);
1101 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001102 }
1103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105
1106 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001107 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108
1109 // snapshot isFailover, because sendConnectedBroadcast() resets it
1110 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001111 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
Robert Greenwalt42acef32009-08-12 16:08:25 -07001113 // if this is a default net and other default is running
1114 // kill the one not preferred
1115 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001116 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1117 if ((type != mNetworkPreference &&
1118 mNetAttributes[mActiveDefaultNetwork].mPriority >
1119 mNetAttributes[type].mPriority) ||
1120 mNetworkPreference == mActiveDefaultNetwork) {
1121 // don't accept this one
Joe Onorato8a9b2202010-02-26 18:56:32 -08001122 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001123 "to torn down network " + info.getTypeName());
1124 teardown(thisNet);
1125 return;
1126 } else {
1127 // tear down the other
1128 NetworkStateTracker otherNet =
1129 mNetTrackers[mActiveDefaultNetwork];
Joe Onorato8a9b2202010-02-26 18:56:32 -08001130 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001131 otherNet.getNetworkInfo().getTypeName() +
1132 " teardown");
1133 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001135 return;
1136 }
1137 if (isFailover) {
1138 otherNet.releaseWakeLock();
1139 }
1140 }
1141 }
1142 mActiveDefaultNetwork = type;
1143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001145 thisNet.updateNetworkSettings();
1146 handleConnectivityChange();
1147 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149
1150 private void handleScanResultsAvailable(NetworkInfo info) {
1151 int networkType = info.getType();
1152 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001153 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001154 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 mNetTrackers[networkType].interpretScanResultsAvailable();
1158 }
1159
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001160 private void handleNotificationChange(boolean visible, int id,
1161 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 NotificationManager notificationManager = (NotificationManager) mContext
1163 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (visible) {
1166 notificationManager.notify(id, notification);
1167 } else {
1168 notificationManager.cancel(id);
1169 }
1170 }
1171
1172 /**
1173 * After any kind of change in the connectivity state of any network,
1174 * make sure that anything that depends on the connectivity state of
1175 * more than one network is set up correctly. We're mainly concerned
1176 * with making sure that the list of DNS servers is set up according
1177 * to which networks are connected, and ensuring that the right routing
1178 * table entries exist.
1179 */
1180 private void handleConnectivityChange() {
1181 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001182 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001183 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 * If both mobile and wifi are enabled, add the host routes that
1185 * will allow MMS traffic to pass on the mobile network. But
1186 * remove the default route for the mobile network, so that there
1187 * will be only one default route, to ensure that all traffic
1188 * except MMS will travel via Wi-Fi.
1189 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001190 handleDnsConfigurationChange();
1191
1192 for (int netType : mPriorityList) {
1193 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1194 if (mNetAttributes[netType].isDefault()) {
1195 mNetTrackers[netType].addDefaultRoute();
1196 } else {
1197 mNetTrackers[netType].addPrivateDnsRoutes();
1198 }
1199 } else {
1200 if (mNetAttributes[netType].isDefault()) {
1201 mNetTrackers[netType].removeDefaultRoute();
1202 } else {
1203 mNetTrackers[netType].removePrivateDnsRoutes();
1204 }
1205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207 }
1208
Robert Greenwalt42acef32009-08-12 16:08:25 -07001209 /**
1210 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1211 * on the highest priority active net which this process requested.
1212 * If there aren't any, clear it out
1213 */
1214 private void reassessPidDns(int myPid, boolean doBump)
1215 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001216 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001217 for(int i : mPriorityList) {
1218 if (mNetAttributes[i].isDefault()) {
1219 continue;
1220 }
1221 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001222 if (nt.getNetworkInfo().isConnected() &&
1223 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001224 List pids = mNetRequestersPids[i];
1225 for (int j=0; j<pids.size(); j++) {
1226 Integer pid = (Integer)pids.get(j);
1227 if (pid.intValue() == myPid) {
1228 String[] dnsList = nt.getNameServers();
1229 writePidDns(dnsList, myPid);
1230 if (doBump) {
1231 bumpDns();
1232 }
1233 return;
1234 }
1235 }
1236 }
1237 }
1238 // nothing found - delete
1239 for (int i = 1; ; i++) {
1240 String prop = "net.dns" + i + "." + myPid;
1241 if (SystemProperties.get(prop).length() == 0) {
1242 if (doBump) {
1243 bumpDns();
1244 }
1245 return;
1246 }
1247 SystemProperties.set(prop, "");
1248 }
1249 }
1250
1251 private void writePidDns(String[] dnsList, int pid) {
1252 int j = 1;
1253 for (String dns : dnsList) {
1254 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1255 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1256 }
1257 }
1258 }
1259
1260 private void bumpDns() {
1261 /*
1262 * Bump the property that tells the name resolver library to reread
1263 * the DNS server list from the properties.
1264 */
1265 String propVal = SystemProperties.get("net.dnschange");
1266 int n = 0;
1267 if (propVal.length() != 0) {
1268 try {
1269 n = Integer.parseInt(propVal);
1270 } catch (NumberFormatException e) {}
1271 }
1272 SystemProperties.set("net.dnschange", "" + (n+1));
1273 }
1274
1275 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001276 // add default net's dns entries
1277 for (int x = mPriorityList.length-1; x>= 0; x--) {
1278 int netType = mPriorityList[x];
1279 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001280 if (nt != null && nt.getNetworkInfo().isConnected() &&
1281 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001283 if (mNetAttributes[netType].isDefault()) {
1284 int j = 1;
1285 for (String dns : dnsList) {
1286 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001287 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 Slog.d(TAG, "adding dns " + dns + " for " +
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001289 nt.getNetworkInfo().getTypeName());
1290 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001291 SystemProperties.set("net.dns" + j++, dns);
1292 }
1293 }
1294 for (int k=j ; k<mNumDnsEntries; k++) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001296 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001297 }
1298 mNumDnsEntries = j;
1299 } else {
1300 // set per-pid dns for attached secondary nets
1301 List pids = mNetRequestersPids[netType];
1302 for (int y=0; y< pids.size(); y++) {
1303 Integer pid = (Integer)pids.get(y);
1304 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306 }
1307 }
1308 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001309
1310 bumpDns();
1311 }
1312
1313 private int getRestoreDefaultNetworkDelay() {
1314 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1315 NETWORK_RESTORE_DELAY_PROP_NAME);
1316 if(restoreDefaultNetworkDelayStr != null &&
1317 restoreDefaultNetworkDelayStr.length() != 0) {
1318 try {
1319 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1320 } catch (NumberFormatException e) {
1321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001323 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325
1326 @Override
1327 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001328 if (mContext.checkCallingOrSelfPermission(
1329 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001331 pw.println("Permission Denial: can't dump ConnectivityService " +
1332 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1333 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 return;
1335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 pw.println();
1337 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001338 if (nst != null) {
1339 if (nst.getNetworkInfo().isConnected()) {
1340 pw.println("Active network: " + nst.getNetworkInfo().
1341 getTypeName());
1342 }
1343 pw.println(nst.getNetworkInfo());
1344 pw.println(nst);
1345 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001348
1349 pw.println("Network Requester Pids:");
1350 for (int net : mPriorityList) {
1351 String pidString = net + ": ";
1352 for (Object pid : mNetRequestersPids[net]) {
1353 pidString = pidString + pid.toString() + ", ";
1354 }
1355 pw.println(pidString);
1356 }
1357 pw.println();
1358
1359 pw.println("FeatureUsers:");
1360 for (Object requester : mFeatureUsers) {
1361 pw.println(requester.toString());
1362 }
1363 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001364
1365 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
1367
Robert Greenwalt42acef32009-08-12 16:08:25 -07001368 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 private class MyHandler extends Handler {
1370 @Override
1371 public void handleMessage(Message msg) {
1372 NetworkInfo info;
1373 switch (msg.what) {
1374 case NetworkStateTracker.EVENT_STATE_CHANGED:
1375 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001376 int type = info.getType();
1377 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001378 // only do this optimization for wifi. It going into scan mode for location
1379 // services generates alot of noise. Meanwhile the mms apn won't send out
1380 // subsequent notifications when on default cellular because it never
1381 // disconnects.. so only do this to wifi notifications. Fixed better when the
1382 // APN notifications are standardized.
1383 if (mNetAttributes[type].mLastState == state &&
1384 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001385 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001386 // TODO - remove this after we validate the dropping doesn't break
1387 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001389 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001390 state + "/" + info.getDetailedState());
1391 }
1392 return;
1393 }
1394 mNetAttributes[type].mLastState = state;
1395
Joe Onorato8a9b2202010-02-26 18:56:32 -08001396 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001397 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001398 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399
1400 // Connectivity state changed:
1401 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001402 // [12-9] Network subtype (for mobile network, as defined
1403 // by TelephonyManager)
1404 // [8-3] Detailed state ordinal (as defined by
1405 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // [2-0] Network type (as defined by ConnectivityManager)
1407 int eventLogParam = (info.getType() & 0x7) |
1408 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1409 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001411 eventLogParam);
1412
1413 if (info.getDetailedState() ==
1414 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001416 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001418 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001420 // the logic here is, handle SUSPENDED the same as
1421 // DISCONNECTED. The only difference being we are
1422 // broadcasting an intent with NetworkInfo that's
1423 // suspended. This allows the applications an
1424 // opportunity to handle DISCONNECTED and SUSPENDED
1425 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001427 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 handleConnect(info);
1429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 break;
1431
1432 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1433 info = (NetworkInfo) msg.obj;
1434 handleScanResultsAvailable(info);
1435 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001438 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1439 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440
1441 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001442 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 break;
1444
1445 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1446 // fill me in
1447 break;
1448
1449 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1450 // fill me in
1451 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001452 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001453 FeatureUser u = (FeatureUser)msg.obj;
1454 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001455 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 }
1458 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001459
1460 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001461 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001462 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001463
1464 if (isTetheringSupported()) {
1465 return mTethering.tether(iface);
1466 } else {
1467 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1468 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001469 }
1470
1471 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001472 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001473 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001474
1475 if (isTetheringSupported()) {
1476 return mTethering.untether(iface);
1477 } else {
1478 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1479 }
1480 }
1481
1482 // javadoc from interface
1483 public int getLastTetherError(String iface) {
1484 enforceTetherAccessPermission();
1485
1486 if (isTetheringSupported()) {
1487 return mTethering.getLastTetherError(iface);
1488 } else {
1489 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1490 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001491 }
1492
1493 // TODO - proper iface API for selection by property, inspection, etc
1494 public String[] getTetherableUsbRegexs() {
1495 enforceTetherAccessPermission();
1496 if (isTetheringSupported()) {
1497 return mTethering.getTetherableUsbRegexs();
1498 } else {
1499 return new String[0];
1500 }
1501 }
1502
1503 public String[] getTetherableWifiRegexs() {
1504 enforceTetherAccessPermission();
1505 if (isTetheringSupported()) {
1506 return mTethering.getTetherableWifiRegexs();
1507 } else {
1508 return new String[0];
1509 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001510 }
1511
1512 // TODO - move iface listing, queries, etc to new module
1513 // javadoc from interface
1514 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001515 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001516 return mTethering.getTetherableIfaces();
1517 }
1518
1519 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001520 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001521 return mTethering.getTetheredIfaces();
1522 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001523
Robert Greenwalt5a735062010-03-02 17:25:02 -08001524 public String[] getTetheringErroredIfaces() {
1525 enforceTetherAccessPermission();
1526 return mTethering.getErroredIfaces();
1527 }
1528
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001529 // if ro.tether.denied = true we default to no tethering
1530 // gservices could set the secure setting to 1 though to enable it on a build where it
1531 // had previously been turned off.
1532 public boolean isTetheringSupported() {
1533 enforceTetherAccessPermission();
1534 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001535 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1536 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1537 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539}