blob: 8603e51d3da853427d929a12e15c89a7d7ac12b0 [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();
923 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
924 } else {
925 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
926 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800927 }
928 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700929 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800930
931 sendStickyBroadcast(intent);
932 /*
933 * If the failover network is already connected, then immediately send
934 * out a followup broadcast indicating successful failover
935 */
936 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
937 sendConnectedBroadcast(newNet.getNetworkInfo());
938 }
939 }
940
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800941 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800942 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700943 /*
944 * If this is a default network, check if other defaults are available
945 * or active
946 */
947 NetworkStateTracker newNet = null;
948 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700949 if (mActiveDefaultNetwork == prevNetType) {
950 mActiveDefaultNetwork = -1;
951 }
952
953 int newType = -1;
954 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800955 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -0800956 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700957 if (checkType == prevNetType) continue;
958 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800959 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
960 noMobileData) {
961 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800962 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt72451bf2010-02-25 12:04:29 -0800963 " because Mobile Data Disabled");
964 }
965 continue;
966 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700967 if (mNetAttributes[checkType].isDefault()) {
968 /* TODO - if we have multiple nets we could use
969 * we may want to put more thought into which we choose
970 */
971 if (checkType == mNetworkPreference) {
972 newType = checkType;
973 break;
974 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700975 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700976 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700977 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700978 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800979 }
980 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700981
982 if (newType != -1) {
983 newNet = mNetTrackers[newType];
984 /**
985 * See if the other network is available to fail over to.
986 * If is not available, we enable it anyway, so that it
987 * will be able to connect when it does become available,
988 * but we report a total loss of connectivity rather than
989 * report that we are attempting to fail over.
990 */
991 if (newNet.isAvailable()) {
992 NetworkInfo switchTo = newNet.getNetworkInfo();
993 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700994 if (!switchTo.isConnectedOrConnecting() ||
995 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 newNet.reconnect();
997 }
998 if (DBG) {
999 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001000 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001001 switchTo.getTypeName());
1002 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001003 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001004 switchTo.getTypeName());
1005 }
1006 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001007 } else {
1008 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001009 newNet = null; // not officially avail.. try anyway, but
1010 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001011 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001012 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001013 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001014
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001015 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001016 }
1017
1018 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001019 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1020 }
1021
1022 private void sendInetConditionBroadcast(NetworkInfo info) {
1023 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1024 }
1025
1026 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1027 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001028 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1029 if (info.isFailover()) {
1030 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1031 info.setFailover(false);
1032 }
1033 if (info.getReason() != null) {
1034 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1035 }
1036 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001037 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1038 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001039 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001040 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001041 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001042 }
1043
1044 /**
1045 * Called when an attempt to fail over to another network has failed.
1046 * @param info the {@link NetworkInfo} for the failed network
1047 */
1048 private void handleConnectionFailure(NetworkInfo info) {
1049 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001050
Robert Greenwalt2034b912009-08-12 16:08:25 -07001051 String reason = info.getReason();
1052 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001053
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 if (DBG) {
1055 String reasonText;
1056 if (reason == null) {
1057 reasonText = ".";
1058 } else {
1059 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001061 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001062 " failed" + reasonText);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001063 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001064
1065 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1066 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1067 if (getActiveNetworkInfo() == null) {
1068 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1069 }
1070 if (reason != null) {
1071 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1072 }
1073 if (extraInfo != null) {
1074 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1075 }
1076 if (info.isFailover()) {
1077 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1078 info.setFailover(false);
1079 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001080
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001081 NetworkStateTracker newNet = null;
1082 if (mNetAttributes[info.getType()].isDefault()) {
1083 newNet = tryFailover(info.getType());
1084 if (newNet != null) {
1085 NetworkInfo switchTo = newNet.getNetworkInfo();
1086 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1087 } else {
1088 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1089 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001090 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001091
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001092 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001093 /*
1094 * If the failover network is already connected, then immediately send
1095 * out a followup broadcast indicating successful failover
1096 */
1097 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1098 sendConnectedBroadcast(newNet.getNetworkInfo());
1099 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001100 }
1101
1102 private void sendStickyBroadcast(Intent intent) {
1103 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001104 if (!mSystemReady) {
1105 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001106 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001107 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1108 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001109 }
1110 }
1111
1112 void systemReady() {
1113 synchronized(this) {
1114 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001115 if (mInitialBroadcast != null) {
1116 mContext.sendStickyBroadcast(mInitialBroadcast);
1117 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001118 }
1119 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001120 }
1121
1122 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001124
1125 // snapshot isFailover, because sendConnectedBroadcast() resets it
1126 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001127 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001128
Robert Greenwalt2034b912009-08-12 16:08:25 -07001129 // if this is a default net and other default is running
1130 // kill the one not preferred
1131 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001132 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1133 if ((type != mNetworkPreference &&
1134 mNetAttributes[mActiveDefaultNetwork].mPriority >
1135 mNetAttributes[type].mPriority) ||
1136 mNetworkPreference == mActiveDefaultNetwork) {
1137 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001138 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001139 "to torn down network " + info.getTypeName());
1140 teardown(thisNet);
1141 return;
1142 } else {
1143 // tear down the other
1144 NetworkStateTracker otherNet =
1145 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001146 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001147 otherNet.getNetworkInfo().getTypeName() +
1148 " teardown");
1149 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001150 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001151 return;
1152 }
1153 if (isFailover) {
1154 otherNet.releaseWakeLock();
1155 }
1156 }
1157 }
1158 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001159 // this will cause us to come up initially as unconnected and switching
1160 // to connected after our normal pause unless somebody reports us as reall
1161 // disconnected
1162 mDefaultInetConditionPublished = 0;
1163 mDefaultConnectionSequence++;
1164 mInetConditionChangeInFlight = false;
1165 // Don't do this - if we never sign in stay, grey
1166 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001167 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001168 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001169 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001170 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001171 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001172 }
1173
1174 private void handleScanResultsAvailable(NetworkInfo info) {
1175 int networkType = info.getType();
1176 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001177 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001178 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001179 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001180
The Android Open Source Project28527d22009-03-03 19:31:44 -08001181 mNetTrackers[networkType].interpretScanResultsAvailable();
1182 }
1183
Robert Greenwalt0659da32009-07-16 17:21:39 -07001184 private void handleNotificationChange(boolean visible, int id,
1185 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001186 NotificationManager notificationManager = (NotificationManager) mContext
1187 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001188
The Android Open Source Project28527d22009-03-03 19:31:44 -08001189 if (visible) {
1190 notificationManager.notify(id, notification);
1191 } else {
1192 notificationManager.cancel(id);
1193 }
1194 }
1195
1196 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001197 * After a change in the connectivity state of any network, We're mainly
1198 * concerned with making sure that the list of DNS servers is setupup
1199 * according to which networks are connected, and ensuring that the
1200 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001201 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001202 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001203 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001205 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001206 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001207 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001208
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001209 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1210 if (mNetAttributes[netType].isDefault()) {
1211 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001212 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001213 mNetTrackers[netType].addPrivateDnsRoutes();
1214 }
1215 } else {
1216 if (mNetAttributes[netType].isDefault()) {
1217 mNetTrackers[netType].removeDefaultRoute();
1218 } else {
1219 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001220 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001221 }
1222 }
1223
Robert Greenwalt2034b912009-08-12 16:08:25 -07001224 /**
1225 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1226 * on the highest priority active net which this process requested.
1227 * If there aren't any, clear it out
1228 */
1229 private void reassessPidDns(int myPid, boolean doBump)
1230 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001231 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001232 for(int i : mPriorityList) {
1233 if (mNetAttributes[i].isDefault()) {
1234 continue;
1235 }
1236 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001237 if (nt.getNetworkInfo().isConnected() &&
1238 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 List pids = mNetRequestersPids[i];
1240 for (int j=0; j<pids.size(); j++) {
1241 Integer pid = (Integer)pids.get(j);
1242 if (pid.intValue() == myPid) {
1243 String[] dnsList = nt.getNameServers();
1244 writePidDns(dnsList, myPid);
1245 if (doBump) {
1246 bumpDns();
1247 }
1248 return;
1249 }
1250 }
1251 }
1252 }
1253 // nothing found - delete
1254 for (int i = 1; ; i++) {
1255 String prop = "net.dns" + i + "." + myPid;
1256 if (SystemProperties.get(prop).length() == 0) {
1257 if (doBump) {
1258 bumpDns();
1259 }
1260 return;
1261 }
1262 SystemProperties.set(prop, "");
1263 }
1264 }
1265
1266 private void writePidDns(String[] dnsList, int pid) {
1267 int j = 1;
1268 for (String dns : dnsList) {
1269 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1270 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1271 }
1272 }
1273 }
1274
1275 private void bumpDns() {
1276 /*
1277 * Bump the property that tells the name resolver library to reread
1278 * the DNS server list from the properties.
1279 */
1280 String propVal = SystemProperties.get("net.dnschange");
1281 int n = 0;
1282 if (propVal.length() != 0) {
1283 try {
1284 n = Integer.parseInt(propVal);
1285 } catch (NumberFormatException e) {}
1286 }
1287 SystemProperties.set("net.dnschange", "" + (n+1));
1288 }
1289
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001290 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001291 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001292 NetworkStateTracker nt = mNetTrackers[netType];
1293 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1294 String[] dnsList = nt.getNameServers();
1295 if (mNetAttributes[netType].isDefault()) {
1296 int j = 1;
1297 for (String dns : dnsList) {
1298 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1299 if (DBG) {
1300 Slog.d(TAG, "adding dns " + dns + " for " +
1301 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001302 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001303 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001304 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001305 }
1306 for (int k=j ; k<mNumDnsEntries; k++) {
1307 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1308 SystemProperties.set("net.dns" + k, "");
1309 }
1310 mNumDnsEntries = j;
1311 } else {
1312 // set per-pid dns for attached secondary nets
1313 List pids = mNetRequestersPids[netType];
1314 for (int y=0; y< pids.size(); y++) {
1315 Integer pid = (Integer)pids.get(y);
1316 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001317 }
1318 }
1319 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 bumpDns();
1321 }
1322
1323 private int getRestoreDefaultNetworkDelay() {
1324 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1325 NETWORK_RESTORE_DELAY_PROP_NAME);
1326 if(restoreDefaultNetworkDelayStr != null &&
1327 restoreDefaultNetworkDelayStr.length() != 0) {
1328 try {
1329 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1330 } catch (NumberFormatException e) {
1331 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001332 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334 }
1335
1336 @Override
1337 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001338 if (mContext.checkCallingOrSelfPermission(
1339 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001340 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001341 pw.println("Permission Denial: can't dump ConnectivityService " +
1342 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1343 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001344 return;
1345 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001346 pw.println();
1347 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001348 if (nst != null) {
1349 if (nst.getNetworkInfo().isConnected()) {
1350 pw.println("Active network: " + nst.getNetworkInfo().
1351 getTypeName());
1352 }
1353 pw.println(nst.getNetworkInfo());
1354 pw.println(nst);
1355 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001357 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001358
1359 pw.println("Network Requester Pids:");
1360 for (int net : mPriorityList) {
1361 String pidString = net + ": ";
1362 for (Object pid : mNetRequestersPids[net]) {
1363 pidString = pidString + pid.toString() + ", ";
1364 }
1365 pw.println(pidString);
1366 }
1367 pw.println();
1368
1369 pw.println("FeatureUsers:");
1370 for (Object requester : mFeatureUsers) {
1371 pw.println(requester.toString());
1372 }
1373 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001374
1375 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001376
1377 if (mInetLog != null) {
1378 pw.println();
1379 pw.println("Inet condition reports:");
1380 for(int i = 0; i < mInetLog.size(); i++) {
1381 pw.println(mInetLog.get(i));
1382 }
1383 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001384 }
1385
Robert Greenwalt2034b912009-08-12 16:08:25 -07001386 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001387 private class MyHandler extends Handler {
1388 @Override
1389 public void handleMessage(Message msg) {
1390 NetworkInfo info;
1391 switch (msg.what) {
1392 case NetworkStateTracker.EVENT_STATE_CHANGED:
1393 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001394 int type = info.getType();
1395 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001396 // only do this optimization for wifi. It going into scan mode for location
1397 // services generates alot of noise. Meanwhile the mms apn won't send out
1398 // subsequent notifications when on default cellular because it never
1399 // disconnects.. so only do this to wifi notifications. Fixed better when the
1400 // APN notifications are standardized.
1401 if (mNetAttributes[type].mLastState == state &&
1402 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001403 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001404 // TODO - remove this after we validate the dropping doesn't break
1405 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001406 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001407 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001408 state + "/" + info.getDetailedState());
1409 }
1410 return;
1411 }
1412 mNetAttributes[type].mLastState = state;
1413
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001414 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001415 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001416 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001417
1418 // Connectivity state changed:
1419 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001420 // [12-9] Network subtype (for mobile network, as defined
1421 // by TelephonyManager)
1422 // [8-3] Detailed state ordinal (as defined by
1423 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001424 // [2-0] Network type (as defined by ConnectivityManager)
1425 int eventLogParam = (info.getType() & 0x7) |
1426 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1427 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001428 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001429 eventLogParam);
1430
1431 if (info.getDetailedState() ==
1432 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001433 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001434 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001435 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001436 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001437 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001438 // the logic here is, handle SUSPENDED the same as
1439 // DISCONNECTED. The only difference being we are
1440 // broadcasting an intent with NetworkInfo that's
1441 // suspended. This allows the applications an
1442 // opportunity to handle DISCONNECTED and SUSPENDED
1443 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001444 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001445 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001446 handleConnect(info);
1447 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001448 break;
1449
1450 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1451 info = (NetworkInfo) msg.obj;
1452 handleScanResultsAvailable(info);
1453 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001454
The Android Open Source Project28527d22009-03-03 19:31:44 -08001455 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001456 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1457 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001458 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001459
1460 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001461 info = (NetworkInfo) msg.obj;
1462 type = info.getType();
1463 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001464 break;
1465
1466 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1467 // fill me in
1468 break;
1469
1470 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1471 // fill me in
1472 break;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001473 case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001474 FeatureUser u = (FeatureUser)msg.obj;
1475 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001476 break;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001477 case NetworkStateTracker.EVENT_INET_CONDITION_CHANGE:
1478 if (DBG) {
1479 Slog.d(TAG, "Inet connectivity change, net=" +
1480 msg.arg1 + ", condition=" + msg.arg2 +
1481 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1482 }
1483 if (mActiveDefaultNetwork == -1) {
1484 if (DBG) Slog.d(TAG, "no active default network - aborting");
1485 break;
1486 }
1487 if (mActiveDefaultNetwork != msg.arg1) {
1488 if (DBG) Slog.d(TAG, "given net not default - aborting");
1489 break;
1490 }
1491 mDefaultInetCondition = msg.arg2;
1492 int delay;
1493 if (mInetConditionChangeInFlight == false) {
1494 if (DBG) Slog.d(TAG, "starting a change hold");
1495 // setup a new hold to debounce this
1496 if (mDefaultInetCondition > 50) {
1497 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1498 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1499 } else {
1500 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1501 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1502 }
1503 mInetConditionChangeInFlight = true;
1504 sendMessageDelayed(obtainMessage(
1505 NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END,
1506 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1507 } else {
1508 // we've set the new condition, when this hold ends that will get
1509 // picked up
1510 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1511 }
1512 break;
1513 case NetworkStateTracker.EVENT_INET_CONDITION_HOLD_END:
1514 if (DBG) {
1515 Slog.d(TAG, "Inet hold end, net=" + msg.arg1 +
1516 ", condition =" + mDefaultInetCondition +
1517 ", published condition =" + mDefaultInetConditionPublished);
1518 }
1519 mInetConditionChangeInFlight = false;
1520
1521 if (mActiveDefaultNetwork == -1) {
1522 if (DBG) Slog.d(TAG, "no active default network - aborting");
1523 break;
1524 }
1525 if (mDefaultConnectionSequence != msg.arg2) {
1526 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1527 break;
1528 }
1529 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1530 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1531 break;
1532 }
1533 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1534 if (networkInfo.isConnected() == false) {
1535 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1536 break;
1537 }
1538 mDefaultInetConditionPublished = mDefaultInetCondition;
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001539 sendInetConditionBroadcast(networkInfo);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001540 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001541 }
1542 }
1543 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001544
1545 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001546 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001547 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001548
1549 if (isTetheringSupported()) {
1550 return mTethering.tether(iface);
1551 } else {
1552 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1553 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001554 }
1555
1556 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001557 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001558 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001559
1560 if (isTetheringSupported()) {
1561 return mTethering.untether(iface);
1562 } else {
1563 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1564 }
1565 }
1566
1567 // javadoc from interface
1568 public int getLastTetherError(String iface) {
1569 enforceTetherAccessPermission();
1570
1571 if (isTetheringSupported()) {
1572 return mTethering.getLastTetherError(iface);
1573 } else {
1574 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1575 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001576 }
1577
1578 // TODO - proper iface API for selection by property, inspection, etc
1579 public String[] getTetherableUsbRegexs() {
1580 enforceTetherAccessPermission();
1581 if (isTetheringSupported()) {
1582 return mTethering.getTetherableUsbRegexs();
1583 } else {
1584 return new String[0];
1585 }
1586 }
1587
1588 public String[] getTetherableWifiRegexs() {
1589 enforceTetherAccessPermission();
1590 if (isTetheringSupported()) {
1591 return mTethering.getTetherableWifiRegexs();
1592 } else {
1593 return new String[0];
1594 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001595 }
1596
1597 // TODO - move iface listing, queries, etc to new module
1598 // javadoc from interface
1599 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001600 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001601 return mTethering.getTetherableIfaces();
1602 }
1603
1604 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001605 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001606 return mTethering.getTetheredIfaces();
1607 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001608
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001609 public String[] getTetheringErroredIfaces() {
1610 enforceTetherAccessPermission();
1611 return mTethering.getErroredIfaces();
1612 }
1613
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001614 // if ro.tether.denied = true we default to no tethering
1615 // gservices could set the secure setting to 1 though to enable it on a build where it
1616 // had previously been turned off.
1617 public boolean isTetheringSupported() {
1618 enforceTetherAccessPermission();
1619 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001620 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1621 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1622 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001623 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001624
1625 // 100 percent is full good, 0 is full bad.
1626 public void reportInetCondition(int networkType, int percentage) {
1627 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1628 mContext.enforceCallingOrSelfPermission(
1629 android.Manifest.permission.STATUS_BAR,
1630 "ConnectivityService");
1631
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001632 if (DBG) {
1633 int pid = getCallingPid();
1634 int uid = getCallingUid();
1635 String s = pid + "(" + uid + ") reports inet is " +
1636 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1637 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1638 mInetLog.add(s);
1639 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1640 mInetLog.remove(0);
1641 }
1642 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001643 mHandler.sendMessage(mHandler.obtainMessage(
1644 NetworkStateTracker.EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1645 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001646}