blob: 8560d16f719a07e2a2727ac17767aebd6e1d6b86 [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;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053030import android.net.NetworkUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import android.net.wifi.WifiStateTracker;
32import android.os.Binder;
33import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070034import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080035import android.os.Looper;
36import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070037import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080038import android.os.ServiceManager;
39import android.os.SystemProperties;
40import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070041import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080042import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080043import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044
Robert Greenwalt2034b912009-08-12 16:08:25 -070045import com.android.internal.telephony.Phone;
46
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080047import com.android.server.connectivity.Tethering;
48
The Android Open Source Project28527d22009-03-03 19:31:44 -080049import java.io.FileDescriptor;
50import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070051import java.util.ArrayList;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070052import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070053import java.util.List;
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +053054import java.net.InetAddress;
55import java.net.UnknownHostException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080056
57/**
58 * @hide
59 */
60public class ConnectivityService extends IConnectivityManager.Stub {
61
Dianne Hackborn8919f5a2010-09-30 18:27:22 -070062 private static final boolean DBG = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -080063 private static final String TAG = "ConnectivityService";
64
Robert Greenwalt2034b912009-08-12 16:08:25 -070065 // how long to wait before switching back to a radio's default network
66 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
67 // system property that can override the above value
68 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
69 "android.telephony.apn-restore";
70
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080071
72 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080073 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080074
The Android Open Source Project28527d22009-03-03 19:31:44 -080075 /**
76 * Sometimes we want to refer to the individual network state
77 * trackers separately, and sometimes we just want to treat them
78 * abstractly.
79 */
80 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070081
82 /**
83 * A per Net list of the PID's that requested access to the net
84 * used both as a refcount and for per-PID DNS selection
85 */
86 private List mNetRequestersPids[];
87
Robert Greenwalt2034b912009-08-12 16:08:25 -070088 // priority order of the nettrackers
89 // (excluding dynamically set mNetworkPreference)
90 // TODO - move mNetworkTypePreference into this
91 private int[] mPriorityList;
92
The Android Open Source Project28527d22009-03-03 19:31:44 -080093 private Context mContext;
94 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070095 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -070096 // 0 is full bad, 100 is full good
97 private int mDefaultInetCondition = 0;
98 private int mDefaultInetConditionPublished = 0;
99 private boolean mInetConditionChangeInFlight = false;
100 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800101
102 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800103
104 private boolean mTestMode;
105 private static ConnectivityService sServiceInstance;
106
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700107 private static final int ENABLED = 1;
108 private static final int DISABLED = 0;
109
110 // Share the event space with NetworkStateTracker (which can't see this
111 // internal class but sends us events). If you change these, change
112 // NetworkStateTracker.java too.
113 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
114 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
115
116 /**
117 * used internally as a delayed event to make us switch back to the
118 * default network
119 */
120 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
121 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
122
123 /**
124 * used internally to change our mobile data enabled flag
125 */
126 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
127 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
128
129 /**
130 * used internally to change our network preference setting
131 * arg1 = networkType to prefer
132 */
133 private static final int EVENT_SET_NETWORK_PREFERENCE =
134 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
135
136 /**
137 * used internally to synchronize inet condition reports
138 * arg1 = networkType
139 * arg2 = condition (0 bad, 100 good)
140 */
141 private static final int EVENT_INET_CONDITION_CHANGE =
142 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
143
144 /**
145 * used internally to mark the end of inet condition hold periods
146 * arg1 = networkType
147 */
148 private static final int EVENT_INET_CONDITION_HOLD_END =
149 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
150
151 /**
152 * used internally to set the background data preference
153 * arg1 = TRUE for enabled, FALSE for disabled
154 */
155 private static final int EVENT_SET_BACKGROUND_DATA =
156 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
157
158 /**
159 * used internally to set enable/disable cellular data
160 * arg1 = ENBALED or DISABLED
161 */
162 private static final int EVENT_SET_MOBILE_DATA =
163 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
164
Robert Greenwalt2034b912009-08-12 16:08:25 -0700165 private Handler mHandler;
166
167 // list of DeathRecipients used to make sure features are turned off when
168 // a process dies
169 private List mFeatureUsers;
170
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400171 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800172 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400173
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700174 // used in DBG mode to track inet condition reports
175 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
176 private ArrayList mInetLog;
177
Robert Greenwalt12c44552009-12-07 11:33:18 -0800178 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700179 /**
180 * Class for holding settings read from resources.
181 */
182 public String mName;
183 public int mType;
184 public int mRadio;
185 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800186 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700187 public NetworkAttributes(String init) {
188 String fragments[] = init.split(",");
189 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700190 mType = Integer.parseInt(fragments[1]);
191 mRadio = Integer.parseInt(fragments[2]);
192 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800193 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700194 }
195 public boolean isDefault() {
196 return (mType == mRadio);
197 }
198 }
199 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700200 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700201
Robert Greenwalt12c44552009-12-07 11:33:18 -0800202 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700203 public int mSimultaneity;
204 public int mType;
205 public RadioAttributes(String init) {
206 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700207 mType = Integer.parseInt(fragments[0]);
208 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700209 }
210 }
211 RadioAttributes[] mRadioAttributes;
212
The Android Open Source Project28527d22009-03-03 19:31:44 -0800213 private static class ConnectivityThread extends Thread {
214 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700215
The Android Open Source Project28527d22009-03-03 19:31:44 -0800216 private ConnectivityThread(Context context) {
217 super("ConnectivityThread");
218 mContext = context;
219 }
220
221 @Override
222 public void run() {
223 Looper.prepare();
224 synchronized (this) {
225 sServiceInstance = new ConnectivityService(mContext);
226 notifyAll();
227 }
228 Looper.loop();
229 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700230
The Android Open Source Project28527d22009-03-03 19:31:44 -0800231 public static ConnectivityService getServiceInstance(Context context) {
232 ConnectivityThread thread = new ConnectivityThread(context);
233 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700234
The Android Open Source Project28527d22009-03-03 19:31:44 -0800235 synchronized (thread) {
236 while (sServiceInstance == null) {
237 try {
238 // Wait until sServiceInstance has been initialized.
239 thread.wait();
240 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800241 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700242 "Unexpected InterruptedException while waiting"+
243 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800244 }
245 }
246 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700247
The Android Open Source Project28527d22009-03-03 19:31:44 -0800248 return sServiceInstance;
249 }
250 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700251
The Android Open Source Project28527d22009-03-03 19:31:44 -0800252 public static ConnectivityService getInstance(Context context) {
253 return ConnectivityThread.getServiceInstance(context);
254 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700255
The Android Open Source Project28527d22009-03-03 19:31:44 -0800256 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800257 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800258
259 // setup our unique device name
260 String id = Settings.Secure.getString(context.getContentResolver(),
261 Settings.Secure.ANDROID_ID);
262 if (id != null && id.length() > 0) {
263 String name = new String("android_").concat(id);
264 SystemProperties.set("net.hostname", name);
265 }
266
The Android Open Source Project28527d22009-03-03 19:31:44 -0800267 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700268 mNetTrackers = new NetworkStateTracker[
269 ConnectivityManager.MAX_NETWORK_TYPE+1];
270 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700271
The Android Open Source Project28527d22009-03-03 19:31:44 -0800272 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700273
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700274 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
275 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
276
Robert Greenwalt2034b912009-08-12 16:08:25 -0700277 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700278 String[] raStrings = context.getResources().getStringArray(
279 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700280 for (String raString : raStrings) {
281 RadioAttributes r = new RadioAttributes(raString);
282 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800283 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700284 continue;
285 }
286 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800287 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700288 r.mType);
289 continue;
290 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700291 mRadioAttributes[r.mType] = r;
292 }
293
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700294 String[] naStrings = context.getResources().getStringArray(
295 com.android.internal.R.array.networkAttributes);
296 for (String naString : naStrings) {
297 try {
298 NetworkAttributes n = new NetworkAttributes(naString);
299 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800300 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 n.mType);
302 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700303 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700304 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800305 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700306 n.mType);
307 continue;
308 }
309 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800310 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 "radio " + n.mRadio + " in network type " + n.mType);
312 continue;
313 }
314 mNetAttributes[n.mType] = n;
315 mNetworksDefined++;
316 } catch(Exception e) {
317 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700318 }
319 }
320
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700321 // high priority first
322 mPriorityList = new int[mNetworksDefined];
323 {
324 int insertionPoint = mNetworksDefined-1;
325 int currentLowest = 0;
326 int nextLowest = 0;
327 while (insertionPoint > -1) {
328 for (NetworkAttributes na : mNetAttributes) {
329 if (na == null) continue;
330 if (na.mPriority < currentLowest) continue;
331 if (na.mPriority > currentLowest) {
332 if (na.mPriority < nextLowest || nextLowest == 0) {
333 nextLowest = na.mPriority;
334 }
335 continue;
336 }
337 mPriorityList[insertionPoint--] = na.mType;
338 }
339 currentLowest = nextLowest;
340 nextLowest = 0;
341 }
342 }
343
344 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
345 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700346 mNetRequestersPids[i] = new ArrayList();
347 }
348
349 mFeatureUsers = new ArrayList();
350
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700351 mNumDnsEntries = 0;
352
353 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
354 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800355 /*
356 * Create the network state trackers for Wi-Fi and mobile
357 * data. Maybe this could be done with a factory class,
358 * but it's not clear that it's worth it, given that
359 * the number of different network types is not going
360 * to change very often.
361 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800362 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700363 for (int netType : mPriorityList) {
364 switch (mNetAttributes[netType].mRadio) {
365 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800366 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700367 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
368 WifiService wifiService = new WifiService(context, wst);
369 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800370 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700371 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
372 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800373
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700374 break;
375 case ConnectivityManager.TYPE_MOBILE:
376 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
377 netType, mNetAttributes[netType].mName);
378 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800379 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800380 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800381 mNetTrackers[netType].teardown();
382 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 break;
384 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800385 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700386 mNetAttributes[netType].mRadio);
387 continue;
388 }
389 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800390
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800391 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800392 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
393 !mTethering.isDunRequired()) &&
394 (mTethering.getTetherableUsbRegexs().length != 0 ||
395 mTethering.getTetherableWifiRegexs().length != 0) &&
396 mTethering.getUpstreamIfaceRegexs().length != 0);
397
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700398 if (DBG) {
399 mInetLog = new ArrayList();
400 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800401 }
402
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700403
The Android Open Source Project28527d22009-03-03 19:31:44 -0800404 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700405 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800406 * @param preference the new preference
407 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700408 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800409 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700410
411 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800412 }
413
414 public int getNetworkPreference() {
415 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700416 int preference;
417 synchronized(this) {
418 preference = mNetworkPreference;
419 }
420 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800421 }
422
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700423 private void handleSetNetworkPreference(int preference) {
424 if (ConnectivityManager.isNetworkTypeValid(preference) &&
425 mNetAttributes[preference] != null &&
426 mNetAttributes[preference].isDefault()) {
427 if (mNetworkPreference != preference) {
428 final ContentResolver cr = mContext.getContentResolver();
429 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
430 synchronized(this) {
431 mNetworkPreference = preference;
432 }
433 enforcePreference();
434 }
435 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800436 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700437
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 private int getPersistedNetworkPreference() {
439 final ContentResolver cr = mContext.getContentResolver();
440
441 final int networkPrefSetting = Settings.Secure
442 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
443 if (networkPrefSetting != -1) {
444 return networkPrefSetting;
445 }
446
447 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
448 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700449
The Android Open Source Project28527d22009-03-03 19:31:44 -0800450 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700451 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800452 * In this method, we only tear down a non-preferred network. Establishing
453 * a connection to the preferred network is taken care of when we handle
454 * the disconnect event from the non-preferred network
455 * (see {@link #handleDisconnect(NetworkInfo)}).
456 */
457 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700458 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800459 return;
460
Robert Greenwalt2034b912009-08-12 16:08:25 -0700461 if (!mNetTrackers[mNetworkPreference].isAvailable())
462 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800463
Robert Greenwalt2034b912009-08-12 16:08:25 -0700464 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700465 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700466 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700467 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800468 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700469 mNetTrackers[t].getNetworkInfo() +
470 " in enforcePreference");
471 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700472 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800473 }
474 }
475 }
476
477 private boolean teardown(NetworkStateTracker netTracker) {
478 if (netTracker.teardown()) {
479 netTracker.setTeardownRequested(true);
480 return true;
481 } else {
482 return false;
483 }
484 }
485
486 /**
487 * Return NetworkInfo for the active (i.e., connected) network interface.
488 * It is assumed that at most one network is active at a time. If more
489 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700490 * @return the info for the active network, or {@code null} if none is
491 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800492 */
493 public NetworkInfo getActiveNetworkInfo() {
494 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700495 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700496 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700497 continue;
498 }
499 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800500 NetworkInfo info = t.getNetworkInfo();
501 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800502 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700503 "connected default network is not " +
504 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800505 return info;
506 }
507 }
508 return null;
509 }
510
511 public NetworkInfo getNetworkInfo(int networkType) {
512 enforceAccessPermission();
513 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
514 NetworkStateTracker t = mNetTrackers[networkType];
515 if (t != null)
516 return t.getNetworkInfo();
517 }
518 return null;
519 }
520
521 public NetworkInfo[] getAllNetworkInfo() {
522 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700523 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 int i = 0;
525 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700526 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800527 }
528 return result;
529 }
530
531 public boolean setRadios(boolean turnOn) {
532 boolean result = true;
533 enforceChangePermission();
534 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700535 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800536 }
537 return result;
538 }
539
540 public boolean setRadio(int netType, boolean turnOn) {
541 enforceChangePermission();
542 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
543 return false;
544 }
545 NetworkStateTracker tracker = mNetTrackers[netType];
546 return tracker != null && tracker.setRadio(turnOn);
547 }
548
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700549 /**
550 * Used to notice when the calling process dies so we can self-expire
551 *
552 * Also used to know if the process has cleaned up after itself when
553 * our auto-expire timer goes off. The timer has a link to an object.
554 *
555 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700556 private class FeatureUser implements IBinder.DeathRecipient {
557 int mNetworkType;
558 String mFeature;
559 IBinder mBinder;
560 int mPid;
561 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800562 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700563
564 FeatureUser(int type, String feature, IBinder binder) {
565 super();
566 mNetworkType = type;
567 mFeature = feature;
568 mBinder = binder;
569 mPid = getCallingPid();
570 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800571 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700572
Robert Greenwalt2034b912009-08-12 16:08:25 -0700573 try {
574 mBinder.linkToDeath(this, 0);
575 } catch (RemoteException e) {
576 binderDied();
577 }
578 }
579
580 void unlinkDeathRecipient() {
581 mBinder.unlinkToDeath(this, 0);
582 }
583
584 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800585 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800586 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
587 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700588 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700589 }
590
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700591 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800592 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800593 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
594 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700595 stopUsingNetworkFeature(this, false);
596 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800597
598 public String toString() {
599 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
600 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
601 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 }
603
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700604 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700605 public int startUsingNetworkFeature(int networkType, String feature,
606 IBinder binder) {
607 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800608 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700609 ": " + feature);
610 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800611 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700612 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
613 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700614 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800615 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700616
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700617 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700618
619 // TODO - move this into the MobileDataStateTracker
620 int usedNetworkType = networkType;
621 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800622 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800623 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800624 return Phone.APN_TYPE_NOT_AVAILABLE;
625 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700626 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
627 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
628 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
629 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
630 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
631 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
632 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
633 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
634 }
635 }
636 NetworkStateTracker network = mNetTrackers[usedNetworkType];
637 if (network != null) {
638 if (usedNetworkType != networkType) {
639 Integer currentPid = new Integer(getCallingPid());
640
641 NetworkStateTracker radio = mNetTrackers[networkType];
642 NetworkInfo ni = network.getNetworkInfo();
643
644 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800645 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700646 return Phone.APN_TYPE_NOT_AVAILABLE;
647 }
648
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700649 synchronized(this) {
650 mFeatureUsers.add(f);
651 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
652 // this gets used for per-pid dns when connected
653 mNetRequestersPids[usedNetworkType].add(currentPid);
654 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700655 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700656 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700657 f), getRestoreDefaultNetworkDelay());
658
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700660 if ((ni.isConnectedOrConnecting() == true) &&
661 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700662 if (ni.isConnected() == true) {
663 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700664 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800665 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700666 return Phone.APN_ALREADY_ACTIVE;
667 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800668 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700669 return Phone.APN_REQUEST_STARTED;
670 }
671
672 // check if the radio in play can make another contact
673 // assume if cannot for now
674
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800675 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700676 network.reconnect();
677 return Phone.APN_REQUEST_STARTED;
678 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700679 synchronized(this) {
680 mFeatureUsers.add(f);
681 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700682 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700683 f), getRestoreDefaultNetworkDelay());
684
Robert Greenwalt2034b912009-08-12 16:08:25 -0700685 return network.startUsingNetworkFeature(feature,
686 getCallingPid(), getCallingUid());
687 }
688 }
689 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800690 }
691
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700692 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800693 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700694 enforceChangePermission();
695
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700696 int pid = getCallingPid();
697 int uid = getCallingUid();
698
699 FeatureUser u = null;
700 boolean found = false;
701
702 synchronized(this) {
703 for (int i = 0; i < mFeatureUsers.size() ; i++) {
704 u = (FeatureUser)mFeatureUsers.get(i);
705 if (uid == u.mUid && pid == u.mPid &&
706 networkType == u.mNetworkType &&
707 TextUtils.equals(feature, u.mFeature)) {
708 found = true;
709 break;
710 }
711 }
712 }
713 if (found && u != null) {
714 // stop regardless of how many other time this proc had called start
715 return stopUsingNetworkFeature(u, true);
716 } else {
717 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800718 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700719 return 1;
720 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700721 }
722
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
724 int networkType = u.mNetworkType;
725 String feature = u.mFeature;
726 int pid = u.mPid;
727 int uid = u.mUid;
728
729 NetworkStateTracker tracker = null;
730 boolean callTeardown = false; // used to carry our decision outside of sync block
731
Robert Greenwalt2034b912009-08-12 16:08:25 -0700732 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800733 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700734 ": " + feature);
735 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700736
The Android Open Source Project28527d22009-03-03 19:31:44 -0800737 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
738 return -1;
739 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700740
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700741 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
742 // sync block
743 synchronized(this) {
744 // check if this process still has an outstanding start request
745 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800746 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700747 return 1;
748 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700749 u.unlinkDeathRecipient();
750 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
751 // If we care about duplicate requests, check for that here.
752 //
753 // This is done to support the extension of a request - the app
754 // can request we start the network feature again and renew the
755 // auto-shutoff delay. Normal "stop" calls from the app though
756 // do not pay attention to duplicate requests - in effect the
757 // API does not refcount and a single stop will counter multiple starts.
758 if (ignoreDups == false) {
759 for (int i = 0; i < mFeatureUsers.size() ; i++) {
760 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
761 if (x.mUid == u.mUid && x.mPid == u.mPid &&
762 x.mNetworkType == u.mNetworkType &&
763 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800764 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700765 return 1;
766 }
767 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700768 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700769
770 // TODO - move to MobileDataStateTracker
771 int usedNetworkType = networkType;
772 if (networkType == ConnectivityManager.TYPE_MOBILE) {
773 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
774 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
775 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
776 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
777 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
778 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
779 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
780 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
781 }
782 }
783 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700784 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800785 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700786 return -1;
787 }
788 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700789 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700790 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800791 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700792 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800793 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700794 "others still using it");
795 return 1;
796 }
797 callTeardown = true;
798 }
799 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800800 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700801 if (callTeardown) {
802 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803 return 1;
804 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700805 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700806 return tracker.stopUsingNetworkFeature(feature, pid, uid);
807 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800808 }
809
810 /**
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530811 * @deprecated use requestRouteToHostAddress instead
812 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800813 * Ensure that a network route exists to deliver traffic to the specified
814 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700815 * @param networkType the type of the network over which traffic to the
816 * specified host is to be routed
817 * @param hostAddress the IP address of the host to which the route is
818 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800819 * @return {@code true} on success, {@code false} on failure
820 */
821 public boolean requestRouteToHost(int networkType, int hostAddress) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530822 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
823
824 if (inetAddress == null) {
825 return false;
826 }
827
828 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
829 }
830
831 /**
832 * Ensure that a network route exists to deliver traffic to the specified
833 * host via the specified network interface.
834 * @param networkType the type of the network over which traffic to the
835 * specified host is to be routed
836 * @param hostAddress the IP address of the host to which the route is
837 * desired
838 * @return {@code true} on success, {@code false} on failure
839 */
840 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800841 enforceChangePermission();
842 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
843 return false;
844 }
845 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700846
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700847 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
848 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700849 if (DBG) {
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530850 Slog.d(TAG, "requestRouteToHostAddress on down network " +
851 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700852 }
853 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800854 }
Banavathu, Srinivas Naik7601f572010-08-10 20:13:53 +0530855
856 try {
857 InetAddress inetAddress = InetAddress.getByAddress(hostAddress);
858 return tracker.requestRouteToHost(inetAddress);
859 } catch (UnknownHostException e) {
860 return false;
861 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800862 }
863
864 /**
865 * @see ConnectivityManager#getBackgroundDataSetting()
866 */
867 public boolean getBackgroundDataSetting() {
868 return Settings.Secure.getInt(mContext.getContentResolver(),
869 Settings.Secure.BACKGROUND_DATA, 1) == 1;
870 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700871
The Android Open Source Project28527d22009-03-03 19:31:44 -0800872 /**
873 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
874 */
875 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
876 mContext.enforceCallingOrSelfPermission(
877 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
878 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700879
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700880 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
881 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
882 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800883
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700884 private void handleSetBackgroundData(boolean enabled) {
885 if (enabled != getBackgroundDataSetting()) {
886 Settings.Secure.putInt(mContext.getContentResolver(),
887 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
888 Intent broadcast = new Intent(
889 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
890 mContext.sendBroadcast(broadcast);
891 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700892 }
893
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800894 /**
895 * @see ConnectivityManager#getMobileDataEnabled()
896 */
897 public boolean getMobileDataEnabled() {
898 enforceAccessPermission();
899 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
900 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800901 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800902 return retVal;
903 }
904
905 /**
906 * @see ConnectivityManager#setMobileDataEnabled(boolean)
907 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700908 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800909 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800910 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800911
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700912 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
913 (enabled ? ENABLED : DISABLED), 0));
914 }
915
916 private void handleSetMobileData(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800917 if (getMobileDataEnabled() == enabled) return;
918
919 Settings.Secure.putInt(mContext.getContentResolver(),
920 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
921
922 if (enabled) {
923 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700924 if (DBG) {
925 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
926 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800927 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
928 }
929 } else {
930 for (NetworkStateTracker nt : mNetTrackers) {
931 if (nt == null) continue;
932 int netType = nt.getNetworkInfo().getType();
933 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800934 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800935 nt.teardown();
936 }
937 }
938 }
939 }
940
The Android Open Source Project28527d22009-03-03 19:31:44 -0800941 private int getNumConnectedNetworks() {
942 int numConnectedNets = 0;
943
944 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700945 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700946 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800947 ++numConnectedNets;
948 }
949 }
950 return numConnectedNets;
951 }
952
953 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700954 mContext.enforceCallingOrSelfPermission(
955 android.Manifest.permission.ACCESS_NETWORK_STATE,
956 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957 }
958
959 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700960 mContext.enforceCallingOrSelfPermission(
961 android.Manifest.permission.CHANGE_NETWORK_STATE,
962 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800963 }
964
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800965 // TODO Make this a special check when it goes public
966 private void enforceTetherChangePermission() {
967 mContext.enforceCallingOrSelfPermission(
968 android.Manifest.permission.CHANGE_NETWORK_STATE,
969 "ConnectivityService");
970 }
971
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800972 private void enforceTetherAccessPermission() {
973 mContext.enforceCallingOrSelfPermission(
974 android.Manifest.permission.ACCESS_NETWORK_STATE,
975 "ConnectivityService");
976 }
977
The Android Open Source Project28527d22009-03-03 19:31:44 -0800978 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700979 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
980 * network, we ignore it. If it is for the active network, we send out a
981 * broadcast. But first, we check whether it might be possible to connect
982 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800983 * @param info the {@code NetworkInfo} for the network
984 */
985 private void handleDisconnect(NetworkInfo info) {
986
Robert Greenwalt2034b912009-08-12 16:08:25 -0700987 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800988
Robert Greenwalt2034b912009-08-12 16:08:25 -0700989 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800990 /*
991 * If the disconnected network is not the active one, then don't report
992 * this as a loss of connectivity. What probably happened is that we're
993 * getting the disconnect for a network that we explicitly disabled
994 * in accordance with network preference policies.
995 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700996 if (!mNetAttributes[prevNetType].isDefault()) {
997 List pids = mNetRequestersPids[prevNetType];
998 for (int i = 0; i<pids.size(); i++) {
999 Integer pid = (Integer)pids.get(i);
1000 // will remove them because the net's no longer connected
1001 // need to do this now as only now do we know the pids and
1002 // can properly null things that are no longer referenced.
1003 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001004 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001005 }
1006
The Android Open Source Project28527d22009-03-03 19:31:44 -08001007 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1008 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1009 if (info.isFailover()) {
1010 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1011 info.setFailover(false);
1012 }
1013 if (info.getReason() != null) {
1014 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1015 }
1016 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001017 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1018 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001019 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001020
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001021 NetworkStateTracker newNet = null;
1022 if (mNetAttributes[prevNetType].isDefault()) {
1023 newNet = tryFailover(prevNetType);
1024 if (newNet != null) {
1025 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001026 if (!switchTo.isConnected()) {
1027 // if the other net is connected they've already reset this and perhaps even gotten
1028 // a positive report we don't want to overwrite, but if not we need to clear this now
1029 // to turn our cellular sig strength white
1030 mDefaultInetConditionPublished = 0;
Robert Greenwaltea8bca02010-12-21 11:43:28 -08001031 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001032 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001033 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1034 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001035 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001036 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1037 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001038 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001039 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001040 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001041 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001042
1043 sendStickyBroadcast(intent);
1044 /*
1045 * If the failover network is already connected, then immediately send
1046 * out a followup broadcast indicating successful failover
1047 */
1048 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1049 sendConnectedBroadcast(newNet.getNetworkInfo());
1050 }
1051 }
1052
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001053 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001054 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001055 /*
1056 * If this is a default network, check if other defaults are available
1057 * or active
1058 */
1059 NetworkStateTracker newNet = null;
1060 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001061 if (mActiveDefaultNetwork == prevNetType) {
1062 mActiveDefaultNetwork = -1;
1063 }
1064
1065 int newType = -1;
1066 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001067 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001068 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001069 if (checkType == prevNetType) continue;
1070 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001071 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1072 noMobileData) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001073 Slog.e(TAG, "not failing over to mobile type " + checkType +
1074 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001075 continue;
1076 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 if (mNetAttributes[checkType].isDefault()) {
1078 /* TODO - if we have multiple nets we could use
1079 * we may want to put more thought into which we choose
1080 */
1081 if (checkType == mNetworkPreference) {
1082 newType = checkType;
1083 break;
1084 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001085 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001086 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001087 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001088 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001089 }
1090 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001091
1092 if (newType != -1) {
1093 newNet = mNetTrackers[newType];
1094 /**
1095 * See if the other network is available to fail over to.
1096 * If is not available, we enable it anyway, so that it
1097 * will be able to connect when it does become available,
1098 * but we report a total loss of connectivity rather than
1099 * report that we are attempting to fail over.
1100 */
1101 if (newNet.isAvailable()) {
1102 NetworkInfo switchTo = newNet.getNetworkInfo();
1103 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001104 if (!switchTo.isConnectedOrConnecting() ||
1105 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001106 newNet.reconnect();
1107 }
1108 if (DBG) {
1109 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001110 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001111 switchTo.getTypeName());
1112 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001113 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001114 switchTo.getTypeName());
1115 }
1116 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001117 } else {
1118 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001119 newNet = null; // not officially avail.. try anyway, but
1120 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001121 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001122 } else {
1123 Slog.e(TAG, "Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001124 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001125 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001126
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001127 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001128 }
1129
1130 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001131 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1132 }
1133
1134 private void sendInetConditionBroadcast(NetworkInfo info) {
1135 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1136 }
1137
1138 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1139 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001140 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1141 if (info.isFailover()) {
1142 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1143 info.setFailover(false);
1144 }
1145 if (info.getReason() != null) {
1146 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1147 }
1148 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001149 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1150 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001151 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001152 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001153 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001154 }
1155
1156 /**
1157 * Called when an attempt to fail over to another network has failed.
1158 * @param info the {@link NetworkInfo} for the failed network
1159 */
1160 private void handleConnectionFailure(NetworkInfo info) {
1161 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001162
Robert Greenwalt2034b912009-08-12 16:08:25 -07001163 String reason = info.getReason();
1164 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001165
Robert Greenwalte981bc52010-10-08 16:35:52 -07001166 String reasonText;
1167 if (reason == null) {
1168 reasonText = ".";
1169 } else {
1170 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001171 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001172 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001173
1174 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1175 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1176 if (getActiveNetworkInfo() == null) {
1177 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1178 }
1179 if (reason != null) {
1180 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1181 }
1182 if (extraInfo != null) {
1183 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1184 }
1185 if (info.isFailover()) {
1186 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1187 info.setFailover(false);
1188 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001189
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001190 NetworkStateTracker newNet = null;
1191 if (mNetAttributes[info.getType()].isDefault()) {
1192 newNet = tryFailover(info.getType());
1193 if (newNet != null) {
1194 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001195 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001196 // if the other net is connected they've already reset this and perhaps
1197 // even gotten a positive report we don't want to overwrite, but if not
1198 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001199 mDefaultInetConditionPublished = 0;
1200 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001201 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1202 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001203 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001204 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1205 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001206 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001207
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001208 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001209 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001210 /*
1211 * If the failover network is already connected, then immediately send
1212 * out a followup broadcast indicating successful failover
1213 */
1214 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1215 sendConnectedBroadcast(newNet.getNetworkInfo());
1216 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001217 }
1218
1219 private void sendStickyBroadcast(Intent intent) {
1220 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001221 if (!mSystemReady) {
1222 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001223 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001224 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1225 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001226 }
1227 }
1228
1229 void systemReady() {
1230 synchronized(this) {
1231 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001232 if (mInitialBroadcast != null) {
1233 mContext.sendStickyBroadcast(mInitialBroadcast);
1234 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001235 }
1236 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001237 }
1238
1239 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001240 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241
1242 // snapshot isFailover, because sendConnectedBroadcast() resets it
1243 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001244 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001245
Robert Greenwalt2034b912009-08-12 16:08:25 -07001246 // if this is a default net and other default is running
1247 // kill the one not preferred
1248 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001249 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1250 if ((type != mNetworkPreference &&
1251 mNetAttributes[mActiveDefaultNetwork].mPriority >
1252 mNetAttributes[type].mPriority) ||
1253 mNetworkPreference == mActiveDefaultNetwork) {
1254 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001255 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001256 "to torn down network " + info.getTypeName());
1257 teardown(thisNet);
1258 return;
1259 } else {
1260 // tear down the other
1261 NetworkStateTracker otherNet =
1262 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001263 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001264 otherNet.getNetworkInfo().getTypeName() +
1265 " teardown");
1266 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001267 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 return;
1269 }
1270 if (isFailover) {
1271 otherNet.releaseWakeLock();
1272 }
1273 }
1274 }
1275 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001276 // this will cause us to come up initially as unconnected and switching
1277 // to connected after our normal pause unless somebody reports us as reall
1278 // disconnected
1279 mDefaultInetConditionPublished = 0;
1280 mDefaultConnectionSequence++;
1281 mInetConditionChangeInFlight = false;
1282 // Don't do this - if we never sign in stay, grey
1283 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001284 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001285 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001286 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001287 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001288 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001289 }
1290
1291 private void handleScanResultsAvailable(NetworkInfo info) {
1292 int networkType = info.getType();
1293 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001294 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001295 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001296 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001297
The Android Open Source Project28527d22009-03-03 19:31:44 -08001298 mNetTrackers[networkType].interpretScanResultsAvailable();
1299 }
1300
Robert Greenwalt0659da32009-07-16 17:21:39 -07001301 private void handleNotificationChange(boolean visible, int id,
1302 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001303 NotificationManager notificationManager = (NotificationManager) mContext
1304 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001305
The Android Open Source Project28527d22009-03-03 19:31:44 -08001306 if (visible) {
1307 notificationManager.notify(id, notification);
1308 } else {
1309 notificationManager.cancel(id);
1310 }
1311 }
1312
1313 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001314 * After a change in the connectivity state of any network, We're mainly
1315 * concerned with making sure that the list of DNS servers is setupup
1316 * according to which networks are connected, and ensuring that the
1317 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001318 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001319 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001320 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001321 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001322 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001323 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001324 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001325
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001326 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1327 if (mNetAttributes[netType].isDefault()) {
1328 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001329 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001330 mNetTrackers[netType].addPrivateDnsRoutes();
1331 }
1332 } else {
1333 if (mNetAttributes[netType].isDefault()) {
1334 mNetTrackers[netType].removeDefaultRoute();
1335 } else {
1336 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001337 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001338 }
1339 }
1340
Robert Greenwalt2034b912009-08-12 16:08:25 -07001341 /**
1342 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1343 * on the highest priority active net which this process requested.
1344 * If there aren't any, clear it out
1345 */
1346 private void reassessPidDns(int myPid, boolean doBump)
1347 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001348 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001349 for(int i : mPriorityList) {
1350 if (mNetAttributes[i].isDefault()) {
1351 continue;
1352 }
1353 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001354 if (nt.getNetworkInfo().isConnected() &&
1355 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 List pids = mNetRequestersPids[i];
1357 for (int j=0; j<pids.size(); j++) {
1358 Integer pid = (Integer)pids.get(j);
1359 if (pid.intValue() == myPid) {
1360 String[] dnsList = nt.getNameServers();
1361 writePidDns(dnsList, myPid);
1362 if (doBump) {
1363 bumpDns();
1364 }
1365 return;
1366 }
1367 }
1368 }
1369 }
1370 // nothing found - delete
1371 for (int i = 1; ; i++) {
1372 String prop = "net.dns" + i + "." + myPid;
1373 if (SystemProperties.get(prop).length() == 0) {
1374 if (doBump) {
1375 bumpDns();
1376 }
1377 return;
1378 }
1379 SystemProperties.set(prop, "");
1380 }
1381 }
1382
1383 private void writePidDns(String[] dnsList, int pid) {
1384 int j = 1;
1385 for (String dns : dnsList) {
1386 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1387 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1388 }
1389 }
1390 }
1391
1392 private void bumpDns() {
1393 /*
1394 * Bump the property that tells the name resolver library to reread
1395 * the DNS server list from the properties.
1396 */
1397 String propVal = SystemProperties.get("net.dnschange");
1398 int n = 0;
1399 if (propVal.length() != 0) {
1400 try {
1401 n = Integer.parseInt(propVal);
1402 } catch (NumberFormatException e) {}
1403 }
1404 SystemProperties.set("net.dnschange", "" + (n+1));
1405 }
1406
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001407 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001408 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001409 NetworkStateTracker nt = mNetTrackers[netType];
1410 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1411 String[] dnsList = nt.getNameServers();
1412 if (mNetAttributes[netType].isDefault()) {
1413 int j = 1;
1414 for (String dns : dnsList) {
1415 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1416 if (DBG) {
1417 Slog.d(TAG, "adding dns " + dns + " for " +
1418 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001419 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001420 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001421 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001422 }
1423 for (int k=j ; k<mNumDnsEntries; k++) {
1424 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1425 SystemProperties.set("net.dns" + k, "");
1426 }
1427 mNumDnsEntries = j;
1428 } else {
1429 // set per-pid dns for attached secondary nets
1430 List pids = mNetRequestersPids[netType];
1431 for (int y=0; y< pids.size(); y++) {
1432 Integer pid = (Integer)pids.get(y);
1433 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001434 }
1435 }
1436 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001437 bumpDns();
1438 }
1439
1440 private int getRestoreDefaultNetworkDelay() {
1441 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1442 NETWORK_RESTORE_DELAY_PROP_NAME);
1443 if(restoreDefaultNetworkDelayStr != null &&
1444 restoreDefaultNetworkDelayStr.length() != 0) {
1445 try {
1446 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1447 } catch (NumberFormatException e) {
1448 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001449 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001450 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001451 }
1452
1453 @Override
1454 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001455 if (mContext.checkCallingOrSelfPermission(
1456 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001457 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001458 pw.println("Permission Denial: can't dump ConnectivityService " +
1459 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1460 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001461 return;
1462 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001463 pw.println();
1464 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001465 if (nst != null) {
1466 if (nst.getNetworkInfo().isConnected()) {
1467 pw.println("Active network: " + nst.getNetworkInfo().
1468 getTypeName());
1469 }
1470 pw.println(nst.getNetworkInfo());
1471 pw.println(nst);
1472 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001473 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001474 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001475
1476 pw.println("Network Requester Pids:");
1477 for (int net : mPriorityList) {
1478 String pidString = net + ": ";
1479 for (Object pid : mNetRequestersPids[net]) {
1480 pidString = pidString + pid.toString() + ", ";
1481 }
1482 pw.println(pidString);
1483 }
1484 pw.println();
1485
1486 pw.println("FeatureUsers:");
1487 for (Object requester : mFeatureUsers) {
1488 pw.println(requester.toString());
1489 }
1490 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001491
1492 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001493
1494 if (mInetLog != null) {
1495 pw.println();
1496 pw.println("Inet condition reports:");
1497 for(int i = 0; i < mInetLog.size(); i++) {
1498 pw.println(mInetLog.get(i));
1499 }
1500 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001501 }
1502
Robert Greenwalt2034b912009-08-12 16:08:25 -07001503 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001504 private class MyHandler extends Handler {
1505 @Override
1506 public void handleMessage(Message msg) {
1507 NetworkInfo info;
1508 switch (msg.what) {
1509 case NetworkStateTracker.EVENT_STATE_CHANGED:
1510 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001511 int type = info.getType();
1512 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001513 // only do this optimization for wifi. It going into scan mode for location
1514 // services generates alot of noise. Meanwhile the mms apn won't send out
1515 // subsequent notifications when on default cellular because it never
1516 // disconnects.. so only do this to wifi notifications. Fixed better when the
1517 // APN notifications are standardized.
1518 if (mNetAttributes[type].mLastState == state &&
1519 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001520 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001521 // TODO - remove this after we validate the dropping doesn't break
1522 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001523 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001524 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001525 state + "/" + info.getDetailedState());
1526 }
1527 return;
1528 }
1529 mNetAttributes[type].mLastState = state;
1530
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001531 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001532 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001533 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001534
1535 // Connectivity state changed:
1536 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001537 // [12-9] Network subtype (for mobile network, as defined
1538 // by TelephonyManager)
1539 // [8-3] Detailed state ordinal (as defined by
1540 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001541 // [2-0] Network type (as defined by ConnectivityManager)
1542 int eventLogParam = (info.getType() & 0x7) |
1543 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1544 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001545 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001546 eventLogParam);
1547
1548 if (info.getDetailedState() ==
1549 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001550 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001551 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001552 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001553 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001554 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001555 // the logic here is, handle SUSPENDED the same as
1556 // DISCONNECTED. The only difference being we are
1557 // broadcasting an intent with NetworkInfo that's
1558 // suspended. This allows the applications an
1559 // opportunity to handle DISCONNECTED and SUSPENDED
1560 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001561 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001562 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001563 handleConnect(info);
1564 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001565 break;
1566
1567 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1568 info = (NetworkInfo) msg.obj;
1569 handleScanResultsAvailable(info);
1570 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001571
The Android Open Source Project28527d22009-03-03 19:31:44 -08001572 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001573 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1574 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001575 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001576
1577 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001578 info = (NetworkInfo) msg.obj;
1579 type = info.getType();
1580 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001581 break;
1582
1583 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1584 // fill me in
1585 break;
1586
1587 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1588 // fill me in
1589 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001590 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001591 FeatureUser u = (FeatureUser)msg.obj;
1592 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001593 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001594 case EVENT_INET_CONDITION_CHANGE:
1595 {
1596 int netType = msg.arg1;
1597 int condition = msg.arg2;
1598 handleInetConditionChange(netType, condition);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001599 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001600 }
1601 case EVENT_INET_CONDITION_HOLD_END:
1602 {
1603 int netType = msg.arg1;
1604 int sequence = msg.arg2;
1605 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001606 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001607 }
1608 case EVENT_SET_NETWORK_PREFERENCE:
1609 {
1610 int preference = msg.arg1;
1611 handleSetNetworkPreference(preference);
1612 break;
1613 }
1614 case EVENT_SET_BACKGROUND_DATA:
1615 {
1616 boolean enabled = (msg.arg1 == ENABLED);
1617 handleSetBackgroundData(enabled);
1618 break;
1619 }
1620 case EVENT_SET_MOBILE_DATA:
1621 {
1622 boolean enabled = (msg.arg1 == ENABLED);
1623 handleSetMobileData(enabled);
1624 break;
1625 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001626 }
1627 }
1628 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001629
1630 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001631 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001632 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001633
1634 if (isTetheringSupported()) {
1635 return mTethering.tether(iface);
1636 } else {
1637 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1638 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001639 }
1640
1641 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001642 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001643 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001644
1645 if (isTetheringSupported()) {
1646 return mTethering.untether(iface);
1647 } else {
1648 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1649 }
1650 }
1651
1652 // javadoc from interface
1653 public int getLastTetherError(String iface) {
1654 enforceTetherAccessPermission();
1655
1656 if (isTetheringSupported()) {
1657 return mTethering.getLastTetherError(iface);
1658 } else {
1659 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1660 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001661 }
1662
1663 // TODO - proper iface API for selection by property, inspection, etc
1664 public String[] getTetherableUsbRegexs() {
1665 enforceTetherAccessPermission();
1666 if (isTetheringSupported()) {
1667 return mTethering.getTetherableUsbRegexs();
1668 } else {
1669 return new String[0];
1670 }
1671 }
1672
1673 public String[] getTetherableWifiRegexs() {
1674 enforceTetherAccessPermission();
1675 if (isTetheringSupported()) {
1676 return mTethering.getTetherableWifiRegexs();
1677 } else {
1678 return new String[0];
1679 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001680 }
1681
1682 // TODO - move iface listing, queries, etc to new module
1683 // javadoc from interface
1684 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001685 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001686 return mTethering.getTetherableIfaces();
1687 }
1688
1689 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001690 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001691 return mTethering.getTetheredIfaces();
1692 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001693
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001694 public String[] getTetheringErroredIfaces() {
1695 enforceTetherAccessPermission();
1696 return mTethering.getErroredIfaces();
1697 }
1698
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001699 // if ro.tether.denied = true we default to no tethering
1700 // gservices could set the secure setting to 1 though to enable it on a build where it
1701 // had previously been turned off.
1702 public boolean isTetheringSupported() {
1703 enforceTetherAccessPermission();
1704 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001705 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1706 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1707 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001708 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001709
1710 // 100 percent is full good, 0 is full bad.
1711 public void reportInetCondition(int networkType, int percentage) {
1712 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1713 mContext.enforceCallingOrSelfPermission(
1714 android.Manifest.permission.STATUS_BAR,
1715 "ConnectivityService");
1716
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001717 if (DBG) {
1718 int pid = getCallingPid();
1719 int uid = getCallingUid();
1720 String s = pid + "(" + uid + ") reports inet is " +
1721 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1722 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1723 mInetLog.add(s);
1724 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1725 mInetLog.remove(0);
1726 }
1727 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001728 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001729 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1730 }
1731
1732 private void handleInetConditionChange(int netType, int condition) {
1733 if (DBG) {
1734 Slog.d(TAG, "Inet connectivity change, net=" +
1735 netType + ", condition=" + condition +
1736 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1737 }
1738 if (mActiveDefaultNetwork == -1) {
1739 if (DBG) Slog.d(TAG, "no active default network - aborting");
1740 return;
1741 }
1742 if (mActiveDefaultNetwork != netType) {
1743 if (DBG) Slog.d(TAG, "given net not default - aborting");
1744 return;
1745 }
1746 mDefaultInetCondition = condition;
1747 int delay;
1748 if (mInetConditionChangeInFlight == false) {
1749 if (DBG) Slog.d(TAG, "starting a change hold");
1750 // setup a new hold to debounce this
1751 if (mDefaultInetCondition > 50) {
1752 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1753 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1754 } else {
1755 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1756 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1757 }
1758 mInetConditionChangeInFlight = true;
1759 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1760 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1761 } else {
1762 // we've set the new condition, when this hold ends that will get
1763 // picked up
1764 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1765 }
1766 }
1767
1768 private void handleInetConditionHoldEnd(int netType, int sequence) {
1769 if (DBG) {
1770 Slog.d(TAG, "Inet hold end, net=" + netType +
1771 ", condition =" + mDefaultInetCondition +
1772 ", published condition =" + mDefaultInetConditionPublished);
1773 }
1774 mInetConditionChangeInFlight = false;
1775
1776 if (mActiveDefaultNetwork == -1) {
1777 if (DBG) Slog.d(TAG, "no active default network - aborting");
1778 return;
1779 }
1780 if (mDefaultConnectionSequence != sequence) {
1781 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1782 return;
1783 }
1784 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1785 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1786 return;
1787 }
1788 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1789 if (networkInfo.isConnected() == false) {
1790 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1791 return;
1792 }
1793 mDefaultInetConditionPublished = mDefaultInetCondition;
1794 sendInetConditionBroadcast(networkInfo);
1795 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001796 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001797}