blob: a3d8ac912e780a51c1011408464432186ef2fceb [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
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080019import android.bluetooth.BluetoothTetheringDataTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070024import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080025import android.net.ConnectivityManager;
Robert Greenwalteb123ac2010-12-06 13:56:24 -080026import android.net.DummyDataStateTracker;
The Android Open Source Project28527d22009-03-03 19:31:44 -080027import android.net.IConnectivityManager;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -080028import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080029import android.net.MobileDataStateTracker;
30import android.net.NetworkInfo;
31import android.net.NetworkStateTracker;
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -070032import android.net.NetworkUtils;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070033import android.net.Proxy;
34import android.net.ProxyProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080035import android.net.wifi.WifiStateTracker;
36import android.os.Binder;
37import android.os.Handler;
Wink Saville775aad62010-09-02 19:23:52 -070038import android.os.HandlerThread;
Robert Greenwalt2034b912009-08-12 16:08:25 -070039import android.os.IBinder;
The Android Open Source Project28527d22009-03-03 19:31:44 -080040import android.os.Looper;
41import android.os.Message;
Robert Greenwalt93dc1042010-06-15 12:19:37 -070042import android.os.PowerManager;
Robert Greenwalt2034b912009-08-12 16:08:25 -070043import android.os.RemoteException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080044import android.os.ServiceManager;
45import android.os.SystemProperties;
46import android.provider.Settings;
Robert Greenwalt2034b912009-08-12 16:08:25 -070047import android.text.TextUtils;
The Android Open Source Project28527d22009-03-03 19:31:44 -080048import android.util.EventLog;
Joe Onoratoc2386bb2010-02-26 18:56:32 -080049import android.util.Slog;
The Android Open Source Project28527d22009-03-03 19:31:44 -080050
Robert Greenwalt2034b912009-08-12 16:08:25 -070051import com.android.internal.telephony.Phone;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080052import com.android.server.connectivity.Tethering;
53
The Android Open Source Project28527d22009-03-03 19:31:44 -080054import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070055import java.io.FileWriter;
56import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080057import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070058import java.net.InetAddress;
59import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070060import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070061import java.util.Collection;
Robert Greenwaltd62c7002010-12-29 16:15:02 -080062import java.util.concurrent.atomic.AtomicBoolean;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070063import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070064import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080065
66/**
67 * @hide
68 */
69public class ConnectivityService extends IConnectivityManager.Stub {
70
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070071 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080072 private static final String TAG = "ConnectivityService";
73
Robert Greenwalt2034b912009-08-12 16:08:25 -070074 // how long to wait before switching back to a radio's default network
75 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
76 // system property that can override the above value
77 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
78 "android.telephony.apn-restore";
79
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080080 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080081 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080082
The Android Open Source Project28527d22009-03-03 19:31:44 -080083 /**
84 * Sometimes we want to refer to the individual network state
85 * trackers separately, and sometimes we just want to treat them
86 * abstractly.
87 */
88 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070089
90 /**
91 * A per Net list of the PID's that requested access to the net
92 * used both as a refcount and for per-PID DNS selection
93 */
94 private List mNetRequestersPids[];
95
Irfan Sheriff653e2a22010-06-07 09:03:04 -070096 private WifiWatchdogService mWifiWatchdogService;
97
Robert Greenwalt2034b912009-08-12 16:08:25 -070098 // priority order of the nettrackers
99 // (excluding dynamically set mNetworkPreference)
100 // TODO - move mNetworkTypePreference into this
101 private int[] mPriorityList;
102
The Android Open Source Project28527d22009-03-03 19:31:44 -0800103 private Context mContext;
104 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700105 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700106 // 0 is full bad, 100 is full good
107 private int mDefaultInetCondition = 0;
108 private int mDefaultInetConditionPublished = 0;
109 private boolean mInetConditionChangeInFlight = false;
110 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800111
112 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800113
114 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700115 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800116
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800117 private AtomicBoolean mBackgroundDataEnabled = new AtomicBoolean(true);
118
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700119 private static final int ENABLED = 1;
120 private static final int DISABLED = 0;
121
122 // Share the event space with NetworkStateTracker (which can't see this
123 // internal class but sends us events). If you change these, change
124 // NetworkStateTracker.java too.
125 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
126 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
127
128 /**
129 * used internally as a delayed event to make us switch back to the
130 * default network
131 */
132 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
133 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
134
135 /**
136 * used internally to change our mobile data enabled flag
137 */
138 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
139 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
140
141 /**
142 * used internally to change our network preference setting
143 * arg1 = networkType to prefer
144 */
145 private static final int EVENT_SET_NETWORK_PREFERENCE =
146 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
147
148 /**
149 * used internally to synchronize inet condition reports
150 * arg1 = networkType
151 * arg2 = condition (0 bad, 100 good)
152 */
153 private static final int EVENT_INET_CONDITION_CHANGE =
154 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
155
156 /**
157 * used internally to mark the end of inet condition hold periods
158 * arg1 = networkType
159 */
160 private static final int EVENT_INET_CONDITION_HOLD_END =
161 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
162
163 /**
164 * used internally to set the background data preference
165 * arg1 = TRUE for enabled, FALSE for disabled
166 */
167 private static final int EVENT_SET_BACKGROUND_DATA =
168 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
169
170 /**
171 * used internally to set enable/disable cellular data
172 * arg1 = ENBALED or DISABLED
173 */
174 private static final int EVENT_SET_MOBILE_DATA =
175 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
176
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700177 /**
178 * used internally to clear a wakelock when transitioning
179 * from one net to another
180 */
181 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
182 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
183
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700184 /**
185 * used internally to reload global proxy settings
186 */
187 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
188 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
189
Robert Greenwalt2034b912009-08-12 16:08:25 -0700190 private Handler mHandler;
191
192 // list of DeathRecipients used to make sure features are turned off when
193 // a process dies
194 private List mFeatureUsers;
195
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400196 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800197 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400198
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700199 private PowerManager.WakeLock mNetTransitionWakeLock;
200 private String mNetTransitionWakeLockCausedBy = "";
201 private int mNetTransitionWakeLockSerialNumber;
202 private int mNetTransitionWakeLockTimeout;
203
Robert Greenwalt94daa182010-09-01 11:34:05 -0700204 private InetAddress mDefaultDns;
205
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700206 // used in DBG mode to track inet condition reports
207 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
208 private ArrayList mInetLog;
209
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700210 // track the current default http proxy - tell the world if we get a new one (real change)
211 private ProxyProperties mDefaultProxy = null;
212 // track the global proxy.
213 private ProxyProperties mGlobalProxy = null;
214 private final Object mGlobalProxyLock = new Object();
215
216 private SettingsObserver mSettingsObserver;
217
Robert Greenwalt12c44552009-12-07 11:33:18 -0800218 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700219 /**
220 * Class for holding settings read from resources.
221 */
222 public String mName;
223 public int mType;
224 public int mRadio;
225 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800226 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700227 public NetworkAttributes(String init) {
228 String fragments[] = init.split(",");
229 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700230 mType = Integer.parseInt(fragments[1]);
231 mRadio = Integer.parseInt(fragments[2]);
232 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800233 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700234 }
235 public boolean isDefault() {
236 return (mType == mRadio);
237 }
238 }
239 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700240 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700241
Robert Greenwalt12c44552009-12-07 11:33:18 -0800242 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700243 public int mSimultaneity;
244 public int mType;
245 public RadioAttributes(String init) {
246 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700247 mType = Integer.parseInt(fragments[0]);
248 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700249 }
250 }
251 RadioAttributes[] mRadioAttributes;
252
Wink Saville775aad62010-09-02 19:23:52 -0700253 public static synchronized ConnectivityService getInstance(Context context) {
254 if (sServiceInstance == null) {
255 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800256 }
Wink Saville775aad62010-09-02 19:23:52 -0700257 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800258 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700259
The Android Open Source Project28527d22009-03-03 19:31:44 -0800260 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800261 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800262
Wink Saville775aad62010-09-02 19:23:52 -0700263 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
264 handlerThread.start();
265 mHandler = new MyHandler(handlerThread.getLooper());
266
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800267 mBackgroundDataEnabled.set(Settings.Secure.getInt(context.getContentResolver(),
268 Settings.Secure.BACKGROUND_DATA, 1) == 1);
269
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800270 // setup our unique device name
Robert Greenwalt82cde132010-12-06 09:30:17 -0800271 if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
272 String id = Settings.Secure.getString(context.getContentResolver(),
273 Settings.Secure.ANDROID_ID);
274 if (id != null && id.length() > 0) {
275 String name = new String("android_").concat(id);
276 SystemProperties.set("net.hostname", name);
277 }
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800278 }
279
Robert Greenwalt94daa182010-09-01 11:34:05 -0700280 // read our default dns server ip
281 String dns = Settings.Secure.getString(context.getContentResolver(),
282 Settings.Secure.DEFAULT_DNS_SERVER);
283 if (dns == null || dns.length() == 0) {
284 dns = context.getResources().getString(
285 com.android.internal.R.string.config_default_dns_server);
286 }
287 try {
288 mDefaultDns = InetAddress.getByName(dns);
289 } catch (UnknownHostException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800290 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700291 }
292
The Android Open Source Project28527d22009-03-03 19:31:44 -0800293 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700294
295 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
296 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
297 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
298 com.android.internal.R.integer.config_networkTransitionTimeout);
299
Robert Greenwalt2034b912009-08-12 16:08:25 -0700300 mNetTrackers = new NetworkStateTracker[
301 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700302
The Android Open Source Project28527d22009-03-03 19:31:44 -0800303 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700304
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700305 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
306 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
307
Robert Greenwalt2034b912009-08-12 16:08:25 -0700308 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700309 String[] raStrings = context.getResources().getStringArray(
310 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700311 for (String raString : raStrings) {
312 RadioAttributes r = new RadioAttributes(raString);
313 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800314 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700315 continue;
316 }
317 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800318 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700319 r.mType);
320 continue;
321 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700322 mRadioAttributes[r.mType] = r;
323 }
324
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700325 String[] naStrings = context.getResources().getStringArray(
326 com.android.internal.R.array.networkAttributes);
327 for (String naString : naStrings) {
328 try {
329 NetworkAttributes n = new NetworkAttributes(naString);
330 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800331 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700332 n.mType);
333 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700334 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700335 if (mNetAttributes[n.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800336 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700337 n.mType);
338 continue;
339 }
340 if (mRadioAttributes[n.mRadio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800341 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700342 "radio " + n.mRadio + " in network type " + n.mType);
343 continue;
344 }
345 mNetAttributes[n.mType] = n;
346 mNetworksDefined++;
347 } catch(Exception e) {
348 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700349 }
350 }
351
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700352 // high priority first
353 mPriorityList = new int[mNetworksDefined];
354 {
355 int insertionPoint = mNetworksDefined-1;
356 int currentLowest = 0;
357 int nextLowest = 0;
358 while (insertionPoint > -1) {
359 for (NetworkAttributes na : mNetAttributes) {
360 if (na == null) continue;
361 if (na.mPriority < currentLowest) continue;
362 if (na.mPriority > currentLowest) {
363 if (na.mPriority < nextLowest || nextLowest == 0) {
364 nextLowest = na.mPriority;
365 }
366 continue;
367 }
368 mPriorityList[insertionPoint--] = na.mType;
369 }
370 currentLowest = nextLowest;
371 nextLowest = 0;
372 }
373 }
374
375 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
376 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700377 mNetRequestersPids[i] = new ArrayList();
378 }
379
380 mFeatureUsers = new ArrayList();
381
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700382 mNumDnsEntries = 0;
383
384 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
385 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800386 /*
387 * Create the network state trackers for Wi-Fi and mobile
388 * data. Maybe this could be done with a factory class,
389 * but it's not clear that it's worth it, given that
390 * the number of different network types is not going
391 * to change very often.
392 */
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700393 for (int netType : mPriorityList) {
394 switch (mNetAttributes[netType].mRadio) {
395 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800396 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700397 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700398 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700399 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700400 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700401 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700402 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800403
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700404 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700405 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700406
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700407 break;
408 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700409 mNetTrackers[netType] = new MobileDataStateTracker(netType,
410 mNetAttributes[netType].mName);
411 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700412 break;
Robert Greenwalteb123ac2010-12-06 13:56:24 -0800413 case ConnectivityManager.TYPE_DUMMY:
414 mNetTrackers[netType] = new DummyDataStateTracker(netType,
415 mNetAttributes[netType].mName);
416 mNetTrackers[netType].startMonitoring(context, mHandler);
417 break;
Jaikumar Ganesh0db51a02010-12-21 22:31:44 -0800418 case ConnectivityManager.TYPE_BLUETOOTH:
419 mNetTrackers[netType] = BluetoothTetheringDataTracker.getInstance();
420 mNetTrackers[netType].startMonitoring(context, mHandler);
421 break;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700422 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800423 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700424 mNetAttributes[netType].mRadio);
425 continue;
426 }
427 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800428
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800429 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800430 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
431 !mTethering.isDunRequired()) &&
432 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700433 mTethering.getTetherableWifiRegexs().length != 0 ||
434 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800435 mTethering.getUpstreamIfaceRegexs().length != 0);
436
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700437 if (DBG) {
438 mInetLog = new ArrayList();
439 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700440
441 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
442 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800443
444 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800445 }
446
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700447
The Android Open Source Project28527d22009-03-03 19:31:44 -0800448 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700449 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800450 * @param preference the new preference
451 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700452 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700454
455 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800456 }
457
458 public int getNetworkPreference() {
459 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700460 int preference;
461 synchronized(this) {
462 preference = mNetworkPreference;
463 }
464 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800465 }
466
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700467 private void handleSetNetworkPreference(int preference) {
468 if (ConnectivityManager.isNetworkTypeValid(preference) &&
469 mNetAttributes[preference] != null &&
470 mNetAttributes[preference].isDefault()) {
471 if (mNetworkPreference != preference) {
472 final ContentResolver cr = mContext.getContentResolver();
473 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
474 synchronized(this) {
475 mNetworkPreference = preference;
476 }
477 enforcePreference();
478 }
479 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800480 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700481
The Android Open Source Project28527d22009-03-03 19:31:44 -0800482 private int getPersistedNetworkPreference() {
483 final ContentResolver cr = mContext.getContentResolver();
484
485 final int networkPrefSetting = Settings.Secure
486 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
487 if (networkPrefSetting != -1) {
488 return networkPrefSetting;
489 }
490
491 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
492 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700493
The Android Open Source Project28527d22009-03-03 19:31:44 -0800494 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700495 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800496 * In this method, we only tear down a non-preferred network. Establishing
497 * a connection to the preferred network is taken care of when we handle
498 * the disconnect event from the non-preferred network
499 * (see {@link #handleDisconnect(NetworkInfo)}).
500 */
501 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700502 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800503 return;
504
Robert Greenwalt2034b912009-08-12 16:08:25 -0700505 if (!mNetTrackers[mNetworkPreference].isAvailable())
506 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800507
Robert Greenwalt2034b912009-08-12 16:08:25 -0700508 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700509 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700510 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700511 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800512 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700513 " in enforcePreference");
514 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700515 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800516 }
517 }
518 }
519
520 private boolean teardown(NetworkStateTracker netTracker) {
521 if (netTracker.teardown()) {
522 netTracker.setTeardownRequested(true);
523 return true;
524 } else {
525 return false;
526 }
527 }
528
529 /**
530 * Return NetworkInfo for the active (i.e., connected) network interface.
531 * It is assumed that at most one network is active at a time. If more
532 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700533 * @return the info for the active network, or {@code null} if none is
534 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800535 */
536 public NetworkInfo getActiveNetworkInfo() {
537 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700538 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700539 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700540 continue;
541 }
542 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800543 NetworkInfo info = t.getNetworkInfo();
544 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800545 if (DBG && type != mActiveDefaultNetwork) {
546 loge("connected default network is not mActiveDefaultNetwork!");
547 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800548 return info;
549 }
550 }
551 return null;
552 }
553
554 public NetworkInfo getNetworkInfo(int networkType) {
555 enforceAccessPermission();
556 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
557 NetworkStateTracker t = mNetTrackers[networkType];
558 if (t != null)
559 return t.getNetworkInfo();
560 }
561 return null;
562 }
563
564 public NetworkInfo[] getAllNetworkInfo() {
565 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700566 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800567 int i = 0;
568 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700569 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800570 }
571 return result;
572 }
573
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700574 /**
575 * Return LinkProperties for the active (i.e., connected) default
576 * network interface. It is assumed that at most one default network
577 * is active at a time. If more than one is active, it is indeterminate
578 * which will be returned.
579 * @return the ip properties for the active network, or {@code null} if
580 * none is active
581 */
582 public LinkProperties getActiveLinkProperties() {
583 enforceAccessPermission();
584 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
585 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
586 continue;
587 }
588 NetworkStateTracker t = mNetTrackers[type];
589 NetworkInfo info = t.getNetworkInfo();
590 if (info.isConnected()) {
591 return t.getLinkProperties();
592 }
593 }
594 return null;
595 }
596
597 public LinkProperties getLinkProperties(int networkType) {
598 enforceAccessPermission();
599 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
600 NetworkStateTracker t = mNetTrackers[networkType];
601 if (t != null) return t.getLinkProperties();
602 }
603 return null;
604 }
605
The Android Open Source Project28527d22009-03-03 19:31:44 -0800606 public boolean setRadios(boolean turnOn) {
607 boolean result = true;
608 enforceChangePermission();
609 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700610 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800611 }
612 return result;
613 }
614
615 public boolean setRadio(int netType, boolean turnOn) {
616 enforceChangePermission();
617 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
618 return false;
619 }
620 NetworkStateTracker tracker = mNetTrackers[netType];
621 return tracker != null && tracker.setRadio(turnOn);
622 }
623
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700624 /**
625 * Used to notice when the calling process dies so we can self-expire
626 *
627 * Also used to know if the process has cleaned up after itself when
628 * our auto-expire timer goes off. The timer has a link to an object.
629 *
630 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700631 private class FeatureUser implements IBinder.DeathRecipient {
632 int mNetworkType;
633 String mFeature;
634 IBinder mBinder;
635 int mPid;
636 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800637 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700638
639 FeatureUser(int type, String feature, IBinder binder) {
640 super();
641 mNetworkType = type;
642 mFeature = feature;
643 mBinder = binder;
644 mPid = getCallingPid();
645 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800646 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700647
Robert Greenwalt2034b912009-08-12 16:08:25 -0700648 try {
649 mBinder.linkToDeath(this, 0);
650 } catch (RemoteException e) {
651 binderDied();
652 }
653 }
654
655 void unlinkDeathRecipient() {
656 mBinder.unlinkToDeath(this, 0);
657 }
658
659 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800660 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
662 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700663 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700664 }
665
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700666 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800667 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800668 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
669 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700670 stopUsingNetworkFeature(this, false);
671 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800672
673 public String toString() {
674 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
675 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
676 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700677 }
678
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700679 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680 public int startUsingNetworkFeature(int networkType, String feature,
681 IBinder binder) {
682 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800683 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700684 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800685 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700686 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
687 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700688 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800689 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700691 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700692
693 // TODO - move this into the MobileDataStateTracker
694 int usedNetworkType = networkType;
695 if(networkType == ConnectivityManager.TYPE_MOBILE) {
696 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
698 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
699 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800700 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) ||
701 TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700702 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
703 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
704 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
705 }
706 }
707 NetworkStateTracker network = mNetTrackers[usedNetworkType];
708 if (network != null) {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800709 Integer currentPid = new Integer(getCallingPid());
Robert Greenwalt2034b912009-08-12 16:08:25 -0700710 if (usedNetworkType != networkType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700711 NetworkStateTracker radio = mNetTrackers[networkType];
712 NetworkInfo ni = network.getNetworkInfo();
713
714 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800715 if (DBG) log("special network not available");
Robert Greenwalt2cc87442010-12-29 14:35:21 -0800716 if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) {
717 return Phone.APN_TYPE_NOT_AVAILABLE;
718 } else {
719 // else make the attempt anyway - probably giving REQUEST_STARTED below
720 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700721 }
722
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700723 synchronized(this) {
724 mFeatureUsers.add(f);
725 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
726 // this gets used for per-pid dns when connected
727 mNetRequestersPids[usedNetworkType].add(currentPid);
728 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700729 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700730 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700731 f), getRestoreDefaultNetworkDelay());
732
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700734 if ((ni.isConnectedOrConnecting() == true) &&
735 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700736 if (ni.isConnected() == true) {
737 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700738 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800739 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700740 return Phone.APN_ALREADY_ACTIVE;
741 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800742 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700743 return Phone.APN_REQUEST_STARTED;
744 }
745
746 // check if the radio in play can make another contact
747 // assume if cannot for now
748
Wink Savillee70c6f52010-12-03 12:01:38 -0800749 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700750 network.reconnect();
751 return Phone.APN_REQUEST_STARTED;
752 } else {
Robert Greenwalt5364d752010-12-15 13:26:33 -0800753 // need to remember this unsupported request so we respond appropriately on stop
754 synchronized(this) {
755 mFeatureUsers.add(f);
756 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
757 // this gets used for per-pid dns when connected
758 mNetRequestersPids[usedNetworkType].add(currentPid);
759 }
760 }
Robert Greenwaltd391e892010-05-18 10:52:51 -0700761 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700762 }
763 }
764 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800765 }
766
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700767 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800768 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700769 enforceChangePermission();
770
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700771 int pid = getCallingPid();
772 int uid = getCallingUid();
773
774 FeatureUser u = null;
775 boolean found = false;
776
777 synchronized(this) {
778 for (int i = 0; i < mFeatureUsers.size() ; i++) {
779 u = (FeatureUser)mFeatureUsers.get(i);
780 if (uid == u.mUid && pid == u.mPid &&
781 networkType == u.mNetworkType &&
782 TextUtils.equals(feature, u.mFeature)) {
783 found = true;
784 break;
785 }
786 }
787 }
788 if (found && u != null) {
789 // stop regardless of how many other time this proc had called start
790 return stopUsingNetworkFeature(u, true);
791 } else {
792 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800793 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700794 return 1;
795 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700796 }
797
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700798 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
799 int networkType = u.mNetworkType;
800 String feature = u.mFeature;
801 int pid = u.mPid;
802 int uid = u.mUid;
803
804 NetworkStateTracker tracker = null;
805 boolean callTeardown = false; // used to carry our decision outside of sync block
806
Robert Greenwalt2034b912009-08-12 16:08:25 -0700807 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800808 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700809 ": " + feature);
810 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700811
The Android Open Source Project28527d22009-03-03 19:31:44 -0800812 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
813 return -1;
814 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700815
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700816 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
817 // sync block
818 synchronized(this) {
819 // check if this process still has an outstanding start request
820 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800821 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700822 return 1;
823 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700824 u.unlinkDeathRecipient();
825 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
826 // If we care about duplicate requests, check for that here.
827 //
828 // This is done to support the extension of a request - the app
829 // can request we start the network feature again and renew the
830 // auto-shutoff delay. Normal "stop" calls from the app though
831 // do not pay attention to duplicate requests - in effect the
832 // API does not refcount and a single stop will counter multiple starts.
833 if (ignoreDups == false) {
834 for (int i = 0; i < mFeatureUsers.size() ; i++) {
835 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
836 if (x.mUid == u.mUid && x.mPid == u.mPid &&
837 x.mNetworkType == u.mNetworkType &&
838 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800839 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700840 return 1;
841 }
842 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700843 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700844
845 // TODO - move to MobileDataStateTracker
846 int usedNetworkType = networkType;
847 if (networkType == ConnectivityManager.TYPE_MOBILE) {
848 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
849 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
850 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
851 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
852 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
853 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
854 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
855 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
856 }
857 }
858 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700859 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800860 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700861 return -1;
862 }
863 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700864 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700865 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800866 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700867 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800868 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700869 "others still using it");
870 return 1;
871 }
872 callTeardown = true;
873 }
874 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800875 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700876 if (callTeardown) {
877 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700878 return 1;
879 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700880 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700881 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800882 }
883
884 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700885 * @deprecated use requestRouteToHostAddress instead
886 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800887 * Ensure that a network route exists to deliver traffic to the specified
888 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700889 * @param networkType the type of the network over which traffic to the
890 * specified host is to be routed
891 * @param hostAddress the IP address of the host to which the route is
892 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800893 * @return {@code true} on success, {@code false} on failure
894 */
895 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700896 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
897
898 if (inetAddress == null) {
899 return false;
900 }
901
902 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
903 }
904
905 /**
906 * Ensure that a network route exists to deliver traffic to the specified
907 * host via the specified network interface.
908 * @param networkType the type of the network over which traffic to the
909 * specified host is to be routed
910 * @param hostAddress the IP address of the host to which the route is
911 * desired
912 * @return {@code true} on success, {@code false} on failure
913 */
914 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800915 enforceChangePermission();
916 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
917 return false;
918 }
919 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700920
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700921 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
922 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700923 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800924 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700925 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700926 }
927 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800928 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700929 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700930 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700931 return addHostRoute(tracker, addr);
932 } catch (UnknownHostException e) {}
933 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700934 }
935
936 /**
937 * Ensure that a network route exists to deliver traffic to the specified
938 * host via the mobile data network.
939 * @param hostAddress the IP address of the host to which the route is desired,
940 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700941 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700942 * @return {@code true} on success, {@code false} on failure
943 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700944 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700945 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
946 return false;
947 }
948
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700949 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700950 if (p == null) return false;
951 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700952
953 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800954 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700955 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700956 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700957 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700958 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800959 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700960 return false;
961 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800962 }
963
964 /**
965 * @see ConnectivityManager#getBackgroundDataSetting()
966 */
967 public boolean getBackgroundDataSetting() {
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800968 return mBackgroundDataEnabled.get();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800969 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700970
The Android Open Source Project28527d22009-03-03 19:31:44 -0800971 /**
972 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
973 */
974 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
975 mContext.enforceCallingOrSelfPermission(
976 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
977 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700978
Robert Greenwaltd62c7002010-12-29 16:15:02 -0800979 mBackgroundDataEnabled.set(allowBackgroundDataUsage);
980
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700981 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
982 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
983 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800984
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700985 private void handleSetBackgroundData(boolean enabled) {
986 if (enabled != getBackgroundDataSetting()) {
987 Settings.Secure.putInt(mContext.getContentResolver(),
988 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
989 Intent broadcast = new Intent(
990 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
991 mContext.sendBroadcast(broadcast);
992 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700993 }
994
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800995 /**
996 * @see ConnectivityManager#getMobileDataEnabled()
997 */
998 public boolean getMobileDataEnabled() {
Wink Savilleb9024c62010-12-07 10:31:02 -0800999 // TODO: This detail should probably be in DataConnectionTracker's
1000 // which is where we store the value and maybe make this
1001 // asynchronous.
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001002 enforceAccessPermission();
1003 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
1004 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -08001005 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001006 return retVal;
1007 }
1008
1009 /**
1010 * @see ConnectivityManager#setMobileDataEnabled(boolean)
1011 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001012 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001013 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -08001014 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001015
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001016 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
1017 (enabled ? ENABLED : DISABLED), 0));
1018 }
1019
1020 private void handleSetMobileData(boolean enabled) {
Wink Savilleb9024c62010-12-07 10:31:02 -08001021 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
1022 if (DBG) {
1023 Slog.d(TAG, mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001024 }
Wink Savilleb9024c62010-12-07 10:31:02 -08001025 mNetTrackers[ConnectivityManager.TYPE_MOBILE].setDataEnable(enabled);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001026 }
1027 }
1028
The Android Open Source Project28527d22009-03-03 19:31:44 -08001029 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001030 mContext.enforceCallingOrSelfPermission(
1031 android.Manifest.permission.ACCESS_NETWORK_STATE,
1032 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001033 }
1034
1035 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001036 mContext.enforceCallingOrSelfPermission(
1037 android.Manifest.permission.CHANGE_NETWORK_STATE,
1038 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001039 }
1040
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001041 // TODO Make this a special check when it goes public
1042 private void enforceTetherChangePermission() {
1043 mContext.enforceCallingOrSelfPermission(
1044 android.Manifest.permission.CHANGE_NETWORK_STATE,
1045 "ConnectivityService");
1046 }
1047
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001048 private void enforceTetherAccessPermission() {
1049 mContext.enforceCallingOrSelfPermission(
1050 android.Manifest.permission.ACCESS_NETWORK_STATE,
1051 "ConnectivityService");
1052 }
1053
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001054 private void enforceConnectivityInternalPermission() {
1055 mContext.enforceCallingOrSelfPermission(
1056 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1057 "ConnectivityService");
1058 }
1059
The Android Open Source Project28527d22009-03-03 19:31:44 -08001060 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001061 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1062 * network, we ignore it. If it is for the active network, we send out a
1063 * broadcast. But first, we check whether it might be possible to connect
1064 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001065 * @param info the {@code NetworkInfo} for the network
1066 */
1067 private void handleDisconnect(NetworkInfo info) {
1068
Robert Greenwalt2034b912009-08-12 16:08:25 -07001069 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001070
Robert Greenwalt2034b912009-08-12 16:08:25 -07001071 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001072 /*
1073 * If the disconnected network is not the active one, then don't report
1074 * this as a loss of connectivity. What probably happened is that we're
1075 * getting the disconnect for a network that we explicitly disabled
1076 * in accordance with network preference policies.
1077 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001078 if (!mNetAttributes[prevNetType].isDefault()) {
1079 List pids = mNetRequestersPids[prevNetType];
1080 for (int i = 0; i<pids.size(); i++) {
1081 Integer pid = (Integer)pids.get(i);
1082 // will remove them because the net's no longer connected
1083 // need to do this now as only now do we know the pids and
1084 // can properly null things that are no longer referenced.
1085 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001086 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001087 }
1088
The Android Open Source Project28527d22009-03-03 19:31:44 -08001089 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1090 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1091 if (info.isFailover()) {
1092 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1093 info.setFailover(false);
1094 }
1095 if (info.getReason() != null) {
1096 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1097 }
1098 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001099 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1100 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001101 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001102
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001103 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001104 tryFailover(prevNetType);
1105 if (mActiveDefaultNetwork != -1) {
1106 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001107 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1108 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001109 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001110 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1111 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001112 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001113 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001114 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001115 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001116
1117 sendStickyBroadcast(intent);
1118 /*
1119 * If the failover network is already connected, then immediately send
1120 * out a followup broadcast indicating successful failover
1121 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001122 if (mActiveDefaultNetwork != -1) {
1123 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001124 }
1125 }
1126
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001127 private void tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001128 /*
Robert Greenwalt92564852011-01-06 15:41:07 -08001129 * If this is a default network, check if other defaults are available.
1130 * Try to reconnect on all available and let them hash it out when
1131 * more than one connects.
Robert Greenwalt2034b912009-08-12 16:08:25 -07001132 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001133 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001134 if (mActiveDefaultNetwork == prevNetType) {
1135 mActiveDefaultNetwork = -1;
1136 }
1137
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001138 // don't signal a reconnect for anything lower or equal priority than our
1139 // current connected default
1140 // TODO - don't filter by priority now - nice optimization but risky
1141// int currentPriority = -1;
1142// if (mActiveDefaultNetwork != -1) {
1143// currentPriority = mNetAttributes[mActiveDefaultNetwork].mPriority;
1144// }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001145 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001146 if (checkType == prevNetType) continue;
1147 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt92564852011-01-06 15:41:07 -08001148 if (!mNetAttributes[checkType].isDefault()) continue;
1149 if (!mNetTrackers[checkType].isAvailable()) continue;
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001150// if (currentPriority >= mNetAttributes[checkType].mPriority) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001151
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001152 NetworkStateTracker checkTracker = mNetTrackers[checkType];
1153 NetworkInfo checkInfo = checkTracker.getNetworkInfo();
1154 if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) {
1155 checkInfo.setFailover(true);
1156 checkTracker.reconnect();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001157 }
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001158 if (DBG) log("Attempting to switch to " + checkInfo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001159 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001160 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001161 }
1162
1163 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001164 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1165 }
1166
1167 private void sendInetConditionBroadcast(NetworkInfo info) {
1168 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1169 }
1170
1171 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1172 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001173 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1174 if (info.isFailover()) {
1175 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1176 info.setFailover(false);
1177 }
1178 if (info.getReason() != null) {
1179 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1180 }
1181 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001182 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1183 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001184 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001185 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001186 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001187 }
1188
1189 /**
1190 * Called when an attempt to fail over to another network has failed.
1191 * @param info the {@link NetworkInfo} for the failed network
1192 */
1193 private void handleConnectionFailure(NetworkInfo info) {
1194 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001195
Robert Greenwalt2034b912009-08-12 16:08:25 -07001196 String reason = info.getReason();
1197 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001198
Robert Greenwalte981bc52010-10-08 16:35:52 -07001199 String reasonText;
1200 if (reason == null) {
1201 reasonText = ".";
1202 } else {
1203 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001204 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001205 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206
1207 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1208 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1209 if (getActiveNetworkInfo() == null) {
1210 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1211 }
1212 if (reason != null) {
1213 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1214 }
1215 if (extraInfo != null) {
1216 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1217 }
1218 if (info.isFailover()) {
1219 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1220 info.setFailover(false);
1221 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001222
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001223 if (mNetAttributes[info.getType()].isDefault()) {
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001224 tryFailover(info.getType());
1225 if (mActiveDefaultNetwork != -1) {
1226 NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001227 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1228 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001229 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001230 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1231 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001232 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001233
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001234 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001235 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001236 /*
1237 * If the failover network is already connected, then immediately send
1238 * out a followup broadcast indicating successful failover
1239 */
Robert Greenwalt4b7c55e2011-01-11 13:56:33 -08001240 if (mActiveDefaultNetwork != -1) {
1241 sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001242 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001243 }
1244
1245 private void sendStickyBroadcast(Intent intent) {
1246 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001247 if (!mSystemReady) {
1248 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001249 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001250 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1251 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001252 }
1253 }
1254
1255 void systemReady() {
1256 synchronized(this) {
1257 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001258 if (mInitialBroadcast != null) {
1259 mContext.sendStickyBroadcast(mInitialBroadcast);
1260 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001261 }
1262 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001263 // load the global proxy at startup
1264 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001265 }
1266
1267 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001268 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001269
1270 // snapshot isFailover, because sendConnectedBroadcast() resets it
1271 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001272 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001273
Robert Greenwalt2034b912009-08-12 16:08:25 -07001274 // if this is a default net and other default is running
1275 // kill the one not preferred
1276 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001277 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1278 if ((type != mNetworkPreference &&
1279 mNetAttributes[mActiveDefaultNetwork].mPriority >
1280 mNetAttributes[type].mPriority) ||
1281 mNetworkPreference == mActiveDefaultNetwork) {
1282 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001283 if (DBG) {
1284 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001285 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001286 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001287 teardown(thisNet);
1288 return;
1289 } else {
1290 // tear down the other
1291 NetworkStateTracker otherNet =
1292 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001293 if (DBG) {
1294 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001295 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001296 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001297 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001298 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001299 return;
1300 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001301 }
1302 }
1303 synchronized (ConnectivityService.this) {
1304 // have a new default network, release the transition wakelock in a second
1305 // if it's held. The second pause is to allow apps to reconnect over the
1306 // new network
1307 if (mNetTransitionWakeLock.isHeld()) {
1308 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001309 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001310 mNetTransitionWakeLockSerialNumber, 0),
1311 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001312 }
1313 }
1314 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001315 // this will cause us to come up initially as unconnected and switching
1316 // to connected after our normal pause unless somebody reports us as reall
1317 // disconnected
1318 mDefaultInetConditionPublished = 0;
1319 mDefaultConnectionSequence++;
1320 mInetConditionChangeInFlight = false;
1321 // Don't do this - if we never sign in stay, grey
1322 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001323 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001325 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001326 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001327 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001328 }
1329
The Android Open Source Project28527d22009-03-03 19:31:44 -08001330 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001331 * After a change in the connectivity state of a network. We're mainly
1332 * concerned with making sure that the list of DNS servers is set up
1333 * according to which networks are connected, and ensuring that the
1334 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001335 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001336 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001337 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001338 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001339 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001340 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001341 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001342
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001343 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1344 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001345 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001346 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001347 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001348 addPrivateDnsRoutes(mNetTrackers[netType]);
1349 }
1350 } else {
1351 if (mNetAttributes[netType].isDefault()) {
1352 removeDefaultRoute(mNetTrackers[netType]);
1353 } else {
1354 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001355 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001356 }
1357 }
1358
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001359 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001360 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001361 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001362 if (p == null) return;
1363 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001364
1365 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001366 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001367 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1368 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001369 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001370 Collection<InetAddress> dnsList = p.getDnses();
1371 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001372 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001373 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001374 }
1375 nt.privateDnsRouteSet(true);
1376 }
1377 }
1378
1379 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1380 // TODO - we should do this explicitly but the NetUtils api doesnt
1381 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001382 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001383 if (p == null) return;
1384 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001385 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1386 if (interfaceName != null && privateDnsRouteSet) {
1387 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001388 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001389 " (" + interfaceName + ")");
1390 }
1391 NetworkUtils.removeHostRoutes(interfaceName);
1392 nt.privateDnsRouteSet(false);
1393 }
1394 }
1395
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001396
1397 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001398 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001399 if (p == null) return;
1400 String interfaceName = p.getInterfaceName();
1401 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001402
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001403 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001404 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001405 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001406 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001407 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1408 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001409 }
1410 }
1411
1412
1413 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001414 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001415 if (p == null) return;
1416 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001417
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001418 if (interfaceName != null) {
1419 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001420 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001421 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422 interfaceName + ")");
1423 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001424 }
1425 }
1426
1427 /**
1428 * Reads the network specific TCP buffer sizes from SystemProperties
1429 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1430 * wide use
1431 */
1432 public void updateNetworkSettings(NetworkStateTracker nt) {
1433 String key = nt.getTcpBufferSizesPropName();
1434 String bufferSizes = SystemProperties.get(key);
1435
1436 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001437 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001438
1439 // Setting to default values so we won't be stuck to previous values
1440 key = "net.tcp.buffersize.default";
1441 bufferSizes = SystemProperties.get(key);
1442 }
1443
1444 // Set values in kernel
1445 if (bufferSizes.length() != 0) {
1446 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001447 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001448 + "] which comes from [" + key + "]");
1449 }
1450 setBufferSize(bufferSizes);
1451 }
1452 }
1453
1454 /**
1455 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1456 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1457 *
1458 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1459 * writeMin, writeInitial, writeMax"
1460 */
1461 private void setBufferSize(String bufferSizes) {
1462 try {
1463 String[] values = bufferSizes.split(",");
1464
1465 if (values.length == 6) {
1466 final String prefix = "/sys/kernel/ipv4/tcp_";
1467 stringToFile(prefix + "rmem_min", values[0]);
1468 stringToFile(prefix + "rmem_def", values[1]);
1469 stringToFile(prefix + "rmem_max", values[2]);
1470 stringToFile(prefix + "wmem_min", values[3]);
1471 stringToFile(prefix + "wmem_def", values[4]);
1472 stringToFile(prefix + "wmem_max", values[5]);
1473 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001474 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001475 }
1476 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001477 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001478 }
1479 }
1480
1481 /**
1482 * Writes string to file. Basically same as "echo -n $string > $filename"
1483 *
1484 * @param filename
1485 * @param string
1486 * @throws IOException
1487 */
1488 private void stringToFile(String filename, String string) throws IOException {
1489 FileWriter out = new FileWriter(filename);
1490 try {
1491 out.write(string);
1492 } finally {
1493 out.close();
1494 }
1495 }
1496
1497
Robert Greenwalt2034b912009-08-12 16:08:25 -07001498 /**
1499 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1500 * on the highest priority active net which this process requested.
1501 * If there aren't any, clear it out
1502 */
1503 private void reassessPidDns(int myPid, boolean doBump)
1504 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001505 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001506 for(int i : mPriorityList) {
1507 if (mNetAttributes[i].isDefault()) {
1508 continue;
1509 }
1510 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001511 if (nt.getNetworkInfo().isConnected() &&
1512 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001513 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001514 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001515 List pids = mNetRequestersPids[i];
1516 for (int j=0; j<pids.size(); j++) {
1517 Integer pid = (Integer)pids.get(j);
1518 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001519 Collection<InetAddress> dnses = p.getDnses();
1520 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001521 if (doBump) {
1522 bumpDns();
1523 }
1524 return;
1525 }
1526 }
1527 }
1528 }
1529 // nothing found - delete
1530 for (int i = 1; ; i++) {
1531 String prop = "net.dns" + i + "." + myPid;
1532 if (SystemProperties.get(prop).length() == 0) {
1533 if (doBump) {
1534 bumpDns();
1535 }
1536 return;
1537 }
1538 SystemProperties.set(prop, "");
1539 }
1540 }
1541
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001542 // return true if results in a change
1543 private boolean writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001544 int j = 1;
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001545 boolean changed = false;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001546 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001547 String dnsString = dns.getHostAddress();
1548 if (changed || !dnsString.equals(SystemProperties.get("net.dns" + j + "." + pid))) {
1549 changed = true;
1550 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
1551 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001552 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001553 return changed;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001554 }
1555
1556 private void bumpDns() {
1557 /*
1558 * Bump the property that tells the name resolver library to reread
1559 * the DNS server list from the properties.
1560 */
1561 String propVal = SystemProperties.get("net.dnschange");
1562 int n = 0;
1563 if (propVal.length() != 0) {
1564 try {
1565 n = Integer.parseInt(propVal);
1566 } catch (NumberFormatException e) {}
1567 }
1568 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001569 /*
1570 * Tell the VMs to toss their DNS caches
1571 */
1572 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1573 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Stan Chesnuttf444f502011-01-05 17:14:03 -08001574 /*
1575 * Connectivity events can happen before boot has completed ...
1576 */
1577 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Robert Greenwalt051642b2010-11-02 14:08:23 -07001578 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001579 }
1580
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001581 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001582 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001583 NetworkStateTracker nt = mNetTrackers[netType];
1584 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001585 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001586 if (p == null) return;
1587 Collection<InetAddress> dnses = p.getDnses();
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001588 boolean changed = false;
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001589 if (mNetAttributes[netType].isDefault()) {
1590 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001591 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001592 String dnsString = mDefaultDns.getHostAddress();
1593 if (!dnsString.equals(SystemProperties.get("net.dns1"))) {
1594 if (DBG) {
1595 log("no dns provided - using " + dnsString);
1596 }
1597 changed = true;
1598 SystemProperties.set("net.dns1", dnsString);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001599 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001600 j++;
1601 } else {
1602 for (InetAddress dns : dnses) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001603 String dnsString = dns.getHostAddress();
1604 if (!changed && dnsString.equals(SystemProperties.get("net.dns" + j))) {
1605 j++;
1606 continue;
1607 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001608 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001609 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001610 nt.getNetworkInfo().getTypeName());
1611 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001612 changed = true;
1613 SystemProperties.set("net.dns" + j++, dnsString);
Robert Greenwalt94daa182010-09-01 11:34:05 -07001614 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001615 }
1616 for (int k=j ; k<mNumDnsEntries; k++) {
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001617 if (changed || !TextUtils.isEmpty(SystemProperties.get("net.dns" + k))) {
1618 if (DBG) log("erasing net.dns" + k);
1619 changed = true;
1620 SystemProperties.set("net.dns" + k, "");
1621 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001622 }
1623 mNumDnsEntries = j;
1624 } else {
1625 // set per-pid dns for attached secondary nets
1626 List pids = mNetRequestersPids[netType];
1627 for (int y=0; y< pids.size(); y++) {
1628 Integer pid = (Integer)pids.get(y);
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001629 changed = writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001630 }
1631 }
Robert Greenwalt8ca88762010-12-17 15:20:36 -08001632 if (changed) bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001633 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001634 }
1635
1636 private int getRestoreDefaultNetworkDelay() {
1637 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1638 NETWORK_RESTORE_DELAY_PROP_NAME);
1639 if(restoreDefaultNetworkDelayStr != null &&
1640 restoreDefaultNetworkDelayStr.length() != 0) {
1641 try {
1642 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1643 } catch (NumberFormatException e) {
1644 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001645 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001646 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001647 }
1648
1649 @Override
1650 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001651 if (mContext.checkCallingOrSelfPermission(
1652 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001653 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001654 pw.println("Permission Denial: can't dump ConnectivityService " +
1655 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1656 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001657 return;
1658 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001659 pw.println();
1660 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001661 if (nst != null) {
1662 if (nst.getNetworkInfo().isConnected()) {
1663 pw.println("Active network: " + nst.getNetworkInfo().
1664 getTypeName());
1665 }
1666 pw.println(nst.getNetworkInfo());
1667 pw.println(nst);
1668 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001669 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001670 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001671
1672 pw.println("Network Requester Pids:");
1673 for (int net : mPriorityList) {
1674 String pidString = net + ": ";
1675 for (Object pid : mNetRequestersPids[net]) {
1676 pidString = pidString + pid.toString() + ", ";
1677 }
1678 pw.println(pidString);
1679 }
1680 pw.println();
1681
1682 pw.println("FeatureUsers:");
1683 for (Object requester : mFeatureUsers) {
1684 pw.println(requester.toString());
1685 }
1686 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001687
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001688 synchronized (this) {
1689 pw.println("NetworkTranstionWakeLock is currently " +
1690 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1691 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1692 }
1693 pw.println();
1694
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001695 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001696
1697 if (mInetLog != null) {
1698 pw.println();
1699 pw.println("Inet condition reports:");
1700 for(int i = 0; i < mInetLog.size(); i++) {
1701 pw.println(mInetLog.get(i));
1702 }
1703 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001704 }
1705
Robert Greenwalt2034b912009-08-12 16:08:25 -07001706 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001707 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001708 public MyHandler(Looper looper) {
1709 super(looper);
1710 }
1711
The Android Open Source Project28527d22009-03-03 19:31:44 -08001712 @Override
1713 public void handleMessage(Message msg) {
1714 NetworkInfo info;
1715 switch (msg.what) {
1716 case NetworkStateTracker.EVENT_STATE_CHANGED:
1717 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001718 int type = info.getType();
1719 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001720 // only do this optimization for wifi. It going into scan mode for location
1721 // services generates alot of noise. Meanwhile the mms apn won't send out
1722 // subsequent notifications when on default cellular because it never
1723 // disconnects.. so only do this to wifi notifications. Fixed better when the
1724 // APN notifications are standardized.
1725 if (mNetAttributes[type].mLastState == state &&
1726 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001727 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001728 // TODO - remove this after we validate the dropping doesn't break
1729 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001730 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001731 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001732 state + "/" + info.getDetailedState());
1733 }
1734 return;
1735 }
1736 mNetAttributes[type].mLastState = state;
1737
Wink Savillee70c6f52010-12-03 12:01:38 -08001738 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001739 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001740 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001741
1742 // Connectivity state changed:
1743 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001744 // [12-9] Network subtype (for mobile network, as defined
1745 // by TelephonyManager)
1746 // [8-3] Detailed state ordinal (as defined by
1747 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001748 // [2-0] Network type (as defined by ConnectivityManager)
1749 int eventLogParam = (info.getType() & 0x7) |
1750 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1751 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001752 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001753 eventLogParam);
1754
1755 if (info.getDetailedState() ==
1756 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001757 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001758 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001759 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001760 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001761 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001762 // the logic here is, handle SUSPENDED the same as
1763 // DISCONNECTED. The only difference being we are
1764 // broadcasting an intent with NetworkInfo that's
1765 // suspended. This allows the applications an
1766 // opportunity to handle DISCONNECTED and SUSPENDED
1767 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001768 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001769 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001770 handleConnect(info);
1771 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001772 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001773 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001774 info = (NetworkInfo) msg.obj;
1775 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001776 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001777 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001778 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001779 String causedBy = null;
1780 synchronized (ConnectivityService.this) {
1781 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1782 mNetTransitionWakeLock.isHeld()) {
1783 mNetTransitionWakeLock.release();
1784 causedBy = mNetTransitionWakeLockCausedBy;
1785 }
1786 }
1787 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001788 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001789 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001790 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001791 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001792 FeatureUser u = (FeatureUser)msg.obj;
1793 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001794 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001795 case EVENT_INET_CONDITION_CHANGE:
1796 {
1797 int netType = msg.arg1;
1798 int condition = msg.arg2;
1799 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001800 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001801 }
1802 case EVENT_INET_CONDITION_HOLD_END:
1803 {
1804 int netType = msg.arg1;
1805 int sequence = msg.arg2;
1806 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001807 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001808 }
1809 case EVENT_SET_NETWORK_PREFERENCE:
1810 {
1811 int preference = msg.arg1;
1812 handleSetNetworkPreference(preference);
1813 break;
1814 }
1815 case EVENT_SET_BACKGROUND_DATA:
1816 {
1817 boolean enabled = (msg.arg1 == ENABLED);
1818 handleSetBackgroundData(enabled);
1819 break;
1820 }
1821 case EVENT_SET_MOBILE_DATA:
1822 {
1823 boolean enabled = (msg.arg1 == ENABLED);
1824 handleSetMobileData(enabled);
1825 break;
1826 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001827 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1828 {
1829 handleDeprecatedGlobalHttpProxy();
1830 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001831 }
1832 }
1833 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001834
1835 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001836 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001837 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001838
1839 if (isTetheringSupported()) {
1840 return mTethering.tether(iface);
1841 } else {
1842 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1843 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001844 }
1845
1846 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001847 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001848 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001849
1850 if (isTetheringSupported()) {
1851 return mTethering.untether(iface);
1852 } else {
1853 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1854 }
1855 }
1856
1857 // javadoc from interface
1858 public int getLastTetherError(String iface) {
1859 enforceTetherAccessPermission();
1860
1861 if (isTetheringSupported()) {
1862 return mTethering.getLastTetherError(iface);
1863 } else {
1864 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1865 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001866 }
1867
1868 // TODO - proper iface API for selection by property, inspection, etc
1869 public String[] getTetherableUsbRegexs() {
1870 enforceTetherAccessPermission();
1871 if (isTetheringSupported()) {
1872 return mTethering.getTetherableUsbRegexs();
1873 } else {
1874 return new String[0];
1875 }
1876 }
1877
1878 public String[] getTetherableWifiRegexs() {
1879 enforceTetherAccessPermission();
1880 if (isTetheringSupported()) {
1881 return mTethering.getTetherableWifiRegexs();
1882 } else {
1883 return new String[0];
1884 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001885 }
1886
Danica Chang96567052010-08-11 14:54:43 -07001887 public String[] getTetherableBluetoothRegexs() {
1888 enforceTetherAccessPermission();
1889 if (isTetheringSupported()) {
1890 return mTethering.getTetherableBluetoothRegexs();
1891 } else {
1892 return new String[0];
1893 }
1894 }
1895
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001896 // TODO - move iface listing, queries, etc to new module
1897 // javadoc from interface
1898 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001899 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001900 return mTethering.getTetherableIfaces();
1901 }
1902
1903 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001904 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001905 return mTethering.getTetheredIfaces();
1906 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001907
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001908 public String[] getTetheringErroredIfaces() {
1909 enforceTetherAccessPermission();
1910 return mTethering.getErroredIfaces();
1911 }
1912
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001913 // if ro.tether.denied = true we default to no tethering
1914 // gservices could set the secure setting to 1 though to enable it on a build where it
1915 // had previously been turned off.
1916 public boolean isTetheringSupported() {
1917 enforceTetherAccessPermission();
1918 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001919 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1920 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1921 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001922 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001923
1924 // An API NetworkStateTrackers can call when they lose their network.
1925 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1926 // whichever happens first. The timer is started by the first caller and not
1927 // restarted by subsequent callers.
1928 public void requestNetworkTransitionWakelock(String forWhom) {
1929 enforceConnectivityInternalPermission();
1930 synchronized (this) {
1931 if (mNetTransitionWakeLock.isHeld()) return;
1932 mNetTransitionWakeLockSerialNumber++;
1933 mNetTransitionWakeLock.acquire();
1934 mNetTransitionWakeLockCausedBy = forWhom;
1935 }
1936 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001937 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001938 mNetTransitionWakeLockSerialNumber, 0),
1939 mNetTransitionWakeLockTimeout);
1940 return;
1941 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001942
Robert Greenwalt986c7412010-09-08 15:24:47 -07001943 // 100 percent is full good, 0 is full bad.
1944 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001945 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001946 mContext.enforceCallingOrSelfPermission(
1947 android.Manifest.permission.STATUS_BAR,
1948 "ConnectivityService");
1949
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001950 if (DBG) {
1951 int pid = getCallingPid();
1952 int uid = getCallingUid();
1953 String s = pid + "(" + uid + ") reports inet is " +
1954 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1955 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1956 mInetLog.add(s);
1957 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1958 mInetLog.remove(0);
1959 }
1960 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001961 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001962 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1963 }
1964
1965 private void handleInetConditionChange(int netType, int condition) {
1966 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001967 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001968 netType + ", condition=" + condition +
1969 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
1970 }
1971 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001972 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001973 return;
1974 }
1975 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001976 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001977 return;
1978 }
1979 mDefaultInetCondition = condition;
1980 int delay;
1981 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001982 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001983 // setup a new hold to debounce this
1984 if (mDefaultInetCondition > 50) {
1985 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1986 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
1987 } else {
1988 delay = Settings.Secure.getInt(mContext.getContentResolver(),
1989 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
1990 }
1991 mInetConditionChangeInFlight = true;
1992 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
1993 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
1994 } else {
1995 // we've set the new condition, when this hold ends that will get
1996 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08001997 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001998 }
1999 }
2000
2001 private void handleInetConditionHoldEnd(int netType, int sequence) {
2002 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002003 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002004 ", condition =" + mDefaultInetCondition +
2005 ", published condition =" + mDefaultInetConditionPublished);
2006 }
2007 mInetConditionChangeInFlight = false;
2008
2009 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002010 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002011 return;
2012 }
2013 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002014 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002015 return;
2016 }
2017 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002018 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002019 return;
2020 }
2021 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2022 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002023 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002024 return;
2025 }
2026 mDefaultInetConditionPublished = mDefaultInetCondition;
2027 sendInetConditionBroadcast(networkInfo);
2028 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002029 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002030
2031 public synchronized ProxyProperties getProxy() {
2032 if (mGlobalProxy != null) return mGlobalProxy;
2033 if (mDefaultProxy != null) return mDefaultProxy;
2034 return null;
2035 }
2036
2037 public void setGlobalProxy(ProxyProperties proxyProperties) {
2038 enforceChangePermission();
2039 synchronized (mGlobalProxyLock) {
2040 if (proxyProperties == mGlobalProxy) return;
2041 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2042 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2043
2044 String host = "";
2045 int port = 0;
2046 String exclList = "";
2047 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2048 mGlobalProxy = new ProxyProperties(proxyProperties);
2049 host = mGlobalProxy.getHost();
2050 port = mGlobalProxy.getPort();
2051 exclList = mGlobalProxy.getExclusionList();
2052 } else {
2053 mGlobalProxy = null;
2054 }
2055 ContentResolver res = mContext.getContentResolver();
2056 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2057 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002058 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002059 exclList);
2060 }
2061
2062 if (mGlobalProxy == null) {
2063 proxyProperties = mDefaultProxy;
2064 }
2065 sendProxyBroadcast(proxyProperties);
2066 }
2067
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002068 private void loadGlobalProxy() {
2069 ContentResolver res = mContext.getContentResolver();
2070 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2071 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2072 String exclList = Settings.Secure.getString(res,
2073 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2074 if (!TextUtils.isEmpty(host)) {
2075 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2076 synchronized (mGlobalProxyLock) {
2077 mGlobalProxy = proxyProperties;
2078 }
2079 }
2080 }
2081
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002082 public ProxyProperties getGlobalProxy() {
2083 synchronized (mGlobalProxyLock) {
2084 return mGlobalProxy;
2085 }
2086 }
2087
2088 private void handleApplyDefaultProxy(int type) {
2089 // check if new default - push it out to all VM if so
2090 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2091 synchronized (this) {
2092 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2093 if (mDefaultProxy == proxy) return;
2094 if (!TextUtils.isEmpty(proxy.getHost())) {
2095 mDefaultProxy = proxy;
2096 } else {
2097 mDefaultProxy = null;
2098 }
2099 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002100 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002101 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2102 if (mGlobalProxy != null) return;
2103 sendProxyBroadcast(proxy);
2104 }
2105
2106 private void handleDeprecatedGlobalHttpProxy() {
2107 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2108 Settings.Secure.HTTP_PROXY);
2109 if (!TextUtils.isEmpty(proxy)) {
2110 String data[] = proxy.split(":");
2111 String proxyHost = data[0];
2112 int proxyPort = 8080;
2113 if (data.length > 1) {
2114 try {
2115 proxyPort = Integer.parseInt(data[1]);
2116 } catch (NumberFormatException e) {
2117 return;
2118 }
2119 }
2120 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2121 setGlobalProxy(p);
2122 }
2123 }
2124
2125 private void sendProxyBroadcast(ProxyProperties proxy) {
Robert Greenwalt611291c2010-12-23 15:51:10 -08002126 if (proxy == null) proxy = new ProxyProperties("", 0, "");
Wink Savillee70c6f52010-12-03 12:01:38 -08002127 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002128 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2129 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2130 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
Robert Greenwaltd93dc8f2010-12-06 11:29:17 -08002131 mContext.sendStickyBroadcast(intent);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002132 }
2133
2134 private static class SettingsObserver extends ContentObserver {
2135 private int mWhat;
2136 private Handler mHandler;
2137 SettingsObserver(Handler handler, int what) {
2138 super(handler);
2139 mHandler = handler;
2140 mWhat = what;
2141 }
2142
2143 void observe(Context context) {
2144 ContentResolver resolver = context.getContentResolver();
2145 resolver.registerContentObserver(Settings.Secure.getUriFor(
2146 Settings.Secure.HTTP_PROXY), false, this);
2147 }
2148
2149 @Override
2150 public void onChange(boolean selfChange) {
2151 mHandler.obtainMessage(mWhat).sendToTarget();
2152 }
2153 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002154
2155 private void log(String s) {
2156 Slog.d(TAG, s);
2157 }
2158
2159 private void loge(String s) {
2160 Slog.e(TAG, s);
2161 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002162}