blob: a0615efbd8470011d602f864f5f5c4d1ef43a215 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Naik9bc709d2010-08-10 20:13:53 +053030import android.net.NetworkUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.wifi.WifiStateTracker;
32import android.os.Binder;
33import android.os.Handler;
Robert Greenwalt42acef32009-08-12 16:08:25 -070034import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Looper;
36import android.os.Message;
Robert Greenwalt42acef32009-08-12 16:08:25 -070037import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.ServiceManager;
39import android.os.SystemProperties;
40import android.provider.Settings;
Robert Greenwalt42acef32009-08-12 16:08:25 -070041import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080043import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Robert Greenwalt42acef32009-08-12 16:08:25 -070045import com.android.internal.telephony.Phone;
46
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080047import com.android.server.connectivity.Tethering;
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.io.FileDescriptor;
50import java.io.PrintWriter;
Robert Greenwalt42acef32009-08-12 16:08:25 -070051import java.util.ArrayList;
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -070052import java.util.GregorianCalendar;
Robert Greenwalt42acef32009-08-12 16:08:25 -070053import java.util.List;
Banavathu, Srinivas Naik9bc709d2010-08-10 20:13:53 +053054import java.net.InetAddress;
55import java.net.UnknownHostException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57/**
58 * @hide
59 */
60public class ConnectivityService extends IConnectivityManager.Stub {
61
Robert Greenwaltd8df1492009-10-06 14:12:53 -070062 private static final boolean DBG = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 private static final String TAG = "ConnectivityService";
64
Robert Greenwalt42acef32009-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 Greenwaltd0e18ff2010-01-26 11:40:34 -080071
72 private Tethering mTethering;
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -080073 private boolean mTetheringConfigValid = false;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -080074
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -080093 private Context mContext;
94 private int mNetworkPreference;
Robert Greenwalt42acef32009-08-12 16:08:25 -070095 private int mActiveDefaultNetwork = -1;
Robert Greenwaltd7085fc2010-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 Project9066cfe2009-03-03 19:31:44 -0800101
102 private int mNumDnsEntries;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 private boolean mTestMode;
105 private static ConnectivityService sServiceInstance;
106
Robert Greenwalt8dcc28b2010-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 Greenwalt42acef32009-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 Lockwood0f79b542009-08-14 14:18:49 -0400171 private boolean mSystemReady;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800172 private Intent mInitialBroadcast;
Mike Lockwood0f79b542009-08-14 14:18:49 -0400173
Robert Greenwalt4e8dfef2010-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 Greenwalt511288a2009-12-07 11:33:18 -0800178 private static class NetworkAttributes {
Robert Greenwalt42acef32009-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 Greenwalt511288a2009-12-07 11:33:18 -0800186 public NetworkInfo.State mLastState;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700187 public NetworkAttributes(String init) {
188 String fragments[] = init.split(",");
189 mName = fragments[0].toLowerCase();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700190 mType = Integer.parseInt(fragments[1]);
191 mRadio = Integer.parseInt(fragments[2]);
192 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt511288a2009-12-07 11:33:18 -0800193 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700194 }
195 public boolean isDefault() {
196 return (mType == mRadio);
197 }
198 }
199 NetworkAttributes[] mNetAttributes;
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700200 int mNetworksDefined;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700201
Robert Greenwalt511288a2009-12-07 11:33:18 -0800202 private static class RadioAttributes {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700203 public int mSimultaneity;
204 public int mType;
205 public RadioAttributes(String init) {
206 String fragments[] = init.split(",");
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700207 mType = Integer.parseInt(fragments[0]);
208 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700209 }
210 }
211 RadioAttributes[] mRadioAttributes;
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 private static class ConnectivityThread extends Thread {
214 private Context mContext;
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700215
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public static ConnectivityService getServiceInstance(Context context) {
232 ConnectivityThread thread = new ConnectivityThread(context);
233 thread.start();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700234
The Android Open Source Project9066cfe2009-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 Onorato8a9b2202010-02-26 18:56:32 -0800241 Slog.e(TAG,
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700242 "Unexpected InterruptedException while waiting"+
243 " for ConnectivityService thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245 }
246 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 return sServiceInstance;
249 }
250 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public static ConnectivityService getInstance(Context context) {
253 return ConnectivityThread.getServiceInstance(context);
254 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 private ConnectivityService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800257 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltde8383c2010-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 Project9066cfe2009-03-03 19:31:44 -0800267 mContext = context;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700268 mNetTrackers = new NetworkStateTracker[
269 ConnectivityManager.MAX_NETWORK_TYPE+1];
270 mHandler = new MyHandler();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700273
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700277 // Load device network attributes from resources
Robert Greenwalt42acef32009-08-12 16:08:25 -0700278 String[] raStrings = context.getResources().getStringArray(
279 com.android.internal.R.array.radioAttributes);
Robert Greenwalt5154ae762009-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 Onorato8a9b2202010-02-26 18:56:32 -0800283 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700284 continue;
285 }
286 if (mRadioAttributes[r.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800287 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700288 r.mType);
289 continue;
290 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700291 mRadioAttributes[r.mType] = r;
292 }
293
Robert Greenwalt5154ae762009-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 Onorato8a9b2202010-02-26 18:56:32 -0800300 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700301 n.mType);
302 continue;
Robert Greenwalt42acef32009-08-12 16:08:25 -0700303 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700304 if (mNetAttributes[n.mType] != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800305 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700306 n.mType);
307 continue;
308 }
309 if (mRadioAttributes[n.mRadio] == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800310 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700318 }
319 }
320
Robert Greenwalt5154ae762009-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 Greenwalt42acef32009-08-12 16:08:25 -0700346 mNetRequestersPids[i] = new ArrayList();
347 }
348
349 mFeatureUsers = new ArrayList();
350
Robert Greenwalt5154ae762009-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 Project9066cfe2009-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 Greenwaltc03fa502010-02-23 18:58:05 -0800362 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700363 for (int netType : mPriorityList) {
364 switch (mNetAttributes[netType].mRadio) {
365 case ConnectivityManager.TYPE_WIFI:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800366 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwalt5154ae762009-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 Sheriff7b009782010-03-11 16:37:45 -0800370 wifiService.startWifi();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700371 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
372 wst.startMonitoring();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373
Robert Greenwalt5154ae762009-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 Greenwaltc03fa502010-02-23 18:58:05 -0800379 if (noMobileData) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800380 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800381 mNetTrackers[netType].teardown();
382 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700383 break;
384 default:
Joe Onorato8a9b2202010-02-26 18:56:32 -0800385 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700386 mNetAttributes[netType].mRadio);
387 continue;
388 }
389 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800390
Robert Greenwaltdfadaea2010-03-11 15:03:08 -0800391 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltc9d5fb72010-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 Greenwalt4e8dfef2010-09-20 14:35:25 -0700398 if (DBG) {
399 mInetLog = new ArrayList();
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700405 * Sets the preferred network.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * @param preference the new preference
407 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700408 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 enforceChangePermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700410
411 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413
414 public int getNetworkPreference() {
415 enforceAccessPermission();
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700416 int preference;
417 synchronized(this) {
418 preference = mNetworkPreference;
419 }
420 return preference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422
Robert Greenwalt8dcc28b2010-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 Project9066cfe2009-03-03 19:31:44 -0800436 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700437
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700451 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700458 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 return;
460
Robert Greenwalt42acef32009-08-12 16:08:25 -0700461 if (!mNetTrackers[mNetworkPreference].isAvailable())
462 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463
Robert Greenwalt42acef32009-08-12 16:08:25 -0700464 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700465 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt42acef32009-08-12 16:08:25 -0700466 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700467 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800468 Slog.d(TAG, "tearing down " +
Robert Greenwaltec9fe462009-08-20 15:25:14 -0700469 mNetTrackers[t].getNetworkInfo() +
470 " in enforcePreference");
471 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 teardown(mNetTrackers[t]);
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800492 */
493 public NetworkInfo getActiveNetworkInfo() {
494 enforceAccessPermission();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700495 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700496 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 continue;
498 }
499 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 NetworkInfo info = t.getNetworkInfo();
501 if (info.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800502 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 "connected default network is not " +
504 "mActiveDefaultNetwork!");
The Android Open Source Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700523 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 int i = 0;
525 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700526 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project9066cfe2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700535 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project9066cfe2009-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 Greenwalt9c75d4a2009-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 Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800562 long mCreateTime;
Robert Greenwalt42acef32009-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 Greenwaltb9285352009-12-21 18:24:07 -0800571 mCreateTime = System.currentTimeMillis();
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700572
Robert Greenwalt42acef32009-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 Onorato8a9b2202010-02-26 18:56:32 -0800585 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800586 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
587 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700588 stopUsingNetworkFeature(this, false);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700589 }
590
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700591 public void expire() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800592 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwaltb9285352009-12-21 18:24:07 -0800593 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
594 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700595 stopUsingNetworkFeature(this, false);
596 }
Robert Greenwaltb9285352009-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 Greenwalt42acef32009-08-12 16:08:25 -0700602 }
603
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700604 // javadoc from interface
Robert Greenwalt42acef32009-08-12 16:08:25 -0700605 public int startUsingNetworkFeature(int networkType, String feature,
606 IBinder binder) {
607 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800608 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700609 ": " + feature);
610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 enforceChangePermission();
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700612 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
613 mNetAttributes[networkType] == null) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700614 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700616
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700617 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt42acef32009-08-12 16:08:25 -0700618
619 // TODO - move this into the MobileDataStateTracker
620 int usedNetworkType = networkType;
621 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800622 if (!getMobileDataEnabled()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800623 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800624 return Phone.APN_TYPE_NOT_AVAILABLE;
625 }
Robert Greenwalt42acef32009-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 Onorato8a9b2202010-02-26 18:56:32 -0800645 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700646 return Phone.APN_TYPE_NOT_AVAILABLE;
647 }
648
Robert Greenwalt9c75d4a2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700655 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700656 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700657 f), getRestoreDefaultNetworkDelay());
658
Robert Greenwalt42acef32009-08-12 16:08:25 -0700659
Robert Greenwalta64bf832009-08-19 20:19:33 -0700660 if ((ni.isConnectedOrConnecting() == true) &&
661 !network.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700662 if (ni.isConnected() == true) {
663 // add the pid-specific dns
Robert Greenwaltb738fb92010-08-13 14:16:12 -0700664 handleDnsConfigurationChange(networkType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700666 return Phone.APN_ALREADY_ACTIVE;
667 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800668 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt42acef32009-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 Onorato8a9b2202010-02-26 18:56:32 -0800675 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700676 network.reconnect();
677 return Phone.APN_REQUEST_STARTED;
678 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700679 synchronized(this) {
680 mFeatureUsers.add(f);
681 }
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700682 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700683 f), getRestoreDefaultNetworkDelay());
684
Robert Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -0800690 }
691
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700692 // javadoc from interface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700694 enforceChangePermission();
695
Robert Greenwalt9c75d4a2009-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 Onorato8a9b2202010-02-26 18:56:32 -0800718 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700719 return 1;
720 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700721 }
722
Robert Greenwalt9c75d4a2009-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 Greenwalt42acef32009-08-12 16:08:25 -0700732 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800733 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt42acef32009-08-12 16:08:25 -0700734 ": " + feature);
735 }
Robert Greenwaltb8f16342009-10-06 17:52:40 -0700736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
738 return -1;
739 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700740
Robert Greenwalt9c75d4a2009-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 Greenwalt78a640a2010-03-10 16:10:43 -0800746 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt42acef32009-08-12 16:08:25 -0700747 return 1;
748 }
Robert Greenwalt9c75d4a2009-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 Onorato8a9b2202010-02-26 18:56:32 -0800764 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700765 return 1;
766 }
767 }
Robert Greenwalt42acef32009-08-12 16:08:25 -0700768 }
Robert Greenwalt9c75d4a2009-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 Greenwalt5154ae762009-10-30 14:17:42 -0700784 if (tracker == null) {
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800785 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700786 return -1;
787 }
788 if (usedNetworkType != networkType) {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700789 Integer currentPid = new Integer(pid);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700790 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt421c72b2009-12-17 14:54:59 -0800791 reassessPidDns(pid, true);
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700792 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800793 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700794 "others still using it");
795 return 1;
796 }
797 callTeardown = true;
798 }
799 }
Robert Greenwalt78a640a2010-03-10 16:10:43 -0800800 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700801 if (callTeardown) {
802 tracker.teardown();
Robert Greenwalt42acef32009-08-12 16:08:25 -0700803 return 1;
804 } else {
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -0700805 // do it the old fashioned way
Robert Greenwalt42acef32009-08-12 16:08:25 -0700806 return tracker.stopUsingNetworkFeature(feature, pid, uid);
807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 /**
Banavathu, Srinivas Naik9bc709d2010-08-10 20:13:53 +0530811 * @deprecated use requestRouteToHostAddress instead
812 *
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-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 Project9066cfe2009-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 Naik9bc709d2010-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 Project9066cfe2009-03-03 19:31:44 -0800841 enforceChangePermission();
842 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
843 return false;
844 }
845 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700846
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700847 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
848 tracker.isTeardownRequested()) {
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700849 if (DBG) {
Banavathu, Srinivas Naik9bc709d2010-08-10 20:13:53 +0530850 Slog.d(TAG, "requestRouteToHostAddress on down network " +
851 "(" + networkType + ") - dropped");
Robert Greenwalt8206ff32009-09-10 15:06:20 -0700852 }
853 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
Banavathu, Srinivas Naik9bc709d2010-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 Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700871
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -0700879
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700880 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
881 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883
Robert Greenwalt8dcc28b2010-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 Greenwalt86e9e552009-07-16 17:21:39 -0700892 }
893
Robert Greenwaltc03fa502010-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 Onorato8a9b2202010-02-26 18:56:32 -0800901 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800902 return retVal;
903 }
904
905 /**
906 * @see ConnectivityManager#setMobileDataEnabled(boolean)
907 */
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -0700908 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800909 enforceChangePermission();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800910 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800911
Robert Greenwalt8dcc28b2010-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 Greenwaltc03fa502010-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 Greenwalt8dcc28b2010-09-23 10:05:56 -0700924 if (DBG) {
925 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
926 }
Robert Greenwaltc03fa502010-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 Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800935 nt.teardown();
936 }
937 }
938 }
939 }
940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 private int getNumConnectedNetworks() {
942 int numConnectedNets = 0;
943
944 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -0700945 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700946 !nt.isTeardownRequested()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 ++numConnectedNets;
948 }
949 }
950 return numConnectedNets;
951 }
952
953 private void enforceAccessPermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700954 mContext.enforceCallingOrSelfPermission(
955 android.Manifest.permission.ACCESS_NETWORK_STATE,
956 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958
959 private void enforceChangePermission() {
Robert Greenwalt86e9e552009-07-16 17:21:39 -0700960 mContext.enforceCallingOrSelfPermission(
961 android.Manifest.permission.CHANGE_NETWORK_STATE,
962 "ConnectivityService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964
Robert Greenwaltd0e18ff2010-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 Greenwalt2a091d72010-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 Project9066cfe2009-03-03 19:31:44 -0800978 /**
Robert Greenwalt86e9e552009-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 Project9066cfe2009-03-03 19:31:44 -0800983 * @param info the {@code NetworkInfo} for the network
984 */
985 private void handleDisconnect(NetworkInfo info) {
986
Robert Greenwalt42acef32009-08-12 16:08:25 -0700987 int prevNetType = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
Robert Greenwalt42acef32009-08-12 16:08:25 -0700989 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project9066cfe2009-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 Greenwalt42acef32009-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 Project9066cfe2009-03-03 19:31:44 -08001004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006
The Android Open Source Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -07001017 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1018 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001020
Robert Greenwaltcc4b4012010-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 Greenwalt029be812010-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 Greenwaltcc4b4012010-01-25 17:54:29 -08001032 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1033 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001034 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001035 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1036 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001037 }
Robert Greenwalt029be812010-09-20 18:01:43 -07001038 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001039 // do this before we broadcast the change
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001040 handleConnectivityChange(prevNetType);
Robert Greenwaltda03c4e2010-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 Greenwaltcc4b4012010-01-25 17:54:29 -08001052 // returns null if no failover available
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001053 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt42acef32009-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 Greenwalt42acef32009-08-12 16:08:25 -07001060 if (mActiveDefaultNetwork == prevNetType) {
1061 mActiveDefaultNetwork = -1;
1062 }
1063
1064 int newType = -1;
1065 int newPriority = -1;
Robert Greenwalt35429592010-02-25 12:04:29 -08001066 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001067 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001068 if (checkType == prevNetType) continue;
1069 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt35429592010-02-25 12:04:29 -08001070 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1071 noMobileData) {
1072 if (DBG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001073 Slog.d(TAG, "not failing over to mobile type " + checkType +
Robert Greenwalt35429592010-02-25 12:04:29 -08001074 " because Mobile Data Disabled");
1075 }
1076 continue;
1077 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001078 if (mNetAttributes[checkType].isDefault()) {
1079 /* TODO - if we have multiple nets we could use
1080 * we may want to put more thought into which we choose
1081 */
1082 if (checkType == mNetworkPreference) {
1083 newType = checkType;
1084 break;
1085 }
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001086 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001087 newType = checkType;
Robert Greenwalt5154ae762009-10-30 14:17:42 -07001088 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt42acef32009-08-12 16:08:25 -07001089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001092
1093 if (newType != -1) {
1094 newNet = mNetTrackers[newType];
1095 /**
1096 * See if the other network is available to fail over to.
1097 * If is not available, we enable it anyway, so that it
1098 * will be able to connect when it does become available,
1099 * but we report a total loss of connectivity rather than
1100 * report that we are attempting to fail over.
1101 */
1102 if (newNet.isAvailable()) {
1103 NetworkInfo switchTo = newNet.getNetworkInfo();
1104 switchTo.setFailover(true);
Robert Greenwalta64bf832009-08-19 20:19:33 -07001105 if (!switchTo.isConnectedOrConnecting() ||
1106 newNet.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001107 newNet.reconnect();
1108 }
1109 if (DBG) {
1110 if (switchTo.isConnected()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001112 switchTo.getTypeName());
1113 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001114 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001115 switchTo.getTypeName());
1116 }
1117 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001118 } else {
1119 newNet.reconnect();
Robert Greenwaltf0fa39e2010-03-09 14:55:08 -08001120 newNet = null; // not officially avail.. try anyway, but
1121 // report no failover
Robert Greenwalt42acef32009-08-12 16:08:25 -07001122 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001125
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001126 return newNet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
1129 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwalt1e9aac22010-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 Project9066cfe2009-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 Greenwalt86e9e552009-07-16 17:21:39 -07001148 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1149 info.getExtraInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001151 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001152 sendStickyBroadcast(intent);
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -08001161
Robert Greenwalt42acef32009-08-12 16:08:25 -07001162 String reason = info.getReason();
1163 String extraInfo = info.getExtraInfo();
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001164
Robert Greenwalt42acef32009-08-12 16:08:25 -07001165 if (DBG) {
1166 String reasonText;
1167 if (reason == null) {
1168 reasonText = ".";
1169 } else {
1170 reasonText = " (" + reason + ").";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 Slog.v(TAG, "Attempt to connect to " + info.getTypeName() +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001173 " failed" + reasonText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001175
1176 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1177 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1178 if (getActiveNetworkInfo() == null) {
1179 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1180 }
1181 if (reason != null) {
1182 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1183 }
1184 if (extraInfo != null) {
1185 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1186 }
1187 if (info.isFailover()) {
1188 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1189 info.setFailover(false);
1190 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001191
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001192 NetworkStateTracker newNet = null;
1193 if (mNetAttributes[info.getType()].isDefault()) {
1194 newNet = tryFailover(info.getType());
1195 if (newNet != null) {
1196 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwalt029be812010-09-20 18:01:43 -07001197 if (!switchTo.isConnected()) {
1198 // if the other net is connected they've already reset this and perhaps even gotten
1199 // a positive report we don't want to overwrite, but if not we need to clear this now
1200 // to turn our cellular sig strength white
1201 mDefaultInetConditionPublished = 0;
1202 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001203 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1204 } else {
Robert Greenwalt029be812010-09-20 18:01:43 -07001205 mDefaultInetConditionPublished = 0;
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001206 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1207 }
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001208 }
Robert Greenwaltcc4b4012010-01-25 17:54:29 -08001209
Robert Greenwalt029be812010-09-20 18:01:43 -07001210 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001211 sendStickyBroadcast(intent);
Robert Greenwaltda03c4e2010-01-20 19:29:41 -08001212 /*
1213 * If the failover network is already connected, then immediately send
1214 * out a followup broadcast indicating successful failover
1215 */
1216 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1217 sendConnectedBroadcast(newNet.getNetworkInfo());
1218 }
Mike Lockwood0f79b542009-08-14 14:18:49 -04001219 }
1220
1221 private void sendStickyBroadcast(Intent intent) {
1222 synchronized(this) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001223 if (!mSystemReady) {
1224 mInitialBroadcast = new Intent(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001225 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001226 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1227 mContext.sendStickyBroadcast(intent);
Mike Lockwood0f79b542009-08-14 14:18:49 -04001228 }
1229 }
1230
1231 void systemReady() {
1232 synchronized(this) {
1233 mSystemReady = true;
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001234 if (mInitialBroadcast != null) {
1235 mContext.sendStickyBroadcast(mInitialBroadcast);
1236 mInitialBroadcast = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -04001237 }
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240
1241 private void handleConnect(NetworkInfo info) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001242 int type = info.getType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243
1244 // snapshot isFailover, because sendConnectedBroadcast() resets it
1245 boolean isFailover = info.isFailover();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001246 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247
Robert Greenwalt42acef32009-08-12 16:08:25 -07001248 // if this is a default net and other default is running
1249 // kill the one not preferred
1250 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001251 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1252 if ((type != mNetworkPreference &&
1253 mNetAttributes[mActiveDefaultNetwork].mPriority >
1254 mNetAttributes[type].mPriority) ||
1255 mNetworkPreference == mActiveDefaultNetwork) {
1256 // don't accept this one
Joe Onorato8a9b2202010-02-26 18:56:32 -08001257 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001258 "to torn down network " + info.getTypeName());
1259 teardown(thisNet);
1260 return;
1261 } else {
1262 // tear down the other
1263 NetworkStateTracker otherNet =
1264 mNetTrackers[mActiveDefaultNetwork];
Joe Onorato8a9b2202010-02-26 18:56:32 -08001265 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt42acef32009-08-12 16:08:25 -07001266 otherNet.getNetworkInfo().getTypeName() +
1267 " teardown");
1268 if (!teardown(otherNet)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt42acef32009-08-12 16:08:25 -07001270 return;
1271 }
1272 if (isFailover) {
1273 otherNet.releaseWakeLock();
1274 }
1275 }
1276 }
1277 mActiveDefaultNetwork = type;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001278 // this will cause us to come up initially as unconnected and switching
1279 // to connected after our normal pause unless somebody reports us as reall
1280 // disconnected
1281 mDefaultInetConditionPublished = 0;
1282 mDefaultConnectionSequence++;
1283 mInetConditionChangeInFlight = false;
1284 // Don't do this - if we never sign in stay, grey
1285 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 thisNet.setTeardownRequested(false);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001288 thisNet.updateNetworkSettings();
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001289 handleConnectivityChange(type);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001290 sendConnectedBroadcast(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292
1293 private void handleScanResultsAvailable(NetworkInfo info) {
1294 int networkType = info.getType();
1295 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001297 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 mNetTrackers[networkType].interpretScanResultsAvailable();
1301 }
1302
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001303 private void handleNotificationChange(boolean visible, int id,
1304 Notification notification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 NotificationManager notificationManager = (NotificationManager) mContext
1306 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 if (visible) {
1309 notificationManager.notify(id, notification);
1310 } else {
1311 notificationManager.cancel(id);
1312 }
1313 }
1314
1315 /**
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001316 * After a change in the connectivity state of any network, We're mainly
1317 * concerned with making sure that the list of DNS servers is setupup
1318 * according to which networks are connected, and ensuring that the
1319 * right routing table entries exist.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 */
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001321 private void handleConnectivityChange(int netType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /*
Robert Greenwalt42acef32009-08-12 16:08:25 -07001323 * If a non-default network is enabled, add the host routes that
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001324 * will allow it's DNS servers to be accessed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 */
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001326 handleDnsConfigurationChange(netType);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001327
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001328 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1329 if (mNetAttributes[netType].isDefault()) {
1330 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001331 } else {
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001332 mNetTrackers[netType].addPrivateDnsRoutes();
1333 }
1334 } else {
1335 if (mNetAttributes[netType].isDefault()) {
1336 mNetTrackers[netType].removeDefaultRoute();
1337 } else {
1338 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342
Robert Greenwalt42acef32009-08-12 16:08:25 -07001343 /**
1344 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1345 * on the highest priority active net which this process requested.
1346 * If there aren't any, clear it out
1347 */
1348 private void reassessPidDns(int myPid, boolean doBump)
1349 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001350 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001351 for(int i : mPriorityList) {
1352 if (mNetAttributes[i].isDefault()) {
1353 continue;
1354 }
1355 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001356 if (nt.getNetworkInfo().isConnected() &&
1357 !nt.isTeardownRequested()) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001358 List pids = mNetRequestersPids[i];
1359 for (int j=0; j<pids.size(); j++) {
1360 Integer pid = (Integer)pids.get(j);
1361 if (pid.intValue() == myPid) {
1362 String[] dnsList = nt.getNameServers();
1363 writePidDns(dnsList, myPid);
1364 if (doBump) {
1365 bumpDns();
1366 }
1367 return;
1368 }
1369 }
1370 }
1371 }
1372 // nothing found - delete
1373 for (int i = 1; ; i++) {
1374 String prop = "net.dns" + i + "." + myPid;
1375 if (SystemProperties.get(prop).length() == 0) {
1376 if (doBump) {
1377 bumpDns();
1378 }
1379 return;
1380 }
1381 SystemProperties.set(prop, "");
1382 }
1383 }
1384
1385 private void writePidDns(String[] dnsList, int pid) {
1386 int j = 1;
1387 for (String dns : dnsList) {
1388 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1389 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1390 }
1391 }
1392 }
1393
1394 private void bumpDns() {
1395 /*
1396 * Bump the property that tells the name resolver library to reread
1397 * the DNS server list from the properties.
1398 */
1399 String propVal = SystemProperties.get("net.dnschange");
1400 int n = 0;
1401 if (propVal.length() != 0) {
1402 try {
1403 n = Integer.parseInt(propVal);
1404 } catch (NumberFormatException e) {}
1405 }
1406 SystemProperties.set("net.dnschange", "" + (n+1));
1407 }
1408
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001409 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -07001410 // add default net's dns entries
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001411 NetworkStateTracker nt = mNetTrackers[netType];
1412 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1413 String[] dnsList = nt.getNameServers();
1414 if (mNetAttributes[netType].isDefault()) {
1415 int j = 1;
1416 for (String dns : dnsList) {
1417 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1418 if (DBG) {
1419 Slog.d(TAG, "adding dns " + dns + " for " +
1420 nt.getNetworkInfo().getTypeName());
Robert Greenwalt42acef32009-08-12 16:08:25 -07001421 }
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001422 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt42acef32009-08-12 16:08:25 -07001423 }
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001424 }
1425 for (int k=j ; k<mNumDnsEntries; k++) {
1426 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1427 SystemProperties.set("net.dns" + k, "");
1428 }
1429 mNumDnsEntries = j;
1430 } else {
1431 // set per-pid dns for attached secondary nets
1432 List pids = mNetRequestersPids[netType];
1433 for (int y=0; y< pids.size(); y++) {
1434 Integer pid = (Integer)pids.get(y);
1435 writePidDns(dnsList, pid.intValue());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437 }
1438 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001439 bumpDns();
1440 }
1441
1442 private int getRestoreDefaultNetworkDelay() {
1443 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1444 NETWORK_RESTORE_DELAY_PROP_NAME);
1445 if(restoreDefaultNetworkDelayStr != null &&
1446 restoreDefaultNetworkDelayStr.length() != 0) {
1447 try {
1448 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1449 } catch (NumberFormatException e) {
1450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
Robert Greenwalt42acef32009-08-12 16:08:25 -07001452 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454
1455 @Override
1456 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001457 if (mContext.checkCallingOrSelfPermission(
1458 android.Manifest.permission.DUMP)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001460 pw.println("Permission Denial: can't dump ConnectivityService " +
1461 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1462 Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 return;
1464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 pw.println();
1466 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwaltb9285352009-12-21 18:24:07 -08001467 if (nst != null) {
1468 if (nst.getNetworkInfo().isConnected()) {
1469 pw.println("Active network: " + nst.getNetworkInfo().
1470 getTypeName());
1471 }
1472 pw.println(nst.getNetworkInfo());
1473 pw.println(nst);
1474 pw.println();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
Robert Greenwaltb9285352009-12-21 18:24:07 -08001477
1478 pw.println("Network Requester Pids:");
1479 for (int net : mPriorityList) {
1480 String pidString = net + ": ";
1481 for (Object pid : mNetRequestersPids[net]) {
1482 pidString = pidString + pid.toString() + ", ";
1483 }
1484 pw.println(pidString);
1485 }
1486 pw.println();
1487
1488 pw.println("FeatureUsers:");
1489 for (Object requester : mFeatureUsers) {
1490 pw.println(requester.toString());
1491 }
1492 pw.println();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001493
1494 mTethering.dump(fd, pw, args);
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001495
1496 if (mInetLog != null) {
1497 pw.println();
1498 pw.println("Inet condition reports:");
1499 for(int i = 0; i < mInetLog.size(); i++) {
1500 pw.println(mInetLog.get(i));
1501 }
1502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
1504
Robert Greenwalt42acef32009-08-12 16:08:25 -07001505 // must be stateless - things change under us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 private class MyHandler extends Handler {
1507 @Override
1508 public void handleMessage(Message msg) {
1509 NetworkInfo info;
1510 switch (msg.what) {
1511 case NetworkStateTracker.EVENT_STATE_CHANGED:
1512 info = (NetworkInfo) msg.obj;
Robert Greenwalt511288a2009-12-07 11:33:18 -08001513 int type = info.getType();
1514 NetworkInfo.State state = info.getState();
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001515 // only do this optimization for wifi. It going into scan mode for location
1516 // services generates alot of noise. Meanwhile the mms apn won't send out
1517 // subsequent notifications when on default cellular because it never
1518 // disconnects.. so only do this to wifi notifications. Fixed better when the
1519 // APN notifications are standardized.
1520 if (mNetAttributes[type].mLastState == state &&
1521 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt511288a2009-12-07 11:33:18 -08001522 if (DBG) {
Robert Greenwalt6e6dec22010-01-25 16:14:00 -08001523 // TODO - remove this after we validate the dropping doesn't break
1524 // anything
Joe Onorato8a9b2202010-02-26 18:56:32 -08001525 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt1193ae42010-01-13 09:36:31 -08001526 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001527 state + "/" + info.getDetailedState());
1528 }
1529 return;
1530 }
1531 mNetAttributes[type].mLastState = state;
1532
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001534 info.getTypeName() + ": " +
Robert Greenwalt511288a2009-12-07 11:33:18 -08001535 state + "/" + info.getDetailedState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
1537 // Connectivity state changed:
1538 // [31-13] Reserved for future use
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001539 // [12-9] Network subtype (for mobile network, as defined
1540 // by TelephonyManager)
1541 // [8-3] Detailed state ordinal (as defined by
1542 // NetworkInfo.DetailedState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 // [2-0] Network type (as defined by ConnectivityManager)
1544 int eventLogParam = (info.getType() & 0x7) |
1545 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1546 (info.getSubtype() << 9);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001548 eventLogParam);
1549
1550 if (info.getDetailedState() ==
1551 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 handleConnectionFailure(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001553 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001555 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 // TODO: need to think this over.
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001557 // the logic here is, handle SUSPENDED the same as
1558 // DISCONNECTED. The only difference being we are
1559 // broadcasting an intent with NetworkInfo that's
1560 // suspended. This allows the applications an
1561 // opportunity to handle DISCONNECTED and SUSPENDED
1562 // differently, or not.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 handleDisconnect(info);
Robert Greenwalt511288a2009-12-07 11:33:18 -08001564 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 handleConnect(info);
1566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 break;
1568
1569 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1570 info = (NetworkInfo) msg.obj;
1571 handleScanResultsAvailable(info);
1572 break;
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt86e9e552009-07-16 17:21:39 -07001575 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1576 (Notification) msg.obj);
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001577 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
1579 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwaltb738fb92010-08-13 14:16:12 -07001580 info = (NetworkInfo) msg.obj;
1581 type = info.getType();
1582 handleDnsConfigurationChange(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 break;
1584
1585 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1586 // fill me in
1587 break;
1588
1589 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1590 // fill me in
1591 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001592 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwalt9c75d4a2009-09-27 17:27:04 -07001593 FeatureUser u = (FeatureUser)msg.obj;
1594 u.expire();
Robert Greenwalt42acef32009-08-12 16:08:25 -07001595 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001596 case EVENT_INET_CONDITION_CHANGE:
1597 {
1598 int netType = msg.arg1;
1599 int condition = msg.arg2;
1600 handleInetConditionChange(netType, condition);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001601 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001602 }
1603 case EVENT_INET_CONDITION_HOLD_END:
1604 {
1605 int netType = msg.arg1;
1606 int sequence = msg.arg2;
1607 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001608 break;
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001609 }
1610 case EVENT_SET_NETWORK_PREFERENCE:
1611 {
1612 int preference = msg.arg1;
1613 handleSetNetworkPreference(preference);
1614 break;
1615 }
1616 case EVENT_SET_BACKGROUND_DATA:
1617 {
1618 boolean enabled = (msg.arg1 == ENABLED);
1619 handleSetBackgroundData(enabled);
1620 break;
1621 }
1622 case EVENT_SET_MOBILE_DATA:
1623 {
1624 boolean enabled = (msg.arg1 == ENABLED);
1625 handleSetMobileData(enabled);
1626 break;
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
1629 }
1630 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001631
1632 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001633 public int tether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001634 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001635
1636 if (isTetheringSupported()) {
1637 return mTethering.tether(iface);
1638 } else {
1639 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1640 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001641 }
1642
1643 // javadoc from interface
Robert Greenwalt5a735062010-03-02 17:25:02 -08001644 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001645 enforceTetherChangePermission();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001646
1647 if (isTetheringSupported()) {
1648 return mTethering.untether(iface);
1649 } else {
1650 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1651 }
1652 }
1653
1654 // javadoc from interface
1655 public int getLastTetherError(String iface) {
1656 enforceTetherAccessPermission();
1657
1658 if (isTetheringSupported()) {
1659 return mTethering.getLastTetherError(iface);
1660 } else {
1661 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1662 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001663 }
1664
1665 // TODO - proper iface API for selection by property, inspection, etc
1666 public String[] getTetherableUsbRegexs() {
1667 enforceTetherAccessPermission();
1668 if (isTetheringSupported()) {
1669 return mTethering.getTetherableUsbRegexs();
1670 } else {
1671 return new String[0];
1672 }
1673 }
1674
1675 public String[] getTetherableWifiRegexs() {
1676 enforceTetherAccessPermission();
1677 if (isTetheringSupported()) {
1678 return mTethering.getTetherableWifiRegexs();
1679 } else {
1680 return new String[0];
1681 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001682 }
1683
1684 // TODO - move iface listing, queries, etc to new module
1685 // javadoc from interface
1686 public String[] getTetherableIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001687 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001688 return mTethering.getTetherableIfaces();
1689 }
1690
1691 public String[] getTetheredIfaces() {
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001692 enforceTetherAccessPermission();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001693 return mTethering.getTetheredIfaces();
1694 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001695
Robert Greenwalt5a735062010-03-02 17:25:02 -08001696 public String[] getTetheringErroredIfaces() {
1697 enforceTetherAccessPermission();
1698 return mTethering.getErroredIfaces();
1699 }
1700
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001701 // if ro.tether.denied = true we default to no tethering
1702 // gservices could set the secure setting to 1 though to enable it on a build where it
1703 // had previously been turned off.
1704 public boolean isTetheringSupported() {
1705 enforceTetherAccessPermission();
1706 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltc9d5fb72010-02-25 12:29:30 -08001707 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1708 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1709 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001710 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001711
1712 // 100 percent is full good, 0 is full bad.
1713 public void reportInetCondition(int networkType, int percentage) {
1714 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1715 mContext.enforceCallingOrSelfPermission(
1716 android.Manifest.permission.STATUS_BAR,
1717 "ConnectivityService");
1718
Robert Greenwalt4e8dfef2010-09-20 14:35:25 -07001719 if (DBG) {
1720 int pid = getCallingPid();
1721 int uid = getCallingUid();
1722 String s = pid + "(" + uid + ") reports inet is " +
1723 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1724 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1725 mInetLog.add(s);
1726 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1727 mInetLog.remove(0);
1728 }
1729 }
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001730 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt8dcc28b2010-09-23 10:05:56 -07001731 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1732 }
1733
1734 private void handleInetConditionChange(int netType, int condition) {
1735 if (DBG) {
1736 Slog.d(TAG, "Inet connectivity change, net=" +
1737 netType + ", condition=" + condition +
1738 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1739 }
1740 if (mActiveDefaultNetwork == -1) {
1741 if (DBG) Slog.d(TAG, "no active default network - aborting");
1742 return;
1743 }
1744 if (mActiveDefaultNetwork != netType) {
1745 if (DBG) Slog.d(TAG, "given net not default - aborting");
1746 return;
1747 }
1748 mDefaultInetCondition = condition;
1749 int delay;
1750 if (mInetConditionChangeInFlight == false) {
1751 if (DBG) Slog.d(TAG, "starting a change hold");
1752 // setup a new hold to debounce this
1753 if (mDefaultInetCondition > 50) {
1754 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1755 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1756 } else {
1757 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1758 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1759 }
1760 mInetConditionChangeInFlight = true;
1761 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1762 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1763 } else {
1764 // we've set the new condition, when this hold ends that will get
1765 // picked up
1766 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1767 }
1768 }
1769
1770 private void handleInetConditionHoldEnd(int netType, int sequence) {
1771 if (DBG) {
1772 Slog.d(TAG, "Inet hold end, net=" + netType +
1773 ", condition =" + mDefaultInetCondition +
1774 ", published condition =" + mDefaultInetConditionPublished);
1775 }
1776 mInetConditionChangeInFlight = false;
1777
1778 if (mActiveDefaultNetwork == -1) {
1779 if (DBG) Slog.d(TAG, "no active default network - aborting");
1780 return;
1781 }
1782 if (mDefaultConnectionSequence != sequence) {
1783 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1784 return;
1785 }
1786 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1787 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1788 return;
1789 }
1790 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1791 if (networkInfo.isConnected() == false) {
1792 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1793 return;
1794 }
1795 mDefaultInetConditionPublished = mDefaultInetCondition;
1796 sendInetConditionBroadcast(networkInfo);
1797 return;
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799}