blob: c751f3decdc0d47a996a2604d6635cedc37bc968 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080042import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080043
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080046import com.android.server.connectivity.Tethering;
47
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import java.util.ArrayList;
51import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080052
53/**
54 * @hide
55 */
56public class ConnectivityService extends IConnectivityManager.Stub {
57
Robert Greenwalta25fd712009-10-06 14:12:53 -070058 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080059 private static final String TAG = "ConnectivityService";
60
Robert Greenwalt2034b912009-08-12 16:08:25 -070061 // how long to wait before switching back to a radio's default network
62 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
63 // system property that can override the above value
64 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
65 "android.telephony.apn-restore";
66
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080067
68 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080069 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080070
The Android Open Source Project28527d22009-03-03 19:31:44 -080071 /**
72 * Sometimes we want to refer to the individual network state
73 * trackers separately, and sometimes we just want to treat them
74 * abstractly.
75 */
76 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070077
78 /**
79 * A per Net list of the PID's that requested access to the net
80 * used both as a refcount and for per-PID DNS selection
81 */
82 private List mNetRequestersPids[];
83
Robert Greenwalt2034b912009-08-12 16:08:25 -070084 // priority order of the nettrackers
85 // (excluding dynamically set mNetworkPreference)
86 // TODO - move mNetworkTypePreference into this
87 private int[] mPriorityList;
88
The Android Open Source Project28527d22009-03-03 19:31:44 -080089 private Context mContext;
90 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070091 private int mActiveDefaultNetwork = -1;
The Android Open Source Project28527d22009-03-03 19:31:44 -080092
93 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080094
95 private boolean mTestMode;
96 private static ConnectivityService sServiceInstance;
97
Robert Greenwalt2034b912009-08-12 16:08:25 -070098 private Handler mHandler;
99
100 // list of DeathRecipients used to make sure features are turned off when
101 // a process dies
102 private List mFeatureUsers;
103
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400104 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800105 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400106
Robert Greenwalt12c44552009-12-07 11:33:18 -0800107 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700108 /**
109 * Class for holding settings read from resources.
110 */
111 public String mName;
112 public int mType;
113 public int mRadio;
114 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800115 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700116 public NetworkAttributes(String init) {
117 String fragments[] = init.split(",");
118 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700119 mType = Integer.parseInt(fragments[1]);
120 mRadio = Integer.parseInt(fragments[2]);
121 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800122 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700123 }
124 public boolean isDefault() {
125 return (mType == mRadio);
126 }
127 }
128 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700129 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700130
Robert Greenwalt12c44552009-12-07 11:33:18 -0800131 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700132 public int mSimultaneity;
133 public int mType;
134 public RadioAttributes(String init) {
135 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700136 mType = Integer.parseInt(fragments[0]);
137 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700138 }
139 }
140 RadioAttributes[] mRadioAttributes;
141
The Android Open Source Project28527d22009-03-03 19:31:44 -0800142 private static class ConnectivityThread extends Thread {
143 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700144
The Android Open Source Project28527d22009-03-03 19:31:44 -0800145 private ConnectivityThread(Context context) {
146 super("ConnectivityThread");
147 mContext = context;
148 }
149
150 @Override
151 public void run() {
152 Looper.prepare();
153 synchronized (this) {
154 sServiceInstance = new ConnectivityService(mContext);
155 notifyAll();
156 }
157 Looper.loop();
158 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700159
The Android Open Source Project28527d22009-03-03 19:31:44 -0800160 public static ConnectivityService getServiceInstance(Context context) {
161 ConnectivityThread thread = new ConnectivityThread(context);
162 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700163
The Android Open Source Project28527d22009-03-03 19:31:44 -0800164 synchronized (thread) {
165 while (sServiceInstance == null) {
166 try {
167 // Wait until sServiceInstance has been initialized.
168 thread.wait();
169 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800170 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700171 "Unexpected InterruptedException while waiting"+
172 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800173 }
174 }
175 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176
The Android Open Source Project28527d22009-03-03 19:31:44 -0800177 return sServiceInstance;
178 }
179 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700180
The Android Open Source Project28527d22009-03-03 19:31:44 -0800181 public static ConnectivityService getInstance(Context context) {
182 return ConnectivityThread.getServiceInstance(context);
183 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700184
The Android Open Source Project28527d22009-03-03 19:31:44 -0800185 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800186 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800187
188 // setup our unique device name
189 String id = Settings.Secure.getString(context.getContentResolver(),
190 Settings.Secure.ANDROID_ID);
191 if (id != null && id.length() > 0) {
192 String name = new String("android_").concat(id);
193 SystemProperties.set("net.hostname", name);
194 }
195
The Android Open Source Project28527d22009-03-03 19:31:44 -0800196 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700197 mNetTrackers = new NetworkStateTracker[
198 ConnectivityManager.MAX_NETWORK_TYPE+1];
199 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700200
The Android Open Source Project28527d22009-03-03 19:31:44 -0800201 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700202
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700203 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
204 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
205
Robert Greenwalt2034b912009-08-12 16:08:25 -0700206 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700207 String[] raStrings = context.getResources().getStringArray(
208 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700209 for (String raString : raStrings) {
210 RadioAttributes r = new RadioAttributes(raString);
211 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800212 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700213 continue;
214 }
215 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800216 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700217 r.mType);
218 continue;
219 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700220 mRadioAttributes[r.mType] = r;
221 }
222
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700223 String[] naStrings = context.getResources().getStringArray(
224 com.android.internal.R.array.networkAttributes);
225 for (String naString : naStrings) {
226 try {
227 NetworkAttributes n = new NetworkAttributes(naString);
228 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800229 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700230 n.mType);
231 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700232 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700233 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800234 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700235 n.mType);
236 continue;
237 }
238 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800239 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 "radio " + n.mRadio + " in network type " + n.mType);
241 continue;
242 }
243 mNetAttributes[n.mType] = n;
244 mNetworksDefined++;
245 } catch(Exception e) {
246 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700247 }
248 }
249
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700250 // high priority first
251 mPriorityList = new int[mNetworksDefined];
252 {
253 int insertionPoint = mNetworksDefined-1;
254 int currentLowest = 0;
255 int nextLowest = 0;
256 while (insertionPoint > -1) {
257 for (NetworkAttributes na : mNetAttributes) {
258 if (na == null) continue;
259 if (na.mPriority < currentLowest) continue;
260 if (na.mPriority > currentLowest) {
261 if (na.mPriority < nextLowest || nextLowest == 0) {
262 nextLowest = na.mPriority;
263 }
264 continue;
265 }
266 mPriorityList[insertionPoint--] = na.mType;
267 }
268 currentLowest = nextLowest;
269 nextLowest = 0;
270 }
271 }
272
273 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
274 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700275 mNetRequestersPids[i] = new ArrayList();
276 }
277
278 mFeatureUsers = new ArrayList();
279
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700280 mNumDnsEntries = 0;
281
282 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
283 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800284 /*
285 * Create the network state trackers for Wi-Fi and mobile
286 * data. Maybe this could be done with a factory class,
287 * but it's not clear that it's worth it, given that
288 * the number of different network types is not going
289 * to change very often.
290 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800291 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700292 for (int netType : mPriorityList) {
293 switch (mNetAttributes[netType].mRadio) {
294 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800295 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700296 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
297 WifiService wifiService = new WifiService(context, wst);
298 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800299 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700300 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
301 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800302
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303 break;
304 case ConnectivityManager.TYPE_MOBILE:
305 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
306 netType, mNetAttributes[netType].mName);
307 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800308 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800309 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800310 mNetTrackers[netType].teardown();
311 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700312 break;
313 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800314 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 mNetAttributes[netType].mRadio);
316 continue;
317 }
318 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800319
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800320 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800321 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
322 !mTethering.isDunRequired()) &&
323 (mTethering.getTetherableUsbRegexs().length != 0 ||
324 mTethering.getTetherableWifiRegexs().length != 0) &&
325 mTethering.getUpstreamIfaceRegexs().length != 0);
326
The Android Open Source Project28527d22009-03-03 19:31:44 -0800327 }
328
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329
The Android Open Source Project28527d22009-03-03 19:31:44 -0800330 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700331 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800332 * @param preference the new preference
333 */
334 public synchronized void setNetworkPreference(int preference) {
335 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700336 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700338 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800339 if (mNetworkPreference != preference) {
340 persistNetworkPreference(preference);
341 mNetworkPreference = preference;
342 enforcePreference();
343 }
344 }
345 }
346
347 public int getNetworkPreference() {
348 enforceAccessPermission();
349 return mNetworkPreference;
350 }
351
352 private void persistNetworkPreference(int networkPreference) {
353 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700354 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
355 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800356 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700357
The Android Open Source Project28527d22009-03-03 19:31:44 -0800358 private int getPersistedNetworkPreference() {
359 final ContentResolver cr = mContext.getContentResolver();
360
361 final int networkPrefSetting = Settings.Secure
362 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
363 if (networkPrefSetting != -1) {
364 return networkPrefSetting;
365 }
366
367 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
368 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700369
The Android Open Source Project28527d22009-03-03 19:31:44 -0800370 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700371 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800372 * In this method, we only tear down a non-preferred network. Establishing
373 * a connection to the preferred network is taken care of when we handle
374 * the disconnect event from the non-preferred network
375 * (see {@link #handleDisconnect(NetworkInfo)}).
376 */
377 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700378 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800379 return;
380
Robert Greenwalt2034b912009-08-12 16:08:25 -0700381 if (!mNetTrackers[mNetworkPreference].isAvailable())
382 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800383
Robert Greenwalt2034b912009-08-12 16:08:25 -0700384 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700385 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700386 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700387 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800388 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700389 mNetTrackers[t].getNetworkInfo() +
390 " in enforcePreference");
391 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700392 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800393 }
394 }
395 }
396
397 private boolean teardown(NetworkStateTracker netTracker) {
398 if (netTracker.teardown()) {
399 netTracker.setTeardownRequested(true);
400 return true;
401 } else {
402 return false;
403 }
404 }
405
406 /**
407 * Return NetworkInfo for the active (i.e., connected) network interface.
408 * It is assumed that at most one network is active at a time. If more
409 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700410 * @return the info for the active network, or {@code null} if none is
411 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800412 */
413 public NetworkInfo getActiveNetworkInfo() {
414 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700415 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700416 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700417 continue;
418 }
419 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800420 NetworkInfo info = t.getNetworkInfo();
421 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800422 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700423 "connected default network is not " +
424 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800425 return info;
426 }
427 }
428 return null;
429 }
430
431 public NetworkInfo getNetworkInfo(int networkType) {
432 enforceAccessPermission();
433 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
434 NetworkStateTracker t = mNetTrackers[networkType];
435 if (t != null)
436 return t.getNetworkInfo();
437 }
438 return null;
439 }
440
441 public NetworkInfo[] getAllNetworkInfo() {
442 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700443 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800444 int i = 0;
445 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700446 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 }
448 return result;
449 }
450
451 public boolean setRadios(boolean turnOn) {
452 boolean result = true;
453 enforceChangePermission();
454 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700455 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 }
457 return result;
458 }
459
460 public boolean setRadio(int netType, boolean turnOn) {
461 enforceChangePermission();
462 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
463 return false;
464 }
465 NetworkStateTracker tracker = mNetTrackers[netType];
466 return tracker != null && tracker.setRadio(turnOn);
467 }
468
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700469 /**
470 * Used to notice when the calling process dies so we can self-expire
471 *
472 * Also used to know if the process has cleaned up after itself when
473 * our auto-expire timer goes off. The timer has a link to an object.
474 *
475 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700476 private class FeatureUser implements IBinder.DeathRecipient {
477 int mNetworkType;
478 String mFeature;
479 IBinder mBinder;
480 int mPid;
481 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800482 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700483
484 FeatureUser(int type, String feature, IBinder binder) {
485 super();
486 mNetworkType = type;
487 mFeature = feature;
488 mBinder = binder;
489 mPid = getCallingPid();
490 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800491 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700492
Robert Greenwalt2034b912009-08-12 16:08:25 -0700493 try {
494 mBinder.linkToDeath(this, 0);
495 } catch (RemoteException e) {
496 binderDied();
497 }
498 }
499
500 void unlinkDeathRecipient() {
501 mBinder.unlinkToDeath(this, 0);
502 }
503
504 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800505 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800506 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
507 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700508 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700509 }
510
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700511 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800512 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800513 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
514 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700515 stopUsingNetworkFeature(this, false);
516 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800517
518 public String toString() {
519 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
520 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
521 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700522 }
523
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700524 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700525 public int startUsingNetworkFeature(int networkType, String feature,
526 IBinder binder) {
527 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800528 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700529 ": " + feature);
530 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800531 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700532 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
533 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800535 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700536
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700537 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700538
539 // TODO - move this into the MobileDataStateTracker
540 int usedNetworkType = networkType;
541 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800542 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800543 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800544 return Phone.APN_TYPE_NOT_AVAILABLE;
545 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700546 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
547 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
548 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
549 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
550 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
551 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
552 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
553 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
554 }
555 }
556 NetworkStateTracker network = mNetTrackers[usedNetworkType];
557 if (network != null) {
558 if (usedNetworkType != networkType) {
559 Integer currentPid = new Integer(getCallingPid());
560
561 NetworkStateTracker radio = mNetTrackers[networkType];
562 NetworkInfo ni = network.getNetworkInfo();
563
564 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800565 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700566 return Phone.APN_TYPE_NOT_AVAILABLE;
567 }
568
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700569 synchronized(this) {
570 mFeatureUsers.add(f);
571 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
572 // this gets used for per-pid dns when connected
573 mNetRequestersPids[usedNetworkType].add(currentPid);
574 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700575 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700576 mHandler.sendMessageDelayed(mHandler.obtainMessage(
577 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
578 f), getRestoreDefaultNetworkDelay());
579
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700581 if ((ni.isConnectedOrConnecting() == true) &&
582 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700583 if (ni.isConnected() == true) {
584 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700585 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800586 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700587 return Phone.APN_ALREADY_ACTIVE;
588 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800589 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700590 return Phone.APN_REQUEST_STARTED;
591 }
592
593 // check if the radio in play can make another contact
594 // assume if cannot for now
595
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800596 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700597 network.reconnect();
598 return Phone.APN_REQUEST_STARTED;
599 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700600 synchronized(this) {
601 mFeatureUsers.add(f);
602 }
603 mHandler.sendMessageDelayed(mHandler.obtainMessage(
604 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
605 f), getRestoreDefaultNetworkDelay());
606
Robert Greenwalt2034b912009-08-12 16:08:25 -0700607 return network.startUsingNetworkFeature(feature,
608 getCallingPid(), getCallingUid());
609 }
610 }
611 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800612 }
613
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700614 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800615 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700616 enforceChangePermission();
617
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700618 int pid = getCallingPid();
619 int uid = getCallingUid();
620
621 FeatureUser u = null;
622 boolean found = false;
623
624 synchronized(this) {
625 for (int i = 0; i < mFeatureUsers.size() ; i++) {
626 u = (FeatureUser)mFeatureUsers.get(i);
627 if (uid == u.mUid && pid == u.mPid &&
628 networkType == u.mNetworkType &&
629 TextUtils.equals(feature, u.mFeature)) {
630 found = true;
631 break;
632 }
633 }
634 }
635 if (found && u != null) {
636 // stop regardless of how many other time this proc had called start
637 return stopUsingNetworkFeature(u, true);
638 } else {
639 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800640 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700641 return 1;
642 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700643 }
644
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700645 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
646 int networkType = u.mNetworkType;
647 String feature = u.mFeature;
648 int pid = u.mPid;
649 int uid = u.mUid;
650
651 NetworkStateTracker tracker = null;
652 boolean callTeardown = false; // used to carry our decision outside of sync block
653
Robert Greenwalt2034b912009-08-12 16:08:25 -0700654 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800655 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700656 ": " + feature);
657 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700658
The Android Open Source Project28527d22009-03-03 19:31:44 -0800659 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
660 return -1;
661 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700662
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
664 // sync block
665 synchronized(this) {
666 // check if this process still has an outstanding start request
667 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800668 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700669 return 1;
670 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700671 u.unlinkDeathRecipient();
672 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
673 // If we care about duplicate requests, check for that here.
674 //
675 // This is done to support the extension of a request - the app
676 // can request we start the network feature again and renew the
677 // auto-shutoff delay. Normal "stop" calls from the app though
678 // do not pay attention to duplicate requests - in effect the
679 // API does not refcount and a single stop will counter multiple starts.
680 if (ignoreDups == false) {
681 for (int i = 0; i < mFeatureUsers.size() ; i++) {
682 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
683 if (x.mUid == u.mUid && x.mPid == u.mPid &&
684 x.mNetworkType == u.mNetworkType &&
685 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800686 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700687 return 1;
688 }
689 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700691
692 // TODO - move to MobileDataStateTracker
693 int usedNetworkType = networkType;
694 if (networkType == ConnectivityManager.TYPE_MOBILE) {
695 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
696 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
697 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
698 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
699 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
700 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
701 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
703 }
704 }
705 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700706 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800707 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700708 return -1;
709 }
710 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700711 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700712 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800713 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700714 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800715 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700716 "others still using it");
717 return 1;
718 }
719 callTeardown = true;
720 }
721 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800722 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723 if (callTeardown) {
724 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700725 return 1;
726 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700727 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700728 return tracker.stopUsingNetworkFeature(feature, pid, uid);
729 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800730 }
731
732 /**
733 * Ensure that a network route exists to deliver traffic to the specified
734 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700735 * @param networkType the type of the network over which traffic to the
736 * specified host is to be routed
737 * @param hostAddress the IP address of the host to which the route is
738 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800739 * @return {@code true} on success, {@code false} on failure
740 */
741 public boolean requestRouteToHost(int networkType, int hostAddress) {
742 enforceChangePermission();
743 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
744 return false;
745 }
746 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700747
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700748 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
749 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700750 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800751 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700752 }
753 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800754 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700755 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800756 }
757
758 /**
759 * @see ConnectivityManager#getBackgroundDataSetting()
760 */
761 public boolean getBackgroundDataSetting() {
762 return Settings.Secure.getInt(mContext.getContentResolver(),
763 Settings.Secure.BACKGROUND_DATA, 1) == 1;
764 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700765
The Android Open Source Project28527d22009-03-03 19:31:44 -0800766 /**
767 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
768 */
769 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
770 mContext.enforceCallingOrSelfPermission(
771 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
772 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700773
The Android Open Source Project28527d22009-03-03 19:31:44 -0800774 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
775
776 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700777 Settings.Secure.BACKGROUND_DATA,
778 allowBackgroundDataUsage ? 1 : 0);
779
The Android Open Source Project28527d22009-03-03 19:31:44 -0800780 Intent broadcast = new Intent(
781 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
782 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700783 }
784
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800785 /**
786 * @see ConnectivityManager#getMobileDataEnabled()
787 */
788 public boolean getMobileDataEnabled() {
789 enforceAccessPermission();
790 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
791 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800792 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800793 return retVal;
794 }
795
796 /**
797 * @see ConnectivityManager#setMobileDataEnabled(boolean)
798 */
799 public synchronized void setMobileDataEnabled(boolean enabled) {
800 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800801 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800802
803 if (getMobileDataEnabled() == enabled) return;
804
805 Settings.Secure.putInt(mContext.getContentResolver(),
806 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
807
808 if (enabled) {
809 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800810 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800811 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
812 }
813 } else {
814 for (NetworkStateTracker nt : mNetTrackers) {
815 if (nt == null) continue;
816 int netType = nt.getNetworkInfo().getType();
817 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800818 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800819 nt.teardown();
820 }
821 }
822 }
823 }
824
The Android Open Source Project28527d22009-03-03 19:31:44 -0800825 private int getNumConnectedNetworks() {
826 int numConnectedNets = 0;
827
828 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700829 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700830 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800831 ++numConnectedNets;
832 }
833 }
834 return numConnectedNets;
835 }
836
837 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700838 mContext.enforceCallingOrSelfPermission(
839 android.Manifest.permission.ACCESS_NETWORK_STATE,
840 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800841 }
842
843 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700844 mContext.enforceCallingOrSelfPermission(
845 android.Manifest.permission.CHANGE_NETWORK_STATE,
846 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800847 }
848
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800849 // TODO Make this a special check when it goes public
850 private void enforceTetherChangePermission() {
851 mContext.enforceCallingOrSelfPermission(
852 android.Manifest.permission.CHANGE_NETWORK_STATE,
853 "ConnectivityService");
854 }
855
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800856 private void enforceTetherAccessPermission() {
857 mContext.enforceCallingOrSelfPermission(
858 android.Manifest.permission.ACCESS_NETWORK_STATE,
859 "ConnectivityService");
860 }
861
The Android Open Source Project28527d22009-03-03 19:31:44 -0800862 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700863 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
864 * network, we ignore it. If it is for the active network, we send out a
865 * broadcast. But first, we check whether it might be possible to connect
866 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800867 * @param info the {@code NetworkInfo} for the network
868 */
869 private void handleDisconnect(NetworkInfo info) {
870
Robert Greenwalt2034b912009-08-12 16:08:25 -0700871 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872
Robert Greenwalt2034b912009-08-12 16:08:25 -0700873 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800874 /*
875 * If the disconnected network is not the active one, then don't report
876 * this as a loss of connectivity. What probably happened is that we're
877 * getting the disconnect for a network that we explicitly disabled
878 * in accordance with network preference policies.
879 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700880 if (!mNetAttributes[prevNetType].isDefault()) {
881 List pids = mNetRequestersPids[prevNetType];
882 for (int i = 0; i<pids.size(); i++) {
883 Integer pid = (Integer)pids.get(i);
884 // will remove them because the net's no longer connected
885 // need to do this now as only now do we know the pids and
886 // can properly null things that are no longer referenced.
887 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800888 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800889 }
890
The Android Open Source Project28527d22009-03-03 19:31:44 -0800891 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
892 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
893 if (info.isFailover()) {
894 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
895 info.setFailover(false);
896 }
897 if (info.getReason() != null) {
898 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
899 }
900 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700901 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
902 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800903 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700904
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800905 NetworkStateTracker newNet = null;
906 if (mNetAttributes[prevNetType].isDefault()) {
907 newNet = tryFailover(prevNetType);
908 if (newNet != null) {
909 NetworkInfo switchTo = newNet.getNetworkInfo();
910 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
911 } else {
912 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
913 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800914 }
915 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700916 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800917
918 sendStickyBroadcast(intent);
919 /*
920 * If the failover network is already connected, then immediately send
921 * out a followup broadcast indicating successful failover
922 */
923 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
924 sendConnectedBroadcast(newNet.getNetworkInfo());
925 }
926 }
927
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800928 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800929 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700930 /*
931 * If this is a default network, check if other defaults are available
932 * or active
933 */
934 NetworkStateTracker newNet = null;
935 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700936 if (mActiveDefaultNetwork == prevNetType) {
937 mActiveDefaultNetwork = -1;
938 }
939
940 int newType = -1;
941 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800942 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800943 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700944 if (checkType == prevNetType) continue;
945 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800946 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
947 noMobileData) {
948 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800949 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800950 " because Mobile Data Disabled");
951 }
952 continue;
953 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700954 if (mNetAttributes[checkType].isDefault()) {
955 /* TODO - if we have multiple nets we could use
956 * we may want to put more thought into which we choose
957 */
958 if (checkType == mNetworkPreference) {
959 newType = checkType;
960 break;
961 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700962 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700963 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700964 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700965 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800966 }
967 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700968
969 if (newType != -1) {
970 newNet = mNetTrackers[newType];
971 /**
972 * See if the other network is available to fail over to.
973 * If is not available, we enable it anyway, so that it
974 * will be able to connect when it does become available,
975 * but we report a total loss of connectivity rather than
976 * report that we are attempting to fail over.
977 */
978 if (newNet.isAvailable()) {
979 NetworkInfo switchTo = newNet.getNetworkInfo();
980 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700981 if (!switchTo.isConnectedOrConnecting() ||
982 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700983 newNet.reconnect();
984 }
985 if (DBG) {
986 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800987 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700988 switchTo.getTypeName());
989 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800990 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700991 switchTo.getTypeName());
992 }
993 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700994 } else {
995 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -0800996 newNet = null; // not officially avail.. try anyway, but
997 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -0700998 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700999 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001000 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001001
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001002 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001003 }
1004
1005 private void sendConnectedBroadcast(NetworkInfo info) {
1006 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1007 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1008 if (info.isFailover()) {
1009 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1010 info.setFailover(false);
1011 }
1012 if (info.getReason() != null) {
1013 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1014 }
1015 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001016 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1017 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001018 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001019 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 }
1021
1022 /**
1023 * Called when an attempt to fail over to another network has failed.
1024 * @param info the {@link NetworkInfo} for the failed network
1025 */
1026 private void handleConnectionFailure(NetworkInfo info) {
1027 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001028
Robert Greenwalt2034b912009-08-12 16:08:25 -07001029 String reason = info.getReason();
1030 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001031
Robert Greenwalt2034b912009-08-12 16:08:25 -07001032 if (DBG) {
1033 String reasonText;
1034 if (reason == null) {
1035 reasonText = ".";
1036 } else {
1037 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001038 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001039 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001040 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001041 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001042
1043 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1044 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1045 if (getActiveNetworkInfo() == null) {
1046 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1047 }
1048 if (reason != null) {
1049 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1050 }
1051 if (extraInfo != null) {
1052 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1053 }
1054 if (info.isFailover()) {
1055 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1056 info.setFailover(false);
1057 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001058
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001059 NetworkStateTracker newNet = null;
1060 if (mNetAttributes[info.getType()].isDefault()) {
1061 newNet = tryFailover(info.getType());
1062 if (newNet != null) {
1063 NetworkInfo switchTo = newNet.getNetworkInfo();
1064 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1065 } else {
1066 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1067 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001068 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001069
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001070 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001071 /*
1072 * If the failover network is already connected, then immediately send
1073 * out a followup broadcast indicating successful failover
1074 */
1075 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1076 sendConnectedBroadcast(newNet.getNetworkInfo());
1077 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001078 }
1079
1080 private void sendStickyBroadcast(Intent intent) {
1081 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001082 if (!mSystemReady) {
1083 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001084 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001085 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1086 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001087 }
1088 }
1089
1090 void systemReady() {
1091 synchronized(this) {
1092 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001093 if (mInitialBroadcast != null) {
1094 mContext.sendStickyBroadcast(mInitialBroadcast);
1095 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001096 }
1097 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001098 }
1099
1100 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001101 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001102
1103 // snapshot isFailover, because sendConnectedBroadcast() resets it
1104 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001105 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001106
Robert Greenwalt2034b912009-08-12 16:08:25 -07001107 // if this is a default net and other default is running
1108 // kill the one not preferred
1109 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001110 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1111 if ((type != mNetworkPreference &&
1112 mNetAttributes[mActiveDefaultNetwork].mPriority >
1113 mNetAttributes[type].mPriority) ||
1114 mNetworkPreference == mActiveDefaultNetwork) {
1115 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001116 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001117 "to torn down network " + info.getTypeName());
1118 teardown(thisNet);
1119 return;
1120 } else {
1121 // tear down the other
1122 NetworkStateTracker otherNet =
1123 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001124 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001125 otherNet.getNetworkInfo().getTypeName() +
1126 " teardown");
1127 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001128 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 return;
1130 }
1131 if (isFailover) {
1132 otherNet.releaseWakeLock();
1133 }
1134 }
1135 }
1136 mActiveDefaultNetwork = type;
1137 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001138 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001139 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001140 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001141 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001142 }
1143
1144 private void handleScanResultsAvailable(NetworkInfo info) {
1145 int networkType = info.getType();
1146 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001147 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001148 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001149 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001150
The Android Open Source Project28527d22009-03-03 19:31:44 -08001151 mNetTrackers[networkType].interpretScanResultsAvailable();
1152 }
1153
Robert Greenwalt0659da32009-07-16 17:21:39 -07001154 private void handleNotificationChange(boolean visible, int id,
1155 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001156 NotificationManager notificationManager = (NotificationManager) mContext
1157 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001158
The Android Open Source Project28527d22009-03-03 19:31:44 -08001159 if (visible) {
1160 notificationManager.notify(id, notification);
1161 } else {
1162 notificationManager.cancel(id);
1163 }
1164 }
1165
1166 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001167 * After a change in the connectivity state of any network, We're mainly
1168 * concerned with making sure that the list of DNS servers is setupup
1169 * according to which networks are connected, and ensuring that the
1170 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001171 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001172 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001174 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001175 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001176 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001177 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001178
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001179 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1180 if (mNetAttributes[netType].isDefault()) {
1181 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001183 mNetTrackers[netType].addPrivateDnsRoutes();
1184 }
1185 } else {
1186 if (mNetAttributes[netType].isDefault()) {
1187 mNetTrackers[netType].removeDefaultRoute();
1188 } else {
1189 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001190 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001191 }
1192 }
1193
Robert Greenwalt2034b912009-08-12 16:08:25 -07001194 /**
1195 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1196 * on the highest priority active net which this process requested.
1197 * If there aren't any, clear it out
1198 */
1199 private void reassessPidDns(int myPid, boolean doBump)
1200 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001201 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001202 for(int i : mPriorityList) {
1203 if (mNetAttributes[i].isDefault()) {
1204 continue;
1205 }
1206 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001207 if (nt.getNetworkInfo().isConnected() &&
1208 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001209 List pids = mNetRequestersPids[i];
1210 for (int j=0; j<pids.size(); j++) {
1211 Integer pid = (Integer)pids.get(j);
1212 if (pid.intValue() == myPid) {
1213 String[] dnsList = nt.getNameServers();
1214 writePidDns(dnsList, myPid);
1215 if (doBump) {
1216 bumpDns();
1217 }
1218 return;
1219 }
1220 }
1221 }
1222 }
1223 // nothing found - delete
1224 for (int i = 1; ; i++) {
1225 String prop = "net.dns" + i + "." + myPid;
1226 if (SystemProperties.get(prop).length() == 0) {
1227 if (doBump) {
1228 bumpDns();
1229 }
1230 return;
1231 }
1232 SystemProperties.set(prop, "");
1233 }
1234 }
1235
1236 private void writePidDns(String[] dnsList, int pid) {
1237 int j = 1;
1238 for (String dns : dnsList) {
1239 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1240 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1241 }
1242 }
1243 }
1244
1245 private void bumpDns() {
1246 /*
1247 * Bump the property that tells the name resolver library to reread
1248 * the DNS server list from the properties.
1249 */
1250 String propVal = SystemProperties.get("net.dnschange");
1251 int n = 0;
1252 if (propVal.length() != 0) {
1253 try {
1254 n = Integer.parseInt(propVal);
1255 } catch (NumberFormatException e) {}
1256 }
1257 SystemProperties.set("net.dnschange", "" + (n+1));
1258 }
1259
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001260 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001261 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001262 NetworkStateTracker nt = mNetTrackers[netType];
1263 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1264 String[] dnsList = nt.getNameServers();
1265 if (mNetAttributes[netType].isDefault()) {
1266 int j = 1;
1267 for (String dns : dnsList) {
1268 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1269 if (DBG) {
1270 Slog.d(TAG, "adding dns " + dns + " for " +
1271 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001272 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001273 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001274 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001275 }
1276 for (int k=j ; k<mNumDnsEntries; k++) {
1277 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1278 SystemProperties.set("net.dns" + k, "");
1279 }
1280 mNumDnsEntries = j;
1281 } else {
1282 // set per-pid dns for attached secondary nets
1283 List pids = mNetRequestersPids[netType];
1284 for (int y=0; y< pids.size(); y++) {
1285 Integer pid = (Integer)pids.get(y);
1286 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001287 }
1288 }
1289 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001290 bumpDns();
1291 }
1292
1293 private int getRestoreDefaultNetworkDelay() {
1294 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1295 NETWORK_RESTORE_DELAY_PROP_NAME);
1296 if(restoreDefaultNetworkDelayStr != null &&
1297 restoreDefaultNetworkDelayStr.length() != 0) {
1298 try {
1299 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1300 } catch (NumberFormatException e) {
1301 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001302 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001303 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001304 }
1305
1306 @Override
1307 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001308 if (mContext.checkCallingOrSelfPermission(
1309 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001310 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001311 pw.println("Permission Denial: can't dump ConnectivityService " +
1312 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1313 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001314 return;
1315 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001316 pw.println();
1317 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001318 if (nst != null) {
1319 if (nst.getNetworkInfo().isConnected()) {
1320 pw.println("Active network: " + nst.getNetworkInfo().
1321 getTypeName());
1322 }
1323 pw.println(nst.getNetworkInfo());
1324 pw.println(nst);
1325 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001326 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001327 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001328
1329 pw.println("Network Requester Pids:");
1330 for (int net : mPriorityList) {
1331 String pidString = net + ": ";
1332 for (Object pid : mNetRequestersPids[net]) {
1333 pidString = pidString + pid.toString() + ", ";
1334 }
1335 pw.println(pidString);
1336 }
1337 pw.println();
1338
1339 pw.println("FeatureUsers:");
1340 for (Object requester : mFeatureUsers) {
1341 pw.println(requester.toString());
1342 }
1343 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001344
1345 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001346 }
1347
Robert Greenwalt2034b912009-08-12 16:08:25 -07001348 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001349 private class MyHandler extends Handler {
1350 @Override
1351 public void handleMessage(Message msg) {
1352 NetworkInfo info;
1353 switch (msg.what) {
1354 case NetworkStateTracker.EVENT_STATE_CHANGED:
1355 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001356 int type = info.getType();
1357 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001358 // only do this optimization for wifi. It going into scan mode for location
1359 // services generates alot of noise. Meanwhile the mms apn won't send out
1360 // subsequent notifications when on default cellular because it never
1361 // disconnects.. so only do this to wifi notifications. Fixed better when the
1362 // APN notifications are standardized.
1363 if (mNetAttributes[type].mLastState == state &&
1364 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001365 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001366 // TODO - remove this after we validate the dropping doesn't break
1367 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001368 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001369 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001370 state + "/" + info.getDetailedState());
1371 }
1372 return;
1373 }
1374 mNetAttributes[type].mLastState = state;
1375
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001376 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001377 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001378 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001379
1380 // Connectivity state changed:
1381 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001382 // [12-9] Network subtype (for mobile network, as defined
1383 // by TelephonyManager)
1384 // [8-3] Detailed state ordinal (as defined by
1385 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001386 // [2-0] Network type (as defined by ConnectivityManager)
1387 int eventLogParam = (info.getType() & 0x7) |
1388 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1389 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001390 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001391 eventLogParam);
1392
1393 if (info.getDetailedState() ==
1394 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001395 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001396 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001397 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001398 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001399 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001400 // the logic here is, handle SUSPENDED the same as
1401 // DISCONNECTED. The only difference being we are
1402 // broadcasting an intent with NetworkInfo that's
1403 // suspended. This allows the applications an
1404 // opportunity to handle DISCONNECTED and SUSPENDED
1405 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001406 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001407 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001408 handleConnect(info);
1409 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001410 break;
1411
1412 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1413 info = (NetworkInfo) msg.obj;
1414 handleScanResultsAvailable(info);
1415 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001416
The Android Open Source Project28527d22009-03-03 19:31:44 -08001417 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001418 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1419 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001420 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001421
1422 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001423 info = (NetworkInfo) msg.obj;
1424 type = info.getType();
1425 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001426 break;
1427
1428 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1429 // fill me in
1430 break;
1431
1432 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1433 // fill me in
1434 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001435 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001436 FeatureUser u = (FeatureUser)msg.obj;
1437 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001438 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001439 }
1440 }
1441 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001442
1443 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001444 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001445 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001446
1447 if (isTetheringSupported()) {
1448 return mTethering.tether(iface);
1449 } else {
1450 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1451 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001452 }
1453
1454 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001455 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001456 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001457
1458 if (isTetheringSupported()) {
1459 return mTethering.untether(iface);
1460 } else {
1461 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1462 }
1463 }
1464
1465 // javadoc from interface
1466 public int getLastTetherError(String iface) {
1467 enforceTetherAccessPermission();
1468
1469 if (isTetheringSupported()) {
1470 return mTethering.getLastTetherError(iface);
1471 } else {
1472 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1473 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001474 }
1475
1476 // TODO - proper iface API for selection by property, inspection, etc
1477 public String[] getTetherableUsbRegexs() {
1478 enforceTetherAccessPermission();
1479 if (isTetheringSupported()) {
1480 return mTethering.getTetherableUsbRegexs();
1481 } else {
1482 return new String[0];
1483 }
1484 }
1485
1486 public String[] getTetherableWifiRegexs() {
1487 enforceTetherAccessPermission();
1488 if (isTetheringSupported()) {
1489 return mTethering.getTetherableWifiRegexs();
1490 } else {
1491 return new String[0];
1492 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001493 }
1494
1495 // TODO - move iface listing, queries, etc to new module
1496 // javadoc from interface
1497 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001498 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001499 return mTethering.getTetherableIfaces();
1500 }
1501
1502 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001503 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001504 return mTethering.getTetheredIfaces();
1505 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001506
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001507 public String[] getTetheringErroredIfaces() {
1508 enforceTetherAccessPermission();
1509 return mTethering.getErroredIfaces();
1510 }
1511
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001512 // if ro.tether.denied = true we default to no tethering
1513 // gservices could set the secure setting to 1 though to enable it on a build where it
1514 // had previously been turned off.
1515 public boolean isTetheringSupported() {
1516 enforceTetherAccessPermission();
1517 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001518 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1519 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1520 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001521 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001522}