blob: 72e7435db674556b8e701c43dcae6b1a1c255297 [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;
Robert Greenwalt986c7412010-09-08 15:24:47 -070095 // 0 is full bad, 100 is full good
96 private int mDefaultInetCondition = 0;
97 private int mDefaultInetConditionPublished = 0;
98 private boolean mInetConditionChangeInFlight = false;
99 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100
101 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800102
103 private boolean mTestMode;
104 private static ConnectivityService sServiceInstance;
105
Robert Greenwalt2034b912009-08-12 16:08:25 -0700106 private Handler mHandler;
107
108 // list of DeathRecipients used to make sure features are turned off when
109 // a process dies
110 private List mFeatureUsers;
111
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400112 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800113 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400114
Robert Greenwalt12c44552009-12-07 11:33:18 -0800115 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 /**
117 * Class for holding settings read from resources.
118 */
119 public String mName;
120 public int mType;
121 public int mRadio;
122 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800123 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700124 public NetworkAttributes(String init) {
125 String fragments[] = init.split(",");
126 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700127 mType = Integer.parseInt(fragments[1]);
128 mRadio = Integer.parseInt(fragments[2]);
129 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800130 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700131 }
132 public boolean isDefault() {
133 return (mType == mRadio);
134 }
135 }
136 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700137 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700138
Robert Greenwalt12c44552009-12-07 11:33:18 -0800139 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700140 public int mSimultaneity;
141 public int mType;
142 public RadioAttributes(String init) {
143 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700144 mType = Integer.parseInt(fragments[0]);
145 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700146 }
147 }
148 RadioAttributes[] mRadioAttributes;
149
The Android Open Source Project28527d22009-03-03 19:31:44 -0800150 private static class ConnectivityThread extends Thread {
151 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700152
The Android Open Source Project28527d22009-03-03 19:31:44 -0800153 private ConnectivityThread(Context context) {
154 super("ConnectivityThread");
155 mContext = context;
156 }
157
158 @Override
159 public void run() {
160 Looper.prepare();
161 synchronized (this) {
162 sServiceInstance = new ConnectivityService(mContext);
163 notifyAll();
164 }
165 Looper.loop();
166 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700167
The Android Open Source Project28527d22009-03-03 19:31:44 -0800168 public static ConnectivityService getServiceInstance(Context context) {
169 ConnectivityThread thread = new ConnectivityThread(context);
170 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700171
The Android Open Source Project28527d22009-03-03 19:31:44 -0800172 synchronized (thread) {
173 while (sServiceInstance == null) {
174 try {
175 // Wait until sServiceInstance has been initialized.
176 thread.wait();
177 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800178 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700179 "Unexpected InterruptedException while waiting"+
180 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 }
182 }
183 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700184
The Android Open Source Project28527d22009-03-03 19:31:44 -0800185 return sServiceInstance;
186 }
187 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700188
The Android Open Source Project28527d22009-03-03 19:31:44 -0800189 public static ConnectivityService getInstance(Context context) {
190 return ConnectivityThread.getServiceInstance(context);
191 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700192
The Android Open Source Project28527d22009-03-03 19:31:44 -0800193 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800194 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800195
196 // setup our unique device name
197 String id = Settings.Secure.getString(context.getContentResolver(),
198 Settings.Secure.ANDROID_ID);
199 if (id != null && id.length() > 0) {
200 String name = new String("android_").concat(id);
201 SystemProperties.set("net.hostname", name);
202 }
203
The Android Open Source Project28527d22009-03-03 19:31:44 -0800204 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700205 mNetTrackers = new NetworkStateTracker[
206 ConnectivityManager.MAX_NETWORK_TYPE+1];
207 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700208
The Android Open Source Project28527d22009-03-03 19:31:44 -0800209 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700210
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700211 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
212 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
213
Robert Greenwalt2034b912009-08-12 16:08:25 -0700214 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700215 String[] raStrings = context.getResources().getStringArray(
216 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700217 for (String raString : raStrings) {
218 RadioAttributes r = new RadioAttributes(raString);
219 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800220 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700221 continue;
222 }
223 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800224 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700225 r.mType);
226 continue;
227 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700228 mRadioAttributes[r.mType] = r;
229 }
230
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700231 String[] naStrings = context.getResources().getStringArray(
232 com.android.internal.R.array.networkAttributes);
233 for (String naString : naStrings) {
234 try {
235 NetworkAttributes n = new NetworkAttributes(naString);
236 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800237 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700238 n.mType);
239 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700240 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700241 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800242 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700243 n.mType);
244 continue;
245 }
246 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800247 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700248 "radio " + n.mRadio + " in network type " + n.mType);
249 continue;
250 }
251 mNetAttributes[n.mType] = n;
252 mNetworksDefined++;
253 } catch(Exception e) {
254 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700255 }
256 }
257
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700258 // high priority first
259 mPriorityList = new int[mNetworksDefined];
260 {
261 int insertionPoint = mNetworksDefined-1;
262 int currentLowest = 0;
263 int nextLowest = 0;
264 while (insertionPoint > -1) {
265 for (NetworkAttributes na : mNetAttributes) {
266 if (na == null) continue;
267 if (na.mPriority < currentLowest) continue;
268 if (na.mPriority > currentLowest) {
269 if (na.mPriority < nextLowest || nextLowest == 0) {
270 nextLowest = na.mPriority;
271 }
272 continue;
273 }
274 mPriorityList[insertionPoint--] = na.mType;
275 }
276 currentLowest = nextLowest;
277 nextLowest = 0;
278 }
279 }
280
281 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
282 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700283 mNetRequestersPids[i] = new ArrayList();
284 }
285
286 mFeatureUsers = new ArrayList();
287
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700288 mNumDnsEntries = 0;
289
290 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
291 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800292 /*
293 * Create the network state trackers for Wi-Fi and mobile
294 * data. Maybe this could be done with a factory class,
295 * but it's not clear that it's worth it, given that
296 * the number of different network types is not going
297 * to change very often.
298 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800299 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700300 for (int netType : mPriorityList) {
301 switch (mNetAttributes[netType].mRadio) {
302 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800303 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700304 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
305 WifiService wifiService = new WifiService(context, wst);
306 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800307 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
309 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800310
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 break;
312 case ConnectivityManager.TYPE_MOBILE:
313 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
314 netType, mNetAttributes[netType].mName);
315 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800316 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800317 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800318 mNetTrackers[netType].teardown();
319 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320 break;
321 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800322 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700323 mNetAttributes[netType].mRadio);
324 continue;
325 }
326 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800327
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800328 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800329 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
330 !mTethering.isDunRequired()) &&
331 (mTethering.getTetherableUsbRegexs().length != 0 ||
332 mTethering.getTetherableWifiRegexs().length != 0) &&
333 mTethering.getUpstreamIfaceRegexs().length != 0);
334
The Android Open Source Project28527d22009-03-03 19:31:44 -0800335 }
336
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337
The Android Open Source Project28527d22009-03-03 19:31:44 -0800338 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700339 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800340 * @param preference the new preference
341 */
342 public synchronized void setNetworkPreference(int preference) {
343 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700344 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700345 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700346 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800347 if (mNetworkPreference != preference) {
348 persistNetworkPreference(preference);
349 mNetworkPreference = preference;
350 enforcePreference();
351 }
352 }
353 }
354
355 public int getNetworkPreference() {
356 enforceAccessPermission();
357 return mNetworkPreference;
358 }
359
360 private void persistNetworkPreference(int networkPreference) {
361 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700362 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
363 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800364 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700365
The Android Open Source Project28527d22009-03-03 19:31:44 -0800366 private int getPersistedNetworkPreference() {
367 final ContentResolver cr = mContext.getContentResolver();
368
369 final int networkPrefSetting = Settings.Secure
370 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
371 if (networkPrefSetting != -1) {
372 return networkPrefSetting;
373 }
374
375 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
376 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700377
The Android Open Source Project28527d22009-03-03 19:31:44 -0800378 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700379 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800380 * In this method, we only tear down a non-preferred network. Establishing
381 * a connection to the preferred network is taken care of when we handle
382 * the disconnect event from the non-preferred network
383 * (see {@link #handleDisconnect(NetworkInfo)}).
384 */
385 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700386 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800387 return;
388
Robert Greenwalt2034b912009-08-12 16:08:25 -0700389 if (!mNetTrackers[mNetworkPreference].isAvailable())
390 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800391
Robert Greenwalt2034b912009-08-12 16:08:25 -0700392 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700393 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700394 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700395 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800396 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700397 mNetTrackers[t].getNetworkInfo() +
398 " in enforcePreference");
399 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700400 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800401 }
402 }
403 }
404
405 private boolean teardown(NetworkStateTracker netTracker) {
406 if (netTracker.teardown()) {
407 netTracker.setTeardownRequested(true);
408 return true;
409 } else {
410 return false;
411 }
412 }
413
414 /**
415 * Return NetworkInfo for the active (i.e., connected) network interface.
416 * It is assumed that at most one network is active at a time. If more
417 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700418 * @return the info for the active network, or {@code null} if none is
419 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800420 */
421 public NetworkInfo getActiveNetworkInfo() {
422 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700423 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700424 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700425 continue;
426 }
427 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800428 NetworkInfo info = t.getNetworkInfo();
429 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800430 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700431 "connected default network is not " +
432 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800433 return info;
434 }
435 }
436 return null;
437 }
438
439 public NetworkInfo getNetworkInfo(int networkType) {
440 enforceAccessPermission();
441 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
442 NetworkStateTracker t = mNetTrackers[networkType];
443 if (t != null)
444 return t.getNetworkInfo();
445 }
446 return null;
447 }
448
449 public NetworkInfo[] getAllNetworkInfo() {
450 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700451 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800452 int i = 0;
453 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700454 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800455 }
456 return result;
457 }
458
459 public boolean setRadios(boolean turnOn) {
460 boolean result = true;
461 enforceChangePermission();
462 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700463 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800464 }
465 return result;
466 }
467
468 public boolean setRadio(int netType, boolean turnOn) {
469 enforceChangePermission();
470 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
471 return false;
472 }
473 NetworkStateTracker tracker = mNetTrackers[netType];
474 return tracker != null && tracker.setRadio(turnOn);
475 }
476
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700477 /**
478 * Used to notice when the calling process dies so we can self-expire
479 *
480 * Also used to know if the process has cleaned up after itself when
481 * our auto-expire timer goes off. The timer has a link to an object.
482 *
483 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700484 private class FeatureUser implements IBinder.DeathRecipient {
485 int mNetworkType;
486 String mFeature;
487 IBinder mBinder;
488 int mPid;
489 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800490 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700491
492 FeatureUser(int type, String feature, IBinder binder) {
493 super();
494 mNetworkType = type;
495 mFeature = feature;
496 mBinder = binder;
497 mPid = getCallingPid();
498 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800499 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700500
Robert Greenwalt2034b912009-08-12 16:08:25 -0700501 try {
502 mBinder.linkToDeath(this, 0);
503 } catch (RemoteException e) {
504 binderDied();
505 }
506 }
507
508 void unlinkDeathRecipient() {
509 mBinder.unlinkToDeath(this, 0);
510 }
511
512 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800513 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800514 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
515 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700516 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700517 }
518
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700519 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800520 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800521 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
522 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700523 stopUsingNetworkFeature(this, false);
524 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800525
526 public String toString() {
527 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
528 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
529 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 }
531
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700532 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700533 public int startUsingNetworkFeature(int networkType, String feature,
534 IBinder binder) {
535 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800536 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700537 ": " + feature);
538 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800539 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700540 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
541 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700542 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800543 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700544
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700545 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700546
547 // TODO - move this into the MobileDataStateTracker
548 int usedNetworkType = networkType;
549 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800550 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800551 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800552 return Phone.APN_TYPE_NOT_AVAILABLE;
553 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700554 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
555 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
556 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
557 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
558 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
559 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
560 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
561 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
562 }
563 }
564 NetworkStateTracker network = mNetTrackers[usedNetworkType];
565 if (network != null) {
566 if (usedNetworkType != networkType) {
567 Integer currentPid = new Integer(getCallingPid());
568
569 NetworkStateTracker radio = mNetTrackers[networkType];
570 NetworkInfo ni = network.getNetworkInfo();
571
572 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800573 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700574 return Phone.APN_TYPE_NOT_AVAILABLE;
575 }
576
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700577 synchronized(this) {
578 mFeatureUsers.add(f);
579 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
580 // this gets used for per-pid dns when connected
581 mNetRequestersPids[usedNetworkType].add(currentPid);
582 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700583 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700584 mHandler.sendMessageDelayed(mHandler.obtainMessage(
585 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
586 f), getRestoreDefaultNetworkDelay());
587
Robert Greenwalt2034b912009-08-12 16:08:25 -0700588
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700589 if ((ni.isConnectedOrConnecting() == true) &&
590 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700591 if (ni.isConnected() == true) {
592 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700593 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800594 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 return Phone.APN_ALREADY_ACTIVE;
596 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800597 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700598 return Phone.APN_REQUEST_STARTED;
599 }
600
601 // check if the radio in play can make another contact
602 // assume if cannot for now
603
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800604 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700605 network.reconnect();
606 return Phone.APN_REQUEST_STARTED;
607 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700608 synchronized(this) {
609 mFeatureUsers.add(f);
610 }
611 mHandler.sendMessageDelayed(mHandler.obtainMessage(
612 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
613 f), getRestoreDefaultNetworkDelay());
614
Robert Greenwalt2034b912009-08-12 16:08:25 -0700615 return network.startUsingNetworkFeature(feature,
616 getCallingPid(), getCallingUid());
617 }
618 }
619 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800620 }
621
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700622 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800623 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700624 enforceChangePermission();
625
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700626 int pid = getCallingPid();
627 int uid = getCallingUid();
628
629 FeatureUser u = null;
630 boolean found = false;
631
632 synchronized(this) {
633 for (int i = 0; i < mFeatureUsers.size() ; i++) {
634 u = (FeatureUser)mFeatureUsers.get(i);
635 if (uid == u.mUid && pid == u.mPid &&
636 networkType == u.mNetworkType &&
637 TextUtils.equals(feature, u.mFeature)) {
638 found = true;
639 break;
640 }
641 }
642 }
643 if (found && u != null) {
644 // stop regardless of how many other time this proc had called start
645 return stopUsingNetworkFeature(u, true);
646 } else {
647 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800648 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700649 return 1;
650 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700651 }
652
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700653 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
654 int networkType = u.mNetworkType;
655 String feature = u.mFeature;
656 int pid = u.mPid;
657 int uid = u.mUid;
658
659 NetworkStateTracker tracker = null;
660 boolean callTeardown = false; // used to carry our decision outside of sync block
661
Robert Greenwalt2034b912009-08-12 16:08:25 -0700662 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800663 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700664 ": " + feature);
665 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700666
The Android Open Source Project28527d22009-03-03 19:31:44 -0800667 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
668 return -1;
669 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700671 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
672 // sync block
673 synchronized(this) {
674 // check if this process still has an outstanding start request
675 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800676 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677 return 1;
678 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700679 u.unlinkDeathRecipient();
680 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
681 // If we care about duplicate requests, check for that here.
682 //
683 // This is done to support the extension of a request - the app
684 // can request we start the network feature again and renew the
685 // auto-shutoff delay. Normal "stop" calls from the app though
686 // do not pay attention to duplicate requests - in effect the
687 // API does not refcount and a single stop will counter multiple starts.
688 if (ignoreDups == false) {
689 for (int i = 0; i < mFeatureUsers.size() ; i++) {
690 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
691 if (x.mUid == u.mUid && x.mPid == u.mPid &&
692 x.mNetworkType == u.mNetworkType &&
693 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800694 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700695 return 1;
696 }
697 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700698 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700699
700 // TODO - move to MobileDataStateTracker
701 int usedNetworkType = networkType;
702 if (networkType == ConnectivityManager.TYPE_MOBILE) {
703 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
704 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
705 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
706 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
707 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
708 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
709 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
710 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
711 }
712 }
713 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700714 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800715 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700716 return -1;
717 }
718 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700720 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800721 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700722 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800723 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700724 "others still using it");
725 return 1;
726 }
727 callTeardown = true;
728 }
729 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800730 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700731 if (callTeardown) {
732 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733 return 1;
734 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700735 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700736 return tracker.stopUsingNetworkFeature(feature, pid, uid);
737 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800738 }
739
740 /**
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530741 * @deprecated use requestRouteToHostAddress instead
742 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800743 * Ensure that a network route exists to deliver traffic to the specified
744 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700745 * @param networkType the type of the network over which traffic to the
746 * specified host is to be routed
747 * @param hostAddress the IP address of the host to which the route is
748 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800749 * @return {@code true} on success, {@code false} on failure
750 */
751 public boolean requestRouteToHost(int networkType, int hostAddress) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530752 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
753
754 if (inetAddress == null) {
755 return false;
756 }
757
758 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
759 }
760
761 /**
762 * Ensure that a network route exists to deliver traffic to the specified
763 * host via the specified network interface.
764 * @param networkType the type of the network over which traffic to the
765 * specified host is to be routed
766 * @param hostAddress the IP address of the host to which the route is
767 * desired
768 * @return {@code true} on success, {@code false} on failure
769 */
770 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800771 enforceChangePermission();
772 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
773 return false;
774 }
775 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700776
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700777 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
778 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700779 if (DBG) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530780 Slog.d(TAG, "requestRouteToHostAddress on down network " +
781 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700782 }
783 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800784 }
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530785
786 try {
787 InetAddress inetAddress = InetAddress.getByAddress(hostAddress);
788 return tracker.requestRouteToHost(inetAddress);
789 } catch (UnknownHostException e) {
790 return false;
791 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800792 }
793
794 /**
795 * @see ConnectivityManager#getBackgroundDataSetting()
796 */
797 public boolean getBackgroundDataSetting() {
798 return Settings.Secure.getInt(mContext.getContentResolver(),
799 Settings.Secure.BACKGROUND_DATA, 1) == 1;
800 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700801
The Android Open Source Project28527d22009-03-03 19:31:44 -0800802 /**
803 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
804 */
805 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
806 mContext.enforceCallingOrSelfPermission(
807 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
808 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700809
The Android Open Source Project28527d22009-03-03 19:31:44 -0800810 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
811
812 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700813 Settings.Secure.BACKGROUND_DATA,
814 allowBackgroundDataUsage ? 1 : 0);
815
The Android Open Source Project28527d22009-03-03 19:31:44 -0800816 Intent broadcast = new Intent(
817 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
818 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700819 }
820
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800821 /**
822 * @see ConnectivityManager#getMobileDataEnabled()
823 */
824 public boolean getMobileDataEnabled() {
825 enforceAccessPermission();
826 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
827 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800828 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800829 return retVal;
830 }
831
832 /**
833 * @see ConnectivityManager#setMobileDataEnabled(boolean)
834 */
835 public synchronized void setMobileDataEnabled(boolean enabled) {
836 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800837 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800838
839 if (getMobileDataEnabled() == enabled) return;
840
841 Settings.Secure.putInt(mContext.getContentResolver(),
842 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
843
844 if (enabled) {
845 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800846 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800847 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
848 }
849 } else {
850 for (NetworkStateTracker nt : mNetTrackers) {
851 if (nt == null) continue;
852 int netType = nt.getNetworkInfo().getType();
853 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800854 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800855 nt.teardown();
856 }
857 }
858 }
859 }
860
The Android Open Source Project28527d22009-03-03 19:31:44 -0800861 private int getNumConnectedNetworks() {
862 int numConnectedNets = 0;
863
864 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700865 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700866 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800867 ++numConnectedNets;
868 }
869 }
870 return numConnectedNets;
871 }
872
873 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700874 mContext.enforceCallingOrSelfPermission(
875 android.Manifest.permission.ACCESS_NETWORK_STATE,
876 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800877 }
878
879 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700880 mContext.enforceCallingOrSelfPermission(
881 android.Manifest.permission.CHANGE_NETWORK_STATE,
882 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800883 }
884
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800885 // TODO Make this a special check when it goes public
886 private void enforceTetherChangePermission() {
887 mContext.enforceCallingOrSelfPermission(
888 android.Manifest.permission.CHANGE_NETWORK_STATE,
889 "ConnectivityService");
890 }
891
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800892 private void enforceTetherAccessPermission() {
893 mContext.enforceCallingOrSelfPermission(
894 android.Manifest.permission.ACCESS_NETWORK_STATE,
895 "ConnectivityService");
896 }
897
The Android Open Source Project28527d22009-03-03 19:31:44 -0800898 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700899 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
900 * network, we ignore it. If it is for the active network, we send out a
901 * broadcast. But first, we check whether it might be possible to connect
902 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903 * @param info the {@code NetworkInfo} for the network
904 */
905 private void handleDisconnect(NetworkInfo info) {
906
Robert Greenwalt2034b912009-08-12 16:08:25 -0700907 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800908
Robert Greenwalt2034b912009-08-12 16:08:25 -0700909 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800910 /*
911 * If the disconnected network is not the active one, then don't report
912 * this as a loss of connectivity. What probably happened is that we're
913 * getting the disconnect for a network that we explicitly disabled
914 * in accordance with network preference policies.
915 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700916 if (!mNetAttributes[prevNetType].isDefault()) {
917 List pids = mNetRequestersPids[prevNetType];
918 for (int i = 0; i<pids.size(); i++) {
919 Integer pid = (Integer)pids.get(i);
920 // will remove them because the net's no longer connected
921 // need to do this now as only now do we know the pids and
922 // can properly null things that are no longer referenced.
923 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800924 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800925 }
926
The Android Open Source Project28527d22009-03-03 19:31:44 -0800927 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
928 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
929 if (info.isFailover()) {
930 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
931 info.setFailover(false);
932 }
933 if (info.getReason() != null) {
934 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
935 }
936 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700937 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
938 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800939 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700940
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800941 NetworkStateTracker newNet = null;
942 if (mNetAttributes[prevNetType].isDefault()) {
943 newNet = tryFailover(prevNetType);
944 if (newNet != null) {
945 NetworkInfo switchTo = newNet.getNetworkInfo();
946 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
947 } else {
948 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
949 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800950 }
951 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700952 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800953
954 sendStickyBroadcast(intent);
955 /*
956 * If the failover network is already connected, then immediately send
957 * out a followup broadcast indicating successful failover
958 */
959 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
960 sendConnectedBroadcast(newNet.getNetworkInfo());
961 }
962 }
963
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800964 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800965 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700966 /*
967 * If this is a default network, check if other defaults are available
968 * or active
969 */
970 NetworkStateTracker newNet = null;
971 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700972 if (mActiveDefaultNetwork == prevNetType) {
973 mActiveDefaultNetwork = -1;
974 }
975
976 int newType = -1;
977 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800978 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800979 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700980 if (checkType == prevNetType) continue;
981 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800982 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
983 noMobileData) {
984 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800985 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800986 " because Mobile Data Disabled");
987 }
988 continue;
989 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700990 if (mNetAttributes[checkType].isDefault()) {
991 /* TODO - if we have multiple nets we could use
992 * we may want to put more thought into which we choose
993 */
994 if (checkType == mNetworkPreference) {
995 newType = checkType;
996 break;
997 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700998 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700999 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001000 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001001 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001002 }
1003 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001004
1005 if (newType != -1) {
1006 newNet = mNetTrackers[newType];
1007 /**
1008 * See if the other network is available to fail over to.
1009 * If is not available, we enable it anyway, so that it
1010 * will be able to connect when it does become available,
1011 * but we report a total loss of connectivity rather than
1012 * report that we are attempting to fail over.
1013 */
1014 if (newNet.isAvailable()) {
1015 NetworkInfo switchTo = newNet.getNetworkInfo();
1016 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001017 if (!switchTo.isConnectedOrConnecting() ||
1018 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 newNet.reconnect();
1020 }
1021 if (DBG) {
1022 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001023 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001024 switchTo.getTypeName());
1025 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001026 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001027 switchTo.getTypeName());
1028 }
1029 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001030 } else {
1031 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001032 newNet = null; // not officially avail.. try anyway, but
1033 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001034 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001035 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001036 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001037
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001038 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001039 }
1040
1041 private void sendConnectedBroadcast(NetworkInfo info) {
1042 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1043 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1044 if (info.isFailover()) {
1045 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1046 info.setFailover(false);
1047 }
1048 if (info.getReason() != null) {
1049 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1050 }
1051 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001052 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1053 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001054 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001055 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001056 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001057 }
1058
1059 /**
1060 * Called when an attempt to fail over to another network has failed.
1061 * @param info the {@link NetworkInfo} for the failed network
1062 */
1063 private void handleConnectionFailure(NetworkInfo info) {
1064 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001065
Robert Greenwalt2034b912009-08-12 16:08:25 -07001066 String reason = info.getReason();
1067 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001068
Robert Greenwalt2034b912009-08-12 16:08:25 -07001069 if (DBG) {
1070 String reasonText;
1071 if (reason == null) {
1072 reasonText = ".";
1073 } else {
1074 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001075 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001076 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001079
1080 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1081 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1082 if (getActiveNetworkInfo() == null) {
1083 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1084 }
1085 if (reason != null) {
1086 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1087 }
1088 if (extraInfo != null) {
1089 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1090 }
1091 if (info.isFailover()) {
1092 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1093 info.setFailover(false);
1094 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001095
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001096 NetworkStateTracker newNet = null;
1097 if (mNetAttributes[info.getType()].isDefault()) {
1098 newNet = tryFailover(info.getType());
1099 if (newNet != null) {
1100 NetworkInfo switchTo = newNet.getNetworkInfo();
1101 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1102 } else {
1103 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1104 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001105 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001106
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001107 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001108 /*
1109 * If the failover network is already connected, then immediately send
1110 * out a followup broadcast indicating successful failover
1111 */
1112 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1113 sendConnectedBroadcast(newNet.getNetworkInfo());
1114 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001115 }
1116
1117 private void sendStickyBroadcast(Intent intent) {
1118 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001119 if (!mSystemReady) {
1120 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001121 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001122 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1123 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001124 }
1125 }
1126
1127 void systemReady() {
1128 synchronized(this) {
1129 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001130 if (mInitialBroadcast != null) {
1131 mContext.sendStickyBroadcast(mInitialBroadcast);
1132 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001133 }
1134 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001135 }
1136
1137 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001138 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001139
1140 // snapshot isFailover, because sendConnectedBroadcast() resets it
1141 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001142 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001143
Robert Greenwalt2034b912009-08-12 16:08:25 -07001144 // if this is a default net and other default is running
1145 // kill the one not preferred
1146 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001147 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1148 if ((type != mNetworkPreference &&
1149 mNetAttributes[mActiveDefaultNetwork].mPriority >
1150 mNetAttributes[type].mPriority) ||
1151 mNetworkPreference == mActiveDefaultNetwork) {
1152 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001153 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001154 "to torn down network " + info.getTypeName());
1155 teardown(thisNet);
1156 return;
1157 } else {
1158 // tear down the other
1159 NetworkStateTracker otherNet =
1160 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001161 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 otherNet.getNetworkInfo().getTypeName() +
1163 " teardown");
1164 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001165 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001166 return;
1167 }
1168 if (isFailover) {
1169 otherNet.releaseWakeLock();
1170 }
1171 }
1172 }
1173 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001174 // this will cause us to come up initially as unconnected and switching
1175 // to connected after our normal pause unless somebody reports us as reall
1176 // disconnected
1177 mDefaultInetConditionPublished = 0;
1178 mDefaultConnectionSequence++;
1179 mInetConditionChangeInFlight = false;
1180 // Don't do this - if we never sign in stay, grey
1181 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001183 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001184 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001185 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001186 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 }
1188
1189 private void handleScanResultsAvailable(NetworkInfo info) {
1190 int networkType = info.getType();
1191 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001192 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001193 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001194 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001195
The Android Open Source Project28527d22009-03-03 19:31:44 -08001196 mNetTrackers[networkType].interpretScanResultsAvailable();
1197 }
1198
Robert Greenwalt0659da32009-07-16 17:21:39 -07001199 private void handleNotificationChange(boolean visible, int id,
1200 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001201 NotificationManager notificationManager = (NotificationManager) mContext
1202 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001203
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 if (visible) {
1205 notificationManager.notify(id, notification);
1206 } else {
1207 notificationManager.cancel(id);
1208 }
1209 }
1210
1211 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001212 * After a change in the connectivity state of any network, We're mainly
1213 * concerned with making sure that the list of DNS servers is setupup
1214 * according to which networks are connected, and ensuring that the
1215 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001216 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001217 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001218 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001219 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001220 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001222 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001223
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001224 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1225 if (mNetAttributes[netType].isDefault()) {
1226 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001227 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001228 mNetTrackers[netType].addPrivateDnsRoutes();
1229 }
1230 } else {
1231 if (mNetAttributes[netType].isDefault()) {
1232 mNetTrackers[netType].removeDefaultRoute();
1233 } else {
1234 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001235 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236 }
1237 }
1238
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 /**
1240 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1241 * on the highest priority active net which this process requested.
1242 * If there aren't any, clear it out
1243 */
1244 private void reassessPidDns(int myPid, boolean doBump)
1245 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001246 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001247 for(int i : mPriorityList) {
1248 if (mNetAttributes[i].isDefault()) {
1249 continue;
1250 }
1251 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001252 if (nt.getNetworkInfo().isConnected() &&
1253 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001254 List pids = mNetRequestersPids[i];
1255 for (int j=0; j<pids.size(); j++) {
1256 Integer pid = (Integer)pids.get(j);
1257 if (pid.intValue() == myPid) {
1258 String[] dnsList = nt.getNameServers();
1259 writePidDns(dnsList, myPid);
1260 if (doBump) {
1261 bumpDns();
1262 }
1263 return;
1264 }
1265 }
1266 }
1267 }
1268 // nothing found - delete
1269 for (int i = 1; ; i++) {
1270 String prop = "net.dns" + i + "." + myPid;
1271 if (SystemProperties.get(prop).length() == 0) {
1272 if (doBump) {
1273 bumpDns();
1274 }
1275 return;
1276 }
1277 SystemProperties.set(prop, "");
1278 }
1279 }
1280
1281 private void writePidDns(String[] dnsList, int pid) {
1282 int j = 1;
1283 for (String dns : dnsList) {
1284 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1285 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1286 }
1287 }
1288 }
1289
1290 private void bumpDns() {
1291 /*
1292 * Bump the property that tells the name resolver library to reread
1293 * the DNS server list from the properties.
1294 */
1295 String propVal = SystemProperties.get("net.dnschange");
1296 int n = 0;
1297 if (propVal.length() != 0) {
1298 try {
1299 n = Integer.parseInt(propVal);
1300 } catch (NumberFormatException e) {}
1301 }
1302 SystemProperties.set("net.dnschange", "" + (n+1));
1303 }
1304
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001305 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001306 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001307 NetworkStateTracker nt = mNetTrackers[netType];
1308 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1309 String[] dnsList = nt.getNameServers();
1310 if (mNetAttributes[netType].isDefault()) {
1311 int j = 1;
1312 for (String dns : dnsList) {
1313 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1314 if (DBG) {
1315 Slog.d(TAG, "adding dns " + dns + " for " +
1316 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001318 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001319 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001320 }
1321 for (int k=j ; k<mNumDnsEntries; k++) {
1322 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1323 SystemProperties.set("net.dns" + k, "");
1324 }
1325 mNumDnsEntries = j;
1326 } else {
1327 // set per-pid dns for attached secondary nets
1328 List pids = mNetRequestersPids[netType];
1329 for (int y=0; y< pids.size(); y++) {
1330 Integer pid = (Integer)pids.get(y);
1331 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001332 }
1333 }
1334 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001335 bumpDns();
1336 }
1337
1338 private int getRestoreDefaultNetworkDelay() {
1339 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1340 NETWORK_RESTORE_DELAY_PROP_NAME);
1341 if(restoreDefaultNetworkDelayStr != null &&
1342 restoreDefaultNetworkDelayStr.length() != 0) {
1343 try {
1344 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1345 } catch (NumberFormatException e) {
1346 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001347 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001348 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001349 }
1350
1351 @Override
1352 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001353 if (mContext.checkCallingOrSelfPermission(
1354 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001355 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001356 pw.println("Permission Denial: can't dump ConnectivityService " +
1357 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1358 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001359 return;
1360 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001361 pw.println();
1362 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001363 if (nst != null) {
1364 if (nst.getNetworkInfo().isConnected()) {
1365 pw.println("Active network: " + nst.getNetworkInfo().
1366 getTypeName());
1367 }
1368 pw.println(nst.getNetworkInfo());
1369 pw.println(nst);
1370 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001371 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001372 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001373
1374 pw.println("Network Requester Pids:");
1375 for (int net : mPriorityList) {
1376 String pidString = net + ": ";
1377 for (Object pid : mNetRequestersPids[net]) {
1378 pidString = pidString + pid.toString() + ", ";
1379 }
1380 pw.println(pidString);
1381 }
1382 pw.println();
1383
1384 pw.println("FeatureUsers:");
1385 for (Object requester : mFeatureUsers) {
1386 pw.println(requester.toString());
1387 }
1388 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001389
1390 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001391 }
1392
Robert Greenwalt2034b912009-08-12 16:08:25 -07001393 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001394 private class MyHandler extends Handler {
1395 @Override
1396 public void handleMessage(Message msg) {
1397 NetworkInfo info;
1398 switch (msg.what) {
1399 case NetworkStateTracker.EVENT_STATE_CHANGED:
1400 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001401 int type = info.getType();
1402 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001403 // only do this optimization for wifi. It going into scan mode for location
1404 // services generates alot of noise. Meanwhile the mms apn won't send out
1405 // subsequent notifications when on default cellular because it never
1406 // disconnects.. so only do this to wifi notifications. Fixed better when the
1407 // APN notifications are standardized.
1408 if (mNetAttributes[type].mLastState == state &&
1409 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001410 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001411 // TODO - remove this after we validate the dropping doesn't break
1412 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001413 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001414 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001415 state + "/" + info.getDetailedState());
1416 }
1417 return;
1418 }
1419 mNetAttributes[type].mLastState = state;
1420
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001421 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001422 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001423 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001424
1425 // Connectivity state changed:
1426 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001427 // [12-9] Network subtype (for mobile network, as defined
1428 // by TelephonyManager)
1429 // [8-3] Detailed state ordinal (as defined by
1430 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001431 // [2-0] Network type (as defined by ConnectivityManager)
1432 int eventLogParam = (info.getType() & 0x7) |
1433 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1434 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001435 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001436 eventLogParam);
1437
1438 if (info.getDetailedState() ==
1439 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001440 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001441 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001442 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001443 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001444 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001445 // the logic here is, handle SUSPENDED the same as
1446 // DISCONNECTED. The only difference being we are
1447 // broadcasting an intent with NetworkInfo that's
1448 // suspended. This allows the applications an
1449 // opportunity to handle DISCONNECTED and SUSPENDED
1450 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001451 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001452 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001453 handleConnect(info);
1454 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001455 break;
1456
1457 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1458 info = (NetworkInfo) msg.obj;
1459 handleScanResultsAvailable(info);
1460 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001461
The Android Open Source Project28527d22009-03-03 19:31:44 -08001462 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001463 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1464 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001465 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001466
1467 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001468 info = (NetworkInfo) msg.obj;
1469 type = info.getType();
1470 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001471 break;
1472
1473 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1474 // fill me in
1475 break;
1476
1477 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1478 // fill me in
1479 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001480 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001481 FeatureUser u = (FeatureUser)msg.obj;
1482 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001483 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001484 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1485 if (DBG) {
1486 Slog.d(TAG, "Inet connectivity change, net=" +
1487 msg.arg1 + ", condition=" + msg.arg2 +
1488 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1489 }
1490 if (mActiveDefaultNetwork == -1) {
1491 if (DBG) Slog.d(TAG, "no active default network - aborting");
1492 break;
1493 }
1494 if (mActiveDefaultNetwork != msg.arg1) {
1495 if (DBG) Slog.d(TAG, "given net not default - aborting");
1496 break;
1497 }
1498 mDefaultInetCondition = msg.arg2;
1499 int delay;
1500 if (mInetConditionChangeInFlight == false) {
1501 if (DBG) Slog.d(TAG, "starting a change hold");
1502 // setup a new hold to debounce this
1503 if (mDefaultInetCondition > 50) {
1504 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1505 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1506 } else {
1507 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1508 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1509 }
1510 mInetConditionChangeInFlight = true;
1511 sendMessageDelayed(obtainMessage(
1512 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1513 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1514 } else {
1515 // we've set the new condition, when this hold ends that will get
1516 // picked up
1517 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1518 }
1519 break;
1520 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1521 if (DBG) {
1522 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1523 ", condition =" + mDefaultInetCondition +
1524 ", published condition =" + mDefaultInetConditionPublished);
1525 }
1526 mInetConditionChangeInFlight = false;
1527
1528 if (mActiveDefaultNetwork == -1) {
1529 if (DBG) Slog.d(TAG, "no active default network - aborting");
1530 break;
1531 }
1532 if (mDefaultConnectionSequence != msg.arg2) {
1533 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1534 break;
1535 }
1536 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1537 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1538 break;
1539 }
1540 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1541 if (networkInfo.isConnected() == false) {
1542 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1543 break;
1544 }
1545 mDefaultInetConditionPublished = mDefaultInetCondition;
1546 sendConnectedBroadcast(networkInfo);
1547 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001548 }
1549 }
1550 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001551
1552 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001553 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001554 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001555
1556 if (isTetheringSupported()) {
1557 return mTethering.tether(iface);
1558 } else {
1559 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1560 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001561 }
1562
1563 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001564 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001565 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001566
1567 if (isTetheringSupported()) {
1568 return mTethering.untether(iface);
1569 } else {
1570 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1571 }
1572 }
1573
1574 // javadoc from interface
1575 public int getLastTetherError(String iface) {
1576 enforceTetherAccessPermission();
1577
1578 if (isTetheringSupported()) {
1579 return mTethering.getLastTetherError(iface);
1580 } else {
1581 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1582 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001583 }
1584
1585 // TODO - proper iface API for selection by property, inspection, etc
1586 public String[] getTetherableUsbRegexs() {
1587 enforceTetherAccessPermission();
1588 if (isTetheringSupported()) {
1589 return mTethering.getTetherableUsbRegexs();
1590 } else {
1591 return new String[0];
1592 }
1593 }
1594
1595 public String[] getTetherableWifiRegexs() {
1596 enforceTetherAccessPermission();
1597 if (isTetheringSupported()) {
1598 return mTethering.getTetherableWifiRegexs();
1599 } else {
1600 return new String[0];
1601 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001602 }
1603
1604 // TODO - move iface listing, queries, etc to new module
1605 // javadoc from interface
1606 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001607 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001608 return mTethering.getTetherableIfaces();
1609 }
1610
1611 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001612 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001613 return mTethering.getTetheredIfaces();
1614 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001615
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001616 public String[] getTetheringErroredIfaces() {
1617 enforceTetherAccessPermission();
1618 return mTethering.getErroredIfaces();
1619 }
1620
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001621 // if ro.tether.denied = true we default to no tethering
1622 // gservices could set the secure setting to 1 though to enable it on a build where it
1623 // had previously been turned off.
1624 public boolean isTetheringSupported() {
1625 enforceTetherAccessPermission();
1626 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001627 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1628 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1629 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001630 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001631
1632 // 100 percent is full good, 0 is full bad.
1633 public void reportInetCondition(int networkType, int percentage) {
1634 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1635 mContext.enforceCallingOrSelfPermission(
1636 android.Manifest.permission.STATUS_BAR,
1637 "ConnectivityService");
1638
1639 mHandler.sendMessage(mHandler.obtainMessage(
1640 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1641 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001642}