blob: 4e5aa334fa274c369126e0a7770690e5633eacb8 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.net.ConnectivityManager;
26import android.net.IConnectivityManager;
27import android.net.MobileDataStateTracker;
28import android.net.NetworkInfo;
29import android.net.NetworkStateTracker;
30import android.net.wifi.WifiStateTracker;
31import android.os.Binder;
32import android.os.Handler;
Robert Greenwalt2034b912009-08-12 16:08:25 -070033import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt2034b912009-08-12 16:08:25 -070036import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080037import android.os.ServiceManager;
38import android.os.SystemProperties;
39import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070040import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080041import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080042import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080043
Robert Greenwalt2034b912009-08-12 16:08:25 -070044import com.android.internal.telephony.Phone;
45
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080046import com.android.server.connectivity.Tethering;
47
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Robert Greenwalt2034b912009-08-12 16:08:25 -070050import java.util.ArrayList;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070051import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070052import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080053
54/**
55 * @hide
56 */
57public class ConnectivityService extends IConnectivityManager.Stub {
58
Dianne Hackborn8919f5a2010-09-30 18:27:22 -070059 private static final boolean DBG = false;
The Android Open Source Project28527d22009-03-03 19:31:44 -080060 private static final String TAG = "ConnectivityService";
61
Robert Greenwalt2034b912009-08-12 16:08:25 -070062 // how long to wait before switching back to a radio's default network
63 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
64 // system property that can override the above value
65 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
66 "android.telephony.apn-restore";
67
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080068
69 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080070 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080071
The Android Open Source Project28527d22009-03-03 19:31:44 -080072 /**
73 * Sometimes we want to refer to the individual network state
74 * trackers separately, and sometimes we just want to treat them
75 * abstractly.
76 */
77 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070078
79 /**
80 * A per Net list of the PID's that requested access to the net
81 * used both as a refcount and for per-PID DNS selection
82 */
83 private List mNetRequestersPids[];
84
Robert Greenwalt2034b912009-08-12 16:08:25 -070085 // priority order of the nettrackers
86 // (excluding dynamically set mNetworkPreference)
87 // TODO - move mNetworkTypePreference into this
88 private int[] mPriorityList;
89
The Android Open Source Project28527d22009-03-03 19:31:44 -080090 private Context mContext;
91 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -070092 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -070093 // 0 is full bad, 100 is full good
94 private int mDefaultInetCondition = 0;
95 private int mDefaultInetConditionPublished = 0;
96 private boolean mInetConditionChangeInFlight = false;
97 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -080098
99 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800100
101 private boolean mTestMode;
102 private static ConnectivityService sServiceInstance;
103
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700104 private static final int ENABLED = 1;
105 private static final int DISABLED = 0;
106
107 // Share the event space with NetworkStateTracker (which can't see this
108 // internal class but sends us events). If you change these, change
109 // NetworkStateTracker.java too.
110 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
111 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
112
113 /**
114 * used internally as a delayed event to make us switch back to the
115 * default network
116 */
117 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
118 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
119
120 /**
121 * used internally to change our mobile data enabled flag
122 */
123 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
124 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
125
126 /**
127 * used internally to change our network preference setting
128 * arg1 = networkType to prefer
129 */
130 private static final int EVENT_SET_NETWORK_PREFERENCE =
131 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
132
133 /**
134 * used internally to synchronize inet condition reports
135 * arg1 = networkType
136 * arg2 = condition (0 bad, 100 good)
137 */
138 private static final int EVENT_INET_CONDITION_CHANGE =
139 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
140
141 /**
142 * used internally to mark the end of inet condition hold periods
143 * arg1 = networkType
144 */
145 private static final int EVENT_INET_CONDITION_HOLD_END =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
147
148 /**
149 * used internally to set the background data preference
150 * arg1 = TRUE for enabled, FALSE for disabled
151 */
152 private static final int EVENT_SET_BACKGROUND_DATA =
153 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
154
155 /**
156 * used internally to set enable/disable cellular data
157 * arg1 = ENBALED or DISABLED
158 */
159 private static final int EVENT_SET_MOBILE_DATA =
160 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
161
Robert Greenwalt2034b912009-08-12 16:08:25 -0700162 private Handler mHandler;
163
164 // list of DeathRecipients used to make sure features are turned off when
165 // a process dies
166 private List mFeatureUsers;
167
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400168 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800169 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400170
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700171 // used in DBG mode to track inet condition reports
172 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
173 private ArrayList mInetLog;
174
Robert Greenwalt12c44552009-12-07 11:33:18 -0800175 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700176 /**
177 * Class for holding settings read from resources.
178 */
179 public String mName;
180 public int mType;
181 public int mRadio;
182 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800183 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700184 public NetworkAttributes(String init) {
185 String fragments[] = init.split(",");
186 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700187 mType = Integer.parseInt(fragments[1]);
188 mRadio = Integer.parseInt(fragments[2]);
189 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800190 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700191 }
192 public boolean isDefault() {
193 return (mType == mRadio);
194 }
195 }
196 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700197 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700198
Robert Greenwalt12c44552009-12-07 11:33:18 -0800199 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700200 public int mSimultaneity;
201 public int mType;
202 public RadioAttributes(String init) {
203 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700204 mType = Integer.parseInt(fragments[0]);
205 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700206 }
207 }
208 RadioAttributes[] mRadioAttributes;
209
The Android Open Source Project28527d22009-03-03 19:31:44 -0800210 private static class ConnectivityThread extends Thread {
211 private Context mContext;
Robert Greenwalt0659da32009-07-16 17:21:39 -0700212
The Android Open Source Project28527d22009-03-03 19:31:44 -0800213 private ConnectivityThread(Context context) {
214 super("ConnectivityThread");
215 mContext = context;
216 }
217
218 @Override
219 public void run() {
220 Looper.prepare();
221 synchronized (this) {
222 sServiceInstance = new ConnectivityService(mContext);
223 notifyAll();
224 }
225 Looper.loop();
226 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700227
The Android Open Source Project28527d22009-03-03 19:31:44 -0800228 public static ConnectivityService getServiceInstance(Context context) {
229 ConnectivityThread thread = new ConnectivityThread(context);
230 thread.start();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700231
The Android Open Source Project28527d22009-03-03 19:31:44 -0800232 synchronized (thread) {
233 while (sServiceInstance == null) {
234 try {
235 // Wait until sServiceInstance has been initialized.
236 thread.wait();
237 } catch (InterruptedException ignore) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800238 Slog.e(TAG,
Robert Greenwalt0659da32009-07-16 17:21:39 -0700239 "Unexpected InterruptedException while waiting"+
240 " for ConnectivityService thread");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800241 }
242 }
243 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700244
The Android Open Source Project28527d22009-03-03 19:31:44 -0800245 return sServiceInstance;
246 }
247 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700248
The Android Open Source Project28527d22009-03-03 19:31:44 -0800249 public static ConnectivityService getInstance(Context context) {
250 return ConnectivityThread.getServiceInstance(context);
251 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700252
The Android Open Source Project28527d22009-03-03 19:31:44 -0800253 private ConnectivityService(Context context) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800254 if (DBG) Slog.v(TAG, "ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800255
256 // setup our unique device name
257 String id = Settings.Secure.getString(context.getContentResolver(),
258 Settings.Secure.ANDROID_ID);
259 if (id != null && id.length() > 0) {
260 String name = new String("android_").concat(id);
261 SystemProperties.set("net.hostname", name);
262 }
263
The Android Open Source Project28527d22009-03-03 19:31:44 -0800264 mContext = context;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700265 mNetTrackers = new NetworkStateTracker[
266 ConnectivityManager.MAX_NETWORK_TYPE+1];
267 mHandler = new MyHandler();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700268
The Android Open Source Project28527d22009-03-03 19:31:44 -0800269 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700270
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700271 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
272 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
273
Robert Greenwalt2034b912009-08-12 16:08:25 -0700274 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700275 String[] raStrings = context.getResources().getStringArray(
276 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700277 for (String raString : raStrings) {
278 RadioAttributes r = new RadioAttributes(raString);
279 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800280 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700281 continue;
282 }
283 if (mRadioAttributes[r.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800284 Slog.e(TAG, "Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700285 r.mType);
286 continue;
287 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700288 mRadioAttributes[r.mType] = r;
289 }
290
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700291 String[] naStrings = context.getResources().getStringArray(
292 com.android.internal.R.array.networkAttributes);
293 for (String naString : naStrings) {
294 try {
295 NetworkAttributes n = new NetworkAttributes(naString);
296 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800297 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700298 n.mType);
299 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700301 if (mNetAttributes[n.mType] != null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800302 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700303 n.mType);
304 continue;
305 }
306 if (mRadioAttributes[n.mRadio] == null) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800307 Slog.e(TAG, "Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700308 "radio " + n.mRadio + " in network type " + n.mType);
309 continue;
310 }
311 mNetAttributes[n.mType] = n;
312 mNetworksDefined++;
313 } catch(Exception e) {
314 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700315 }
316 }
317
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700318 // high priority first
319 mPriorityList = new int[mNetworksDefined];
320 {
321 int insertionPoint = mNetworksDefined-1;
322 int currentLowest = 0;
323 int nextLowest = 0;
324 while (insertionPoint > -1) {
325 for (NetworkAttributes na : mNetAttributes) {
326 if (na == null) continue;
327 if (na.mPriority < currentLowest) continue;
328 if (na.mPriority > currentLowest) {
329 if (na.mPriority < nextLowest || nextLowest == 0) {
330 nextLowest = na.mPriority;
331 }
332 continue;
333 }
334 mPriorityList[insertionPoint--] = na.mType;
335 }
336 currentLowest = nextLowest;
337 nextLowest = 0;
338 }
339 }
340
341 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
342 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700343 mNetRequestersPids[i] = new ArrayList();
344 }
345
346 mFeatureUsers = new ArrayList();
347
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700348 mNumDnsEntries = 0;
349
350 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
351 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800352 /*
353 * Create the network state trackers for Wi-Fi and mobile
354 * data. Maybe this could be done with a factory class,
355 * but it's not clear that it's worth it, given that
356 * the number of different network types is not going
357 * to change very often.
358 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800359 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700360 for (int netType : mPriorityList) {
361 switch (mNetAttributes[netType].mRadio) {
362 case ConnectivityManager.TYPE_WIFI:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800363 if (DBG) Slog.v(TAG, "Starting Wifi Service.");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700364 WifiStateTracker wst = new WifiStateTracker(context, mHandler);
365 WifiService wifiService = new WifiService(context, wst);
366 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff324ec572010-03-11 16:37:45 -0800367 wifiService.startWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700368 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
369 wst.startMonitoring();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800370
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700371 break;
372 case ConnectivityManager.TYPE_MOBILE:
373 mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler,
374 netType, mNetAttributes[netType].mName);
375 mNetTrackers[netType].startMonitoring();
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800376 if (noMobileData) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800377 if (DBG) Slog.d(TAG, "tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800378 mNetTrackers[netType].teardown();
379 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700380 break;
381 default:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800382 Slog.e(TAG, "Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700383 mNetAttributes[netType].mRadio);
384 continue;
385 }
386 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800387
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800388 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800389 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
390 !mTethering.isDunRequired()) &&
391 (mTethering.getTetherableUsbRegexs().length != 0 ||
392 mTethering.getTetherableWifiRegexs().length != 0) &&
393 mTethering.getUpstreamIfaceRegexs().length != 0);
394
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700395 if (DBG) {
396 mInetLog = new ArrayList();
397 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398 }
399
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700400
The Android Open Source Project28527d22009-03-03 19:31:44 -0800401 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700402 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800403 * @param preference the new preference
404 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700405 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800406 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700407
408 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800409 }
410
411 public int getNetworkPreference() {
412 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700413 int preference;
414 synchronized(this) {
415 preference = mNetworkPreference;
416 }
417 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800418 }
419
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700420 private void handleSetNetworkPreference(int preference) {
421 if (ConnectivityManager.isNetworkTypeValid(preference) &&
422 mNetAttributes[preference] != null &&
423 mNetAttributes[preference].isDefault()) {
424 if (mNetworkPreference != preference) {
425 final ContentResolver cr = mContext.getContentResolver();
426 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
427 synchronized(this) {
428 mNetworkPreference = preference;
429 }
430 enforcePreference();
431 }
432 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800433 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700434
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435 private int getPersistedNetworkPreference() {
436 final ContentResolver cr = mContext.getContentResolver();
437
438 final int networkPrefSetting = Settings.Secure
439 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
440 if (networkPrefSetting != -1) {
441 return networkPrefSetting;
442 }
443
444 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
445 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700446
The Android Open Source Project28527d22009-03-03 19:31:44 -0800447 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700448 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800449 * In this method, we only tear down a non-preferred network. Establishing
450 * a connection to the preferred network is taken care of when we handle
451 * the disconnect event from the non-preferred network
452 * (see {@link #handleDisconnect(NetworkInfo)}).
453 */
454 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700455 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 return;
457
Robert Greenwalt2034b912009-08-12 16:08:25 -0700458 if (!mNetTrackers[mNetworkPreference].isAvailable())
459 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800460
Robert Greenwalt2034b912009-08-12 16:08:25 -0700461 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700462 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700463 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700464 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800465 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700466 mNetTrackers[t].getNetworkInfo() +
467 " in enforcePreference");
468 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700469 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 }
471 }
472 }
473
474 private boolean teardown(NetworkStateTracker netTracker) {
475 if (netTracker.teardown()) {
476 netTracker.setTeardownRequested(true);
477 return true;
478 } else {
479 return false;
480 }
481 }
482
483 /**
484 * Return NetworkInfo for the active (i.e., connected) network interface.
485 * It is assumed that at most one network is active at a time. If more
486 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700487 * @return the info for the active network, or {@code null} if none is
488 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800489 */
490 public NetworkInfo getActiveNetworkInfo() {
491 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700492 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700493 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700494 continue;
495 }
496 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497 NetworkInfo info = t.getNetworkInfo();
498 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800499 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 "connected default network is not " +
501 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800502 return info;
503 }
504 }
505 return null;
506 }
507
508 public NetworkInfo getNetworkInfo(int networkType) {
509 enforceAccessPermission();
510 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
511 NetworkStateTracker t = mNetTrackers[networkType];
512 if (t != null)
513 return t.getNetworkInfo();
514 }
515 return null;
516 }
517
518 public NetworkInfo[] getAllNetworkInfo() {
519 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700520 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800521 int i = 0;
522 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700523 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 }
525 return result;
526 }
527
528 public boolean setRadios(boolean turnOn) {
529 boolean result = true;
530 enforceChangePermission();
531 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700532 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800533 }
534 return result;
535 }
536
537 public boolean setRadio(int netType, boolean turnOn) {
538 enforceChangePermission();
539 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
540 return false;
541 }
542 NetworkStateTracker tracker = mNetTrackers[netType];
543 return tracker != null && tracker.setRadio(turnOn);
544 }
545
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700546 /**
547 * Used to notice when the calling process dies so we can self-expire
548 *
549 * Also used to know if the process has cleaned up after itself when
550 * our auto-expire timer goes off. The timer has a link to an object.
551 *
552 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700553 private class FeatureUser implements IBinder.DeathRecipient {
554 int mNetworkType;
555 String mFeature;
556 IBinder mBinder;
557 int mPid;
558 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800559 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700560
561 FeatureUser(int type, String feature, IBinder binder) {
562 super();
563 mNetworkType = type;
564 mFeature = feature;
565 mBinder = binder;
566 mPid = getCallingPid();
567 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800568 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700569
Robert Greenwalt2034b912009-08-12 16:08:25 -0700570 try {
571 mBinder.linkToDeath(this, 0);
572 } catch (RemoteException e) {
573 binderDied();
574 }
575 }
576
577 void unlinkDeathRecipient() {
578 mBinder.unlinkToDeath(this, 0);
579 }
580
581 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800582 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800583 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
584 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700585 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700586 }
587
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700588 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800589 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800590 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
591 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700592 stopUsingNetworkFeature(this, false);
593 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800594
595 public String toString() {
596 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
597 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
598 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700599 }
600
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700601 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700602 public int startUsingNetworkFeature(int networkType, String feature,
603 IBinder binder) {
604 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800605 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700606 ": " + feature);
607 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800608 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700609 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
610 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700611 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800612 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700613
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700614 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700615
616 // TODO - move this into the MobileDataStateTracker
617 int usedNetworkType = networkType;
618 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800619 if (!getMobileDataEnabled()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800620 if (DBG) Slog.d(TAG, "requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800621 return Phone.APN_TYPE_NOT_AVAILABLE;
622 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700623 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
624 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
625 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
626 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
627 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
628 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
629 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
630 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
631 }
632 }
633 NetworkStateTracker network = mNetTrackers[usedNetworkType];
634 if (network != null) {
635 if (usedNetworkType != networkType) {
636 Integer currentPid = new Integer(getCallingPid());
637
638 NetworkStateTracker radio = mNetTrackers[networkType];
639 NetworkInfo ni = network.getNetworkInfo();
640
641 if (ni.isAvailable() == false) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800642 if (DBG) Slog.d(TAG, "special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700643 return Phone.APN_TYPE_NOT_AVAILABLE;
644 }
645
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700646 synchronized(this) {
647 mFeatureUsers.add(f);
648 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
649 // this gets used for per-pid dns when connected
650 mNetRequestersPids[usedNetworkType].add(currentPid);
651 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700652 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700653 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700654 f), getRestoreDefaultNetworkDelay());
655
Robert Greenwalt2034b912009-08-12 16:08:25 -0700656
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700657 if ((ni.isConnectedOrConnecting() == true) &&
658 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700659 if (ni.isConnected() == true) {
660 // add the pid-specific dns
Robert Greenwalt0ef68752010-08-13 14:16:12 -0700661 handleDnsConfigurationChange(networkType);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800662 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700663 return Phone.APN_ALREADY_ACTIVE;
664 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800665 if (DBG) Slog.d(TAG, "special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700666 return Phone.APN_REQUEST_STARTED;
667 }
668
669 // check if the radio in play can make another contact
670 // assume if cannot for now
671
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800672 if (DBG) Slog.d(TAG, "reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 network.reconnect();
674 return Phone.APN_REQUEST_STARTED;
675 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700676 synchronized(this) {
677 mFeatureUsers.add(f);
678 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700679 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700680 f), getRestoreDefaultNetworkDelay());
681
Robert Greenwalt2034b912009-08-12 16:08:25 -0700682 return network.startUsingNetworkFeature(feature,
683 getCallingPid(), getCallingUid());
684 }
685 }
686 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800687 }
688
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700689 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800690 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700691 enforceChangePermission();
692
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700693 int pid = getCallingPid();
694 int uid = getCallingUid();
695
696 FeatureUser u = null;
697 boolean found = false;
698
699 synchronized(this) {
700 for (int i = 0; i < mFeatureUsers.size() ; i++) {
701 u = (FeatureUser)mFeatureUsers.get(i);
702 if (uid == u.mUid && pid == u.mPid &&
703 networkType == u.mNetworkType &&
704 TextUtils.equals(feature, u.mFeature)) {
705 found = true;
706 break;
707 }
708 }
709 }
710 if (found && u != null) {
711 // stop regardless of how many other time this proc had called start
712 return stopUsingNetworkFeature(u, true);
713 } else {
714 // none found!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800715 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700716 return 1;
717 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700718 }
719
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700720 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
721 int networkType = u.mNetworkType;
722 String feature = u.mFeature;
723 int pid = u.mPid;
724 int uid = u.mUid;
725
726 NetworkStateTracker tracker = null;
727 boolean callTeardown = false; // used to carry our decision outside of sync block
728
Robert Greenwalt2034b912009-08-12 16:08:25 -0700729 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800730 Slog.d(TAG, "stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700731 ": " + feature);
732 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700733
The Android Open Source Project28527d22009-03-03 19:31:44 -0800734 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
735 return -1;
736 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700737
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700738 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
739 // sync block
740 synchronized(this) {
741 // check if this process still has an outstanding start request
742 if (!mFeatureUsers.contains(u)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800743 if (DBG) Slog.d(TAG, "ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 return 1;
745 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700746 u.unlinkDeathRecipient();
747 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
748 // If we care about duplicate requests, check for that here.
749 //
750 // This is done to support the extension of a request - the app
751 // can request we start the network feature again and renew the
752 // auto-shutoff delay. Normal "stop" calls from the app though
753 // do not pay attention to duplicate requests - in effect the
754 // API does not refcount and a single stop will counter multiple starts.
755 if (ignoreDups == false) {
756 for (int i = 0; i < mFeatureUsers.size() ; i++) {
757 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
758 if (x.mUid == u.mUid && x.mPid == u.mPid &&
759 x.mNetworkType == u.mNetworkType &&
760 TextUtils.equals(x.mFeature, u.mFeature)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800761 if (DBG) Slog.d(TAG, "ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700762 return 1;
763 }
764 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700765 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700766
767 // TODO - move to MobileDataStateTracker
768 int usedNetworkType = networkType;
769 if (networkType == ConnectivityManager.TYPE_MOBILE) {
770 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
771 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
772 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
773 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
774 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
775 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
776 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
777 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
778 }
779 }
780 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700781 if (tracker == null) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800782 if (DBG) Slog.d(TAG, "ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700783 return -1;
784 }
785 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700786 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700787 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800788 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700789 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800790 if (DBG) Slog.d(TAG, "not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700791 "others still using it");
792 return 1;
793 }
794 callTeardown = true;
795 }
796 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800797 if (DBG) Slog.d(TAG, "Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700798 if (callTeardown) {
799 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700800 return 1;
801 } else {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700802 // do it the old fashioned way
Robert Greenwalt2034b912009-08-12 16:08:25 -0700803 return tracker.stopUsingNetworkFeature(feature, pid, uid);
804 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800805 }
806
807 /**
808 * Ensure that a network route exists to deliver traffic to the specified
809 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700810 * @param networkType the type of the network over which traffic to the
811 * specified host is to be routed
812 * @param hostAddress the IP address of the host to which the route is
813 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800814 * @return {@code true} on success, {@code false} on failure
815 */
816 public boolean requestRouteToHost(int networkType, int hostAddress) {
817 enforceChangePermission();
818 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
819 return false;
820 }
821 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700822
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700823 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
824 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700825 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800826 Slog.d(TAG, "requestRouteToHost on down network (" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700827 }
828 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800829 }
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700830 return tracker.requestRouteToHost(hostAddress);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800831 }
832
833 /**
834 * @see ConnectivityManager#getBackgroundDataSetting()
835 */
836 public boolean getBackgroundDataSetting() {
837 return Settings.Secure.getInt(mContext.getContentResolver(),
838 Settings.Secure.BACKGROUND_DATA, 1) == 1;
839 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700840
The Android Open Source Project28527d22009-03-03 19:31:44 -0800841 /**
842 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
843 */
844 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
845 mContext.enforceCallingOrSelfPermission(
846 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
847 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700848
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700849 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
850 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
851 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800852
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700853 private void handleSetBackgroundData(boolean enabled) {
854 if (enabled != getBackgroundDataSetting()) {
855 Settings.Secure.putInt(mContext.getContentResolver(),
856 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
857 Intent broadcast = new Intent(
858 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
859 mContext.sendBroadcast(broadcast);
860 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700861 }
862
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800863 /**
864 * @see ConnectivityManager#getMobileDataEnabled()
865 */
866 public boolean getMobileDataEnabled() {
867 enforceAccessPermission();
868 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
869 Settings.Secure.MOBILE_DATA, 1) == 1;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800870 if (DBG) Slog.d(TAG, "getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800871 return retVal;
872 }
873
874 /**
875 * @see ConnectivityManager#setMobileDataEnabled(boolean)
876 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700877 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800878 enforceChangePermission();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800879 if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800880
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700881 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
882 (enabled ? ENABLED : DISABLED), 0));
883 }
884
885 private void handleSetMobileData(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800886 if (getMobileDataEnabled() == enabled) return;
887
888 Settings.Secure.putInt(mContext.getContentResolver(),
889 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
890
891 if (enabled) {
892 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700893 if (DBG) {
894 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
895 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800896 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
897 }
898 } else {
899 for (NetworkStateTracker nt : mNetTrackers) {
900 if (nt == null) continue;
901 int netType = nt.getNetworkInfo().getType();
902 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800903 if (DBG) Slog.d(TAG, "tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800904 nt.teardown();
905 }
906 }
907 }
908 }
909
The Android Open Source Project28527d22009-03-03 19:31:44 -0800910 private int getNumConnectedNetworks() {
911 int numConnectedNets = 0;
912
913 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700914 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -0700915 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800916 ++numConnectedNets;
917 }
918 }
919 return numConnectedNets;
920 }
921
922 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700923 mContext.enforceCallingOrSelfPermission(
924 android.Manifest.permission.ACCESS_NETWORK_STATE,
925 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800926 }
927
928 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700929 mContext.enforceCallingOrSelfPermission(
930 android.Manifest.permission.CHANGE_NETWORK_STATE,
931 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800932 }
933
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800934 // TODO Make this a special check when it goes public
935 private void enforceTetherChangePermission() {
936 mContext.enforceCallingOrSelfPermission(
937 android.Manifest.permission.CHANGE_NETWORK_STATE,
938 "ConnectivityService");
939 }
940
Robert Greenwalt8e87f122010-02-11 18:18:40 -0800941 private void enforceTetherAccessPermission() {
942 mContext.enforceCallingOrSelfPermission(
943 android.Manifest.permission.ACCESS_NETWORK_STATE,
944 "ConnectivityService");
945 }
946
The Android Open Source Project28527d22009-03-03 19:31:44 -0800947 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700948 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
949 * network, we ignore it. If it is for the active network, we send out a
950 * broadcast. But first, we check whether it might be possible to connect
951 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800952 * @param info the {@code NetworkInfo} for the network
953 */
954 private void handleDisconnect(NetworkInfo info) {
955
Robert Greenwalt2034b912009-08-12 16:08:25 -0700956 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800957
Robert Greenwalt2034b912009-08-12 16:08:25 -0700958 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800959 /*
960 * If the disconnected network is not the active one, then don't report
961 * this as a loss of connectivity. What probably happened is that we're
962 * getting the disconnect for a network that we explicitly disabled
963 * in accordance with network preference policies.
964 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700965 if (!mNetAttributes[prevNetType].isDefault()) {
966 List pids = mNetRequestersPids[prevNetType];
967 for (int i = 0; i<pids.size(); i++) {
968 Integer pid = (Integer)pids.get(i);
969 // will remove them because the net's no longer connected
970 // need to do this now as only now do we know the pids and
971 // can properly null things that are no longer referenced.
972 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800973 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800974 }
975
The Android Open Source Project28527d22009-03-03 19:31:44 -0800976 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
977 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
978 if (info.isFailover()) {
979 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
980 info.setFailover(false);
981 }
982 if (info.getReason() != null) {
983 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
984 }
985 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -0700986 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
987 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -0800988 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700989
Robert Greenwalt3cc68d32010-01-25 17:54:29 -0800990 NetworkStateTracker newNet = null;
991 if (mNetAttributes[prevNetType].isDefault()) {
992 newNet = tryFailover(prevNetType);
993 if (newNet != null) {
994 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -0700995 if (!switchTo.isConnected()) {
996 // if the other net is connected they've already reset this and perhaps even gotten
997 // a positive report we don't want to overwrite, but if not we need to clear this now
998 // to turn our cellular sig strength white
999 mDefaultInetConditionPublished = 0;
Robert Greenwaltea8bca02010-12-21 11:43:28 -08001000 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001001 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001002 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1003 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001004 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001005 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1006 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001007 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001008 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001009 // do this before we broadcast the change
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001010 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001011
1012 sendStickyBroadcast(intent);
1013 /*
1014 * If the failover network is already connected, then immediately send
1015 * out a followup broadcast indicating successful failover
1016 */
1017 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1018 sendConnectedBroadcast(newNet.getNetworkInfo());
1019 }
1020 }
1021
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001022 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001023 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001024 /*
1025 * If this is a default network, check if other defaults are available
1026 * or active
1027 */
1028 NetworkStateTracker newNet = null;
1029 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001030 if (mActiveDefaultNetwork == prevNetType) {
1031 mActiveDefaultNetwork = -1;
1032 }
1033
1034 int newType = -1;
1035 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001036 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001037 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001038 if (checkType == prevNetType) continue;
1039 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt084e4772011-02-11 17:32:58 -08001040 if (mNetAttributes[checkType].isDefault() == false) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001041 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1042 noMobileData) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001043 Slog.e(TAG, "not failing over to mobile type " + checkType +
1044 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001045 continue;
1046 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001047 NetworkStateTracker tracker = mNetTrackers[checkType];
1048 NetworkInfo info = tracker.getNetworkInfo();
1049 if (!info.isConnectedOrConnecting() ||
1050 tracker.isTeardownRequested()) {
1051 info.setFailover(true);
1052 tracker.reconnect();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001053 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001054 if (DBG) Slog.d(TAG, "Attempting to switch to " + info.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001055
Robert Greenwalt084e4772011-02-11 17:32:58 -08001056 // figure out if this is the highest priority network
1057 // so we send an appropriate return value
1058 if (checkType == mNetworkPreference) {
1059 newType = checkType;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001060 }
Robert Greenwalt084e4772011-02-11 17:32:58 -08001061 if (mNetAttributes[checkType].mPriority > newPriority &&
1062 newType != mNetworkPreference) {
1063 newType = checkType;
1064 newPriority = mNetAttributes[checkType].mPriority;
1065 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001066 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001067 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001068
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001069 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001070 }
1071
1072 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001073 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1074 }
1075
1076 private void sendInetConditionBroadcast(NetworkInfo info) {
1077 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1078 }
1079
1080 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1081 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001082 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1083 if (info.isFailover()) {
1084 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1085 info.setFailover(false);
1086 }
1087 if (info.getReason() != null) {
1088 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1089 }
1090 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001091 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1092 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001093 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001094 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001095 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001096 }
1097
1098 /**
1099 * Called when an attempt to fail over to another network has failed.
1100 * @param info the {@link NetworkInfo} for the failed network
1101 */
1102 private void handleConnectionFailure(NetworkInfo info) {
1103 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001104
Robert Greenwalt2034b912009-08-12 16:08:25 -07001105 String reason = info.getReason();
1106 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001107
Robert Greenwalte981bc52010-10-08 16:35:52 -07001108 String reasonText;
1109 if (reason == null) {
1110 reasonText = ".";
1111 } else {
1112 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001113 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001114 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001115
1116 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1117 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1118 if (getActiveNetworkInfo() == null) {
1119 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1120 }
1121 if (reason != null) {
1122 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1123 }
1124 if (extraInfo != null) {
1125 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1126 }
1127 if (info.isFailover()) {
1128 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1129 info.setFailover(false);
1130 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001131
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001132 NetworkStateTracker newNet = null;
1133 if (mNetAttributes[info.getType()].isDefault()) {
1134 newNet = tryFailover(info.getType());
1135 if (newNet != null) {
1136 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001137 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001138 // if the other net is connected they've already reset this and perhaps
1139 // even gotten a positive report we don't want to overwrite, but if not
1140 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001141 mDefaultInetConditionPublished = 0;
1142 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001143 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1144 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001145 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001146 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1147 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001148 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001149
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001150 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001151 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001152 /*
1153 * If the failover network is already connected, then immediately send
1154 * out a followup broadcast indicating successful failover
1155 */
1156 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1157 sendConnectedBroadcast(newNet.getNetworkInfo());
1158 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001159 }
1160
1161 private void sendStickyBroadcast(Intent intent) {
1162 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001163 if (!mSystemReady) {
1164 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001165 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001166 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1167 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001168 }
1169 }
1170
1171 void systemReady() {
1172 synchronized(this) {
1173 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001174 if (mInitialBroadcast != null) {
1175 mContext.sendStickyBroadcast(mInitialBroadcast);
1176 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001177 }
1178 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001179 }
1180
1181 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001182 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001183
1184 // snapshot isFailover, because sendConnectedBroadcast() resets it
1185 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001186 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187
Robert Greenwalt2034b912009-08-12 16:08:25 -07001188 // if this is a default net and other default is running
1189 // kill the one not preferred
1190 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001191 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1192 if ((type != mNetworkPreference &&
1193 mNetAttributes[mActiveDefaultNetwork].mPriority >
1194 mNetAttributes[type].mPriority) ||
1195 mNetworkPreference == mActiveDefaultNetwork) {
1196 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001197 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001198 "to torn down network " + info.getTypeName());
1199 teardown(thisNet);
1200 return;
1201 } else {
1202 // tear down the other
1203 NetworkStateTracker otherNet =
1204 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001205 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 otherNet.getNetworkInfo().getTypeName() +
1207 " teardown");
1208 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001209 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001210 return;
1211 }
1212 if (isFailover) {
1213 otherNet.releaseWakeLock();
1214 }
1215 }
1216 }
1217 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001218 // this will cause us to come up initially as unconnected and switching
1219 // to connected after our normal pause unless somebody reports us as reall
1220 // disconnected
1221 mDefaultInetConditionPublished = 0;
1222 mDefaultConnectionSequence++;
1223 mInetConditionChangeInFlight = false;
1224 // Don't do this - if we never sign in stay, grey
1225 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001226 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001227 thisNet.setTeardownRequested(false);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001228 thisNet.updateNetworkSettings();
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001229 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001230 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001231 }
1232
1233 private void handleScanResultsAvailable(NetworkInfo info) {
1234 int networkType = info.getType();
1235 if (networkType != ConnectivityManager.TYPE_WIFI) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001236 if (DBG) Slog.v(TAG, "Got ScanResultsAvailable for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001237 info.getTypeName() + " network. Don't know how to handle.");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238 }
Robert Greenwalt0659da32009-07-16 17:21:39 -07001239
The Android Open Source Project28527d22009-03-03 19:31:44 -08001240 mNetTrackers[networkType].interpretScanResultsAvailable();
1241 }
1242
Robert Greenwalt0659da32009-07-16 17:21:39 -07001243 private void handleNotificationChange(boolean visible, int id,
1244 Notification notification) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001245 NotificationManager notificationManager = (NotificationManager) mContext
1246 .getSystemService(Context.NOTIFICATION_SERVICE);
Robert Greenwalt0659da32009-07-16 17:21:39 -07001247
The Android Open Source Project28527d22009-03-03 19:31:44 -08001248 if (visible) {
1249 notificationManager.notify(id, notification);
1250 } else {
1251 notificationManager.cancel(id);
1252 }
1253 }
1254
1255 /**
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001256 * After a change in the connectivity state of any network, We're mainly
1257 * concerned with making sure that the list of DNS servers is setupup
1258 * according to which networks are connected, and ensuring that the
1259 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001260 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001261 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001262 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001263 * If a non-default network is enabled, add the host routes that
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001264 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001265 */
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001266 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001267
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001268 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1269 if (mNetAttributes[netType].isDefault()) {
1270 mNetTrackers[netType].addDefaultRoute();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001271 } else {
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001272 mNetTrackers[netType].addPrivateDnsRoutes();
1273 }
1274 } else {
1275 if (mNetAttributes[netType].isDefault()) {
1276 mNetTrackers[netType].removeDefaultRoute();
1277 } else {
1278 mNetTrackers[netType].removePrivateDnsRoutes();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001279 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001280 }
1281 }
1282
Robert Greenwalt2034b912009-08-12 16:08:25 -07001283 /**
1284 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1285 * on the highest priority active net which this process requested.
1286 * If there aren't any, clear it out
1287 */
1288 private void reassessPidDns(int myPid, boolean doBump)
1289 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001290 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001291 for(int i : mPriorityList) {
1292 if (mNetAttributes[i].isDefault()) {
1293 continue;
1294 }
1295 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001296 if (nt.getNetworkInfo().isConnected() &&
1297 !nt.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001298 List pids = mNetRequestersPids[i];
1299 for (int j=0; j<pids.size(); j++) {
1300 Integer pid = (Integer)pids.get(j);
1301 if (pid.intValue() == myPid) {
1302 String[] dnsList = nt.getNameServers();
1303 writePidDns(dnsList, myPid);
1304 if (doBump) {
1305 bumpDns();
1306 }
1307 return;
1308 }
1309 }
1310 }
1311 }
1312 // nothing found - delete
1313 for (int i = 1; ; i++) {
1314 String prop = "net.dns" + i + "." + myPid;
1315 if (SystemProperties.get(prop).length() == 0) {
1316 if (doBump) {
1317 bumpDns();
1318 }
1319 return;
1320 }
1321 SystemProperties.set(prop, "");
1322 }
1323 }
1324
1325 private void writePidDns(String[] dnsList, int pid) {
1326 int j = 1;
1327 for (String dns : dnsList) {
1328 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1329 SystemProperties.set("net.dns" + j++ + "." + pid, dns);
1330 }
1331 }
1332 }
1333
1334 private void bumpDns() {
1335 /*
1336 * Bump the property that tells the name resolver library to reread
1337 * the DNS server list from the properties.
1338 */
1339 String propVal = SystemProperties.get("net.dnschange");
1340 int n = 0;
1341 if (propVal.length() != 0) {
1342 try {
1343 n = Integer.parseInt(propVal);
1344 } catch (NumberFormatException e) {}
1345 }
1346 SystemProperties.set("net.dnschange", "" + (n+1));
1347 }
1348
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001349 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001350 // add default net's dns entries
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001351 NetworkStateTracker nt = mNetTrackers[netType];
1352 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
1353 String[] dnsList = nt.getNameServers();
1354 if (mNetAttributes[netType].isDefault()) {
1355 int j = 1;
1356 for (String dns : dnsList) {
1357 if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) {
1358 if (DBG) {
1359 Slog.d(TAG, "adding dns " + dns + " for " +
1360 nt.getNetworkInfo().getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001361 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001362 SystemProperties.set("net.dns" + j++, dns);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001363 }
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001364 }
1365 for (int k=j ; k<mNumDnsEntries; k++) {
1366 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1367 SystemProperties.set("net.dns" + k, "");
1368 }
1369 mNumDnsEntries = j;
1370 } else {
1371 // set per-pid dns for attached secondary nets
1372 List pids = mNetRequestersPids[netType];
1373 for (int y=0; y< pids.size(); y++) {
1374 Integer pid = (Integer)pids.get(y);
1375 writePidDns(dnsList, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001376 }
1377 }
1378 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001379 bumpDns();
1380 }
1381
1382 private int getRestoreDefaultNetworkDelay() {
1383 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1384 NETWORK_RESTORE_DELAY_PROP_NAME);
1385 if(restoreDefaultNetworkDelayStr != null &&
1386 restoreDefaultNetworkDelayStr.length() != 0) {
1387 try {
1388 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1389 } catch (NumberFormatException e) {
1390 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001391 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001392 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001393 }
1394
1395 @Override
1396 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001397 if (mContext.checkCallingOrSelfPermission(
1398 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001399 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001400 pw.println("Permission Denial: can't dump ConnectivityService " +
1401 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1402 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001403 return;
1404 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001405 pw.println();
1406 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001407 if (nst != null) {
1408 if (nst.getNetworkInfo().isConnected()) {
1409 pw.println("Active network: " + nst.getNetworkInfo().
1410 getTypeName());
1411 }
1412 pw.println(nst.getNetworkInfo());
1413 pw.println(nst);
1414 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001415 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001416 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001417
1418 pw.println("Network Requester Pids:");
1419 for (int net : mPriorityList) {
1420 String pidString = net + ": ";
1421 for (Object pid : mNetRequestersPids[net]) {
1422 pidString = pidString + pid.toString() + ", ";
1423 }
1424 pw.println(pidString);
1425 }
1426 pw.println();
1427
1428 pw.println("FeatureUsers:");
1429 for (Object requester : mFeatureUsers) {
1430 pw.println(requester.toString());
1431 }
1432 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001433
1434 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001435
1436 if (mInetLog != null) {
1437 pw.println();
1438 pw.println("Inet condition reports:");
1439 for(int i = 0; i < mInetLog.size(); i++) {
1440 pw.println(mInetLog.get(i));
1441 }
1442 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001443 }
1444
Robert Greenwalt2034b912009-08-12 16:08:25 -07001445 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001446 private class MyHandler extends Handler {
1447 @Override
1448 public void handleMessage(Message msg) {
1449 NetworkInfo info;
1450 switch (msg.what) {
1451 case NetworkStateTracker.EVENT_STATE_CHANGED:
1452 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001453 int type = info.getType();
1454 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001455 // only do this optimization for wifi. It going into scan mode for location
1456 // services generates alot of noise. Meanwhile the mms apn won't send out
1457 // subsequent notifications when on default cellular because it never
1458 // disconnects.. so only do this to wifi notifications. Fixed better when the
1459 // APN notifications are standardized.
1460 if (mNetAttributes[type].mLastState == state &&
1461 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001462 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001463 // TODO - remove this after we validate the dropping doesn't break
1464 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001465 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001466 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001467 state + "/" + info.getDetailedState());
1468 }
1469 return;
1470 }
1471 mNetAttributes[type].mLastState = state;
1472
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001473 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001474 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001475 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001476
1477 // Connectivity state changed:
1478 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001479 // [12-9] Network subtype (for mobile network, as defined
1480 // by TelephonyManager)
1481 // [8-3] Detailed state ordinal (as defined by
1482 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001483 // [2-0] Network type (as defined by ConnectivityManager)
1484 int eventLogParam = (info.getType() & 0x7) |
1485 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1486 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001487 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001488 eventLogParam);
1489
1490 if (info.getDetailedState() ==
1491 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001492 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001493 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001494 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001495 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001496 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001497 // the logic here is, handle SUSPENDED the same as
1498 // DISCONNECTED. The only difference being we are
1499 // broadcasting an intent with NetworkInfo that's
1500 // suspended. This allows the applications an
1501 // opportunity to handle DISCONNECTED and SUSPENDED
1502 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001503 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001504 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001505 handleConnect(info);
1506 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001507 break;
1508
1509 case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE:
1510 info = (NetworkInfo) msg.obj;
1511 handleScanResultsAvailable(info);
1512 break;
Robert Greenwalt0659da32009-07-16 17:21:39 -07001513
The Android Open Source Project28527d22009-03-03 19:31:44 -08001514 case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED:
Robert Greenwalt0659da32009-07-16 17:21:39 -07001515 handleNotificationChange(msg.arg1 == 1, msg.arg2,
1516 (Notification) msg.obj);
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001517 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001518
1519 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt0ef68752010-08-13 14:16:12 -07001520 info = (NetworkInfo) msg.obj;
1521 type = info.getType();
1522 handleDnsConfigurationChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001523 break;
1524
1525 case NetworkStateTracker.EVENT_ROAMING_CHANGED:
1526 // fill me in
1527 break;
1528
1529 case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED:
1530 // fill me in
1531 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001532 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001533 FeatureUser u = (FeatureUser)msg.obj;
1534 u.expire();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001535 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001536 case EVENT_INET_CONDITION_CHANGE:
1537 {
1538 int netType = msg.arg1;
1539 int condition = msg.arg2;
1540 handleInetConditionChange(netType, condition);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001541 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001542 }
1543 case EVENT_INET_CONDITION_HOLD_END:
1544 {
1545 int netType = msg.arg1;
1546 int sequence = msg.arg2;
1547 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001548 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001549 }
1550 case EVENT_SET_NETWORK_PREFERENCE:
1551 {
1552 int preference = msg.arg1;
1553 handleSetNetworkPreference(preference);
1554 break;
1555 }
1556 case EVENT_SET_BACKGROUND_DATA:
1557 {
1558 boolean enabled = (msg.arg1 == ENABLED);
1559 handleSetBackgroundData(enabled);
1560 break;
1561 }
1562 case EVENT_SET_MOBILE_DATA:
1563 {
1564 boolean enabled = (msg.arg1 == ENABLED);
1565 handleSetMobileData(enabled);
1566 break;
1567 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001568 }
1569 }
1570 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001571
1572 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001573 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001574 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001575
1576 if (isTetheringSupported()) {
1577 return mTethering.tether(iface);
1578 } else {
1579 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1580 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001581 }
1582
1583 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001584 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001585 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001586
1587 if (isTetheringSupported()) {
1588 return mTethering.untether(iface);
1589 } else {
1590 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1591 }
1592 }
1593
1594 // javadoc from interface
1595 public int getLastTetherError(String iface) {
1596 enforceTetherAccessPermission();
1597
1598 if (isTetheringSupported()) {
1599 return mTethering.getLastTetherError(iface);
1600 } else {
1601 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1602 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001603 }
1604
1605 // TODO - proper iface API for selection by property, inspection, etc
1606 public String[] getTetherableUsbRegexs() {
1607 enforceTetherAccessPermission();
1608 if (isTetheringSupported()) {
1609 return mTethering.getTetherableUsbRegexs();
1610 } else {
1611 return new String[0];
1612 }
1613 }
1614
1615 public String[] getTetherableWifiRegexs() {
1616 enforceTetherAccessPermission();
1617 if (isTetheringSupported()) {
1618 return mTethering.getTetherableWifiRegexs();
1619 } else {
1620 return new String[0];
1621 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001622 }
1623
1624 // TODO - move iface listing, queries, etc to new module
1625 // javadoc from interface
1626 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001627 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001628 return mTethering.getTetherableIfaces();
1629 }
1630
1631 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001632 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001633 return mTethering.getTetheredIfaces();
1634 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001635
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001636 public String[] getTetheringErroredIfaces() {
1637 enforceTetherAccessPermission();
1638 return mTethering.getErroredIfaces();
1639 }
1640
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001641 // if ro.tether.denied = true we default to no tethering
1642 // gservices could set the secure setting to 1 though to enable it on a build where it
1643 // had previously been turned off.
1644 public boolean isTetheringSupported() {
1645 enforceTetherAccessPermission();
1646 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001647 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1648 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1649 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001650 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001651
1652 // 100 percent is full good, 0 is full bad.
1653 public void reportInetCondition(int networkType, int percentage) {
1654 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1655 mContext.enforceCallingOrSelfPermission(
1656 android.Manifest.permission.STATUS_BAR,
1657 "ConnectivityService");
1658
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001659 if (DBG) {
1660 int pid = getCallingPid();
1661 int uid = getCallingUid();
1662 String s = pid + "(" + uid + ") reports inet is " +
1663 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1664 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1665 mInetLog.add(s);
1666 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1667 mInetLog.remove(0);
1668 }
1669 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001670 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001671 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1672 }
1673
1674 private void handleInetConditionChange(int netType, int condition) {
1675 if (DBG) {
1676 Slog.d(TAG, "Inet connectivity change, net=" +
1677 netType + ", condition=" + condition +
1678 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1679 }
1680 if (mActiveDefaultNetwork == -1) {
1681 if (DBG) Slog.d(TAG, "no active default network - aborting");
1682 return;
1683 }
1684 if (mActiveDefaultNetwork != netType) {
1685 if (DBG) Slog.d(TAG, "given net not default - aborting");
1686 return;
1687 }
1688 mDefaultInetCondition = condition;
1689 int delay;
1690 if (mInetConditionChangeInFlight == false) {
1691 if (DBG) Slog.d(TAG, "starting a change hold");
1692 // setup a new hold to debounce this
1693 if (mDefaultInetCondition > 50) {
1694 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1695 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1696 } else {
1697 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1698 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1699 }
1700 mInetConditionChangeInFlight = true;
1701 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1702 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1703 } else {
1704 // we've set the new condition, when this hold ends that will get
1705 // picked up
1706 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
1707 }
1708 }
1709
1710 private void handleInetConditionHoldEnd(int netType, int sequence) {
1711 if (DBG) {
1712 Slog.d(TAG, "Inet hold end, net=" + netType +
1713 ", condition =" + mDefaultInetCondition +
1714 ", published condition =" + mDefaultInetConditionPublished);
1715 }
1716 mInetConditionChangeInFlight = false;
1717
1718 if (mActiveDefaultNetwork == -1) {
1719 if (DBG) Slog.d(TAG, "no active default network - aborting");
1720 return;
1721 }
1722 if (mDefaultConnectionSequence != sequence) {
1723 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
1724 return;
1725 }
1726 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
1727 if (DBG) Slog.d(TAG, "no change in condition - aborting");
1728 return;
1729 }
1730 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
1731 if (networkInfo.isConnected() == false) {
1732 if (DBG) Slog.d(TAG, "default network not connected - aborting");
1733 return;
1734 }
1735 mDefaultInetConditionPublished = mDefaultInetCondition;
1736 sendInetConditionBroadcast(networkInfo);
1737 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001738 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001739}