blob: 7c9b547b0d44bd7919358e3e267cf587a790fea1 [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;
1031 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001032 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1033 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001034 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001035 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1036 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001037 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001038 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001039 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001040 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001041
1042 sendStickyBroadcast(intent);
1043 /*
1044 * If the failover network is already connected, then immediately send
1045 * out a followup broadcast indicating successful failover
1046 */
1047 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1048 sendConnectedBroadcast(newNet.getNetworkInfo());
1049 }
1050 }
1051
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001052 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001053 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001054 /*
1055 * If this is a default network, check if other defaults are available
1056 * or active
1057 */
1058 NetworkStateTracker newNet = null;
1059 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060 if (mActiveDefaultNetwork == prevNetType) {
1061 mActiveDefaultNetwork = -1;
1062 }
1063
1064 int newType = -1;
1065 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001066 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001067 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001068 if (checkType == prevNetType) continue;
1069 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001070 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1071 noMobileData) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001072 Slog.e(TAG, "not failing over to mobile type " + checkType +
1073 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001074 continue;
1075 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001076 if (mNetAttributes[checkType].isDefault()) {
1077 /* TODO - if we have multiple nets we could use
1078 * we may want to put more thought into which we choose
1079 */
1080 if (checkType == mNetworkPreference) {
1081 newType = checkType;
1082 break;
1083 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001084 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001085 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001086 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001087 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001088 }
1089 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001090
1091 if (newType != -1) {
1092 newNet = mNetTrackers[newType];
1093 /**
1094 * See if the other network is available to fail over to.
1095 * If is not available, we enable it anyway, so that it
1096 * will be able to connect when it does become available,
1097 * but we report a total loss of connectivity rather than
1098 * report that we are attempting to fail over.
1099 */
1100 if (newNet.isAvailable()) {
1101 NetworkInfo switchTo = newNet.getNetworkInfo();
1102 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001103 if (!switchTo.isConnectedOrConnecting() ||
1104 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001105 newNet.reconnect();
1106 }
1107 if (DBG) {
1108 if (switchTo.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001109 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001110 switchTo.getTypeName());
1111 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001112 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001113 switchTo.getTypeName());
1114 }
1115 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001116 } else {
1117 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001118 newNet = null; // not officially avail.. try anyway, but
1119 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001120 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001121 } else {
1122 Slog.e(TAG, "Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001123 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001124 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001125
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001126 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001127 }
1128
1129 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001130 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1131 }
1132
1133 private void sendInetConditionBroadcast(NetworkInfo info) {
1134 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1135 }
1136
1137 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1138 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001139 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1140 if (info.isFailover()) {
1141 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1142 info.setFailover(false);
1143 }
1144 if (info.getReason() != null) {
1145 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1146 }
1147 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001148 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1149 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001150 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001151 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001152 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001153 }
1154
1155 /**
1156 * Called when an attempt to fail over to another network has failed.
1157 * @param info the {@link NetworkInfo} for the failed network
1158 */
1159 private void handleConnectionFailure(NetworkInfo info) {
1160 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001161
Robert Greenwalt2034b912009-08-12 16:08:25 -07001162 String reason = info.getReason();
1163 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001164
Robert Greenwalte981bc52010-10-08 16:35:52 -07001165 String reasonText;
1166 if (reason == null) {
1167 reasonText = ".";
1168 } else {
1169 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001170 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001171 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001172
1173 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1174 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1175 if (getActiveNetworkInfo() == null) {
1176 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1177 }
1178 if (reason != null) {
1179 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1180 }
1181 if (extraInfo != null) {
1182 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1183 }
1184 if (info.isFailover()) {
1185 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1186 info.setFailover(false);
1187 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001188
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001189 NetworkStateTracker newNet = null;
1190 if (mNetAttributes[info.getType()].isDefault()) {
1191 newNet = tryFailover(info.getType());
1192 if (newNet != null) {
1193 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001194 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001195 // if the other net is connected they've already reset this and perhaps
1196 // even gotten a positive report we don't want to overwrite, but if not
1197 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001198 mDefaultInetConditionPublished = 0;
1199 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001200 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1201 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001202 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001203 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1204 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001205 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001206
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001207 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001208 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001209 /*
1210 * If the failover network is already connected, then immediately send
1211 * out a followup broadcast indicating successful failover
1212 */
1213 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1214 sendConnectedBroadcast(newNet.getNetworkInfo());
1215 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001216 }
1217
1218 private void sendStickyBroadcast(Intent intent) {
1219 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001220 if (!mSystemReady) {
1221 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001222 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001223 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1224 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001225 }
1226 }
1227
1228 void systemReady() {
1229 synchronized(this) {
1230 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001231 if (mInitialBroadcast != null) {
1232 mContext.sendStickyBroadcast(mInitialBroadcast);
1233 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001234 }
1235 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236 }
1237
1238 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001239 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240
1241 // snapshot isFailover, because sendConnectedBroadcast() resets it
1242 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001243 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001244
Robert Greenwalt2034b912009-08-12 16:08:25 -07001245 // if this is a default net and other default is running
1246 // kill the one not preferred
1247 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001248 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1249 if ((type != mNetworkPreference &&
1250 mNetAttributes[mActiveDefaultNetwork].mPriority >
1251 mNetAttributes[type].mPriority) ||
1252 mNetworkPreference == mActiveDefaultNetwork) {
1253 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001254 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001255 "to torn down network " + info.getTypeName());
1256 teardown(thisNet);
1257 return;
1258 } else {
1259 // tear down the other
1260 NetworkStateTracker otherNet =
1261 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001262 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001263 otherNet.getNetworkInfo().getTypeName() +
1264 " teardown");
1265 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001266 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001267 return;
1268 }
1269 if (isFailover) {
1270 otherNet.releaseWakeLock();
1271 }
1272 }
1273 }
1274 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001275 // this will cause us to come up initially as unconnected and switching
1276 // to connected after our normal pause unless somebody reports us as reall
1277 // disconnected
1278 mDefaultInetConditionPublished = 0;
1279 mDefaultConnectionSequence++;
1280 mInetConditionChangeInFlight = false;
1281 // Don't do this - if we never sign in stay, grey
1282 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001283 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001284 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001285 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001286 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001287 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001288 }
1289
1290 private void handleScanResultsAvailable(NetworkInfo info) {
1291 int networkType = info.getType();
1292 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001293 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001294 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001295 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001296
The Android Open Source Project28527d22009-03-03 19:31:44 -08001297 mNetTrackers[networkType].interpretScanResultsAvailable();
1298 }
1299
Robert Greenwalt0659da32009-07-16 17:21:39 -07001300 private void handleNotificationChange(boolean visible, int id,
1301 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001302 NotificationManager notificationManager = (NotificationManager) mContext
1303 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001304
The Android Open Source Project28527d22009-03-03 19:31:44 -08001305 if (visible) {
1306 notificationManager.notify(id, notification);
1307 } else {
1308 notificationManager.cancel(id);
1309 }
1310 }
1311
1312 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001313 * After a change in the connectivity state of any network, We're mainly
1314 * concerned with making sure that the list of DNS servers is setupup
1315 * according to which networks are connected, and ensuring that the
1316 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001317 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001318 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001319 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001320 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001321 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001322 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001323 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001324
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001325 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1326 if (mNetAttributes[netType].isDefault()) {
1327 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001328 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001329 mNetTrackers[netType].addPrivateDnsRoutes();
1330 }
1331 } else {
1332 if (mNetAttributes[netType].isDefault()) {
1333 mNetTrackers[netType].removeDefaultRoute();
1334 } else {
1335 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001336 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001337 }
1338 }
1339
Robert Greenwalt2034b912009-08-12 16:08:25 -07001340 /**
1341 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1342 * on the highest priority active net which this process requested.
1343 * If there aren't any, clear it out
1344 */
1345 private void reassessPidDns(int myPid, boolean doBump)
1346 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001347 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001348 for(int i : mPriorityList) {
1349 if (mNetAttributes[i].isDefault()) {
1350 continue;
1351 }
1352 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001353 if (nt.getNetworkInfo().isConnected() &&
1354 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001355 List pids = mNetRequestersPids[i];
1356 for (int j=0; j<pids.size(); j++) {
1357 Integer pid = (Integer)pids.get(j);
1358 if (pid.intValue() == myPid) {
1359 String[] dnsList = nt.getNameServers();
1360 writePidDns(dnsList, myPid);
1361 if (doBump) {
1362 bumpDns();
1363 }
1364 return;
1365 }
1366 }
1367 }
1368 }
1369 // nothing found - delete
1370 for (int i = 1; ; i++) {
1371 String prop = "net.dns" + i + "." + myPid;
1372 if (SystemProperties.get(prop).length() == 0) {
1373 if (doBump) {
1374 bumpDns();
1375 }
1376 return;
1377 }
1378 SystemProperties.set(prop, "");
1379 }
1380 }
1381
1382 private void writePidDns(String[] dnsList, int pid) {
1383 int j = 1;
1384 for (String dns : dnsList) {
1385 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1386 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1387 }
1388 }
1389 }
1390
1391 private void bumpDns() {
1392 /*
1393 * Bump the property that tells the name resolver library to reread
1394 * the DNS server list from the properties.
1395 */
1396 String propVal = SystemProperties.get("net.dnschange");
1397 int n = 0;
1398 if (propVal.length() != 0) {
1399 try {
1400 n = Integer.parseInt(propVal);
1401 } catch (NumberFormatException e) {}
1402 }
1403 SystemProperties.set("net.dnschange", "" + (n+1));
1404 }
1405
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001406 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001407 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001408 NetworkStateTracker nt = mNetTrackers[netType];
1409 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1410 String[] dnsList = nt.getNameServers();
1411 if (mNetAttributes[netType].isDefault()) {
1412 int j = 1;
1413 for (String dns : dnsList) {
1414 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1415 if (DBG) {
1416 Slog.d(TAG, "adding dns " + dns + " for " +
1417 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001418 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001419 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001420 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001421 }
1422 for (int k=j ; k<mNumDnsEntries; k++) {
1423 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1424 SystemProperties.set("net.dns" + k, "");
1425 }
1426 mNumDnsEntries = j;
1427 } else {
1428 // set per-pid dns for attached secondary nets
1429 List pids = mNetRequestersPids[netType];
1430 for (int y=0; y< pids.size(); y++) {
1431 Integer pid = (Integer)pids.get(y);
1432 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001433 }
1434 }
1435 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001436 bumpDns();
1437 }
1438
1439 private int getRestoreDefaultNetworkDelay() {
1440 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1441 NETWORK_RESTORE_DELAY_PROP_NAME);
1442 if(restoreDefaultNetworkDelayStr != null &&
1443 restoreDefaultNetworkDelayStr.length() != 0) {
1444 try {
1445 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1446 } catch (NumberFormatException e) {
1447 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001448 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001449 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001450 }
1451
1452 @Override
1453 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001454 if (mContext.checkCallingOrSelfPermission(
1455 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001456 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001457 pw.println("Permission Denial: can't dump ConnectivityService " +
1458 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1459 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001460 return;
1461 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001462 pw.println();
1463 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001464 if (nst != null) {
1465 if (nst.getNetworkInfo().isConnected()) {
1466 pw.println("Active network: " + nst.getNetworkInfo().
1467 getTypeName());
1468 }
1469 pw.println(nst.getNetworkInfo());
1470 pw.println(nst);
1471 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001472 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001473 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001474
1475 pw.println("Network Requester Pids:");
1476 for (int net : mPriorityList) {
1477 String pidString = net + ": ";
1478 for (Object pid : mNetRequestersPids[net]) {
1479 pidString = pidString + pid.toString() + ", ";
1480 }
1481 pw.println(pidString);
1482 }
1483 pw.println();
1484
1485 pw.println("FeatureUsers:");
1486 for (Object requester : mFeatureUsers) {
1487 pw.println(requester.toString());
1488 }
1489 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001490
1491 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001492
1493 if (mInetLog != null) {
1494 pw.println();
1495 pw.println("Inet condition reports:");
1496 for(int i = 0; i < mInetLog.size(); i++) {
1497 pw.println(mInetLog.get(i));
1498 }
1499 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001500 }
1501
Robert Greenwalt2034b912009-08-12 16:08:25 -07001502 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001503 private class MyHandler extends Handler {
1504 @Override
1505 public void handleMessage(Message msg) {
1506 NetworkInfo info;
1507 switch (msg.what) {
1508 case NetworkStateTracker.EVENT_STATE_CHANGED:
1509 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001510 int type = info.getType();
1511 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001512 // only do this optimization for wifi. It going into scan mode for location
1513 // services generates alot of noise. Meanwhile the mms apn won't send out
1514 // subsequent notifications when on default cellular because it never
1515 // disconnects.. so only do this to wifi notifications. Fixed better when the
1516 // APN notifications are standardized.
1517 if (mNetAttributes[type].mLastState == state &&
1518 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001519 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001520 // TODO - remove this after we validate the dropping doesn't break
1521 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001522 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001523 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001524 state + "/" + info.getDetailedState());
1525 }
1526 return;
1527 }
1528 mNetAttributes[type].mLastState = state;
1529
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001530 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001531 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001532 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001533
1534 // Connectivity state changed:
1535 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001536 // [12-9] Network subtype (for mobile network, as defined
1537 // by TelephonyManager)
1538 // [8-3] Detailed state ordinal (as defined by
1539 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001540 // [2-0] Network type (as defined by ConnectivityManager)
1541 int eventLogParam = (info.getType() & 0x7) |
1542 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1543 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001544 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001545 eventLogParam);
1546
1547 if (info.getDetailedState() ==
1548 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001549 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001550 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001551 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001552 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001553 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001554 // the logic here is, handle SUSPENDED the same as
1555 // DISCONNECTED. The only difference being we are
1556 // broadcasting an intent with NetworkInfo that's
1557 // suspended. This allows the applications an
1558 // opportunity to handle DISCONNECTED and SUSPENDED
1559 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001560 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001561 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001562 handleConnect(info);
1563 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001564 break;
1565
1566 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1567 info = (NetworkInfo) msg.obj;
1568 handleScanResultsAvailable(info);
1569 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001570
The Android Open Source Project28527d22009-03-03 19:31:44 -08001571 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001572 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1573 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001574 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001575
1576 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001577 info = (NetworkInfo) msg.obj;
1578 type = info.getType();
1579 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001580 break;
1581
1582 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1583 // fill me in
1584 break;
1585
1586 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1587 // fill me in
1588 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001589 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001590 FeatureUser u = (FeatureUser)msg.obj;
1591 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001592 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001593 case EVENT_INET_CONDITION_CHANGE:
1594 {
1595 int netType = msg.arg1;
1596 int condition = msg.arg2;
1597 handleInetConditionChange(netType, condition);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001598 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001599 }
1600 case EVENT_INET_CONDITION_HOLD_END:
1601 {
1602 int netType = msg.arg1;
1603 int sequence = msg.arg2;
1604 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001605 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001606 }
1607 case EVENT_SET_NETWORK_PREFERENCE:
1608 {
1609 int preference = msg.arg1;
1610 handleSetNetworkPreference(preference);
1611 break;
1612 }
1613 case EVENT_SET_BACKGROUND_DATA:
1614 {
1615 boolean enabled = (msg.arg1 == ENABLED);
1616 handleSetBackgroundData(enabled);
1617 break;
1618 }
1619 case EVENT_SET_MOBILE_DATA:
1620 {
1621 boolean enabled = (msg.arg1 == ENABLED);
1622 handleSetMobileData(enabled);
1623 break;
1624 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001625 }
1626 }
1627 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001628
1629 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001630 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001631 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001632
1633 if (isTetheringSupported()) {
1634 return mTethering.tether(iface);
1635 } else {
1636 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1637 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001638 }
1639
1640 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001641 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001642 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001643
1644 if (isTetheringSupported()) {
1645 return mTethering.untether(iface);
1646 } else {
1647 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1648 }
1649 }
1650
1651 // javadoc from interface
1652 public int getLastTetherError(String iface) {
1653 enforceTetherAccessPermission();
1654
1655 if (isTetheringSupported()) {
1656 return mTethering.getLastTetherError(iface);
1657 } else {
1658 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1659 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001660 }
1661
1662 // TODO - proper iface API for selection by property, inspection, etc
1663 public String[] getTetherableUsbRegexs() {
1664 enforceTetherAccessPermission();
1665 if (isTetheringSupported()) {
1666 return mTethering.getTetherableUsbRegexs();
1667 } else {
1668 return new String[0];
1669 }
1670 }
1671
1672 public String[] getTetherableWifiRegexs() {
1673 enforceTetherAccessPermission();
1674 if (isTetheringSupported()) {
1675 return mTethering.getTetherableWifiRegexs();
1676 } else {
1677 return new String[0];
1678 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001679 }
1680
1681 // TODO - move iface listing, queries, etc to new module
1682 // javadoc from interface
1683 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001684 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001685 return mTethering.getTetherableIfaces();
1686 }
1687
1688 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001689 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001690 return mTethering.getTetheredIfaces();
1691 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001692
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001693 public String[] getTetheringErroredIfaces() {
1694 enforceTetherAccessPermission();
1695 return mTethering.getErroredIfaces();
1696 }
1697
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001698 // if ro.tether.denied = true we default to no tethering
1699 // gservices could set the secure setting to 1 though to enable it on a build where it
1700 // had previously been turned off.
1701 public boolean isTetheringSupported() {
1702 enforceTetherAccessPermission();
1703 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001704 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1705 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1706 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001707 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001708
1709 // 100 percent is full good, 0 is full bad.
1710 public void reportInetCondition(int networkType, int percentage) {
1711 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1712 mContext.enforceCallingOrSelfPermission(
1713 android.Manifest.permission.STATUS_BAR,
1714 "ConnectivityService");
1715
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001716 if (DBG) {
1717 int pid = getCallingPid();
1718 int uid = getCallingUid();
1719 String s = pid + "(" + uid + ") reports inet is " +
1720 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1721 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1722 mInetLog.add(s);
1723 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1724 mInetLog.remove(0);
1725 }
1726 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001727 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001728 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1729 }
1730
1731 private void handleInetConditionChange(int netType, int condition) {
1732 if (DBG) {
1733 Slog.d(TAG, "Inet connectivity change, net=" +
1734 netType + ", condition=" + condition +
1735 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1736 }
1737 if (mActiveDefaultNetwork == -1) {
1738 if (DBG) Slog.d(TAG, "no active default network - aborting");
1739 return;
1740 }
1741 if (mActiveDefaultNetwork != netType) {
1742 if (DBG) Slog.d(TAG, "given net not default - aborting");
1743 return;
1744 }
1745 mDefaultInetCondition = condition;
1746 int delay;
1747 if (mInetConditionChangeInFlight == false) {
1748 if (DBG) Slog.d(TAG, "starting a change hold");
1749 // setup a new hold to debounce this
1750 if (mDefaultInetCondition > 50) {
1751 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1752 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1753 } else {
1754 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1755 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1756 }
1757 mInetConditionChangeInFlight = true;
1758 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1759 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1760 } else {
1761 // we've set the new condition, when this hold ends that will get
1762 // picked up
1763 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1764 }
1765 }
1766
1767 private void handleInetConditionHoldEnd(int netType, int sequence) {
1768 if (DBG) {
1769 Slog.d(TAG, "Inet hold end, net=" + netType +
1770 ", condition =" + mDefaultInetCondition +
1771 ", published condition =" + mDefaultInetConditionPublished);
1772 }
1773 mInetConditionChangeInFlight = false;
1774
1775 if (mActiveDefaultNetwork == -1) {
1776 if (DBG) Slog.d(TAG, "no active default network - aborting");
1777 return;
1778 }
1779 if (mDefaultConnectionSequence != sequence) {
1780 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1781 return;
1782 }
1783 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1784 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1785 return;
1786 }
1787 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1788 if (networkInfo.isConnected() == false) {
1789 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1790 return;
1791 }
1792 mDefaultInetConditionPublished = mDefaultInetCondition;
1793 sendInetConditionBroadcast(networkInfo);
1794 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001795 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001796}