blob: c5505d1139a0b92cc973902a5229c30541deef4f [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) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001011 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1012 }
1013
1014 private void sendInetConditionBroadcast(NetworkInfo info) {
1015 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1016 }
1017
1018 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1019 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001020 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1021 if (info.isFailover()) {
1022 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1023 info.setFailover(false);
1024 }
1025 if (info.getReason() != null) {
1026 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1027 }
1028 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001029 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1030 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001031 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001032 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001033 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001034 }
1035
1036 /**
1037 * Called when an attempt to fail over to another network has failed.
1038 * @param info the {@link NetworkInfo} for the failed network
1039 */
1040 private void handleConnectionFailure(NetworkInfo info) {
1041 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001042
Robert Greenwalt2034b912009-08-12 16:08:25 -07001043 String reason = info.getReason();
1044 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001045
Robert Greenwalt2034b912009-08-12 16:08:25 -07001046 if (DBG) {
1047 String reasonText;
1048 if (reason == null) {
1049 reasonText = ".";
1050 } else {
1051 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001052 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001053 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001055 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001056
1057 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1058 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1059 if (getActiveNetworkInfo() == null) {
1060 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1061 }
1062 if (reason != null) {
1063 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1064 }
1065 if (extraInfo != null) {
1066 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1067 }
1068 if (info.isFailover()) {
1069 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1070 info.setFailover(false);
1071 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001072
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001073 NetworkStateTracker newNet = null;
1074 if (mNetAttributes[info.getType()].isDefault()) {
1075 newNet = tryFailover(info.getType());
1076 if (newNet != null) {
1077 NetworkInfo switchTo = newNet.getNetworkInfo();
1078 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1079 } else {
1080 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1081 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001082 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001083
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001084 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001085 /*
1086 * If the failover network is already connected, then immediately send
1087 * out a followup broadcast indicating successful failover
1088 */
1089 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1090 sendConnectedBroadcast(newNet.getNetworkInfo());
1091 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001092 }
1093
1094 private void sendStickyBroadcast(Intent intent) {
1095 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001096 if (!mSystemReady) {
1097 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001098 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001099 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1100 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001101 }
1102 }
1103
1104 void systemReady() {
1105 synchronized(this) {
1106 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001107 if (mInitialBroadcast != null) {
1108 mContext.sendStickyBroadcast(mInitialBroadcast);
1109 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001110 }
1111 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001112 }
1113
1114 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001115 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001116
1117 // snapshot isFailover, because sendConnectedBroadcast() resets it
1118 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001119 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120
Robert Greenwalt2034b912009-08-12 16:08:25 -07001121 // if this is a default net and other default is running
1122 // kill the one not preferred
1123 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001124 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1125 if ((type != mNetworkPreference &&
1126 mNetAttributes[mActiveDefaultNetwork].mPriority >
1127 mNetAttributes[type].mPriority) ||
1128 mNetworkPreference == mActiveDefaultNetwork) {
1129 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001130 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001131 "to torn down network " + info.getTypeName());
1132 teardown(thisNet);
1133 return;
1134 } else {
1135 // tear down the other
1136 NetworkStateTracker otherNet =
1137 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001138 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001139 otherNet.getNetworkInfo().getTypeName() +
1140 " teardown");
1141 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001142 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001143 return;
1144 }
1145 if (isFailover) {
1146 otherNet.releaseWakeLock();
1147 }
1148 }
1149 }
1150 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001151 // this will cause us to come up initially as unconnected and switching
1152 // to connected after our normal pause unless somebody reports us as reall
1153 // disconnected
1154 mDefaultInetConditionPublished = 0;
1155 mDefaultConnectionSequence++;
1156 mInetConditionChangeInFlight = false;
1157 // Don't do this - if we never sign in stay, grey
1158 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001159 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001160 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001161 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001162 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001163 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001164 }
1165
1166 private void handleScanResultsAvailable(NetworkInfo info) {
1167 int networkType = info.getType();
1168 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001169 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001170 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001171 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001172
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173 mNetTrackers[networkType].interpretScanResultsAvailable();
1174 }
1175
Robert Greenwalt0659da32009-07-16 17:21:39 -07001176 private void handleNotificationChange(boolean visible, int id,
1177 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001178 NotificationManager notificationManager = (NotificationManager) mContext
1179 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001180
The Android Open Source Project28527d22009-03-03 19:31:44 -08001181 if (visible) {
1182 notificationManager.notify(id, notification);
1183 } else {
1184 notificationManager.cancel(id);
1185 }
1186 }
1187
1188 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001189 * After a change in the connectivity state of any network, We're mainly
1190 * concerned with making sure that the list of DNS servers is setupup
1191 * according to which networks are connected, and ensuring that the
1192 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001193 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001194 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001195 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001197 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001198 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001199 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001201 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1202 if (mNetAttributes[netType].isDefault()) {
1203 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001205 mNetTrackers[netType].addPrivateDnsRoutes();
1206 }
1207 } else {
1208 if (mNetAttributes[netType].isDefault()) {
1209 mNetTrackers[netType].removeDefaultRoute();
1210 } else {
1211 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001212 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 }
1214 }
1215
Robert Greenwalt2034b912009-08-12 16:08:25 -07001216 /**
1217 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1218 * on the highest priority active net which this process requested.
1219 * If there aren't any, clear it out
1220 */
1221 private void reassessPidDns(int myPid, boolean doBump)
1222 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001223 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001224 for(int i : mPriorityList) {
1225 if (mNetAttributes[i].isDefault()) {
1226 continue;
1227 }
1228 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001229 if (nt.getNetworkInfo().isConnected() &&
1230 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001231 List pids = mNetRequestersPids[i];
1232 for (int j=0; j<pids.size(); j++) {
1233 Integer pid = (Integer)pids.get(j);
1234 if (pid.intValue() == myPid) {
1235 String[] dnsList = nt.getNameServers();
1236 writePidDns(dnsList, myPid);
1237 if (doBump) {
1238 bumpDns();
1239 }
1240 return;
1241 }
1242 }
1243 }
1244 }
1245 // nothing found - delete
1246 for (int i = 1; ; i++) {
1247 String prop = "net.dns" + i + "." + myPid;
1248 if (SystemProperties.get(prop).length() == 0) {
1249 if (doBump) {
1250 bumpDns();
1251 }
1252 return;
1253 }
1254 SystemProperties.set(prop, "");
1255 }
1256 }
1257
1258 private void writePidDns(String[] dnsList, int pid) {
1259 int j = 1;
1260 for (String dns : dnsList) {
1261 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1262 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1263 }
1264 }
1265 }
1266
1267 private void bumpDns() {
1268 /*
1269 * Bump the property that tells the name resolver library to reread
1270 * the DNS server list from the properties.
1271 */
1272 String propVal = SystemProperties.get("net.dnschange");
1273 int n = 0;
1274 if (propVal.length() != 0) {
1275 try {
1276 n = Integer.parseInt(propVal);
1277 } catch (NumberFormatException e) {}
1278 }
1279 SystemProperties.set("net.dnschange", "" + (n+1));
1280 }
1281
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001282 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001283 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001284 NetworkStateTracker nt = mNetTrackers[netType];
1285 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1286 String[] dnsList = nt.getNameServers();
1287 if (mNetAttributes[netType].isDefault()) {
1288 int j = 1;
1289 for (String dns : dnsList) {
1290 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1291 if (DBG) {
1292 Slog.d(TAG, "adding dns " + dns + " for " +
1293 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001294 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001295 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001296 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001297 }
1298 for (int k=j ; k<mNumDnsEntries; k++) {
1299 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1300 SystemProperties.set("net.dns" + k, "");
1301 }
1302 mNumDnsEntries = j;
1303 } else {
1304 // set per-pid dns for attached secondary nets
1305 List pids = mNetRequestersPids[netType];
1306 for (int y=0; y< pids.size(); y++) {
1307 Integer pid = (Integer)pids.get(y);
1308 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001309 }
1310 }
1311 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001312 bumpDns();
1313 }
1314
1315 private int getRestoreDefaultNetworkDelay() {
1316 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1317 NETWORK_RESTORE_DELAY_PROP_NAME);
1318 if(restoreDefaultNetworkDelayStr != null &&
1319 restoreDefaultNetworkDelayStr.length() != 0) {
1320 try {
1321 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1322 } catch (NumberFormatException e) {
1323 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001325 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001326 }
1327
1328 @Override
1329 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001330 if (mContext.checkCallingOrSelfPermission(
1331 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001332 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001333 pw.println("Permission Denial: can't dump ConnectivityService " +
1334 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1335 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001336 return;
1337 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001338 pw.println();
1339 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001340 if (nst != null) {
1341 if (nst.getNetworkInfo().isConnected()) {
1342 pw.println("Active network: " + nst.getNetworkInfo().
1343 getTypeName());
1344 }
1345 pw.println(nst.getNetworkInfo());
1346 pw.println(nst);
1347 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001348 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001349 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001350
1351 pw.println("Network Requester Pids:");
1352 for (int net : mPriorityList) {
1353 String pidString = net + ": ";
1354 for (Object pid : mNetRequestersPids[net]) {
1355 pidString = pidString + pid.toString() + ", ";
1356 }
1357 pw.println(pidString);
1358 }
1359 pw.println();
1360
1361 pw.println("FeatureUsers:");
1362 for (Object requester : mFeatureUsers) {
1363 pw.println(requester.toString());
1364 }
1365 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001366
1367 mTethering.dump(fd, pw, args);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001368 }
1369
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001371 private class MyHandler extends Handler {
1372 @Override
1373 public void handleMessage(Message msg) {
1374 NetworkInfo info;
1375 switch (msg.what) {
1376 case NetworkStateTracker.EVENT_STATE_CHANGED:
1377 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001378 int type = info.getType();
1379 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001380 // only do this optimization for wifi. It going into scan mode for location
1381 // services generates alot of noise. Meanwhile the mms apn won't send out
1382 // subsequent notifications when on default cellular because it never
1383 // disconnects.. so only do this to wifi notifications. Fixed better when the
1384 // APN notifications are standardized.
1385 if (mNetAttributes[type].mLastState == state &&
1386 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001387 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001388 // TODO - remove this after we validate the dropping doesn't break
1389 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001390 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001391 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001392 state + "/" + info.getDetailedState());
1393 }
1394 return;
1395 }
1396 mNetAttributes[type].mLastState = state;
1397
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001398 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001399 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001400 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001401
1402 // Connectivity state changed:
1403 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001404 // [12-9] Network subtype (for mobile network, as defined
1405 // by TelephonyManager)
1406 // [8-3] Detailed state ordinal (as defined by
1407 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001408 // [2-0] Network type (as defined by ConnectivityManager)
1409 int eventLogParam = (info.getType() & 0x7) |
1410 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1411 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001412 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001413 eventLogParam);
1414
1415 if (info.getDetailedState() ==
1416 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001417 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001418 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001419 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001420 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001421 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001422 // the logic here is, handle SUSPENDED the same as
1423 // DISCONNECTED. The only difference being we are
1424 // broadcasting an intent with NetworkInfo that's
1425 // suspended. This allows the applications an
1426 // opportunity to handle DISCONNECTED and SUSPENDED
1427 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001428 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001429 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001430 handleConnect(info);
1431 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001432 break;
1433
1434 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1435 info = (NetworkInfo) msg.obj;
1436 handleScanResultsAvailable(info);
1437 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001438
The Android Open Source Project28527d22009-03-03 19:31:44 -08001439 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001440 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1441 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001442 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001443
1444 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001445 info = (NetworkInfo) msg.obj;
1446 type = info.getType();
1447 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001448 break;
1449
1450 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1451 // fill me in
1452 break;
1453
1454 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1455 // fill me in
1456 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001457 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001458 FeatureUser u = (FeatureUser)msg.obj;
1459 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001460 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001461 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1462 if (DBG) {
1463 Slog.d(TAG, "Inet connectivity change, net=" +
1464 msg.arg1 + ", condition=" + msg.arg2 +
1465 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1466 }
1467 if (mActiveDefaultNetwork == -1) {
1468 if (DBG) Slog.d(TAG, "no active default network - aborting");
1469 break;
1470 }
1471 if (mActiveDefaultNetwork != msg.arg1) {
1472 if (DBG) Slog.d(TAG, "given net not default - aborting");
1473 break;
1474 }
1475 mDefaultInetCondition = msg.arg2;
1476 int delay;
1477 if (mInetConditionChangeInFlight == false) {
1478 if (DBG) Slog.d(TAG, "starting a change hold");
1479 // setup a new hold to debounce this
1480 if (mDefaultInetCondition > 50) {
1481 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1482 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1483 } else {
1484 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1485 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1486 }
1487 mInetConditionChangeInFlight = true;
1488 sendMessageDelayed(obtainMessage(
1489 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1490 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1491 } else {
1492 // we've set the new condition, when this hold ends that will get
1493 // picked up
1494 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1495 }
1496 break;
1497 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1498 if (DBG) {
1499 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1500 ", condition =" + mDefaultInetCondition +
1501 ", published condition =" + mDefaultInetConditionPublished);
1502 }
1503 mInetConditionChangeInFlight = false;
1504
1505 if (mActiveDefaultNetwork == -1) {
1506 if (DBG) Slog.d(TAG, "no active default network - aborting");
1507 break;
1508 }
1509 if (mDefaultConnectionSequence != msg.arg2) {
1510 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1511 break;
1512 }
1513 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1514 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1515 break;
1516 }
1517 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1518 if (networkInfo.isConnected() == false) {
1519 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1520 break;
1521 }
1522 mDefaultInetConditionPublished = mDefaultInetCondition;
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001523 sendInetConditionBroadcast(networkInfo);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001524 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001525 }
1526 }
1527 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001528
1529 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001530 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001531 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001532
1533 if (isTetheringSupported()) {
1534 return mTethering.tether(iface);
1535 } else {
1536 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1537 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001538 }
1539
1540 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001541 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001542 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001543
1544 if (isTetheringSupported()) {
1545 return mTethering.untether(iface);
1546 } else {
1547 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1548 }
1549 }
1550
1551 // javadoc from interface
1552 public int getLastTetherError(String iface) {
1553 enforceTetherAccessPermission();
1554
1555 if (isTetheringSupported()) {
1556 return mTethering.getLastTetherError(iface);
1557 } else {
1558 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1559 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001560 }
1561
1562 // TODO - proper iface API for selection by property, inspection, etc
1563 public String[] getTetherableUsbRegexs() {
1564 enforceTetherAccessPermission();
1565 if (isTetheringSupported()) {
1566 return mTethering.getTetherableUsbRegexs();
1567 } else {
1568 return new String[0];
1569 }
1570 }
1571
1572 public String[] getTetherableWifiRegexs() {
1573 enforceTetherAccessPermission();
1574 if (isTetheringSupported()) {
1575 return mTethering.getTetherableWifiRegexs();
1576 } else {
1577 return new String[0];
1578 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001579 }
1580
1581 // TODO - move iface listing, queries, etc to new module
1582 // javadoc from interface
1583 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001584 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001585 return mTethering.getTetherableIfaces();
1586 }
1587
1588 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001589 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001590 return mTethering.getTetheredIfaces();
1591 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001592
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001593 public String[] getTetheringErroredIfaces() {
1594 enforceTetherAccessPermission();
1595 return mTethering.getErroredIfaces();
1596 }
1597
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001598 // if ro.tether.denied = true we default to no tethering
1599 // gservices could set the secure setting to 1 though to enable it on a build where it
1600 // had previously been turned off.
1601 public boolean isTetheringSupported() {
1602 enforceTetherAccessPermission();
1603 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001604 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1605 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1606 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001607 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001608
1609 // 100 percent is full good, 0 is full bad.
1610 public void reportInetCondition(int networkType, int percentage) {
1611 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1612 mContext.enforceCallingOrSelfPermission(
1613 android.Manifest.permission.STATUS_BAR,
1614 "ConnectivityService");
1615
1616 mHandler.sendMessage(mHandler.obtainMessage(
1617 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1618 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001619}