blob: 1ec8d0a6c9c17a783fe0b2b39da4c6886d26ff31 [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 Greenwalt02648a42010-05-18 10:52:51 -0700600 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700601 }
602 }
603 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700606 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700608 enforceChangePermission();
609
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700610 int pid = getCallingPid();
611 int uid = getCallingUid();
612
613 FeatureUser u = null;
614 boolean found = false;
615
616 synchronized(this) {
617 for (int i = 0; i < mFeatureUsers.size() ; i++) {
618 u = (FeatureUser)mFeatureUsers.get(i);
619 if (uid == u.mUid && pid == u.mPid &&
620 networkType == u.mNetworkType &&
621 TextUtils.equals(feature, u.mFeature)) {
622 found = true;
623 break;
624 }
625 }
626 }
627 if (found && u != null) {
628 // stop regardless of how many other time this proc had called start
629 return stopUsingNetworkFeature(u, true);
630 } else {
631 // none found!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800632 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700633 return 1;
634 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700635 }
636
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700637 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
638 int networkType = u.mNetworkType;
639 String feature = u.mFeature;
640 int pid = u.mPid;
641 int uid = u.mUid;
642
643 NetworkStateTracker tracker = null;
644 boolean callTeardown = false; // used to carry our decision outside of sync block
645
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800647 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700648 ": " + feature);
649 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
652 return -1;
653 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700654
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700655 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
656 // sync block
657 synchronized(this) {
658 // check if this process still has an outstanding start request
659 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800660 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700661 return 1;
662 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700663 u.unlinkDeathRecipient();
664 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
665 // If we care about duplicate requests, check for that here.
666 //
667 // This is done to support the extension of a request - the app
668 // can request we start the network feature again and renew the
669 // auto-shutoff delay. Normal "stop" calls from the app though
670 // do not pay attention to duplicate requests - in effect the
671 // API does not refcount and a single stop will counter multiple starts.
672 if (ignoreDups == false) {
673 for (int i = 0; i < mFeatureUsers.size() ; i++) {
674 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
675 if (x.mUid == u.mUid && x.mPid == u.mPid &&
676 x.mNetworkType == u.mNetworkType &&
677 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800678 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700679 return 1;
680 }
681 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700682 }
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700683
684 // TODO - move to MobileDataStateTracker
685 int usedNetworkType = networkType;
686 if (networkType == ConnectivityManager.TYPE_MOBILE) {
687 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
688 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
689 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
690 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
691 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
692 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
693 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
694 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
695 }
696 }
697 tracker = mNetTrackers[usedNetworkType];
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700698 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800699 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700700 return -1;
701 }
702 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700703 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700704 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800705 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700706 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800707 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700708 "others still using it");
709 return 1;
710 }
711 callTeardown = true;
712 }
713 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800714 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700715 if (callTeardown) {
716 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700717 return 1;
718 } else {
Robert Greenwalt02648a42010-05-18 10:52:51 -0700719 return -1;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
723 /**
724 * Ensure that a network route exists to deliver traffic to the specified
725 * host via the specified network interface.
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700726 * @param networkType the type of the network over which traffic to the
727 * specified host is to be routed
728 * @param hostAddress the IP address of the host to which the route is
729 * desired
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 * @return {@code true} on success, {@code false} on failure
731 */
732 public boolean requestRouteToHost(int networkType, int hostAddress) {
733 enforceChangePermission();
734 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
735 return false;
736 }
737 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700738
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700739 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
740 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700741 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700743 }
744 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700746 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
749 /**
750 * @see ConnectivityManager#getBackgroundDataSetting()
751 */
752 public boolean getBackgroundDataSetting() {
753 return Settings.Secure.getInt(mContext.getContentResolver(),
754 Settings.Secure.BACKGROUND_DATA, 1) == 1;
755 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 /**
758 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
759 */
760 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
761 mContext.enforceCallingOrSelfPermission(
762 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
763 "ConnectivityService");
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
766
767 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700768 Settings.Secure.BACKGROUND_DATA,
769 allowBackgroundDataUsage ? 1 : 0);
770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 Intent broadcast = new Intent(
772 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
773 mContext.sendBroadcast(broadcast);
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700774 }
775
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800776 /**
777 * @see ConnectivityManager#getMobileDataEnabled()
778 */
779 public boolean getMobileDataEnabled() {
780 enforceAccessPermission();
781 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
782 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800784 return retVal;
785 }
786
787 /**
788 * @see ConnectivityManager#setMobileDataEnabled(boolean)
789 */
790 public synchronized void setMobileDataEnabled(boolean enabled) {
791 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800793
794 if (getMobileDataEnabled() == enabled) return;
795
796 Settings.Secure.putInt(mContext.getContentResolver(),
797 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
798
799 if (enabled) {
800 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800802 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
803 }
804 } else {
805 for (NetworkStateTracker nt : mNetTrackers) {
806 if (nt == null) continue;
807 int netType = nt.getNetworkInfo().getType();
808 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800810 nt.teardown();
811 }
812 }
813 }
814 }
815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 private int getNumConnectedNetworks() {
817 int numConnectedNets = 0;
818
819 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700820 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700821 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 ++numConnectedNets;
823 }
824 }
825 return numConnectedNets;
826 }
827
828 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700829 mContext.enforceCallingOrSelfPermission(
830 android.Manifest.permission.ACCESS_NETWORK_STATE,
831 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833
834 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700835 mContext.enforceCallingOrSelfPermission(
836 android.Manifest.permission.CHANGE_NETWORK_STATE,
837 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
839
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800840 // TODO Make this a special check when it goes public
841 private void enforceTetherChangePermission() {
842 mContext.enforceCallingOrSelfPermission(
843 android.Manifest.permission.CHANGE_NETWORK_STATE,
844 "ConnectivityService");
845 }
846
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800847 private void enforceTetherAccessPermission() {
848 mContext.enforceCallingOrSelfPermission(
849 android.Manifest.permission.ACCESS_NETWORK_STATE,
850 "ConnectivityService");
851 }
852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700854 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
855 * network, we ignore it. If it is for the active network, we send out a
856 * broadcast. But first, we check whether it might be possible to connect
857 * to a different network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 * @param info the {@code NetworkInfo} for the network
859 */
860 private void handleDisconnect(NetworkInfo info) {
861
Robert Greenwalt42acef32009-08-12 16:08:25 -0700862 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863
Robert Greenwalt42acef32009-08-12 16:08:25 -0700864 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 /*
866 * If the disconnected network is not the active one, then don't report
867 * this as a loss of connectivity. What probably happened is that we're
868 * getting the disconnect for a network that we explicitly disabled
869 * in accordance with network preference policies.
870 */
Robert Greenwalt42acef32009-08-12 16:08:25 -0700871 if (!mNetAttributes[prevNetType].isDefault()) {
872 List pids = mNetRequestersPids[prevNetType];
873 for (int i = 0; i<pids.size(); i++) {
874 Integer pid = (Integer)pids.get(i);
875 // will remove them because the net's no longer connected
876 // need to do this now as only now do we know the pids and
877 // can properly null things that are no longer referenced.
878 reassessPidDns(pid.intValue(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800883 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
885 if (info.isFailover()) {
886 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
887 info.setFailover(false);
888 }
889 if (info.getReason() != null) {
890 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
891 }
892 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700893 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
894 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700896
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800897 NetworkStateTracker newNet = null;
898 if (mNetAttributes[prevNetType].isDefault()) {
899 newNet = tryFailover(prevNetType);
900 if (newNet != null) {
901 NetworkInfo switchTo = newNet.getNetworkInfo();
902 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
903 } else {
904 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
905 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800906 }
907 // do this before we broadcast the change
908 handleConnectivityChange();
909
910 sendStickyBroadcast(intent);
911 /*
912 * If the failover network is already connected, then immediately send
913 * out a followup broadcast indicating successful failover
914 */
915 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
916 sendConnectedBroadcast(newNet.getNetworkInfo());
917 }
918 }
919
Robert Greenwaltcc4b4012010-01-25 17:54:29 -0800920 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800921 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700922 /*
923 * If this is a default network, check if other defaults are available
924 * or active
925 */
926 NetworkStateTracker newNet = null;
927 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700928 if (mActiveDefaultNetwork == prevNetType) {
929 mActiveDefaultNetwork = -1;
930 }
931
932 int newType = -1;
933 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -0800934 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800935 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700936 if (checkType == prevNetType) continue;
937 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -0800938 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
939 noMobileData) {
940 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800941 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -0800942 " because Mobile Data Disabled");
943 }
944 continue;
945 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700946 if (mNetAttributes[checkType].isDefault()) {
947 /* TODO - if we have multiple nets we could use
948 * we may want to put more thought into which we choose
949 */
950 if (checkType == mNetworkPreference) {
951 newType = checkType;
952 break;
953 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700954 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700955 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700956 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700960
961 if (newType != -1) {
962 newNet = mNetTrackers[newType];
963 /**
964 * See if the other network is available to fail over to.
965 * If is not available, we enable it anyway, so that it
966 * will be able to connect when it does become available,
967 * but we report a total loss of connectivity rather than
968 * report that we are attempting to fail over.
969 */
970 if (newNet.isAvailable()) {
971 NetworkInfo switchTo = newNet.getNetworkInfo();
972 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -0700973 if (!switchTo.isConnectedOrConnecting() ||
974 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700975 newNet.reconnect();
976 }
977 if (DBG) {
978 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800979 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700980 switchTo.getTypeName());
981 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700983 switchTo.getTypeName());
984 }
985 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700986 } else {
987 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -0800988 newNet = null; // not officially avail.. try anyway, but
989 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -0700990 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700993
Robert Greenwaltda03c4e2010-01-20 19:29:41 -0800994 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996
997 private void sendConnectedBroadcast(NetworkInfo info) {
998 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800999 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1001 if (info.isFailover()) {
1002 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1003 info.setFailover(false);
1004 }
1005 if (info.getReason() != null) {
1006 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1007 }
1008 if (info.getExtraInfo() != null) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001009 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1010 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001012 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014
1015 /**
1016 * Called when an attempt to fail over to another network has failed.
1017 * @param info the {@link NetworkInfo} for the failed network
1018 */
1019 private void handleConnectionFailure(NetworkInfo info) {
1020 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021
Robert Greenwalt42acef32009-08-12 16:08:25 -07001022 String reason = info.getReason();
1023 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001024
Robert Greenwalt42acef32009-08-12 16:08:25 -07001025 if (DBG) {
1026 String reasonText;
1027 if (reason == null) {
1028 reasonText = ".";
1029 } else {
1030 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001032 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001033 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001035
1036 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001037 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001038 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1039 if (getActiveNetworkInfo() == null) {
1040 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1041 }
1042 if (reason != null) {
1043 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1044 }
1045 if (extraInfo != null) {
1046 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1047 }
1048 if (info.isFailover()) {
1049 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1050 info.setFailover(false);
1051 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001052
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001053 NetworkStateTracker newNet = null;
1054 if (mNetAttributes[info.getType()].isDefault()) {
1055 newNet = tryFailover(info.getType());
1056 if (newNet != null) {
1057 NetworkInfo switchTo = newNet.getNetworkInfo();
1058 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1059 } else {
1060 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1061 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001062 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001063
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001064 // do this before we broadcast the change
1065 handleConnectivityChange();
1066
Mike Lockwood0f79b542009-08-14 14:18:49 -04001067 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001068 /*
1069 * If the failover network is already connected, then immediately send
1070 * out a followup broadcast indicating successful failover
1071 */
1072 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1073 sendConnectedBroadcast(newNet.getNetworkInfo());
1074 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001075 }
1076
1077 private void sendStickyBroadcast(Intent intent) {
1078 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001079 if (!mSystemReady) {
1080 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001081 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001082 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1083 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001084 }
1085 }
1086
1087 void systemReady() {
1088 synchronized(this) {
1089 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001090 if (mInitialBroadcast != null) {
1091 mContext.sendStickyBroadcast(mInitialBroadcast);
1092 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001093 }
1094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096
1097 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001098 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099
1100 // snapshot isFailover, because sendConnectedBroadcast() resets it
1101 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001102 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103
Robert Greenwalt42acef32009-08-12 16:08:25 -07001104 // if this is a default net and other default is running
1105 // kill the one not preferred
1106 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001107 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1108 if ((type != mNetworkPreference &&
1109 mNetAttributes[mActiveDefaultNetwork].mPriority >
1110 mNetAttributes[type].mPriority) ||
1111 mNetworkPreference == mActiveDefaultNetwork) {
1112 // don't accept this one
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001114 "to torn down network " + info.getTypeName());
1115 teardown(thisNet);
1116 return;
1117 } else {
1118 // tear down the other
1119 NetworkStateTracker otherNet =
1120 mNetTrackers[mActiveDefaultNetwork];
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001122 otherNet.getNetworkInfo().getTypeName() +
1123 " teardown");
1124 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001126 return;
1127 }
1128 if (isFailover) {
1129 otherNet.releaseWakeLock();
1130 }
1131 }
1132 }
1133 mActiveDefaultNetwork = type;
1134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001136 thisNet.updateNetworkSettings();
1137 handleConnectivityChange();
1138 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140
1141 private void handleScanResultsAvailable(NetworkInfo info) {
1142 int networkType = info.getType();
1143 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001145 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 mNetTrackers[networkType].interpretScanResultsAvailable();
1149 }
1150
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001151 private void handleNotificationChange(boolean visible, int id,
1152 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 NotificationManager notificationManager = (NotificationManager) mContext
1154 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 if (visible) {
1157 notificationManager.notify(id, notification);
1158 } else {
1159 notificationManager.cancel(id);
1160 }
1161 }
1162
1163 /**
1164 * After any kind of change in the connectivity state of any network,
1165 * make sure that anything that depends on the connectivity state of
1166 * more than one network is set up correctly. We're mainly concerned
1167 * with making sure that the list of DNS servers is set up according
1168 * to which networks are connected, and ensuring that the right routing
1169 * table entries exist.
1170 */
1171 private void handleConnectivityChange() {
1172 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 * If a non-default network is enabled, add the host routes that
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001174 * will allow it's DNS servers to be accessed. Only
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 * If both mobile and wifi are enabled, add the host routes that
1176 * will allow MMS traffic to pass on the mobile network. But
1177 * remove the default route for the mobile network, so that there
1178 * will be only one default route, to ensure that all traffic
1179 * except MMS will travel via Wi-Fi.
1180 */
Robert Greenwalt42acef32009-08-12 16:08:25 -07001181 handleDnsConfigurationChange();
1182
1183 for (int netType : mPriorityList) {
1184 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1185 if (mNetAttributes[netType].isDefault()) {
1186 mNetTrackers[netType].addDefaultRoute();
1187 } else {
1188 mNetTrackers[netType].addPrivateDnsRoutes();
1189 }
1190 } else {
1191 if (mNetAttributes[netType].isDefault()) {
1192 mNetTrackers[netType].removeDefaultRoute();
1193 } else {
1194 mNetTrackers[netType].removePrivateDnsRoutes();
1195 }
1196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198 }
1199
Robert Greenwalt42acef32009-08-12 16:08:25 -07001200 /**
1201 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1202 * on the highest priority active net which this process requested.
1203 * If there aren't any, clear it out
1204 */
1205 private void reassessPidDns(int myPid, boolean doBump)
1206 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001207 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001208 for(int i : mPriorityList) {
1209 if (mNetAttributes[i].isDefault()) {
1210 continue;
1211 }
1212 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001213 if (nt.getNetworkInfo().isConnected() &&
1214 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001215 List pids = mNetRequestersPids[i];
1216 for (int j=0; j<pids.size(); j++) {
1217 Integer pid = (Integer)pids.get(j);
1218 if (pid.intValue() == myPid) {
1219 String[] dnsList = nt.getNameServers();
1220 writePidDns(dnsList, myPid);
1221 if (doBump) {
1222 bumpDns();
1223 }
1224 return;
1225 }
1226 }
1227 }
1228 }
1229 // nothing found - delete
1230 for (int i = 1; ; i++) {
1231 String prop = "net.dns" + i + "." + myPid;
1232 if (SystemProperties.get(prop).length() == 0) {
1233 if (doBump) {
1234 bumpDns();
1235 }
1236 return;
1237 }
1238 SystemProperties.set(prop, "");
1239 }
1240 }
1241
1242 private void writePidDns(String[] dnsList, int pid) {
1243 int j = 1;
1244 for (String dns : dnsList) {
1245 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1246 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1247 }
1248 }
1249 }
1250
1251 private void bumpDns() {
1252 /*
1253 * Bump the property that tells the name resolver library to reread
1254 * the DNS server list from the properties.
1255 */
1256 String propVal = SystemProperties.get("net.dnschange");
1257 int n = 0;
1258 if (propVal.length() != 0) {
1259 try {
1260 n = Integer.parseInt(propVal);
1261 } catch (NumberFormatException e) {}
1262 }
1263 SystemProperties.set("net.dnschange", "" + (n+1));
1264 }
1265
1266 private void handleDnsConfigurationChange() {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001267 // add default net's dns entries
1268 for (int x = mPriorityList.length-1; x>= 0; x--) {
1269 int netType = mPriorityList[x];
1270 NetworkStateTracker nt = mNetTrackers[netType];
Robert Greenwalt42acef32009-08-12 16:08:25 -07001271 if (nt != null && nt.getNetworkInfo().isConnected() &&
1272 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 String[] dnsList = nt.getNameServers();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001274 if (mNetAttributes[netType].isDefault()) {
1275 int j = 1;
1276 for (String dns : dnsList) {
1277 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001278 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001279 Slog.d(TAG, "adding dns " + dns + " for " +
Robert Greenwalt1ef95f92009-09-30 21:01:30 -07001280 nt.getNetworkInfo().getTypeName());
1281 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001282 SystemProperties.set("net.dns" + j++, dns);
1283 }
1284 }
1285 for (int k=j ; k<mNumDnsEntries; k++) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
Robert Greenwaltb06324a2009-08-25 14:00:10 -07001287 SystemProperties.set("net.dns" + k, "");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001288 }
1289 mNumDnsEntries = j;
1290 } else {
1291 // set per-pid dns for attached secondary nets
1292 List pids = mNetRequestersPids[netType];
1293 for (int y=0; y< pids.size(); y++) {
1294 Integer pid = (Integer)pids.get(y);
1295 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297 }
1298 }
1299 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001300
1301 bumpDns();
1302 }
1303
1304 private int getRestoreDefaultNetworkDelay() {
1305 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1306 NETWORK_RESTORE_DELAY_PROP_NAME);
1307 if(restoreDefaultNetworkDelayStr != null &&
1308 restoreDefaultNetworkDelayStr.length() != 0) {
1309 try {
1310 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1311 } catch (NumberFormatException e) {
1312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001314 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316
1317 @Override
1318 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001319 if (mContext.checkCallingOrSelfPermission(
1320 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001322 pw.println("Permission Denial: can't dump ConnectivityService " +
1323 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1324 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 return;
1326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 pw.println();
1328 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001329 if (nst != null) {
1330 if (nst.getNetworkInfo().isConnected()) {
1331 pw.println("Active network: " + nst.getNetworkInfo().
1332 getTypeName());
1333 }
1334 pw.println(nst.getNetworkInfo());
1335 pw.println(nst);
1336 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001339
1340 pw.println("Network Requester Pids:");
1341 for (int net : mPriorityList) {
1342 String pidString = net + ": ";
1343 for (Object pid : mNetRequestersPids[net]) {
1344 pidString = pidString + pid.toString() + ", ";
1345 }
1346 pw.println(pidString);
1347 }
1348 pw.println();
1349
1350 pw.println("FeatureUsers:");
1351 for (Object requester : mFeatureUsers) {
1352 pw.println(requester.toString());
1353 }
1354 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001355
1356 mTethering.dump(fd, pw, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358
Robert Greenwalt42acef32009-08-12 16:08:25 -07001359 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 private class MyHandler extends Handler {
1361 @Override
1362 public void handleMessage(Message msg) {
1363 NetworkInfo info;
1364 switch (msg.what) {
1365 case NetworkStateTracker.EVENT_STATE_CHANGED:
1366 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001367 int type = info.getType();
1368 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001369 // only do this optimization for wifi. It going into scan mode for location
1370 // services generates alot of noise. Meanwhile the mms apn won't send out
1371 // subsequent notifications when on default cellular because it never
1372 // disconnects.. so only do this to wifi notifications. Fixed better when the
1373 // APN notifications are standardized.
1374 if (mNetAttributes[type].mLastState == state &&
1375 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001376 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001377 // TODO - remove this after we validate the dropping doesn't break
1378 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001380 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001381 state + "/" + info.getDetailedState());
1382 }
1383 return;
1384 }
1385 mNetAttributes[type].mLastState = state;
1386
Joe Onorato8a9b2202010-02-26 18:56:32 -08001387 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001388 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001389 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390
1391 // Connectivity state changed:
1392 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001393 // [12-9] Network subtype (for mobile network, as defined
1394 // by TelephonyManager)
1395 // [8-3] Detailed state ordinal (as defined by
1396 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 // [2-0] Network type (as defined by ConnectivityManager)
1398 int eventLogParam = (info.getType() & 0x7) |
1399 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1400 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001402 eventLogParam);
1403
1404 if (info.getDetailedState() ==
1405 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001407 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001409 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001411 // the logic here is, handle SUSPENDED the same as
1412 // DISCONNECTED. The only difference being we are
1413 // broadcasting an intent with NetworkInfo that's
1414 // suspended. This allows the applications an
1415 // opportunity to handle DISCONNECTED and SUSPENDED
1416 // differently, or not.
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.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 handleConnect(info);
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 break;
1422
1423 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1424 info = (NetworkInfo) msg.obj;
1425 handleScanResultsAvailable(info);
1426 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001429 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1430 (Notification) msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431
1432 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt42acef32009-08-12 16:08:25 -07001433 handleDnsConfigurationChange();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 break;
1435
1436 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1437 // fill me in
1438 break;
1439
1440 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1441 // fill me in
1442 break;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001443 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001444 FeatureUser u = (FeatureUser)msg.obj;
1445 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001446 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 }
1449 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001450
1451 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001452 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001453 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001454
1455 if (isTetheringSupported()) {
1456 return mTethering.tether(iface);
1457 } else {
1458 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1459 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001460 }
1461
1462 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001463 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001464 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001465
1466 if (isTetheringSupported()) {
1467 return mTethering.untether(iface);
1468 } else {
1469 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1470 }
1471 }
1472
1473 // javadoc from interface
1474 public int getLastTetherError(String iface) {
1475 enforceTetherAccessPermission();
1476
1477 if (isTetheringSupported()) {
1478 return mTethering.getLastTetherError(iface);
1479 } else {
1480 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1481 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001482 }
1483
1484 // TODO - proper iface API for selection by property, inspection, etc
1485 public String[] getTetherableUsbRegexs() {
1486 enforceTetherAccessPermission();
1487 if (isTetheringSupported()) {
1488 return mTethering.getTetherableUsbRegexs();
1489 } else {
1490 return new String[0];
1491 }
1492 }
1493
1494 public String[] getTetherableWifiRegexs() {
1495 enforceTetherAccessPermission();
1496 if (isTetheringSupported()) {
1497 return mTethering.getTetherableWifiRegexs();
1498 } else {
1499 return new String[0];
1500 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001501 }
1502
1503 // TODO - move iface listing, queries, etc to new module
1504 // javadoc from interface
1505 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001506 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001507 return mTethering.getTetherableIfaces();
1508 }
1509
1510 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001511 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001512 return mTethering.getTetheredIfaces();
1513 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001514
Robert Greenwalt5a735062010-03-02 17:25:02 -08001515 public String[] getTetheringErroredIfaces() {
1516 enforceTetherAccessPermission();
1517 return mTethering.getErroredIfaces();
1518 }
1519
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001520 // if ro.tether.denied = true we default to no tethering
1521 // gservices could set the secure setting to 1 though to enable it on a build where it
1522 // had previously been turned off.
1523 public boolean isTetheringSupported() {
1524 enforceTetherAccessPermission();
1525 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001526 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1527 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1528 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530}