blob: 4c81380b526cde37765c78cd91bbeb10af0f1c6e [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053030import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.wifi.WifiStateTracker;
32import android.os.Binder;
33import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070034import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080035import android.os.Looper;
36import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070037import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.os.ServiceManager;
39import android.os.SystemProperties;
40import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070041import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080043import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import com.android.internal.telephony.Phone;
46
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080047import com.android.server.connectivity.Tethering;
48
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import java.io.FileDescriptor;
50import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070051import java.util.ArrayList;
52import java.util.List;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053053import java.net.InetAddress;
54import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080055
56/**
57 * @hide
58 */
59public class ConnectivityService extends IConnectivityManager.Stub {
60
Robert Greenwalta25fd712009-10-06 14:12:53 -070061 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080062 private static final String TAG = "ConnectivityService";
63
Robert Greenwalt2034b912009-08-12 16:08:25 -070064 // how long to wait before switching back to a radio's default network
65 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
66 // system property that can override the above value
67 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
68 "android.telephony.apn-restore";
69
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080070
71 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080072 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080073
The Android Open Source Project28527d22009-03-03 19:31:44 -080074 /**
75 * Sometimes we want to refer to the individual network state
76 * trackers separately, and sometimes we just want to treat them
77 * abstractly.
78 */
79 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070080
81 /**
82 * A per Net list of the PID's that requested access to the net
83 * used both as a refcount and for per-PID DNS selection
84 */
85 private List mNetRequestersPids[];
86
Robert Greenwalt2034b912009-08-12 16:08:25 -070087 // priority order of the nettrackers
88 // (excluding dynamically set mNetworkPreference)
89 // TODO - move mNetworkTypePreference into this
90 private int[] mPriorityList;
91
The Android Open Source Project28527d22009-03-03 19:31:44 -080092 private Context mContext;
93 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070094 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080095
96 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080097
98 private boolean mTestMode;
99 private static ConnectivityService sServiceInstance;
100
Robert Greenwalt2034b912009-08-12 16:08:25 -0700101 private Handler mHandler;
102
103 // list of DeathRecipients used to make sure features are turned off when
104 // a process dies
105 private List mFeatureUsers;
106
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400107 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800108 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400109
Robert Greenwalt12c44552009-12-07 11:33:18 -0800110 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700111 /**
112 * Class for holding settings read from resources.
113 */
114 public String mName;
115 public int mType;
116 public int mRadio;
117 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800118 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700119 public NetworkAttributes(String init) {
120 String fragments[] = init.split(",");
121 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700122 mType = Integer.parseInt(fragments[1]);
123 mRadio = Integer.parseInt(fragments[2]);
124 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800125 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700126 }
127 public boolean isDefault() {
128 return (mType == mRadio);
129 }
130 }
131 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700132 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133
Robert Greenwalt12c44552009-12-07 11:33:18 -0800134 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700135 public int mSimultaneity;
136 public int mType;
137 public RadioAttributes(String init) {
138 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700139 mType = Integer.parseInt(fragments[0]);
140 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700141 }
142 }
143 RadioAttributes[] mRadioAttributes;
144
The Android Open Source Project28527d22009-03-03 19:31:44 -0800145 private static class ConnectivityThread extends Thread {
146 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700147
The Android Open Source Project28527d22009-03-03 19:31:44 -0800148 private ConnectivityThread(Context context) {
149 super("ConnectivityThread");
150 mContext = context;
151 }
152
153 @Override
154 public void run() {
155 Looper.prepare();
156 synchronized (this) {
157 sServiceInstance = new ConnectivityService(mContext);
158 notifyAll();
159 }
160 Looper.loop();
161 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700162
The Android Open Source Project28527d22009-03-03 19:31:44 -0800163 public static ConnectivityService getServiceInstance(Context context) {
164 ConnectivityThread thread = new ConnectivityThread(context);
165 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700166
The Android Open Source Project28527d22009-03-03 19:31:44 -0800167 synchronized (thread) {
168 while (sServiceInstance == null) {
169 try {
170 // Wait until sServiceInstance has been initialized.
171 thread.wait();
172 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800173 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700174 "Unexpected InterruptedException while waiting"+
175 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800176 }
177 }
178 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700179
The Android Open Source Project28527d22009-03-03 19:31:44 -0800180 return sServiceInstance;
181 }
182 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700183
The Android Open Source Project28527d22009-03-03 19:31:44 -0800184 public static ConnectivityService getInstance(Context context) {
185 return ConnectivityThread.getServiceInstance(context);
186 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700187
The Android Open Source Project28527d22009-03-03 19:31:44 -0800188 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800189 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800190
191 // setup our unique device name
192 String id = Settings.Secure.getString(context.getContentResolver(),
193 Settings.Secure.ANDROID_ID);
194 if (id != null && id.length() > 0) {
195 String name = new String("android_").concat(id);
196 SystemProperties.set("net.hostname", name);
197 }
198
The Android Open Source Project28527d22009-03-03 19:31:44 -0800199 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700200 mNetTrackers = new NetworkStateTracker[
201 ConnectivityManager.MAX_NETWORK_TYPE+1];
202 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700203
The Android Open Source Project28527d22009-03-03 19:31:44 -0800204 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700205
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700206 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
207 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
208
Robert Greenwalt2034b912009-08-12 16:08:25 -0700209 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700210 String[] raStrings = context.getResources().getStringArray(
211 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700212 for (String raString : raStrings) {
213 RadioAttributes r = new RadioAttributes(raString);
214 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800215 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700216 continue;
217 }
218 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800219 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700220 r.mType);
221 continue;
222 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700223 mRadioAttributes[r.mType] = r;
224 }
225
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700226 String[] naStrings = context.getResources().getStringArray(
227 com.android.internal.R.array.networkAttributes);
228 for (String naString : naStrings) {
229 try {
230 NetworkAttributes n = new NetworkAttributes(naString);
231 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800232 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700233 n.mType);
234 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700235 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700236 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800237 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700238 n.mType);
239 continue;
240 }
241 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800242 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700243 "radio " + n.mRadio + " in network type " + n.mType);
244 continue;
245 }
246 mNetAttributes[n.mType] = n;
247 mNetworksDefined++;
248 } catch(Exception e) {
249 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700250 }
251 }
252
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700253 // high priority first
254 mPriorityList = new int[mNetworksDefined];
255 {
256 int insertionPoint = mNetworksDefined-1;
257 int currentLowest = 0;
258 int nextLowest = 0;
259 while (insertionPoint > -1) {
260 for (NetworkAttributes na : mNetAttributes) {
261 if (na == null) continue;
262 if (na.mPriority < currentLowest) continue;
263 if (na.mPriority > currentLowest) {
264 if (na.mPriority < nextLowest || nextLowest == 0) {
265 nextLowest = na.mPriority;
266 }
267 continue;
268 }
269 mPriorityList[insertionPoint--] = na.mType;
270 }
271 currentLowest = nextLowest;
272 nextLowest = 0;
273 }
274 }
275
276 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
277 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700278 mNetRequestersPids[i] = new ArrayList();
279 }
280
281 mFeatureUsers = new ArrayList();
282
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700283 mNumDnsEntries = 0;
284
285 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
286 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800287 /*
288 * Create the network state trackers for Wi-Fi and mobile
289 * data. Maybe this could be done with a factory class,
290 * but it's not clear that it's worth it, given that
291 * the number of different network types is not going
292 * to change very often.
293 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800294 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700295 for (int netType : mPriorityList) {
296 switch (mNetAttributes[netType].mRadio) {
297 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800298 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700299 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
300 WifiService wifiService = new WifiService(context, wst);
301 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800302 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
304 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800305
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700306 break;
307 case ConnectivityManager.TYPE_MOBILE:
308 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
309 netType, mNetAttributes[netType].mName);
310 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800311 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800312 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800313 mNetTrackers[netType].teardown();
314 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 break;
316 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800317 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700318 mNetAttributes[netType].mRadio);
319 continue;
320 }
321 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800322
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800323 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800324 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
325 !mTethering.isDunRequired()) &&
326 (mTethering.getTetherableUsbRegexs().length != 0 ||
327 mTethering.getTetherableWifiRegexs().length != 0) &&
328 mTethering.getUpstreamIfaceRegexs().length != 0);
329
The Android Open Source Project28527d22009-03-03 19:31:44 -0800330 }
331
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700332
The Android Open Source Project28527d22009-03-03 19:31:44 -0800333 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700334 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800335 * @param preference the new preference
336 */
337 public synchronized void setNetworkPreference(int preference) {
338 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700339 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700340 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700341 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800342 if (mNetworkPreference != preference) {
343 persistNetworkPreference(preference);
344 mNetworkPreference = preference;
345 enforcePreference();
346 }
347 }
348 }
349
350 public int getNetworkPreference() {
351 enforceAccessPermission();
352 return mNetworkPreference;
353 }
354
355 private void persistNetworkPreference(int networkPreference) {
356 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700357 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
358 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800359 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700360
The Android Open Source Project28527d22009-03-03 19:31:44 -0800361 private int getPersistedNetworkPreference() {
362 final ContentResolver cr = mContext.getContentResolver();
363
364 final int networkPrefSetting = Settings.Secure
365 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
366 if (networkPrefSetting != -1) {
367 return networkPrefSetting;
368 }
369
370 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
371 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700372
The Android Open Source Project28527d22009-03-03 19:31:44 -0800373 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700374 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800375 * In this method, we only tear down a non-preferred network. Establishing
376 * a connection to the preferred network is taken care of when we handle
377 * the disconnect event from the non-preferred network
378 * (see {@link #handleDisconnect(NetworkInfo)}).
379 */
380 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700381 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800382 return;
383
Robert Greenwalt2034b912009-08-12 16:08:25 -0700384 if (!mNetTrackers[mNetworkPreference].isAvailable())
385 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800386
Robert Greenwalt2034b912009-08-12 16:08:25 -0700387 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700388 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700389 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700390 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800391 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700392 mNetTrackers[t].getNetworkInfo() +
393 " in enforcePreference");
394 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700395 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800396 }
397 }
398 }
399
400 private boolean teardown(NetworkStateTracker netTracker) {
401 if (netTracker.teardown()) {
402 netTracker.setTeardownRequested(true);
403 return true;
404 } else {
405 return false;
406 }
407 }
408
409 /**
410 * Return NetworkInfo for the active (i.e., connected) network interface.
411 * It is assumed that at most one network is active at a time. If more
412 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700413 * @return the info for the active network, or {@code null} if none is
414 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800415 */
416 public NetworkInfo getActiveNetworkInfo() {
417 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700418 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700419 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700420 continue;
421 }
422 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800423 NetworkInfo info = t.getNetworkInfo();
424 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800425 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700426 "connected default network is not " +
427 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800428 return info;
429 }
430 }
431 return null;
432 }
433
434 public NetworkInfo getNetworkInfo(int networkType) {
435 enforceAccessPermission();
436 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
437 NetworkStateTracker t = mNetTrackers[networkType];
438 if (t != null)
439 return t.getNetworkInfo();
440 }
441 return null;
442 }
443
444 public NetworkInfo[] getAllNetworkInfo() {
445 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700446 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 int i = 0;
448 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700449 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800450 }
451 return result;
452 }
453
454 public boolean setRadios(boolean turnOn) {
455 boolean result = true;
456 enforceChangePermission();
457 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700458 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 }
460 return result;
461 }
462
463 public boolean setRadio(int netType, boolean turnOn) {
464 enforceChangePermission();
465 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
466 return false;
467 }
468 NetworkStateTracker tracker = mNetTrackers[netType];
469 return tracker != null && tracker.setRadio(turnOn);
470 }
471
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700472 /**
473 * Used to notice when the calling process dies so we can self-expire
474 *
475 * Also used to know if the process has cleaned up after itself when
476 * our auto-expire timer goes off. The timer has a link to an object.
477 *
478 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700479 private class FeatureUser implements IBinder.DeathRecipient {
480 int mNetworkType;
481 String mFeature;
482 IBinder mBinder;
483 int mPid;
484 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800485 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700486
487 FeatureUser(int type, String feature, IBinder binder) {
488 super();
489 mNetworkType = type;
490 mFeature = feature;
491 mBinder = binder;
492 mPid = getCallingPid();
493 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800494 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700495
Robert Greenwalt2034b912009-08-12 16:08:25 -0700496 try {
497 mBinder.linkToDeath(this, 0);
498 } catch (RemoteException e) {
499 binderDied();
500 }
501 }
502
503 void unlinkDeathRecipient() {
504 mBinder.unlinkToDeath(this, 0);
505 }
506
507 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800508 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800509 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
510 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700511 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700512 }
513
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700514 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800515 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800516 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
517 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700518 stopUsingNetworkFeature(this, false);
519 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800520
521 public String toString() {
522 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
523 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
524 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700525 }
526
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700527 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700528 public int startUsingNetworkFeature(int networkType, String feature,
529 IBinder binder) {
530 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800531 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700532 ": " + feature);
533 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800534 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700535 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
536 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700537 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800538 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700539
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700540 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700541
542 // TODO - move this into the MobileDataStateTracker
543 int usedNetworkType = networkType;
544 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800545 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800546 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800547 return Phone.APN_TYPE_NOT_AVAILABLE;
548 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
550 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
551 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
552 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
553 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
554 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
555 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
556 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
557 }
558 }
559 NetworkStateTracker network = mNetTrackers[usedNetworkType];
560 if (network != null) {
561 if (usedNetworkType != networkType) {
562 Integer currentPid = new Integer(getCallingPid());
563
564 NetworkStateTracker radio = mNetTrackers[networkType];
565 NetworkInfo ni = network.getNetworkInfo();
566
567 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800568 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700569 return Phone.APN_TYPE_NOT_AVAILABLE;
570 }
571
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700572 synchronized(this) {
573 mFeatureUsers.add(f);
574 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
575 // this gets used for per-pid dns when connected
576 mNetRequestersPids[usedNetworkType].add(currentPid);
577 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700578 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700579 mHandler.sendMessageDelayed(mHandler.obtainMessage(
580 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
581 f), getRestoreDefaultNetworkDelay());
582
Robert Greenwalt2034b912009-08-12 16:08:25 -0700583
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700584 if ((ni.isConnectedOrConnecting() == true) &&
585 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700586 if (ni.isConnected() == true) {
587 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700588 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800589 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700590 return Phone.APN_ALREADY_ACTIVE;
591 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800592 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593 return Phone.APN_REQUEST_STARTED;
594 }
595
596 // check if the radio in play can make another contact
597 // assume if cannot for now
598
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800599 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700600 network.reconnect();
601 return Phone.APN_REQUEST_STARTED;
602 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700603 synchronized(this) {
604 mFeatureUsers.add(f);
605 }
606 mHandler.sendMessageDelayed(mHandler.obtainMessage(
607 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
608 f), getRestoreDefaultNetworkDelay());
609
Robert Greenwalt2034b912009-08-12 16:08:25 -0700610 return network.startUsingNetworkFeature(feature,
611 getCallingPid(), getCallingUid());
612 }
613 }
614 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800615 }
616
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800618 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700619 enforceChangePermission();
620
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700621 int pid = getCallingPid();
622 int uid = getCallingUid();
623
624 FeatureUser u = null;
625 boolean found = false;
626
627 synchronized(this) {
628 for (int i = 0; i < mFeatureUsers.size() ; i++) {
629 u = (FeatureUser)mFeatureUsers.get(i);
630 if (uid == u.mUid && pid == u.mPid &&
631 networkType == u.mNetworkType &&
632 TextUtils.equals(feature, u.mFeature)) {
633 found = true;
634 break;
635 }
636 }
637 }
638 if (found && u != null) {
639 // stop regardless of how many other time this proc had called start
640 return stopUsingNetworkFeature(u, true);
641 } else {
642 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800643 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700644 return 1;
645 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700646 }
647
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700648 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
649 int networkType = u.mNetworkType;
650 String feature = u.mFeature;
651 int pid = u.mPid;
652 int uid = u.mUid;
653
654 NetworkStateTracker tracker = null;
655 boolean callTeardown = false; // used to carry our decision outside of sync block
656
Robert Greenwalt2034b912009-08-12 16:08:25 -0700657 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800658 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659 ": " + feature);
660 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700661
The Android Open Source Project28527d22009-03-03 19:31:44 -0800662 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
663 return -1;
664 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700665
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
667 // sync block
668 synchronized(this) {
669 // check if this process still has an outstanding start request
670 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800671 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700672 return 1;
673 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700674 u.unlinkDeathRecipient();
675 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
676 // If we care about duplicate requests, check for that here.
677 //
678 // This is done to support the extension of a request - the app
679 // can request we start the network feature again and renew the
680 // auto-shutoff delay. Normal "stop" calls from the app though
681 // do not pay attention to duplicate requests - in effect the
682 // API does not refcount and a single stop will counter multiple starts.
683 if (ignoreDups == false) {
684 for (int i = 0; i < mFeatureUsers.size() ; i++) {
685 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
686 if (x.mUid == u.mUid && x.mPid == u.mPid &&
687 x.mNetworkType == u.mNetworkType &&
688 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800689 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700690 return 1;
691 }
692 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700693 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700694
695 // TODO - move to MobileDataStateTracker
696 int usedNetworkType = networkType;
697 if (networkType == ConnectivityManager.TYPE_MOBILE) {
698 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
700 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
701 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
702 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
703 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
704 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
706 }
707 }
708 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700709 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800710 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700711 return -1;
712 }
713 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700714 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700715 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800716 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700717 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800718 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 "others still using it");
720 return 1;
721 }
722 callTeardown = true;
723 }
724 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800725 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700726 if (callTeardown) {
727 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 return 1;
729 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700730 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 return tracker.stopUsingNetworkFeature(feature, pid, uid);
732 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800733 }
734
735 /**
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530736 * @deprecated use requestRouteToHostAddress instead
737 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800738 * Ensure that a network route exists to deliver traffic to the specified
739 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700740 * @param networkType the type of the network over which traffic to the
741 * specified host is to be routed
742 * @param hostAddress the IP address of the host to which the route is
743 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800744 * @return {@code true} on success, {@code false} on failure
745 */
746 public boolean requestRouteToHost(int networkType, int hostAddress) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530747 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
748
749 if (inetAddress == null) {
750 return false;
751 }
752
753 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
754 }
755
756 /**
757 * Ensure that a network route exists to deliver traffic to the specified
758 * host via the specified network interface.
759 * @param networkType the type of the network over which traffic to the
760 * specified host is to be routed
761 * @param hostAddress the IP address of the host to which the route is
762 * desired
763 * @return {@code true} on success, {@code false} on failure
764 */
765 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800766 enforceChangePermission();
767 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
768 return false;
769 }
770 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700771
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700772 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
773 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700774 if (DBG) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530775 Slog.d(TAG, "requestRouteToHostAddress on down network " +
776 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700777 }
778 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 }
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530780
781 try {
782 InetAddress inetAddress = InetAddress.getByAddress(hostAddress);
783 return tracker.requestRouteToHost(inetAddress);
784 } catch (UnknownHostException e) {
785 return false;
786 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800787 }
788
789 /**
790 * @see ConnectivityManager#getBackgroundDataSetting()
791 */
792 public boolean getBackgroundDataSetting() {
793 return Settings.Secure.getInt(mContext.getContentResolver(),
794 Settings.Secure.BACKGROUND_DATA, 1) == 1;
795 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700796
The Android Open Source Project28527d22009-03-03 19:31:44 -0800797 /**
798 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
799 */
800 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
801 mContext.enforceCallingOrSelfPermission(
802 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
803 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700804
The Android Open Source Project28527d22009-03-03 19:31:44 -0800805 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
806
807 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700808 Settings.Secure.BACKGROUND_DATA,
809 allowBackgroundDataUsage ? 1 : 0);
810
The Android Open Source Project28527d22009-03-03 19:31:44 -0800811 Intent broadcast = new Intent(
812 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
813 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700814 }
815
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800816 /**
817 * @see ConnectivityManager#getMobileDataEnabled()
818 */
819 public boolean getMobileDataEnabled() {
820 enforceAccessPermission();
821 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
822 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800823 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800824 return retVal;
825 }
826
827 /**
828 * @see ConnectivityManager#setMobileDataEnabled(boolean)
829 */
830 public synchronized void setMobileDataEnabled(boolean enabled) {
831 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800832 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800833
834 if (getMobileDataEnabled() == enabled) return;
835
836 Settings.Secure.putInt(mContext.getContentResolver(),
837 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
838
839 if (enabled) {
840 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800841 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800842 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
843 }
844 } else {
845 for (NetworkStateTracker nt : mNetTrackers) {
846 if (nt == null) continue;
847 int netType = nt.getNetworkInfo().getType();
848 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800849 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800850 nt.teardown();
851 }
852 }
853 }
854 }
855
The Android Open Source Project28527d22009-03-03 19:31:44 -0800856 private int getNumConnectedNetworks() {
857 int numConnectedNets = 0;
858
859 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700860 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700861 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800862 ++numConnectedNets;
863 }
864 }
865 return numConnectedNets;
866 }
867
868 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700869 mContext.enforceCallingOrSelfPermission(
870 android.Manifest.permission.ACCESS_NETWORK_STATE,
871 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 }
873
874 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700875 mContext.enforceCallingOrSelfPermission(
876 android.Manifest.permission.CHANGE_NETWORK_STATE,
877 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800878 }
879
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800880 // TODO Make this a special check when it goes public
881 private void enforceTetherChangePermission() {
882 mContext.enforceCallingOrSelfPermission(
883 android.Manifest.permission.CHANGE_NETWORK_STATE,
884 "ConnectivityService");
885 }
886
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800887 private void enforceTetherAccessPermission() {
888 mContext.enforceCallingOrSelfPermission(
889 android.Manifest.permission.ACCESS_NETWORK_STATE,
890 "ConnectivityService");
891 }
892
The Android Open Source Project28527d22009-03-03 19:31:44 -0800893 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700894 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
895 * network, we ignore it. If it is for the active network, we send out a
896 * broadcast. But first, we check whether it might be possible to connect
897 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800898 * @param info the {@code NetworkInfo} for the network
899 */
900 private void handleDisconnect(NetworkInfo info) {
901
Robert Greenwalt2034b912009-08-12 16:08:25 -0700902 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903
Robert Greenwalt2034b912009-08-12 16:08:25 -0700904 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800905 /*
906 * If the disconnected network is not the active one, then don't report
907 * this as a loss of connectivity. What probably happened is that we're
908 * getting the disconnect for a network that we explicitly disabled
909 * in accordance with network preference policies.
910 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700911 if (!mNetAttributes[prevNetType].isDefault()) {
912 List pids = mNetRequestersPids[prevNetType];
913 for (int i = 0; i<pids.size(); i++) {
914 Integer pid = (Integer)pids.get(i);
915 // will remove them because the net's no longer connected
916 // need to do this now as only now do we know the pids and
917 // can properly null things that are no longer referenced.
918 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800919 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800920 }
921
The Android Open Source Project28527d22009-03-03 19:31:44 -0800922 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
923 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
924 if (info.isFailover()) {
925 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
926 info.setFailover(false);
927 }
928 if (info.getReason() != null) {
929 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
930 }
931 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700932 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
933 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800934 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700935
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800936 NetworkStateTracker newNet = null;
937 if (mNetAttributes[prevNetType].isDefault()) {
938 newNet = tryFailover(prevNetType);
939 if (newNet != null) {
940 NetworkInfo switchTo = newNet.getNetworkInfo();
941 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
942 } else {
943 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
944 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800945 }
946 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700947 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800948
949 sendStickyBroadcast(intent);
950 /*
951 * If the failover network is already connected, then immediately send
952 * out a followup broadcast indicating successful failover
953 */
954 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
955 sendConnectedBroadcast(newNet.getNetworkInfo());
956 }
957 }
958
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800959 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800960 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700961 /*
962 * If this is a default network, check if other defaults are available
963 * or active
964 */
965 NetworkStateTracker newNet = null;
966 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700967 if (mActiveDefaultNetwork == prevNetType) {
968 mActiveDefaultNetwork = -1;
969 }
970
971 int newType = -1;
972 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800973 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800974 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700975 if (checkType == prevNetType) continue;
976 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800977 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
978 noMobileData) {
979 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800980 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800981 " because Mobile Data Disabled");
982 }
983 continue;
984 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700985 if (mNetAttributes[checkType].isDefault()) {
986 /* TODO - if we have multiple nets we could use
987 * we may want to put more thought into which we choose
988 */
989 if (checkType == mNetworkPreference) {
990 newType = checkType;
991 break;
992 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700993 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700994 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700995 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800997 }
998 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700999
1000 if (newType != -1) {
1001 newNet = mNetTrackers[newType];
1002 /**
1003 * See if the other network is available to fail over to.
1004 * If is not available, we enable it anyway, so that it
1005 * will be able to connect when it does become available,
1006 * but we report a total loss of connectivity rather than
1007 * report that we are attempting to fail over.
1008 */
1009 if (newNet.isAvailable()) {
1010 NetworkInfo switchTo = newNet.getNetworkInfo();
1011 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001012 if (!switchTo.isConnectedOrConnecting() ||
1013 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001014 newNet.reconnect();
1015 }
1016 if (DBG) {
1017 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001018 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 switchTo.getTypeName());
1020 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001021 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001022 switchTo.getTypeName());
1023 }
1024 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001025 } else {
1026 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001027 newNet = null; // not officially avail.. try anyway, but
1028 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001029 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001030 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001031 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001032
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001033 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001034 }
1035
1036 private void sendConnectedBroadcast(NetworkInfo info) {
1037 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1038 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1039 if (info.isFailover()) {
1040 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1041 info.setFailover(false);
1042 }
1043 if (info.getReason() != null) {
1044 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1045 }
1046 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001047 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1048 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001049 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001050 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001051 }
1052
1053 /**
1054 * Called when an attempt to fail over to another network has failed.
1055 * @param info the {@link NetworkInfo} for the failed network
1056 */
1057 private void handleConnectionFailure(NetworkInfo info) {
1058 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001059
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060 String reason = info.getReason();
1061 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001062
Robert Greenwalt2034b912009-08-12 16:08:25 -07001063 if (DBG) {
1064 String reasonText;
1065 if (reason == null) {
1066 reasonText = ".";
1067 } else {
1068 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001069 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001070 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001071 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001073
1074 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1075 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1076 if (getActiveNetworkInfo() == null) {
1077 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1078 }
1079 if (reason != null) {
1080 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1081 }
1082 if (extraInfo != null) {
1083 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1084 }
1085 if (info.isFailover()) {
1086 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1087 info.setFailover(false);
1088 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001089
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001090 NetworkStateTracker newNet = null;
1091 if (mNetAttributes[info.getType()].isDefault()) {
1092 newNet = tryFailover(info.getType());
1093 if (newNet != null) {
1094 NetworkInfo switchTo = newNet.getNetworkInfo();
1095 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1096 } else {
1097 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1098 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001099 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001100
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001101 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001102 /*
1103 * If the failover network is already connected, then immediately send
1104 * out a followup broadcast indicating successful failover
1105 */
1106 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1107 sendConnectedBroadcast(newNet.getNetworkInfo());
1108 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001109 }
1110
1111 private void sendStickyBroadcast(Intent intent) {
1112 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001113 if (!mSystemReady) {
1114 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001115 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001116 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1117 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001118 }
1119 }
1120
1121 void systemReady() {
1122 synchronized(this) {
1123 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001124 if (mInitialBroadcast != null) {
1125 mContext.sendStickyBroadcast(mInitialBroadcast);
1126 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001127 }
1128 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001129 }
1130
1131 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001132 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001133
1134 // snapshot isFailover, because sendConnectedBroadcast() resets it
1135 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001136 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001137
Robert Greenwalt2034b912009-08-12 16:08:25 -07001138 // if this is a default net and other default is running
1139 // kill the one not preferred
1140 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001141 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1142 if ((type != mNetworkPreference &&
1143 mNetAttributes[mActiveDefaultNetwork].mPriority >
1144 mNetAttributes[type].mPriority) ||
1145 mNetworkPreference == mActiveDefaultNetwork) {
1146 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001147 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001148 "to torn down network " + info.getTypeName());
1149 teardown(thisNet);
1150 return;
1151 } else {
1152 // tear down the other
1153 NetworkStateTracker otherNet =
1154 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001155 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001156 otherNet.getNetworkInfo().getTypeName() +
1157 " teardown");
1158 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001159 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001160 return;
1161 }
1162 if (isFailover) {
1163 otherNet.releaseWakeLock();
1164 }
1165 }
1166 }
1167 mActiveDefaultNetwork = type;
1168 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001169 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001170 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001171 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001172 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173 }
1174
1175 private void handleScanResultsAvailable(NetworkInfo info) {
1176 int networkType = info.getType();
1177 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001178 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001179 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001180 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001181
The Android Open Source Project28527d22009-03-03 19:31:44 -08001182 mNetTrackers[networkType].interpretScanResultsAvailable();
1183 }
1184
Robert Greenwalt0659da32009-07-16 17:21:39 -07001185 private void handleNotificationChange(boolean visible, int id,
1186 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 NotificationManager notificationManager = (NotificationManager) mContext
1188 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001189
The Android Open Source Project28527d22009-03-03 19:31:44 -08001190 if (visible) {
1191 notificationManager.notify(id, notification);
1192 } else {
1193 notificationManager.cancel(id);
1194 }
1195 }
1196
1197 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001198 * After a change in the connectivity state of any network, We're mainly
1199 * concerned with making sure that the list of DNS servers is setupup
1200 * according to which networks are connected, and ensuring that the
1201 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001202 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001203 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001205 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001206 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001207 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001208 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001209
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001210 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1211 if (mNetAttributes[netType].isDefault()) {
1212 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001213 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001214 mNetTrackers[netType].addPrivateDnsRoutes();
1215 }
1216 } else {
1217 if (mNetAttributes[netType].isDefault()) {
1218 mNetTrackers[netType].removeDefaultRoute();
1219 } else {
1220 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001221 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222 }
1223 }
1224
Robert Greenwalt2034b912009-08-12 16:08:25 -07001225 /**
1226 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1227 * on the highest priority active net which this process requested.
1228 * If there aren't any, clear it out
1229 */
1230 private void reassessPidDns(int myPid, boolean doBump)
1231 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001232 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001233 for(int i : mPriorityList) {
1234 if (mNetAttributes[i].isDefault()) {
1235 continue;
1236 }
1237 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001238 if (nt.getNetworkInfo().isConnected() &&
1239 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001240 List pids = mNetRequestersPids[i];
1241 for (int j=0; j<pids.size(); j++) {
1242 Integer pid = (Integer)pids.get(j);
1243 if (pid.intValue() == myPid) {
1244 String[] dnsList = nt.getNameServers();
1245 writePidDns(dnsList, myPid);
1246 if (doBump) {
1247 bumpDns();
1248 }
1249 return;
1250 }
1251 }
1252 }
1253 }
1254 // nothing found - delete
1255 for (int i = 1; ; i++) {
1256 String prop = "net.dns" + i + "." + myPid;
1257 if (SystemProperties.get(prop).length() == 0) {
1258 if (doBump) {
1259 bumpDns();
1260 }
1261 return;
1262 }
1263 SystemProperties.set(prop, "");
1264 }
1265 }
1266
1267 private void writePidDns(String[] dnsList, int pid) {
1268 int j = 1;
1269 for (String dns : dnsList) {
1270 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1271 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1272 }
1273 }
1274 }
1275
1276 private void bumpDns() {
1277 /*
1278 * Bump the property that tells the name resolver library to reread
1279 * the DNS server list from the properties.
1280 */
1281 String propVal = SystemProperties.get("net.dnschange");
1282 int n = 0;
1283 if (propVal.length() != 0) {
1284 try {
1285 n = Integer.parseInt(propVal);
1286 } catch (NumberFormatException e) {}
1287 }
1288 SystemProperties.set("net.dnschange", "" + (n+1));
1289 }
1290
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001291 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001292 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001293 NetworkStateTracker nt = mNetTrackers[netType];
1294 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1295 String[] dnsList = nt.getNameServers();
1296 if (mNetAttributes[netType].isDefault()) {
1297 int j = 1;
1298 for (String dns : dnsList) {
1299 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1300 if (DBG) {
1301 Slog.d(TAG, "adding dns " + dns + " for " +
1302 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001303 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001304 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001305 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001306 }
1307 for (int k=j ; k<mNumDnsEntries; k++) {
1308 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1309 SystemProperties.set("net.dns" + k, "");
1310 }
1311 mNumDnsEntries = j;
1312 } else {
1313 // set per-pid dns for attached secondary nets
1314 List pids = mNetRequestersPids[netType];
1315 for (int y=0; y< pids.size(); y++) {
1316 Integer pid = (Integer)pids.get(y);
1317 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001318 }
1319 }
1320 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 bumpDns();
1322 }
1323
1324 private int getRestoreDefaultNetworkDelay() {
1325 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1326 NETWORK_RESTORE_DELAY_PROP_NAME);
1327 if(restoreDefaultNetworkDelayStr != null &&
1328 restoreDefaultNetworkDelayStr.length() != 0) {
1329 try {
1330 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1331 } catch (NumberFormatException e) {
1332 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001334 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001335 }
1336
1337 @Override
1338 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001339 if (mContext.checkCallingOrSelfPermission(
1340 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001341 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001342 pw.println("Permission Denial: can't dump ConnectivityService " +
1343 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1344 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001345 return;
1346 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001347 pw.println();
1348 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001349 if (nst != null) {
1350 if (nst.getNetworkInfo().isConnected()) {
1351 pw.println("Active network: " + nst.getNetworkInfo().
1352 getTypeName());
1353 }
1354 pw.println(nst.getNetworkInfo());
1355 pw.println(nst);
1356 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001357 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001358 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001359
1360 pw.println("Network Requester Pids:");
1361 for (int net : mPriorityList) {
1362 String pidString = net + ": ";
1363 for (Object pid : mNetRequestersPids[net]) {
1364 pidString = pidString + pid.toString() + ", ";
1365 }
1366 pw.println(pidString);
1367 }
1368 pw.println();
1369
1370 pw.println("FeatureUsers:");
1371 for (Object requester : mFeatureUsers) {
1372 pw.println(requester.toString());
1373 }
1374 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001375
1376 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001377 }
1378
Robert Greenwalt2034b912009-08-12 16:08:25 -07001379 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001380 private class MyHandler extends Handler {
1381 @Override
1382 public void handleMessage(Message msg) {
1383 NetworkInfo info;
1384 switch (msg.what) {
1385 case NetworkStateTracker.EVENT_STATE_CHANGED:
1386 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001387 int type = info.getType();
1388 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001389 // only do this optimization for wifi. It going into scan mode for location
1390 // services generates alot of noise. Meanwhile the mms apn won't send out
1391 // subsequent notifications when on default cellular because it never
1392 // disconnects.. so only do this to wifi notifications. Fixed better when the
1393 // APN notifications are standardized.
1394 if (mNetAttributes[type].mLastState == state &&
1395 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001396 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001397 // TODO - remove this after we validate the dropping doesn't break
1398 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001399 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001400 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001401 state + "/" + info.getDetailedState());
1402 }
1403 return;
1404 }
1405 mNetAttributes[type].mLastState = state;
1406
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001407 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001408 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001409 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410
1411 // Connectivity state changed:
1412 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001413 // [12-9] Network subtype (for mobile network, as defined
1414 // by TelephonyManager)
1415 // [8-3] Detailed state ordinal (as defined by
1416 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001417 // [2-0] Network type (as defined by ConnectivityManager)
1418 int eventLogParam = (info.getType() & 0x7) |
1419 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1420 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001421 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001422 eventLogParam);
1423
1424 if (info.getDetailedState() ==
1425 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001426 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001427 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001428 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001429 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001430 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001431 // the logic here is, handle SUSPENDED the same as
1432 // DISCONNECTED. The only difference being we are
1433 // broadcasting an intent with NetworkInfo that's
1434 // suspended. This allows the applications an
1435 // opportunity to handle DISCONNECTED and SUSPENDED
1436 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001437 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001438 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001439 handleConnect(info);
1440 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001441 break;
1442
1443 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1444 info = (NetworkInfo) msg.obj;
1445 handleScanResultsAvailable(info);
1446 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001447
The Android Open Source Project28527d22009-03-03 19:31:44 -08001448 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001449 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1450 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001451 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001452
1453 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001454 info = (NetworkInfo) msg.obj;
1455 type = info.getType();
1456 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001457 break;
1458
1459 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1460 // fill me in
1461 break;
1462
1463 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1464 // fill me in
1465 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001466 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001467 FeatureUser u = (FeatureUser)msg.obj;
1468 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001469 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001470 }
1471 }
1472 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001473
1474 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001475 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001476 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001477
1478 if (isTetheringSupported()) {
1479 return mTethering.tether(iface);
1480 } else {
1481 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1482 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001483 }
1484
1485 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001486 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001487 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001488
1489 if (isTetheringSupported()) {
1490 return mTethering.untether(iface);
1491 } else {
1492 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1493 }
1494 }
1495
1496 // javadoc from interface
1497 public int getLastTetherError(String iface) {
1498 enforceTetherAccessPermission();
1499
1500 if (isTetheringSupported()) {
1501 return mTethering.getLastTetherError(iface);
1502 } else {
1503 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1504 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001505 }
1506
1507 // TODO - proper iface API for selection by property, inspection, etc
1508 public String[] getTetherableUsbRegexs() {
1509 enforceTetherAccessPermission();
1510 if (isTetheringSupported()) {
1511 return mTethering.getTetherableUsbRegexs();
1512 } else {
1513 return new String[0];
1514 }
1515 }
1516
1517 public String[] getTetherableWifiRegexs() {
1518 enforceTetherAccessPermission();
1519 if (isTetheringSupported()) {
1520 return mTethering.getTetherableWifiRegexs();
1521 } else {
1522 return new String[0];
1523 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001524 }
1525
1526 // TODO - move iface listing, queries, etc to new module
1527 // javadoc from interface
1528 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001529 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001530 return mTethering.getTetherableIfaces();
1531 }
1532
1533 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001534 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001535 return mTethering.getTetheredIfaces();
1536 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001537
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001538 public String[] getTetheringErroredIfaces() {
1539 enforceTetherAccessPermission();
1540 return mTethering.getErroredIfaces();
1541 }
1542
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001543 // if ro.tether.denied = true we default to no tethering
1544 // gservices could set the secure setting to 1 though to enable it on a build where it
1545 // had previously been turned off.
1546 public boolean isTetheringSupported() {
1547 enforceTetherAccessPermission();
1548 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001549 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1550 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1551 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001552 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001553}