blob: f38748b44e839fe3650596008c4b20cc7554a0e9 [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;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070051import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070052import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053
54/**
55 * @hide
56 */
57public class ConnectivityService extends IConnectivityManager.Stub {
58
Robert Greenwalta25fd712009-10-06 14:12:53 -070059 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080060 private static final String TAG = "ConnectivityService";
61
Robert Greenwalt2034b912009-08-12 16:08:25 -070062 // how long to wait before switching back to a radio's default network
63 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
64 // system property that can override the above value
65 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
66 "android.telephony.apn-restore";
67
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080068
69 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080070 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080071
The Android Open Source Project28527d22009-03-03 19:31:44 -080072 /**
73 * Sometimes we want to refer to the individual network state
74 * trackers separately, and sometimes we just want to treat them
75 * abstractly.
76 */
77 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070078
79 /**
80 * A per Net list of the PID's that requested access to the net
81 * used both as a refcount and for per-PID DNS selection
82 */
83 private List mNetRequestersPids[];
84
Robert Greenwalt2034b912009-08-12 16:08:25 -070085 // priority order of the nettrackers
86 // (excluding dynamically set mNetworkPreference)
87 // TODO - move mNetworkTypePreference into this
88 private int[] mPriorityList;
89
The Android Open Source Project28527d22009-03-03 19:31:44 -080090 private Context mContext;
91 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070092 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -070093 // 0 is full bad, 100 is full good
94 private int mDefaultInetCondition = 0;
95 private int mDefaultInetConditionPublished = 0;
96 private boolean mInetConditionChangeInFlight = false;
97 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -080098
99 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100
101 private boolean mTestMode;
102 private static ConnectivityService sServiceInstance;
103
Robert Greenwalt2034b912009-08-12 16:08:25 -0700104 private Handler mHandler;
105
106 // list of DeathRecipients used to make sure features are turned off when
107 // a process dies
108 private List mFeatureUsers;
109
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400110 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800111 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400112
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700113 // used in DBG mode to track inet condition reports
114 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
115 private ArrayList mInetLog;
116
Robert Greenwalt12c44552009-12-07 11:33:18 -0800117 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700118 /**
119 * Class for holding settings read from resources.
120 */
121 public String mName;
122 public int mType;
123 public int mRadio;
124 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800125 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700126 public NetworkAttributes(String init) {
127 String fragments[] = init.split(",");
128 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700129 mType = Integer.parseInt(fragments[1]);
130 mRadio = Integer.parseInt(fragments[2]);
131 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800132 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700133 }
134 public boolean isDefault() {
135 return (mType == mRadio);
136 }
137 }
138 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700139 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700140
Robert Greenwalt12c44552009-12-07 11:33:18 -0800141 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700142 public int mSimultaneity;
143 public int mType;
144 public RadioAttributes(String init) {
145 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700146 mType = Integer.parseInt(fragments[0]);
147 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700148 }
149 }
150 RadioAttributes[] mRadioAttributes;
151
The Android Open Source Project28527d22009-03-03 19:31:44 -0800152 private static class ConnectivityThread extends Thread {
153 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700154
The Android Open Source Project28527d22009-03-03 19:31:44 -0800155 private ConnectivityThread(Context context) {
156 super("ConnectivityThread");
157 mContext = context;
158 }
159
160 @Override
161 public void run() {
162 Looper.prepare();
163 synchronized (this) {
164 sServiceInstance = new ConnectivityService(mContext);
165 notifyAll();
166 }
167 Looper.loop();
168 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700169
The Android Open Source Project28527d22009-03-03 19:31:44 -0800170 public static ConnectivityService getServiceInstance(Context context) {
171 ConnectivityThread thread = new ConnectivityThread(context);
172 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700173
The Android Open Source Project28527d22009-03-03 19:31:44 -0800174 synchronized (thread) {
175 while (sServiceInstance == null) {
176 try {
177 // Wait until sServiceInstance has been initialized.
178 thread.wait();
179 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800180 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700181 "Unexpected InterruptedException while waiting"+
182 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800183 }
184 }
185 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700186
The Android Open Source Project28527d22009-03-03 19:31:44 -0800187 return sServiceInstance;
188 }
189 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700190
The Android Open Source Project28527d22009-03-03 19:31:44 -0800191 public static ConnectivityService getInstance(Context context) {
192 return ConnectivityThread.getServiceInstance(context);
193 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700194
The Android Open Source Project28527d22009-03-03 19:31:44 -0800195 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800196 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800197
198 // setup our unique device name
199 String id = Settings.Secure.getString(context.getContentResolver(),
200 Settings.Secure.ANDROID_ID);
201 if (id != null && id.length() > 0) {
202 String name = new String("android_").concat(id);
203 SystemProperties.set("net.hostname", name);
204 }
205
The Android Open Source Project28527d22009-03-03 19:31:44 -0800206 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700207 mNetTrackers = new NetworkStateTracker[
208 ConnectivityManager.MAX_NETWORK_TYPE+1];
209 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700210
The Android Open Source Project28527d22009-03-03 19:31:44 -0800211 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700212
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700213 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
214 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
215
Robert Greenwalt2034b912009-08-12 16:08:25 -0700216 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700217 String[] raStrings = context.getResources().getStringArray(
218 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700219 for (String raString : raStrings) {
220 RadioAttributes r = new RadioAttributes(raString);
221 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800222 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700223 continue;
224 }
225 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800226 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700227 r.mType);
228 continue;
229 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700230 mRadioAttributes[r.mType] = r;
231 }
232
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700233 String[] naStrings = context.getResources().getStringArray(
234 com.android.internal.R.array.networkAttributes);
235 for (String naString : naStrings) {
236 try {
237 NetworkAttributes n = new NetworkAttributes(naString);
238 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800239 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 n.mType);
241 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700242 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700243 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800244 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700245 n.mType);
246 continue;
247 }
248 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800249 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700250 "radio " + n.mRadio + " in network type " + n.mType);
251 continue;
252 }
253 mNetAttributes[n.mType] = n;
254 mNetworksDefined++;
255 } catch(Exception e) {
256 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700257 }
258 }
259
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700260 // high priority first
261 mPriorityList = new int[mNetworksDefined];
262 {
263 int insertionPoint = mNetworksDefined-1;
264 int currentLowest = 0;
265 int nextLowest = 0;
266 while (insertionPoint > -1) {
267 for (NetworkAttributes na : mNetAttributes) {
268 if (na == null) continue;
269 if (na.mPriority < currentLowest) continue;
270 if (na.mPriority > currentLowest) {
271 if (na.mPriority < nextLowest || nextLowest == 0) {
272 nextLowest = na.mPriority;
273 }
274 continue;
275 }
276 mPriorityList[insertionPoint--] = na.mType;
277 }
278 currentLowest = nextLowest;
279 nextLowest = 0;
280 }
281 }
282
283 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
284 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700285 mNetRequestersPids[i] = new ArrayList();
286 }
287
288 mFeatureUsers = new ArrayList();
289
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700290 mNumDnsEntries = 0;
291
292 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
293 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800294 /*
295 * Create the network state trackers for Wi-Fi and mobile
296 * data. Maybe this could be done with a factory class,
297 * but it's not clear that it's worth it, given that
298 * the number of different network types is not going
299 * to change very often.
300 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800301 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700302 for (int netType : mPriorityList) {
303 switch (mNetAttributes[netType].mRadio) {
304 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800305 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700306 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
307 WifiService wifiService = new WifiService(context, wst);
308 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800309 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700310 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
311 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800312
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700313 break;
314 case ConnectivityManager.TYPE_MOBILE:
315 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
316 netType, mNetAttributes[netType].mName);
317 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800318 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800319 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800320 mNetTrackers[netType].teardown();
321 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700322 break;
323 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800324 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700325 mNetAttributes[netType].mRadio);
326 continue;
327 }
328 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800329
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800330 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800331 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
332 !mTethering.isDunRequired()) &&
333 (mTethering.getTetherableUsbRegexs().length != 0 ||
334 mTethering.getTetherableWifiRegexs().length != 0) &&
335 mTethering.getUpstreamIfaceRegexs().length != 0);
336
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700337 if (DBG) {
338 mInetLog = new ArrayList();
339 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800340 }
341
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700342
The Android Open Source Project28527d22009-03-03 19:31:44 -0800343 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700344 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800345 * @param preference the new preference
346 */
347 public synchronized void setNetworkPreference(int preference) {
348 enforceChangePermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700349 if (ConnectivityManager.isNetworkTypeValid(preference) &&
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700350 mNetAttributes[preference] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700351 mNetAttributes[preference].isDefault()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800352 if (mNetworkPreference != preference) {
353 persistNetworkPreference(preference);
354 mNetworkPreference = preference;
355 enforcePreference();
356 }
357 }
358 }
359
360 public int getNetworkPreference() {
361 enforceAccessPermission();
362 return mNetworkPreference;
363 }
364
365 private void persistNetworkPreference(int networkPreference) {
366 final ContentResolver cr = mContext.getContentResolver();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700367 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE,
368 networkPreference);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800369 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700370
The Android Open Source Project28527d22009-03-03 19:31:44 -0800371 private int getPersistedNetworkPreference() {
372 final ContentResolver cr = mContext.getContentResolver();
373
374 final int networkPrefSetting = Settings.Secure
375 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
376 if (networkPrefSetting != -1) {
377 return networkPrefSetting;
378 }
379
380 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
381 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700382
The Android Open Source Project28527d22009-03-03 19:31:44 -0800383 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700384 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800385 * In this method, we only tear down a non-preferred network. Establishing
386 * a connection to the preferred network is taken care of when we handle
387 * the disconnect event from the non-preferred network
388 * (see {@link #handleDisconnect(NetworkInfo)}).
389 */
390 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700391 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800392 return;
393
Robert Greenwalt2034b912009-08-12 16:08:25 -0700394 if (!mNetTrackers[mNetworkPreference].isAvailable())
395 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800396
Robert Greenwalt2034b912009-08-12 16:08:25 -0700397 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700398 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700399 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700400 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800401 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700402 mNetTrackers[t].getNetworkInfo() +
403 " in enforcePreference");
404 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700405 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800406 }
407 }
408 }
409
410 private boolean teardown(NetworkStateTracker netTracker) {
411 if (netTracker.teardown()) {
412 netTracker.setTeardownRequested(true);
413 return true;
414 } else {
415 return false;
416 }
417 }
418
419 /**
420 * Return NetworkInfo for the active (i.e., connected) network interface.
421 * It is assumed that at most one network is active at a time. If more
422 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700423 * @return the info for the active network, or {@code null} if none is
424 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800425 */
426 public NetworkInfo getActiveNetworkInfo() {
427 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700428 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700429 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700430 continue;
431 }
432 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800433 NetworkInfo info = t.getNetworkInfo();
434 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800435 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700436 "connected default network is not " +
437 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 return info;
439 }
440 }
441 return null;
442 }
443
444 public NetworkInfo getNetworkInfo(int networkType) {
445 enforceAccessPermission();
446 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
447 NetworkStateTracker t = mNetTrackers[networkType];
448 if (t != null)
449 return t.getNetworkInfo();
450 }
451 return null;
452 }
453
454 public NetworkInfo[] getAllNetworkInfo() {
455 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700456 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800457 int i = 0;
458 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700459 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800460 }
461 return result;
462 }
463
464 public boolean setRadios(boolean turnOn) {
465 boolean result = true;
466 enforceChangePermission();
467 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700468 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800469 }
470 return result;
471 }
472
473 public boolean setRadio(int netType, boolean turnOn) {
474 enforceChangePermission();
475 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
476 return false;
477 }
478 NetworkStateTracker tracker = mNetTrackers[netType];
479 return tracker != null && tracker.setRadio(turnOn);
480 }
481
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700482 /**
483 * Used to notice when the calling process dies so we can self-expire
484 *
485 * Also used to know if the process has cleaned up after itself when
486 * our auto-expire timer goes off. The timer has a link to an object.
487 *
488 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700489 private class FeatureUser implements IBinder.DeathRecipient {
490 int mNetworkType;
491 String mFeature;
492 IBinder mBinder;
493 int mPid;
494 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800495 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700496
497 FeatureUser(int type, String feature, IBinder binder) {
498 super();
499 mNetworkType = type;
500 mFeature = feature;
501 mBinder = binder;
502 mPid = getCallingPid();
503 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800504 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700505
Robert Greenwalt2034b912009-08-12 16:08:25 -0700506 try {
507 mBinder.linkToDeath(this, 0);
508 } catch (RemoteException e) {
509 binderDied();
510 }
511 }
512
513 void unlinkDeathRecipient() {
514 mBinder.unlinkToDeath(this, 0);
515 }
516
517 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800518 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800519 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
520 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700521 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700522 }
523
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700524 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800525 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800526 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
527 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700528 stopUsingNetworkFeature(this, false);
529 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800530
531 public String toString() {
532 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
533 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
534 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700535 }
536
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700537 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700538 public int startUsingNetworkFeature(int networkType, String feature,
539 IBinder binder) {
540 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800541 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700542 ": " + feature);
543 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800544 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700545 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
546 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700547 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800548 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700549
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700550 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700551
552 // TODO - move this into the MobileDataStateTracker
553 int usedNetworkType = networkType;
554 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800555 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800556 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800557 return Phone.APN_TYPE_NOT_AVAILABLE;
558 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700559 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
560 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
561 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
562 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
563 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
564 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
565 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
566 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
567 }
568 }
569 NetworkStateTracker network = mNetTrackers[usedNetworkType];
570 if (network != null) {
571 if (usedNetworkType != networkType) {
572 Integer currentPid = new Integer(getCallingPid());
573
574 NetworkStateTracker radio = mNetTrackers[networkType];
575 NetworkInfo ni = network.getNetworkInfo();
576
577 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800578 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700579 return Phone.APN_TYPE_NOT_AVAILABLE;
580 }
581
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700582 synchronized(this) {
583 mFeatureUsers.add(f);
584 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
585 // this gets used for per-pid dns when connected
586 mNetRequestersPids[usedNetworkType].add(currentPid);
587 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700588 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700589 mHandler.sendMessageDelayed(mHandler.obtainMessage(
590 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
591 f), getRestoreDefaultNetworkDelay());
592
Robert Greenwalt2034b912009-08-12 16:08:25 -0700593
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700594 if ((ni.isConnectedOrConnecting() == true) &&
595 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700596 if (ni.isConnected() == true) {
597 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700598 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800599 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700600 return Phone.APN_ALREADY_ACTIVE;
601 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800602 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700603 return Phone.APN_REQUEST_STARTED;
604 }
605
606 // check if the radio in play can make another contact
607 // assume if cannot for now
608
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800609 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700610 network.reconnect();
611 return Phone.APN_REQUEST_STARTED;
612 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700613 synchronized(this) {
614 mFeatureUsers.add(f);
615 }
616 mHandler.sendMessageDelayed(mHandler.obtainMessage(
617 NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK,
618 f), getRestoreDefaultNetworkDelay());
619
Robert Greenwalt2034b912009-08-12 16:08:25 -0700620 return network.startUsingNetworkFeature(feature,
621 getCallingPid(), getCallingUid());
622 }
623 }
624 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800625 }
626
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700627 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800628 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700629 enforceChangePermission();
630
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700631 int pid = getCallingPid();
632 int uid = getCallingUid();
633
634 FeatureUser u = null;
635 boolean found = false;
636
637 synchronized(this) {
638 for (int i = 0; i < mFeatureUsers.size() ; i++) {
639 u = (FeatureUser)mFeatureUsers.get(i);
640 if (uid == u.mUid && pid == u.mPid &&
641 networkType == u.mNetworkType &&
642 TextUtils.equals(feature, u.mFeature)) {
643 found = true;
644 break;
645 }
646 }
647 }
648 if (found && u != null) {
649 // stop regardless of how many other time this proc had called start
650 return stopUsingNetworkFeature(u, true);
651 } else {
652 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800653 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 return 1;
655 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700656 }
657
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700658 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
659 int networkType = u.mNetworkType;
660 String feature = u.mFeature;
661 int pid = u.mPid;
662 int uid = u.mUid;
663
664 NetworkStateTracker tracker = null;
665 boolean callTeardown = false; // used to carry our decision outside of sync block
666
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800668 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700669 ": " + feature);
670 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700671
The Android Open Source Project28527d22009-03-03 19:31:44 -0800672 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
673 return -1;
674 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700675
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700676 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
677 // sync block
678 synchronized(this) {
679 // check if this process still has an outstanding start request
680 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800681 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700682 return 1;
683 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700684 u.unlinkDeathRecipient();
685 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
686 // If we care about duplicate requests, check for that here.
687 //
688 // This is done to support the extension of a request - the app
689 // can request we start the network feature again and renew the
690 // auto-shutoff delay. Normal "stop" calls from the app though
691 // do not pay attention to duplicate requests - in effect the
692 // API does not refcount and a single stop will counter multiple starts.
693 if (ignoreDups == false) {
694 for (int i = 0; i < mFeatureUsers.size() ; i++) {
695 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
696 if (x.mUid == u.mUid && x.mPid == u.mPid &&
697 x.mNetworkType == u.mNetworkType &&
698 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800699 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700700 return 1;
701 }
702 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700703 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700704
705 // TODO - move to MobileDataStateTracker
706 int usedNetworkType = networkType;
707 if (networkType == ConnectivityManager.TYPE_MOBILE) {
708 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
709 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
710 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
711 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
712 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
713 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
714 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
715 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
716 }
717 }
718 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700719 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800720 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700721 return -1;
722 }
723 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700724 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700725 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800726 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700727 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800728 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700729 "others still using it");
730 return 1;
731 }
732 callTeardown = true;
733 }
734 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800735 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700736 if (callTeardown) {
737 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700738 return 1;
739 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700740 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700741 return tracker.stopUsingNetworkFeature(feature, pid, uid);
742 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800743 }
744
745 /**
746 * Ensure that a network route exists to deliver traffic to the specified
747 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700748 * @param networkType the type of the network over which traffic to the
749 * specified host is to be routed
750 * @param hostAddress the IP address of the host to which the route is
751 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800752 * @return {@code true} on success, {@code false} on failure
753 */
754 public boolean requestRouteToHost(int networkType, int hostAddress) {
755 enforceChangePermission();
756 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
757 return false;
758 }
759 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700760
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700761 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
762 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700763 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800764 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700765 }
766 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800767 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700768 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800769 }
770
771 /**
772 * @see ConnectivityManager#getBackgroundDataSetting()
773 */
774 public boolean getBackgroundDataSetting() {
775 return Settings.Secure.getInt(mContext.getContentResolver(),
776 Settings.Secure.BACKGROUND_DATA, 1) == 1;
777 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700778
The Android Open Source Project28527d22009-03-03 19:31:44 -0800779 /**
780 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
781 */
782 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
783 mContext.enforceCallingOrSelfPermission(
784 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
785 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700786
The Android Open Source Project28527d22009-03-03 19:31:44 -0800787 if (getBackgroundDataSetting() == allowBackgroundDataUsage) return;
788
789 Settings.Secure.putInt(mContext.getContentResolver(),
Robert Greenwalt0659da32009-07-16 17:21:39 -0700790 Settings.Secure.BACKGROUND_DATA,
791 allowBackgroundDataUsage ? 1 : 0);
792
The Android Open Source Project28527d22009-03-03 19:31:44 -0800793 Intent broadcast = new Intent(
794 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
795 mContext.sendBroadcast(broadcast);
Robert Greenwalt0659da32009-07-16 17:21:39 -0700796 }
797
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800798 /**
799 * @see ConnectivityManager#getMobileDataEnabled()
800 */
801 public boolean getMobileDataEnabled() {
802 enforceAccessPermission();
803 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
804 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800805 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800806 return retVal;
807 }
808
809 /**
810 * @see ConnectivityManager#setMobileDataEnabled(boolean)
811 */
812 public synchronized void setMobileDataEnabled(boolean enabled) {
813 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800814 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800815
816 if (getMobileDataEnabled() == enabled) return;
817
818 Settings.Secure.putInt(mContext.getContentResolver(),
819 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
820
821 if (enabled) {
822 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800823 if (DBG) Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800824 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
825 }
826 } else {
827 for (NetworkStateTracker nt : mNetTrackers) {
828 if (nt == null) continue;
829 int netType = nt.getNetworkInfo().getType();
830 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800831 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800832 nt.teardown();
833 }
834 }
835 }
836 }
837
The Android Open Source Project28527d22009-03-03 19:31:44 -0800838 private int getNumConnectedNetworks() {
839 int numConnectedNets = 0;
840
841 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700842 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700843 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800844 ++numConnectedNets;
845 }
846 }
847 return numConnectedNets;
848 }
849
850 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700851 mContext.enforceCallingOrSelfPermission(
852 android.Manifest.permission.ACCESS_NETWORK_STATE,
853 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800854 }
855
856 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700857 mContext.enforceCallingOrSelfPermission(
858 android.Manifest.permission.CHANGE_NETWORK_STATE,
859 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800860 }
861
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800862 // TODO Make this a special check when it goes public
863 private void enforceTetherChangePermission() {
864 mContext.enforceCallingOrSelfPermission(
865 android.Manifest.permission.CHANGE_NETWORK_STATE,
866 "ConnectivityService");
867 }
868
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800869 private void enforceTetherAccessPermission() {
870 mContext.enforceCallingOrSelfPermission(
871 android.Manifest.permission.ACCESS_NETWORK_STATE,
872 "ConnectivityService");
873 }
874
The Android Open Source Project28527d22009-03-03 19:31:44 -0800875 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700876 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
877 * network, we ignore it. If it is for the active network, we send out a
878 * broadcast. But first, we check whether it might be possible to connect
879 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800880 * @param info the {@code NetworkInfo} for the network
881 */
882 private void handleDisconnect(NetworkInfo info) {
883
Robert Greenwalt2034b912009-08-12 16:08:25 -0700884 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800885
Robert Greenwalt2034b912009-08-12 16:08:25 -0700886 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800887 /*
888 * If the disconnected network is not the active one, then don't report
889 * this as a loss of connectivity. What probably happened is that we're
890 * getting the disconnect for a network that we explicitly disabled
891 * in accordance with network preference policies.
892 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700893 if (!mNetAttributes[prevNetType].isDefault()) {
894 List pids = mNetRequestersPids[prevNetType];
895 for (int i = 0; i<pids.size(); i++) {
896 Integer pid = (Integer)pids.get(i);
897 // will remove them because the net's no longer connected
898 // need to do this now as only now do we know the pids and
899 // can properly null things that are no longer referenced.
900 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800901 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800902 }
903
The Android Open Source Project28527d22009-03-03 19:31:44 -0800904 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
905 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
906 if (info.isFailover()) {
907 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
908 info.setFailover(false);
909 }
910 if (info.getReason() != null) {
911 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
912 }
913 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700914 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
915 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700917
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800918 NetworkStateTracker newNet = null;
919 if (mNetAttributes[prevNetType].isDefault()) {
920 newNet = tryFailover(prevNetType);
921 if (newNet != null) {
922 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -0700923 if (!switchTo.isConnected()) {
924 // if the other net is connected they've already reset this and perhaps even gotten
925 // a positive report we don't want to overwrite, but if not we need to clear this now
926 // to turn our cellular sig strength white
927 mDefaultInetConditionPublished = 0;
928 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800929 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
930 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -0700931 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800932 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
933 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800934 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -0700935 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800936 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700937 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800938
939 sendStickyBroadcast(intent);
940 /*
941 * If the failover network is already connected, then immediately send
942 * out a followup broadcast indicating successful failover
943 */
944 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
945 sendConnectedBroadcast(newNet.getNetworkInfo());
946 }
947 }
948
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800949 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800950 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700951 /*
952 * If this is a default network, check if other defaults are available
953 * or active
954 */
955 NetworkStateTracker newNet = null;
956 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700957 if (mActiveDefaultNetwork == prevNetType) {
958 mActiveDefaultNetwork = -1;
959 }
960
961 int newType = -1;
962 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800963 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800964 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700965 if (checkType == prevNetType) continue;
966 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800967 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
968 noMobileData) {
969 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800970 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800971 " because Mobile Data Disabled");
972 }
973 continue;
974 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700975 if (mNetAttributes[checkType].isDefault()) {
976 /* TODO - if we have multiple nets we could use
977 * we may want to put more thought into which we choose
978 */
979 if (checkType == mNetworkPreference) {
980 newType = checkType;
981 break;
982 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700983 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700984 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700985 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700986 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800987 }
988 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700989
990 if (newType != -1) {
991 newNet = mNetTrackers[newType];
992 /**
993 * See if the other network is available to fail over to.
994 * If is not available, we enable it anyway, so that it
995 * will be able to connect when it does become available,
996 * but we report a total loss of connectivity rather than
997 * report that we are attempting to fail over.
998 */
999 if (newNet.isAvailable()) {
1000 NetworkInfo switchTo = newNet.getNetworkInfo();
1001 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001002 if (!switchTo.isConnectedOrConnecting() ||
1003 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001004 newNet.reconnect();
1005 }
1006 if (DBG) {
1007 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001008 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001009 switchTo.getTypeName());
1010 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001011 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001012 switchTo.getTypeName());
1013 }
1014 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001015 } else {
1016 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001017 newNet = null; // not officially avail.. try anyway, but
1018 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001019 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001020 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001021 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001022
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001023 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001024 }
1025
1026 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001027 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1028 }
1029
1030 private void sendInetConditionBroadcast(NetworkInfo info) {
1031 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1032 }
1033
1034 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1035 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001036 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1037 if (info.isFailover()) {
1038 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1039 info.setFailover(false);
1040 }
1041 if (info.getReason() != null) {
1042 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1043 }
1044 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001045 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1046 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001047 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001048 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001049 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001050 }
1051
1052 /**
1053 * Called when an attempt to fail over to another network has failed.
1054 * @param info the {@link NetworkInfo} for the failed network
1055 */
1056 private void handleConnectionFailure(NetworkInfo info) {
1057 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001058
Robert Greenwalt2034b912009-08-12 16:08:25 -07001059 String reason = info.getReason();
1060 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001061
Robert Greenwalt2034b912009-08-12 16:08:25 -07001062 if (DBG) {
1063 String reasonText;
1064 if (reason == null) {
1065 reasonText = ".";
1066 } else {
1067 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001068 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001069 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001070 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001071 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001072
1073 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1074 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1075 if (getActiveNetworkInfo() == null) {
1076 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1077 }
1078 if (reason != null) {
1079 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1080 }
1081 if (extraInfo != null) {
1082 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1083 }
1084 if (info.isFailover()) {
1085 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1086 info.setFailover(false);
1087 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001088
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001089 NetworkStateTracker newNet = null;
1090 if (mNetAttributes[info.getType()].isDefault()) {
1091 newNet = tryFailover(info.getType());
1092 if (newNet != null) {
1093 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001094 if (!switchTo.isConnected()) {
1095 // if the other net is connected they've already reset this and perhaps even gotten
1096 // a positive report we don't want to overwrite, but if not we need to clear this now
1097 // to turn our cellular sig strength white
1098 mDefaultInetConditionPublished = 0;
1099 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001100 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1101 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001102 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001103 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1104 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001105 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001106
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001107 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001108 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001109 /*
1110 * If the failover network is already connected, then immediately send
1111 * out a followup broadcast indicating successful failover
1112 */
1113 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1114 sendConnectedBroadcast(newNet.getNetworkInfo());
1115 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001116 }
1117
1118 private void sendStickyBroadcast(Intent intent) {
1119 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001120 if (!mSystemReady) {
1121 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001122 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001123 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1124 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001125 }
1126 }
1127
1128 void systemReady() {
1129 synchronized(this) {
1130 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001131 if (mInitialBroadcast != null) {
1132 mContext.sendStickyBroadcast(mInitialBroadcast);
1133 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001134 }
1135 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001136 }
1137
1138 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001139 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001140
1141 // snapshot isFailover, because sendConnectedBroadcast() resets it
1142 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001143 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001144
Robert Greenwalt2034b912009-08-12 16:08:25 -07001145 // if this is a default net and other default is running
1146 // kill the one not preferred
1147 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001148 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1149 if ((type != mNetworkPreference &&
1150 mNetAttributes[mActiveDefaultNetwork].mPriority >
1151 mNetAttributes[type].mPriority) ||
1152 mNetworkPreference == mActiveDefaultNetwork) {
1153 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001154 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001155 "to torn down network " + info.getTypeName());
1156 teardown(thisNet);
1157 return;
1158 } else {
1159 // tear down the other
1160 NetworkStateTracker otherNet =
1161 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001162 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001163 otherNet.getNetworkInfo().getTypeName() +
1164 " teardown");
1165 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001166 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001167 return;
1168 }
1169 if (isFailover) {
1170 otherNet.releaseWakeLock();
1171 }
1172 }
1173 }
1174 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001175 // this will cause us to come up initially as unconnected and switching
1176 // to connected after our normal pause unless somebody reports us as reall
1177 // disconnected
1178 mDefaultInetConditionPublished = 0;
1179 mDefaultConnectionSequence++;
1180 mInetConditionChangeInFlight = false;
1181 // Don't do this - if we never sign in stay, grey
1182 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001183 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001185 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001186 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001187 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001188 }
1189
1190 private void handleScanResultsAvailable(NetworkInfo info) {
1191 int networkType = info.getType();
1192 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001193 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001194 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001195 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001196
The Android Open Source Project28527d22009-03-03 19:31:44 -08001197 mNetTrackers[networkType].interpretScanResultsAvailable();
1198 }
1199
Robert Greenwalt0659da32009-07-16 17:21:39 -07001200 private void handleNotificationChange(boolean visible, int id,
1201 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001202 NotificationManager notificationManager = (NotificationManager) mContext
1203 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001204
The Android Open Source Project28527d22009-03-03 19:31:44 -08001205 if (visible) {
1206 notificationManager.notify(id, notification);
1207 } else {
1208 notificationManager.cancel(id);
1209 }
1210 }
1211
1212 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001213 * After a change in the connectivity state of any network, We're mainly
1214 * concerned with making sure that the list of DNS servers is setupup
1215 * according to which networks are connected, and ensuring that the
1216 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001217 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001218 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001219 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001220 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001221 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001222 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001223 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001224
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001225 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1226 if (mNetAttributes[netType].isDefault()) {
1227 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001228 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001229 mNetTrackers[netType].addPrivateDnsRoutes();
1230 }
1231 } else {
1232 if (mNetAttributes[netType].isDefault()) {
1233 mNetTrackers[netType].removeDefaultRoute();
1234 } else {
1235 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001236 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001237 }
1238 }
1239
Robert Greenwalt2034b912009-08-12 16:08:25 -07001240 /**
1241 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1242 * on the highest priority active net which this process requested.
1243 * If there aren't any, clear it out
1244 */
1245 private void reassessPidDns(int myPid, boolean doBump)
1246 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001247 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001248 for(int i : mPriorityList) {
1249 if (mNetAttributes[i].isDefault()) {
1250 continue;
1251 }
1252 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001253 if (nt.getNetworkInfo().isConnected() &&
1254 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001255 List pids = mNetRequestersPids[i];
1256 for (int j=0; j<pids.size(); j++) {
1257 Integer pid = (Integer)pids.get(j);
1258 if (pid.intValue() == myPid) {
1259 String[] dnsList = nt.getNameServers();
1260 writePidDns(dnsList, myPid);
1261 if (doBump) {
1262 bumpDns();
1263 }
1264 return;
1265 }
1266 }
1267 }
1268 }
1269 // nothing found - delete
1270 for (int i = 1; ; i++) {
1271 String prop = "net.dns" + i + "." + myPid;
1272 if (SystemProperties.get(prop).length() == 0) {
1273 if (doBump) {
1274 bumpDns();
1275 }
1276 return;
1277 }
1278 SystemProperties.set(prop, "");
1279 }
1280 }
1281
1282 private void writePidDns(String[] dnsList, int pid) {
1283 int j = 1;
1284 for (String dns : dnsList) {
1285 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1286 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1287 }
1288 }
1289 }
1290
1291 private void bumpDns() {
1292 /*
1293 * Bump the property that tells the name resolver library to reread
1294 * the DNS server list from the properties.
1295 */
1296 String propVal = SystemProperties.get("net.dnschange");
1297 int n = 0;
1298 if (propVal.length() != 0) {
1299 try {
1300 n = Integer.parseInt(propVal);
1301 } catch (NumberFormatException e) {}
1302 }
1303 SystemProperties.set("net.dnschange", "" + (n+1));
1304 }
1305
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001306 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001307 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001308 NetworkStateTracker nt = mNetTrackers[netType];
1309 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1310 String[] dnsList = nt.getNameServers();
1311 if (mNetAttributes[netType].isDefault()) {
1312 int j = 1;
1313 for (String dns : dnsList) {
1314 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1315 if (DBG) {
1316 Slog.d(TAG, "adding dns " + dns + " for " +
1317 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001318 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001319 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001321 }
1322 for (int k=j ; k<mNumDnsEntries; k++) {
1323 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1324 SystemProperties.set("net.dns" + k, "");
1325 }
1326 mNumDnsEntries = j;
1327 } else {
1328 // set per-pid dns for attached secondary nets
1329 List pids = mNetRequestersPids[netType];
1330 for (int y=0; y< pids.size(); y++) {
1331 Integer pid = (Integer)pids.get(y);
1332 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001333 }
1334 }
1335 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001336 bumpDns();
1337 }
1338
1339 private int getRestoreDefaultNetworkDelay() {
1340 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1341 NETWORK_RESTORE_DELAY_PROP_NAME);
1342 if(restoreDefaultNetworkDelayStr != null &&
1343 restoreDefaultNetworkDelayStr.length() != 0) {
1344 try {
1345 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1346 } catch (NumberFormatException e) {
1347 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001348 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001349 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001350 }
1351
1352 @Override
1353 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001354 if (mContext.checkCallingOrSelfPermission(
1355 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001356 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001357 pw.println("Permission Denial: can't dump ConnectivityService " +
1358 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1359 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001360 return;
1361 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001362 pw.println();
1363 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001364 if (nst != null) {
1365 if (nst.getNetworkInfo().isConnected()) {
1366 pw.println("Active network: " + nst.getNetworkInfo().
1367 getTypeName());
1368 }
1369 pw.println(nst.getNetworkInfo());
1370 pw.println(nst);
1371 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001372 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001373 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001374
1375 pw.println("Network Requester Pids:");
1376 for (int net : mPriorityList) {
1377 String pidString = net + ": ";
1378 for (Object pid : mNetRequestersPids[net]) {
1379 pidString = pidString + pid.toString() + ", ";
1380 }
1381 pw.println(pidString);
1382 }
1383 pw.println();
1384
1385 pw.println("FeatureUsers:");
1386 for (Object requester : mFeatureUsers) {
1387 pw.println(requester.toString());
1388 }
1389 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001390
1391 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001392
1393 if (mInetLog != null) {
1394 pw.println();
1395 pw.println("Inet condition reports:");
1396 for(int i = 0; i < mInetLog.size(); i++) {
1397 pw.println(mInetLog.get(i));
1398 }
1399 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001400 }
1401
Robert Greenwalt2034b912009-08-12 16:08:25 -07001402 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001403 private class MyHandler extends Handler {
1404 @Override
1405 public void handleMessage(Message msg) {
1406 NetworkInfo info;
1407 switch (msg.what) {
1408 case NetworkStateTracker.EVENT_STATE_CHANGED:
1409 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001410 int type = info.getType();
1411 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001412 // only do this optimization for wifi. It going into scan mode for location
1413 // services generates alot of noise. Meanwhile the mms apn won't send out
1414 // subsequent notifications when on default cellular because it never
1415 // disconnects.. so only do this to wifi notifications. Fixed better when the
1416 // APN notifications are standardized.
1417 if (mNetAttributes[type].mLastState == state &&
1418 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001419 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001420 // TODO - remove this after we validate the dropping doesn't break
1421 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001422 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001423 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001424 state + "/" + info.getDetailedState());
1425 }
1426 return;
1427 }
1428 mNetAttributes[type].mLastState = state;
1429
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001430 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001431 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001432 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001433
1434 // Connectivity state changed:
1435 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001436 // [12-9] Network subtype (for mobile network, as defined
1437 // by TelephonyManager)
1438 // [8-3] Detailed state ordinal (as defined by
1439 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001440 // [2-0] Network type (as defined by ConnectivityManager)
1441 int eventLogParam = (info.getType() & 0x7) |
1442 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1443 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001444 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001445 eventLogParam);
1446
1447 if (info.getDetailedState() ==
1448 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001449 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001450 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001451 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001452 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001453 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001454 // the logic here is, handle SUSPENDED the same as
1455 // DISCONNECTED. The only difference being we are
1456 // broadcasting an intent with NetworkInfo that's
1457 // suspended. This allows the applications an
1458 // opportunity to handle DISCONNECTED and SUSPENDED
1459 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001460 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001461 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001462 handleConnect(info);
1463 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001464 break;
1465
1466 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1467 info = (NetworkInfo) msg.obj;
1468 handleScanResultsAvailable(info);
1469 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001470
The Android Open Source Project28527d22009-03-03 19:31:44 -08001471 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001472 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1473 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001474 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001475
1476 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001477 info = (NetworkInfo) msg.obj;
1478 type = info.getType();
1479 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001480 break;
1481
1482 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1483 // fill me in
1484 break;
1485
1486 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1487 // fill me in
1488 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001489 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001490 FeatureUser u = (FeatureUser)msg.obj;
1491 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001492 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001493 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1494 if (DBG) {
1495 Slog.d(TAG, "Inet connectivity change, net=" +
1496 msg.arg1 + ", condition=" + msg.arg2 +
1497 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1498 }
1499 if (mActiveDefaultNetwork == -1) {
1500 if (DBG) Slog.d(TAG, "no active default network - aborting");
1501 break;
1502 }
1503 if (mActiveDefaultNetwork != msg.arg1) {
1504 if (DBG) Slog.d(TAG, "given net not default - aborting");
1505 break;
1506 }
1507 mDefaultInetCondition = msg.arg2;
1508 int delay;
1509 if (mInetConditionChangeInFlight == false) {
1510 if (DBG) Slog.d(TAG, "starting a change hold");
1511 // setup a new hold to debounce this
1512 if (mDefaultInetCondition > 50) {
1513 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1514 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1515 } else {
1516 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1517 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1518 }
1519 mInetConditionChangeInFlight = true;
1520 sendMessageDelayed(obtainMessage(
1521 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1522 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1523 } else {
1524 // we've set the new condition, when this hold ends that will get
1525 // picked up
1526 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1527 }
1528 break;
1529 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1530 if (DBG) {
1531 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1532 ", condition =" + mDefaultInetCondition +
1533 ", published condition =" + mDefaultInetConditionPublished);
1534 }
1535 mInetConditionChangeInFlight = false;
1536
1537 if (mActiveDefaultNetwork == -1) {
1538 if (DBG) Slog.d(TAG, "no active default network - aborting");
1539 break;
1540 }
1541 if (mDefaultConnectionSequence != msg.arg2) {
1542 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1543 break;
1544 }
1545 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1546 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1547 break;
1548 }
1549 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1550 if (networkInfo.isConnected() == false) {
1551 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1552 break;
1553 }
1554 mDefaultInetConditionPublished = mDefaultInetCondition;
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001555 sendInetConditionBroadcast(networkInfo);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001556 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001557 }
1558 }
1559 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001560
1561 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001562 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001563 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001564
1565 if (isTetheringSupported()) {
1566 return mTethering.tether(iface);
1567 } else {
1568 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1569 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001570 }
1571
1572 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001573 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001574 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001575
1576 if (isTetheringSupported()) {
1577 return mTethering.untether(iface);
1578 } else {
1579 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1580 }
1581 }
1582
1583 // javadoc from interface
1584 public int getLastTetherError(String iface) {
1585 enforceTetherAccessPermission();
1586
1587 if (isTetheringSupported()) {
1588 return mTethering.getLastTetherError(iface);
1589 } else {
1590 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1591 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001592 }
1593
1594 // TODO - proper iface API for selection by property, inspection, etc
1595 public String[] getTetherableUsbRegexs() {
1596 enforceTetherAccessPermission();
1597 if (isTetheringSupported()) {
1598 return mTethering.getTetherableUsbRegexs();
1599 } else {
1600 return new String[0];
1601 }
1602 }
1603
1604 public String[] getTetherableWifiRegexs() {
1605 enforceTetherAccessPermission();
1606 if (isTetheringSupported()) {
1607 return mTethering.getTetherableWifiRegexs();
1608 } else {
1609 return new String[0];
1610 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001611 }
1612
1613 // TODO - move iface listing, queries, etc to new module
1614 // javadoc from interface
1615 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001616 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001617 return mTethering.getTetherableIfaces();
1618 }
1619
1620 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001621 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001622 return mTethering.getTetheredIfaces();
1623 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001624
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001625 public String[] getTetheringErroredIfaces() {
1626 enforceTetherAccessPermission();
1627 return mTethering.getErroredIfaces();
1628 }
1629
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001630 // if ro.tether.denied = true we default to no tethering
1631 // gservices could set the secure setting to 1 though to enable it on a build where it
1632 // had previously been turned off.
1633 public boolean isTetheringSupported() {
1634 enforceTetherAccessPermission();
1635 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001636 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1637 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1638 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001639 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001640
1641 // 100 percent is full good, 0 is full bad.
1642 public void reportInetCondition(int networkType, int percentage) {
1643 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1644 mContext.enforceCallingOrSelfPermission(
1645 android.Manifest.permission.STATUS_BAR,
1646 "ConnectivityService");
1647
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001648 if (DBG) {
1649 int pid = getCallingPid();
1650 int uid = getCallingUid();
1651 String s = pid + "(" + uid + ") reports inet is " +
1652 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1653 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1654 mInetLog.add(s);
1655 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1656 mInetLog.remove(0);
1657 }
1658 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001659 mHandler.sendMessage(mHandler.obtainMessage(
1660 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1661 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001662}