blob: 9784d96aedeb3eca21ce1da395247a62b7a28204 [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;
Robert Greenwalt986c7412010-09-08 15:24:47 -070092 // 0 is full bad, 100 is full good
93 private int mDefaultInetCondition = 0;
94 private int mDefaultInetConditionPublished = 0;
95 private boolean mInetConditionChangeInFlight = false;
96 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -080097
98 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -080099
100 private boolean mTestMode;
101 private static ConnectivityService sServiceInstance;
102
Robert Greenwalt2034b912009-08-12 16:08:25 -0700103 private Handler mHandler;
104
105 // list of DeathRecipients used to make sure features are turned off when
106 // a process dies
107 private List mFeatureUsers;
108
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400109 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800110 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400111
Robert Greenwalt12c44552009-12-07 11:33:18 -0800112 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700113 /**
114 * Class for holding settings read from resources.
115 */
116 public String mName;
117 public int mType;
118 public int mRadio;
119 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800120 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700121 public NetworkAttributes(String init) {
122 String fragments[] = init.split(",");
123 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700124 mType = Integer.parseInt(fragments[1]);
125 mRadio = Integer.parseInt(fragments[2]);
126 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800127 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700128 }
129 public boolean isDefault() {
130 return (mType == mRadio);
131 }
132 }
133 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700134 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700135
Robert Greenwalt12c44552009-12-07 11:33:18 -0800136 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700137 public int mSimultaneity;
138 public int mType;
139 public RadioAttributes(String init) {
140 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700141 mType = Integer.parseInt(fragments[0]);
142 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700143 }
144 }
145 RadioAttributes[] mRadioAttributes;
146
The Android Open Source Project28527d22009-03-03 19:31:44 -0800147 private static class ConnectivityThread extends Thread {
148 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700149
The Android Open Source Project28527d22009-03-03 19:31:44 -0800150 private ConnectivityThread(Context context) {
151 super("ConnectivityThread");
152 mContext = context;
153 }
154
155 @Override
156 public void run() {
157 Looper.prepare();
158 synchronized (this) {
159 sServiceInstance = new ConnectivityService(mContext);
160 notifyAll();
161 }
162 Looper.loop();
163 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700164
The Android Open Source Project28527d22009-03-03 19:31:44 -0800165 public static ConnectivityService getServiceInstance(Context context) {
166 ConnectivityThread thread = new ConnectivityThread(context);
167 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700168
The Android Open Source Project28527d22009-03-03 19:31:44 -0800169 synchronized (thread) {
170 while (sServiceInstance == null) {
171 try {
172 // Wait until sServiceInstance has been initialized.
173 thread.wait();
174 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800175 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700176 "Unexpected InterruptedException while waiting"+
177 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800178 }
179 }
180 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700181
The Android Open Source Project28527d22009-03-03 19:31:44 -0800182 return sServiceInstance;
183 }
184 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700185
The Android Open Source Project28527d22009-03-03 19:31:44 -0800186 public static ConnectivityService getInstance(Context context) {
187 return ConnectivityThread.getServiceInstance(context);
188 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700189
The Android Open Source Project28527d22009-03-03 19:31:44 -0800190 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800191 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800192
193 // setup our unique device name
194 String id = Settings.Secure.getString(context.getContentResolver(),
195 Settings.Secure.ANDROID_ID);
196 if (id != null && id.length() > 0) {
197 String name = new String("android_").concat(id);
198 SystemProperties.set("net.hostname", name);
199 }
200
The Android Open Source Project28527d22009-03-03 19:31:44 -0800201 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700202 mNetTrackers = new NetworkStateTracker[
203 ConnectivityManager.MAX_NETWORK_TYPE+1];
204 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700205
The Android Open Source Project28527d22009-03-03 19:31:44 -0800206 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700207
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700208 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
209 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
210
Robert Greenwalt2034b912009-08-12 16:08:25 -0700211 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700212 String[] raStrings = context.getResources().getStringArray(
213 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700214 for (String raString : raStrings) {
215 RadioAttributes r = new RadioAttributes(raString);
216 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800217 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700218 continue;
219 }
220 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800221 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700222 r.mType);
223 continue;
224 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700225 mRadioAttributes[r.mType] = r;
226 }
227
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700228 String[] naStrings = context.getResources().getStringArray(
229 com.android.internal.R.array.networkAttributes);
230 for (String naString : naStrings) {
231 try {
232 NetworkAttributes n = new NetworkAttributes(naString);
233 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800234 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700235 n.mType);
236 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700237 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700238 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800239 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 n.mType);
241 continue;
242 }
243 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800244 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700245 "radio " + n.mRadio + " in network type " + n.mType);
246 continue;
247 }
248 mNetAttributes[n.mType] = n;
249 mNetworksDefined++;
250 } catch(Exception e) {
251 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700252 }
253 }
254
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700255 // high priority first
256 mPriorityList = new int[mNetworksDefined];
257 {
258 int insertionPoint = mNetworksDefined-1;
259 int currentLowest = 0;
260 int nextLowest = 0;
261 while (insertionPoint > -1) {
262 for (NetworkAttributes na : mNetAttributes) {
263 if (na == null) continue;
264 if (na.mPriority < currentLowest) continue;
265 if (na.mPriority > currentLowest) {
266 if (na.mPriority < nextLowest || nextLowest == 0) {
267 nextLowest = na.mPriority;
268 }
269 continue;
270 }
271 mPriorityList[insertionPoint--] = na.mType;
272 }
273 currentLowest = nextLowest;
274 nextLowest = 0;
275 }
276 }
277
278 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
279 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700280 mNetRequestersPids[i] = new ArrayList();
281 }
282
283 mFeatureUsers = new ArrayList();
284
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700285 mNumDnsEntries = 0;
286
287 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
288 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800289 /*
290 * Create the network state trackers for Wi-Fi and mobile
291 * data. Maybe this could be done with a factory class,
292 * but it's not clear that it's worth it, given that
293 * the number of different network types is not going
294 * to change very often.
295 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800296 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700297 for (int netType : mPriorityList) {
298 switch (mNetAttributes[netType].mRadio) {
299 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800300 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
302 WifiService wifiService = new WifiService(context, wst);
303 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800304 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700305 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
306 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800307
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 break;
309 case ConnectivityManager.TYPE_MOBILE:
310 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
311 netType, mNetAttributes[netType].mName);
312 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800313 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800314 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800315 mNetTrackers[netType].teardown();
316 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700317 break;
318 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800319 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700320 mNetAttributes[netType].mRadio);
321 continue;
322 }
323 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800324
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800325 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800326 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
327 !mTethering.isDunRequired()) &&
328 (mTethering.getTetherableUsbRegexs().length != 0 ||
329 mTethering.getTetherableWifiRegexs().length != 0) &&
330 mTethering.getUpstreamIfaceRegexs().length != 0);
331
The Android Open Source Project28527d22009-03-03 19:31:44 -0800332 }
333
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700334
The Android Open Source Project28527d22009-03-03 19:31:44 -0800335 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700336 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800337 * @param preference the new preference
338 */
339 public synchronized void setNetworkPreference(int preference) {
340 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700341 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700342 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700343 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800344 if (mNetworkPreference != preference) {
345 persistNetworkPreference(preference);
346 mNetworkPreference = preference;
347 enforcePreference();
348 }
349 }
350 }
351
352 public int getNetworkPreference() {
353 enforceAccessPermission();
354 return mNetworkPreference;
355 }
356
357 private void persistNetworkPreference(int networkPreference) {
358 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700359 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
360 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800361 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700362
The Android Open Source Project28527d22009-03-03 19:31:44 -0800363 private int getPersistedNetworkPreference() {
364 final ContentResolver cr = mContext.getContentResolver();
365
366 final int networkPrefSetting = Settings.Secure
367 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
368 if (networkPrefSetting != -1) {
369 return networkPrefSetting;
370 }
371
372 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
373 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700374
The Android Open Source Project28527d22009-03-03 19:31:44 -0800375 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700376 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800377 * In this method, we only tear down a non-preferred network. Establishing
378 * a connection to the preferred network is taken care of when we handle
379 * the disconnect event from the non-preferred network
380 * (see {@link #handleDisconnect(NetworkInfo)}).
381 */
382 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700383 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800384 return;
385
Robert Greenwalt2034b912009-08-12 16:08:25 -0700386 if (!mNetTrackers[mNetworkPreference].isAvailable())
387 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800388
Robert Greenwalt2034b912009-08-12 16:08:25 -0700389 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700390 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700391 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700392 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800393 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700394 mNetTrackers[t].getNetworkInfo() +
395 " in enforcePreference");
396 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700397 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398 }
399 }
400 }
401
402 private boolean teardown(NetworkStateTracker netTracker) {
403 if (netTracker.teardown()) {
404 netTracker.setTeardownRequested(true);
405 return true;
406 } else {
407 return false;
408 }
409 }
410
411 /**
412 * Return NetworkInfo for the active (i.e., connected) network interface.
413 * It is assumed that at most one network is active at a time. If more
414 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700415 * @return the info for the active network, or {@code null} if none is
416 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800417 */
418 public NetworkInfo getActiveNetworkInfo() {
419 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700420 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700421 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700422 continue;
423 }
424 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800425 NetworkInfo info = t.getNetworkInfo();
426 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800427 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700428 "connected default network is not " +
429 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800430 return info;
431 }
432 }
433 return null;
434 }
435
436 public NetworkInfo getNetworkInfo(int networkType) {
437 enforceAccessPermission();
438 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
439 NetworkStateTracker t = mNetTrackers[networkType];
440 if (t != null)
441 return t.getNetworkInfo();
442 }
443 return null;
444 }
445
446 public NetworkInfo[] getAllNetworkInfo() {
447 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700448 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800449 int i = 0;
450 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700451 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800452 }
453 return result;
454 }
455
456 public boolean setRadios(boolean turnOn) {
457 boolean result = true;
458 enforceChangePermission();
459 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700460 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800461 }
462 return result;
463 }
464
465 public boolean setRadio(int netType, boolean turnOn) {
466 enforceChangePermission();
467 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
468 return false;
469 }
470 NetworkStateTracker tracker = mNetTrackers[netType];
471 return tracker != null && tracker.setRadio(turnOn);
472 }
473
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700474 /**
475 * Used to notice when the calling process dies so we can self-expire
476 *
477 * Also used to know if the process has cleaned up after itself when
478 * our auto-expire timer goes off. The timer has a link to an object.
479 *
480 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700481 private class FeatureUser implements IBinder.DeathRecipient {
482 int mNetworkType;
483 String mFeature;
484 IBinder mBinder;
485 int mPid;
486 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800487 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700488
489 FeatureUser(int type, String feature, IBinder binder) {
490 super();
491 mNetworkType = type;
492 mFeature = feature;
493 mBinder = binder;
494 mPid = getCallingPid();
495 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800496 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700497
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 try {
499 mBinder.linkToDeath(this, 0);
500 } catch (RemoteException e) {
501 binderDied();
502 }
503 }
504
505 void unlinkDeathRecipient() {
506 mBinder.unlinkToDeath(this, 0);
507 }
508
509 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800510 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800511 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
512 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700513 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700514 }
515
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700516 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800517 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800518 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
519 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700520 stopUsingNetworkFeature(this, false);
521 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800522
523 public String toString() {
524 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
525 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
526 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700527 }
528
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700529 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 public int startUsingNetworkFeature(int networkType, String feature,
531 IBinder binder) {
532 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800533 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700534 ": " + feature);
535 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800536 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700537 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
538 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700539 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800540 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700541
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700542 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700543
544 // TODO - move this into the MobileDataStateTracker
545 int usedNetworkType = networkType;
546 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800547 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800548 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800549 return Phone.APN_TYPE_NOT_AVAILABLE;
550 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
552 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
553 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
554 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
555 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
556 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
557 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
558 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
559 }
560 }
561 NetworkStateTracker network = mNetTrackers[usedNetworkType];
562 if (network != null) {
563 if (usedNetworkType != networkType) {
564 Integer currentPid = new Integer(getCallingPid());
565
566 NetworkStateTracker radio = mNetTrackers[networkType];
567 NetworkInfo ni = network.getNetworkInfo();
568
569 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800570 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700571 return Phone.APN_TYPE_NOT_AVAILABLE;
572 }
573
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700574 synchronized(this) {
575 mFeatureUsers.add(f);
576 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
577 // this gets used for per-pid dns when connected
578 mNetRequestersPids[usedNetworkType].add(currentPid);
579 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700580 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700581 mHandler.sendMessageDelayed(mHandler.obtainMessage(
582 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
583 f), getRestoreDefaultNetworkDelay());
584
Robert Greenwalt2034b912009-08-12 16:08:25 -0700585
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700586 if ((ni.isConnectedOrConnecting() == true) &&
587 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700588 if (ni.isConnected() == true) {
589 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700590 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800591 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700592 return Phone.APN_ALREADY_ACTIVE;
593 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800594 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700595 return Phone.APN_REQUEST_STARTED;
596 }
597
598 // check if the radio in play can make another contact
599 // assume if cannot for now
600
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800601 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 network.reconnect();
603 return Phone.APN_REQUEST_STARTED;
604 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700605 synchronized(this) {
606 mFeatureUsers.add(f);
607 }
608 mHandler.sendMessageDelayed(mHandler.obtainMessage(
609 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
610 f), getRestoreDefaultNetworkDelay());
611
Robert Greenwalt2034b912009-08-12 16:08:25 -0700612 return network.startUsingNetworkFeature(feature,
613 getCallingPid(), getCallingUid());
614 }
615 }
616 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800617 }
618
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700619 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800620 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700621 enforceChangePermission();
622
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700623 int pid = getCallingPid();
624 int uid = getCallingUid();
625
626 FeatureUser u = null;
627 boolean found = false;
628
629 synchronized(this) {
630 for (int i = 0; i < mFeatureUsers.size() ; i++) {
631 u = (FeatureUser)mFeatureUsers.get(i);
632 if (uid == u.mUid && pid == u.mPid &&
633 networkType == u.mNetworkType &&
634 TextUtils.equals(feature, u.mFeature)) {
635 found = true;
636 break;
637 }
638 }
639 }
640 if (found && u != null) {
641 // stop regardless of how many other time this proc had called start
642 return stopUsingNetworkFeature(u, true);
643 } else {
644 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800645 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700646 return 1;
647 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700648 }
649
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700650 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
651 int networkType = u.mNetworkType;
652 String feature = u.mFeature;
653 int pid = u.mPid;
654 int uid = u.mUid;
655
656 NetworkStateTracker tracker = null;
657 boolean callTeardown = false; // used to carry our decision outside of sync block
658
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800660 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700661 ": " + feature);
662 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700663
The Android Open Source Project28527d22009-03-03 19:31:44 -0800664 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
665 return -1;
666 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700668 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
669 // sync block
670 synchronized(this) {
671 // check if this process still has an outstanding start request
672 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800673 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700674 return 1;
675 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700676 u.unlinkDeathRecipient();
677 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
678 // If we care about duplicate requests, check for that here.
679 //
680 // This is done to support the extension of a request - the app
681 // can request we start the network feature again and renew the
682 // auto-shutoff delay. Normal "stop" calls from the app though
683 // do not pay attention to duplicate requests - in effect the
684 // API does not refcount and a single stop will counter multiple starts.
685 if (ignoreDups == false) {
686 for (int i = 0; i < mFeatureUsers.size() ; i++) {
687 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
688 if (x.mUid == u.mUid && x.mPid == u.mPid &&
689 x.mNetworkType == u.mNetworkType &&
690 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800691 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700692 return 1;
693 }
694 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700695 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700696
697 // TODO - move to MobileDataStateTracker
698 int usedNetworkType = networkType;
699 if (networkType == ConnectivityManager.TYPE_MOBILE) {
700 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
701 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
702 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
703 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
704 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
705 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
706 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
707 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
708 }
709 }
710 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700711 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800712 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700713 return -1;
714 }
715 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700716 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700717 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800718 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800720 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721 "others still using it");
722 return 1;
723 }
724 callTeardown = true;
725 }
726 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800727 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700728 if (callTeardown) {
729 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730 return 1;
731 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700732 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733 return tracker.stopUsingNetworkFeature(feature, pid, uid);
734 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800735 }
736
737 /**
738 * Ensure that a network route exists to deliver traffic to the specified
739 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700740 * @param networkType the type of the network over which traffic to the
741 * specified host is to be routed
742 * @param hostAddress the IP address of the host to which the route is
743 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800744 * @return {@code true} on success, {@code false} on failure
745 */
746 public boolean requestRouteToHost(int networkType, int hostAddress) {
747 enforceChangePermission();
748 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
749 return false;
750 }
751 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700752
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700753 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
754 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700755 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800756 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700757 }
758 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800759 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700760 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800761 }
762
763 /**
764 * @see ConnectivityManager#getBackgroundDataSetting()
765 */
766 public boolean getBackgroundDataSetting() {
767 return Settings.Secure.getInt(mContext.getContentResolver(),
768 Settings.Secure.BACKGROUND_DATA, 1) == 1;
769 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700770
The Android Open Source Project28527d22009-03-03 19:31:44 -0800771 /**
772 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
773 */
774 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
775 mContext.enforceCallingOrSelfPermission(
776 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
777 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700778
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
780
781 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700782 Settings.Secure.BACKGROUND_DATA,
783 allowBackgroundDataUsage ? 1 : 0);
784
The Android Open Source Project28527d22009-03-03 19:31:44 -0800785 Intent broadcast = new Intent(
786 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
787 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700788 }
789
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800790 /**
791 * @see ConnectivityManager#getMobileDataEnabled()
792 */
793 public boolean getMobileDataEnabled() {
794 enforceAccessPermission();
795 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
796 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800797 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800798 return retVal;
799 }
800
801 /**
802 * @see ConnectivityManager#setMobileDataEnabled(boolean)
803 */
804 public synchronized void setMobileDataEnabled(boolean enabled) {
805 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800806 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800807
808 if (getMobileDataEnabled() == enabled) return;
809
810 Settings.Secure.putInt(mContext.getContentResolver(),
811 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
812
813 if (enabled) {
814 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800815 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800816 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
817 }
818 } else {
819 for (NetworkStateTracker nt : mNetTrackers) {
820 if (nt == null) continue;
821 int netType = nt.getNetworkInfo().getType();
822 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800823 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800824 nt.teardown();
825 }
826 }
827 }
828 }
829
The Android Open Source Project28527d22009-03-03 19:31:44 -0800830 private int getNumConnectedNetworks() {
831 int numConnectedNets = 0;
832
833 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700834 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700835 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800836 ++numConnectedNets;
837 }
838 }
839 return numConnectedNets;
840 }
841
842 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700843 mContext.enforceCallingOrSelfPermission(
844 android.Manifest.permission.ACCESS_NETWORK_STATE,
845 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800846 }
847
848 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700849 mContext.enforceCallingOrSelfPermission(
850 android.Manifest.permission.CHANGE_NETWORK_STATE,
851 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800852 }
853
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800854 // TODO Make this a special check when it goes public
855 private void enforceTetherChangePermission() {
856 mContext.enforceCallingOrSelfPermission(
857 android.Manifest.permission.CHANGE_NETWORK_STATE,
858 "ConnectivityService");
859 }
860
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800861 private void enforceTetherAccessPermission() {
862 mContext.enforceCallingOrSelfPermission(
863 android.Manifest.permission.ACCESS_NETWORK_STATE,
864 "ConnectivityService");
865 }
866
The Android Open Source Project28527d22009-03-03 19:31:44 -0800867 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700868 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
869 * network, we ignore it. If it is for the active network, we send out a
870 * broadcast. But first, we check whether it might be possible to connect
871 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 * @param info the {@code NetworkInfo} for the network
873 */
874 private void handleDisconnect(NetworkInfo info) {
875
Robert Greenwalt2034b912009-08-12 16:08:25 -0700876 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800877
Robert Greenwalt2034b912009-08-12 16:08:25 -0700878 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800879 /*
880 * If the disconnected network is not the active one, then don't report
881 * this as a loss of connectivity. What probably happened is that we're
882 * getting the disconnect for a network that we explicitly disabled
883 * in accordance with network preference policies.
884 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700885 if (!mNetAttributes[prevNetType].isDefault()) {
886 List pids = mNetRequestersPids[prevNetType];
887 for (int i = 0; i<pids.size(); i++) {
888 Integer pid = (Integer)pids.get(i);
889 // will remove them because the net's no longer connected
890 // need to do this now as only now do we know the pids and
891 // can properly null things that are no longer referenced.
892 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800893 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800894 }
895
The Android Open Source Project28527d22009-03-03 19:31:44 -0800896 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
897 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
898 if (info.isFailover()) {
899 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
900 info.setFailover(false);
901 }
902 if (info.getReason() != null) {
903 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
904 }
905 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700906 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
907 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800908 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700909
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800910 NetworkStateTracker newNet = null;
911 if (mNetAttributes[prevNetType].isDefault()) {
912 newNet = tryFailover(prevNetType);
913 if (newNet != null) {
914 NetworkInfo switchTo = newNet.getNetworkInfo();
915 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
916 } else {
917 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
918 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800919 }
920 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700921 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800922
923 sendStickyBroadcast(intent);
924 /*
925 * If the failover network is already connected, then immediately send
926 * out a followup broadcast indicating successful failover
927 */
928 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
929 sendConnectedBroadcast(newNet.getNetworkInfo());
930 }
931 }
932
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800933 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800934 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700935 /*
936 * If this is a default network, check if other defaults are available
937 * or active
938 */
939 NetworkStateTracker newNet = null;
940 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700941 if (mActiveDefaultNetwork == prevNetType) {
942 mActiveDefaultNetwork = -1;
943 }
944
945 int newType = -1;
946 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800947 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800948 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700949 if (checkType == prevNetType) continue;
950 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800951 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
952 noMobileData) {
953 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800954 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800955 " because Mobile Data Disabled");
956 }
957 continue;
958 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700959 if (mNetAttributes[checkType].isDefault()) {
960 /* TODO - if we have multiple nets we could use
961 * we may want to put more thought into which we choose
962 */
963 if (checkType == mNetworkPreference) {
964 newType = checkType;
965 break;
966 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700967 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700968 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700969 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700970 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800971 }
972 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700973
974 if (newType != -1) {
975 newNet = mNetTrackers[newType];
976 /**
977 * See if the other network is available to fail over to.
978 * If is not available, we enable it anyway, so that it
979 * will be able to connect when it does become available,
980 * but we report a total loss of connectivity rather than
981 * report that we are attempting to fail over.
982 */
983 if (newNet.isAvailable()) {
984 NetworkInfo switchTo = newNet.getNetworkInfo();
985 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700986 if (!switchTo.isConnectedOrConnecting() ||
987 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700988 newNet.reconnect();
989 }
990 if (DBG) {
991 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800992 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700993 switchTo.getTypeName());
994 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800995 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 switchTo.getTypeName());
997 }
998 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700999 } else {
1000 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001001 newNet = null; // not officially avail.. try anyway, but
1002 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001003 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001004 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001005 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001006
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001007 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001008 }
1009
1010 private void sendConnectedBroadcast(NetworkInfo info) {
1011 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1012 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1013 if (info.isFailover()) {
1014 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1015 info.setFailover(false);
1016 }
1017 if (info.getReason() != null) {
1018 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1019 }
1020 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001021 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1022 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001023 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001024 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001025 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001026 }
1027
1028 /**
1029 * Called when an attempt to fail over to another network has failed.
1030 * @param info the {@link NetworkInfo} for the failed network
1031 */
1032 private void handleConnectionFailure(NetworkInfo info) {
1033 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001034
Robert Greenwalt2034b912009-08-12 16:08:25 -07001035 String reason = info.getReason();
1036 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001037
Robert Greenwalt2034b912009-08-12 16:08:25 -07001038 if (DBG) {
1039 String reasonText;
1040 if (reason == null) {
1041 reasonText = ".";
1042 } else {
1043 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001044 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001045 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001047 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001048
1049 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1050 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1051 if (getActiveNetworkInfo() == null) {
1052 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1053 }
1054 if (reason != null) {
1055 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1056 }
1057 if (extraInfo != null) {
1058 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1059 }
1060 if (info.isFailover()) {
1061 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1062 info.setFailover(false);
1063 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001064
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001065 NetworkStateTracker newNet = null;
1066 if (mNetAttributes[info.getType()].isDefault()) {
1067 newNet = tryFailover(info.getType());
1068 if (newNet != null) {
1069 NetworkInfo switchTo = newNet.getNetworkInfo();
1070 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1071 } else {
1072 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1073 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001074 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001075
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001076 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001077 /*
1078 * If the failover network is already connected, then immediately send
1079 * out a followup broadcast indicating successful failover
1080 */
1081 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1082 sendConnectedBroadcast(newNet.getNetworkInfo());
1083 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001084 }
1085
1086 private void sendStickyBroadcast(Intent intent) {
1087 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001088 if (!mSystemReady) {
1089 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001090 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001091 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1092 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001093 }
1094 }
1095
1096 void systemReady() {
1097 synchronized(this) {
1098 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001099 if (mInitialBroadcast != null) {
1100 mContext.sendStickyBroadcast(mInitialBroadcast);
1101 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001102 }
1103 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001104 }
1105
1106 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001107 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001108
1109 // snapshot isFailover, because sendConnectedBroadcast() resets it
1110 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001111 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001112
Robert Greenwalt2034b912009-08-12 16:08:25 -07001113 // if this is a default net and other default is running
1114 // kill the one not preferred
1115 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001116 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1117 if ((type != mNetworkPreference &&
1118 mNetAttributes[mActiveDefaultNetwork].mPriority >
1119 mNetAttributes[type].mPriority) ||
1120 mNetworkPreference == mActiveDefaultNetwork) {
1121 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001122 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 "to torn down network " + info.getTypeName());
1124 teardown(thisNet);
1125 return;
1126 } else {
1127 // tear down the other
1128 NetworkStateTracker otherNet =
1129 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001130 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001131 otherNet.getNetworkInfo().getTypeName() +
1132 " teardown");
1133 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001134 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001135 return;
1136 }
1137 if (isFailover) {
1138 otherNet.releaseWakeLock();
1139 }
1140 }
1141 }
1142 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001143 // this will cause us to come up initially as unconnected and switching
1144 // to connected after our normal pause unless somebody reports us as reall
1145 // disconnected
1146 mDefaultInetConditionPublished = 0;
1147 mDefaultConnectionSequence++;
1148 mInetConditionChangeInFlight = false;
1149 // Don't do this - if we never sign in stay, grey
1150 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001151 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001152 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001153 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001154 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001156 }
1157
1158 private void handleScanResultsAvailable(NetworkInfo info) {
1159 int networkType = info.getType();
1160 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001161 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001162 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001163 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001164
The Android Open Source Project28527d22009-03-03 19:31:44 -08001165 mNetTrackers[networkType].interpretScanResultsAvailable();
1166 }
1167
Robert Greenwalt0659da32009-07-16 17:21:39 -07001168 private void handleNotificationChange(boolean visible, int id,
1169 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001170 NotificationManager notificationManager = (NotificationManager) mContext
1171 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001172
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173 if (visible) {
1174 notificationManager.notify(id, notification);
1175 } else {
1176 notificationManager.cancel(id);
1177 }
1178 }
1179
1180 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001181 * After a change in the connectivity state of any network, We're mainly
1182 * concerned with making sure that the list of DNS servers is setupup
1183 * according to which networks are connected, and ensuring that the
1184 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001185 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001186 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001189 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001190 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001191 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001192
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001193 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1194 if (mNetAttributes[netType].isDefault()) {
1195 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001197 mNetTrackers[netType].addPrivateDnsRoutes();
1198 }
1199 } else {
1200 if (mNetAttributes[netType].isDefault()) {
1201 mNetTrackers[netType].removeDefaultRoute();
1202 } else {
1203 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001205 }
1206 }
1207
Robert Greenwalt2034b912009-08-12 16:08:25 -07001208 /**
1209 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1210 * on the highest priority active net which this process requested.
1211 * If there aren't any, clear it out
1212 */
1213 private void reassessPidDns(int myPid, boolean doBump)
1214 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001215 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001216 for(int i : mPriorityList) {
1217 if (mNetAttributes[i].isDefault()) {
1218 continue;
1219 }
1220 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001221 if (nt.getNetworkInfo().isConnected() &&
1222 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001223 List pids = mNetRequestersPids[i];
1224 for (int j=0; j<pids.size(); j++) {
1225 Integer pid = (Integer)pids.get(j);
1226 if (pid.intValue() == myPid) {
1227 String[] dnsList = nt.getNameServers();
1228 writePidDns(dnsList, myPid);
1229 if (doBump) {
1230 bumpDns();
1231 }
1232 return;
1233 }
1234 }
1235 }
1236 }
1237 // nothing found - delete
1238 for (int i = 1; ; i++) {
1239 String prop = "net.dns" + i + "." + myPid;
1240 if (SystemProperties.get(prop).length() == 0) {
1241 if (doBump) {
1242 bumpDns();
1243 }
1244 return;
1245 }
1246 SystemProperties.set(prop, "");
1247 }
1248 }
1249
1250 private void writePidDns(String[] dnsList, int pid) {
1251 int j = 1;
1252 for (String dns : dnsList) {
1253 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1254 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1255 }
1256 }
1257 }
1258
1259 private void bumpDns() {
1260 /*
1261 * Bump the property that tells the name resolver library to reread
1262 * the DNS server list from the properties.
1263 */
1264 String propVal = SystemProperties.get("net.dnschange");
1265 int n = 0;
1266 if (propVal.length() != 0) {
1267 try {
1268 n = Integer.parseInt(propVal);
1269 } catch (NumberFormatException e) {}
1270 }
1271 SystemProperties.set("net.dnschange", "" + (n+1));
1272 }
1273
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001274 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001275 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001276 NetworkStateTracker nt = mNetTrackers[netType];
1277 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1278 String[] dnsList = nt.getNameServers();
1279 if (mNetAttributes[netType].isDefault()) {
1280 int j = 1;
1281 for (String dns : dnsList) {
1282 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1283 if (DBG) {
1284 Slog.d(TAG, "adding dns " + dns + " for " +
1285 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001287 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001289 }
1290 for (int k=j ; k<mNumDnsEntries; k++) {
1291 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1292 SystemProperties.set("net.dns" + k, "");
1293 }
1294 mNumDnsEntries = j;
1295 } else {
1296 // set per-pid dns for attached secondary nets
1297 List pids = mNetRequestersPids[netType];
1298 for (int y=0; y< pids.size(); y++) {
1299 Integer pid = (Integer)pids.get(y);
1300 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001301 }
1302 }
1303 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001304 bumpDns();
1305 }
1306
1307 private int getRestoreDefaultNetworkDelay() {
1308 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1309 NETWORK_RESTORE_DELAY_PROP_NAME);
1310 if(restoreDefaultNetworkDelayStr != null &&
1311 restoreDefaultNetworkDelayStr.length() != 0) {
1312 try {
1313 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1314 } catch (NumberFormatException e) {
1315 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001316 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001317 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001318 }
1319
1320 @Override
1321 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001322 if (mContext.checkCallingOrSelfPermission(
1323 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001325 pw.println("Permission Denial: can't dump ConnectivityService " +
1326 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1327 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001328 return;
1329 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001330 pw.println();
1331 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001332 if (nst != null) {
1333 if (nst.getNetworkInfo().isConnected()) {
1334 pw.println("Active network: " + nst.getNetworkInfo().
1335 getTypeName());
1336 }
1337 pw.println(nst.getNetworkInfo());
1338 pw.println(nst);
1339 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001340 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001341 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001342
1343 pw.println("Network Requester Pids:");
1344 for (int net : mPriorityList) {
1345 String pidString = net + ": ";
1346 for (Object pid : mNetRequestersPids[net]) {
1347 pidString = pidString + pid.toString() + ", ";
1348 }
1349 pw.println(pidString);
1350 }
1351 pw.println();
1352
1353 pw.println("FeatureUsers:");
1354 for (Object requester : mFeatureUsers) {
1355 pw.println(requester.toString());
1356 }
1357 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001358
1359 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001360 }
1361
Robert Greenwalt2034b912009-08-12 16:08:25 -07001362 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001363 private class MyHandler extends Handler {
1364 @Override
1365 public void handleMessage(Message msg) {
1366 NetworkInfo info;
1367 switch (msg.what) {
1368 case NetworkStateTracker.EVENT_STATE_CHANGED:
1369 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001370 int type = info.getType();
1371 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001372 // only do this optimization for wifi. It going into scan mode for location
1373 // services generates alot of noise. Meanwhile the mms apn won't send out
1374 // subsequent notifications when on default cellular because it never
1375 // disconnects.. so only do this to wifi notifications. Fixed better when the
1376 // APN notifications are standardized.
1377 if (mNetAttributes[type].mLastState == state &&
1378 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001379 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001380 // TODO - remove this after we validate the dropping doesn't break
1381 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001382 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001383 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001384 state + "/" + info.getDetailedState());
1385 }
1386 return;
1387 }
1388 mNetAttributes[type].mLastState = state;
1389
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001390 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001391 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001392 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001393
1394 // Connectivity state changed:
1395 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001396 // [12-9] Network subtype (for mobile network, as defined
1397 // by TelephonyManager)
1398 // [8-3] Detailed state ordinal (as defined by
1399 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001400 // [2-0] Network type (as defined by ConnectivityManager)
1401 int eventLogParam = (info.getType() & 0x7) |
1402 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1403 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001404 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001405 eventLogParam);
1406
1407 if (info.getDetailedState() ==
1408 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001409 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001410 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001411 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001412 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001413 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001414 // the logic here is, handle SUSPENDED the same as
1415 // DISCONNECTED. The only difference being we are
1416 // broadcasting an intent with NetworkInfo that's
1417 // suspended. This allows the applications an
1418 // opportunity to handle DISCONNECTED and SUSPENDED
1419 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001420 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001421 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001422 handleConnect(info);
1423 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001424 break;
1425
1426 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1427 info = (NetworkInfo) msg.obj;
1428 handleScanResultsAvailable(info);
1429 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001430
The Android Open Source Project28527d22009-03-03 19:31:44 -08001431 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001432 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1433 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001434 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001435
1436 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001437 info = (NetworkInfo) msg.obj;
1438 type = info.getType();
1439 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001440 break;
1441
1442 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1443 // fill me in
1444 break;
1445
1446 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1447 // fill me in
1448 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001449 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001450 FeatureUser u = (FeatureUser)msg.obj;
1451 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001452 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001453 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1454 if (DBG) {
1455 Slog.d(TAG, "Inet connectivity change, net=" +
1456 msg.arg1 + ", condition=" + msg.arg2 +
1457 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1458 }
1459 if (mActiveDefaultNetwork == -1) {
1460 if (DBG) Slog.d(TAG, "no active default network - aborting");
1461 break;
1462 }
1463 if (mActiveDefaultNetwork != msg.arg1) {
1464 if (DBG) Slog.d(TAG, "given net not default - aborting");
1465 break;
1466 }
1467 mDefaultInetCondition = msg.arg2;
1468 int delay;
1469 if (mInetConditionChangeInFlight == false) {
1470 if (DBG) Slog.d(TAG, "starting a change hold");
1471 // setup a new hold to debounce this
1472 if (mDefaultInetCondition > 50) {
1473 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1474 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1475 } else {
1476 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1477 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1478 }
1479 mInetConditionChangeInFlight = true;
1480 sendMessageDelayed(obtainMessage(
1481 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1482 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1483 } else {
1484 // we've set the new condition, when this hold ends that will get
1485 // picked up
1486 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1487 }
1488 break;
1489 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1490 if (DBG) {
1491 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1492 ", condition =" + mDefaultInetCondition +
1493 ", published condition =" + mDefaultInetConditionPublished);
1494 }
1495 mInetConditionChangeInFlight = false;
1496
1497 if (mActiveDefaultNetwork == -1) {
1498 if (DBG) Slog.d(TAG, "no active default network - aborting");
1499 break;
1500 }
1501 if (mDefaultConnectionSequence != msg.arg2) {
1502 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1503 break;
1504 }
1505 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1506 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1507 break;
1508 }
1509 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1510 if (networkInfo.isConnected() == false) {
1511 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1512 break;
1513 }
1514 mDefaultInetConditionPublished = mDefaultInetCondition;
1515 sendConnectedBroadcast(networkInfo);
1516 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001517 }
1518 }
1519 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001520
1521 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001522 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001523 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001524
1525 if (isTetheringSupported()) {
1526 return mTethering.tether(iface);
1527 } else {
1528 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1529 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001530 }
1531
1532 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001533 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001534 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001535
1536 if (isTetheringSupported()) {
1537 return mTethering.untether(iface);
1538 } else {
1539 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1540 }
1541 }
1542
1543 // javadoc from interface
1544 public int getLastTetherError(String iface) {
1545 enforceTetherAccessPermission();
1546
1547 if (isTetheringSupported()) {
1548 return mTethering.getLastTetherError(iface);
1549 } else {
1550 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1551 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001552 }
1553
1554 // TODO - proper iface API for selection by property, inspection, etc
1555 public String[] getTetherableUsbRegexs() {
1556 enforceTetherAccessPermission();
1557 if (isTetheringSupported()) {
1558 return mTethering.getTetherableUsbRegexs();
1559 } else {
1560 return new String[0];
1561 }
1562 }
1563
1564 public String[] getTetherableWifiRegexs() {
1565 enforceTetherAccessPermission();
1566 if (isTetheringSupported()) {
1567 return mTethering.getTetherableWifiRegexs();
1568 } else {
1569 return new String[0];
1570 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001571 }
1572
1573 // TODO - move iface listing, queries, etc to new module
1574 // javadoc from interface
1575 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001576 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001577 return mTethering.getTetherableIfaces();
1578 }
1579
1580 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001581 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001582 return mTethering.getTetheredIfaces();
1583 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001584
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001585 public String[] getTetheringErroredIfaces() {
1586 enforceTetherAccessPermission();
1587 return mTethering.getErroredIfaces();
1588 }
1589
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001590 // if ro.tether.denied = true we default to no tethering
1591 // gservices could set the secure setting to 1 though to enable it on a build where it
1592 // had previously been turned off.
1593 public boolean isTetheringSupported() {
1594 enforceTetherAccessPermission();
1595 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001596 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1597 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1598 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001599 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001600
1601 // 100 percent is full good, 0 is full bad.
1602 public void reportInetCondition(int networkType, int percentage) {
1603 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1604 mContext.enforceCallingOrSelfPermission(
1605 android.Manifest.permission.STATUS_BAR,
1606 "ConnectivityService");
1607
1608 mHandler.sendMessage(mHandler.obtainMessage(
1609 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1610 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001611}