blob: 7e76ea5f4a889422d0ea2791e3aa91083f244520 [file] [log] [blame]
The Android Open Source Project28527d22009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070025import android.database.ContentObserver;
The Android Open Source Project28527d22009-03-03 19:31:44 -080026import android.net.ConnectivityManager;
27import android.net.IConnectivityManager;
28import android.net.MobileDataStateTracker;
29import android.net.NetworkInfo;
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -070030import android.net.LinkProperties;
The Android Open Source Project28527d22009-03-03 19:31:44 -080031import 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;
52
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080053import com.android.server.connectivity.Tethering;
54
The Android Open Source Project28527d22009-03-03 19:31:44 -080055import java.io.FileDescriptor;
Irfan Sheriff7f132d92010-06-09 15:39:36 -070056import java.io.FileWriter;
57import java.io.IOException;
The Android Open Source Project28527d22009-03-03 19:31:44 -080058import java.io.PrintWriter;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070059import java.net.InetAddress;
Robert Greenwaltc3c5f862010-10-11 16:00:27 -070060import java.net.InetSocketAddress;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070061import java.net.UnknownHostException;
Robert Greenwalt2034b912009-08-12 16:08:25 -070062import java.util.ArrayList;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -070063import java.util.Collection;
Robert Greenwalt0e80be12010-09-20 14:35:25 -070064import java.util.GregorianCalendar;
Robert Greenwalt2034b912009-08-12 16:08:25 -070065import java.util.List;
The Android Open Source Project28527d22009-03-03 19:31:44 -080066
67/**
68 * @hide
69 */
70public class ConnectivityService extends IConnectivityManager.Stub {
71
Robert Greenwalt063dc7d2010-10-05 19:12:26 -070072 private static final boolean DBG = true;
The Android Open Source Project28527d22009-03-03 19:31:44 -080073 private static final String TAG = "ConnectivityService";
74
Robert Greenwalt2034b912009-08-12 16:08:25 -070075 // how long to wait before switching back to a radio's default network
76 private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000;
77 // system property that can override the above value
78 private static final String NETWORK_RESTORE_DELAY_PROP_NAME =
79 "android.telephony.apn-restore";
80
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080081 private Tethering mTethering;
Robert Greenwaltf1b66e12010-02-25 12:29:30 -080082 private boolean mTetheringConfigValid = false;
Robert Greenwalt0c4828c2010-01-26 11:40:34 -080083
The Android Open Source Project28527d22009-03-03 19:31:44 -080084 /**
85 * Sometimes we want to refer to the individual network state
86 * trackers separately, and sometimes we just want to treat them
87 * abstractly.
88 */
89 private NetworkStateTracker mNetTrackers[];
Robert Greenwalt2034b912009-08-12 16:08:25 -070090
91 /**
92 * A per Net list of the PID's that requested access to the net
93 * used both as a refcount and for per-PID DNS selection
94 */
95 private List mNetRequestersPids[];
96
Irfan Sheriff653e2a22010-06-07 09:03:04 -070097 private WifiWatchdogService mWifiWatchdogService;
98
Robert Greenwalt2034b912009-08-12 16:08:25 -070099 // priority order of the nettrackers
100 // (excluding dynamically set mNetworkPreference)
101 // TODO - move mNetworkTypePreference into this
102 private int[] mPriorityList;
103
The Android Open Source Project28527d22009-03-03 19:31:44 -0800104 private Context mContext;
105 private int mNetworkPreference;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700106 private int mActiveDefaultNetwork = -1;
Robert Greenwalt986c7412010-09-08 15:24:47 -0700107 // 0 is full bad, 100 is full good
108 private int mDefaultInetCondition = 0;
109 private int mDefaultInetConditionPublished = 0;
110 private boolean mInetConditionChangeInFlight = false;
111 private int mDefaultConnectionSequence = 0;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800112
113 private int mNumDnsEntries;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800114
115 private boolean mTestMode;
Joe Onorato56023ad2010-09-01 21:18:22 -0700116 private static ConnectivityService sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800117
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700118 private static final int ENABLED = 1;
119 private static final int DISABLED = 0;
120
121 // Share the event space with NetworkStateTracker (which can't see this
122 // internal class but sends us events). If you change these, change
123 // NetworkStateTracker.java too.
124 private static final int MIN_NETWORK_STATE_TRACKER_EVENT = 1;
125 private static final int MAX_NETWORK_STATE_TRACKER_EVENT = 100;
126
127 /**
128 * used internally as a delayed event to make us switch back to the
129 * default network
130 */
131 private static final int EVENT_RESTORE_DEFAULT_NETWORK =
132 MAX_NETWORK_STATE_TRACKER_EVENT + 1;
133
134 /**
135 * used internally to change our mobile data enabled flag
136 */
137 private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED =
138 MAX_NETWORK_STATE_TRACKER_EVENT + 2;
139
140 /**
141 * used internally to change our network preference setting
142 * arg1 = networkType to prefer
143 */
144 private static final int EVENT_SET_NETWORK_PREFERENCE =
145 MAX_NETWORK_STATE_TRACKER_EVENT + 3;
146
147 /**
148 * used internally to synchronize inet condition reports
149 * arg1 = networkType
150 * arg2 = condition (0 bad, 100 good)
151 */
152 private static final int EVENT_INET_CONDITION_CHANGE =
153 MAX_NETWORK_STATE_TRACKER_EVENT + 4;
154
155 /**
156 * used internally to mark the end of inet condition hold periods
157 * arg1 = networkType
158 */
159 private static final int EVENT_INET_CONDITION_HOLD_END =
160 MAX_NETWORK_STATE_TRACKER_EVENT + 5;
161
162 /**
163 * used internally to set the background data preference
164 * arg1 = TRUE for enabled, FALSE for disabled
165 */
166 private static final int EVENT_SET_BACKGROUND_DATA =
167 MAX_NETWORK_STATE_TRACKER_EVENT + 6;
168
169 /**
170 * used internally to set enable/disable cellular data
171 * arg1 = ENBALED or DISABLED
172 */
173 private static final int EVENT_SET_MOBILE_DATA =
174 MAX_NETWORK_STATE_TRACKER_EVENT + 7;
175
Robert Greenwaltccb36f92010-09-24 14:32:21 -0700176 /**
177 * used internally to clear a wakelock when transitioning
178 * from one net to another
179 */
180 private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK =
181 MAX_NETWORK_STATE_TRACKER_EVENT + 8;
182
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700183 /**
184 * used internally to reload global proxy settings
185 */
186 private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY =
187 MAX_NETWORK_STATE_TRACKER_EVENT + 9;
188
Robert Greenwalt2034b912009-08-12 16:08:25 -0700189 private Handler mHandler;
190
191 // list of DeathRecipients used to make sure features are turned off when
192 // a process dies
193 private List mFeatureUsers;
194
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400195 private boolean mSystemReady;
Dianne Hackborna417ff82009-12-08 19:45:14 -0800196 private Intent mInitialBroadcast;
Mike Lockwoodfde2b762009-08-14 14:18:49 -0400197
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700198 private PowerManager.WakeLock mNetTransitionWakeLock;
199 private String mNetTransitionWakeLockCausedBy = "";
200 private int mNetTransitionWakeLockSerialNumber;
201 private int mNetTransitionWakeLockTimeout;
202
Robert Greenwalt94daa182010-09-01 11:34:05 -0700203 private InetAddress mDefaultDns;
204
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700205 // used in DBG mode to track inet condition reports
206 private static final int INET_CONDITION_LOG_MAX_SIZE = 15;
207 private ArrayList mInetLog;
208
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700209 // track the current default http proxy - tell the world if we get a new one (real change)
210 private ProxyProperties mDefaultProxy = null;
211 // track the global proxy.
212 private ProxyProperties mGlobalProxy = null;
213 private final Object mGlobalProxyLock = new Object();
214
215 private SettingsObserver mSettingsObserver;
216
Robert Greenwalt12c44552009-12-07 11:33:18 -0800217 private static class NetworkAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700218 /**
219 * Class for holding settings read from resources.
220 */
221 public String mName;
222 public int mType;
223 public int mRadio;
224 public int mPriority;
Robert Greenwalt12c44552009-12-07 11:33:18 -0800225 public NetworkInfo.State mLastState;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700226 public NetworkAttributes(String init) {
227 String fragments[] = init.split(",");
228 mName = fragments[0].toLowerCase();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700229 mType = Integer.parseInt(fragments[1]);
230 mRadio = Integer.parseInt(fragments[2]);
231 mPriority = Integer.parseInt(fragments[3]);
Robert Greenwalt12c44552009-12-07 11:33:18 -0800232 mLastState = NetworkInfo.State.UNKNOWN;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700233 }
234 public boolean isDefault() {
235 return (mType == mRadio);
236 }
237 }
238 NetworkAttributes[] mNetAttributes;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700239 int mNetworksDefined;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700240
Robert Greenwalt12c44552009-12-07 11:33:18 -0800241 private static class RadioAttributes {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700242 public int mSimultaneity;
243 public int mType;
244 public RadioAttributes(String init) {
245 String fragments[] = init.split(",");
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700246 mType = Integer.parseInt(fragments[0]);
247 mSimultaneity = Integer.parseInt(fragments[1]);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700248 }
249 }
250 RadioAttributes[] mRadioAttributes;
251
Wink Saville775aad62010-09-02 19:23:52 -0700252 public static synchronized ConnectivityService getInstance(Context context) {
253 if (sServiceInstance == null) {
254 sServiceInstance = new ConnectivityService(context);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800255 }
Wink Saville775aad62010-09-02 19:23:52 -0700256 return sServiceInstance;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800257 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700258
The Android Open Source Project28527d22009-03-03 19:31:44 -0800259 private ConnectivityService(Context context) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800260 if (DBG) log("ConnectivityService starting up");
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800261
Wink Saville775aad62010-09-02 19:23:52 -0700262 HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread");
263 handlerThread.start();
264 mHandler = new MyHandler(handlerThread.getLooper());
265
Robert Greenwaltd48f8ee2010-01-14 17:47:58 -0800266 // setup our unique device name
267 String id = Settings.Secure.getString(context.getContentResolver(),
268 Settings.Secure.ANDROID_ID);
269 if (id != null && id.length() > 0) {
270 String name = new String("android_").concat(id);
271 SystemProperties.set("net.hostname", name);
272 }
273
Robert Greenwalt94daa182010-09-01 11:34:05 -0700274 // read our default dns server ip
275 String dns = Settings.Secure.getString(context.getContentResolver(),
276 Settings.Secure.DEFAULT_DNS_SERVER);
277 if (dns == null || dns.length() == 0) {
278 dns = context.getResources().getString(
279 com.android.internal.R.string.config_default_dns_server);
280 }
281 try {
282 mDefaultDns = InetAddress.getByName(dns);
283 } catch (UnknownHostException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800284 loge("Error setting defaultDns using " + dns);
Robert Greenwalt94daa182010-09-01 11:34:05 -0700285 }
286
The Android Open Source Project28527d22009-03-03 19:31:44 -0800287 mContext = context;
Robert Greenwalt93dc1042010-06-15 12:19:37 -0700288
289 PowerManager powerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
290 mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
291 mNetTransitionWakeLockTimeout = mContext.getResources().getInteger(
292 com.android.internal.R.integer.config_networkTransitionTimeout);
293
Robert Greenwalt2034b912009-08-12 16:08:25 -0700294 mNetTrackers = new NetworkStateTracker[
295 ConnectivityManager.MAX_NETWORK_TYPE+1];
Robert Greenwalt0659da32009-07-16 17:21:39 -0700296
The Android Open Source Project28527d22009-03-03 19:31:44 -0800297 mNetworkPreference = getPersistedNetworkPreference();
Robert Greenwalt0659da32009-07-16 17:21:39 -0700298
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700299 mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1];
300 mNetAttributes = new NetworkAttributes[ConnectivityManager.MAX_NETWORK_TYPE+1];
301
Robert Greenwalt2034b912009-08-12 16:08:25 -0700302 // Load device network attributes from resources
Robert Greenwalt2034b912009-08-12 16:08:25 -0700303 String[] raStrings = context.getResources().getStringArray(
304 com.android.internal.R.array.radioAttributes);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700305 for (String raString : raStrings) {
306 RadioAttributes r = new RadioAttributes(raString);
307 if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800308 loge("Error in radioAttributes - ignoring attempt to define type " + r.mType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700309 continue;
310 }
311 if (mRadioAttributes[r.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800312 loge("Error in radioAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700313 r.mType);
314 continue;
315 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700316 mRadioAttributes[r.mType] = r;
317 }
318
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700319 String[] naStrings = context.getResources().getStringArray(
320 com.android.internal.R.array.networkAttributes);
321 for (String naString : naStrings) {
322 try {
323 NetworkAttributes n = new NetworkAttributes(naString);
324 if (n.mType > ConnectivityManager.MAX_NETWORK_TYPE) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800325 loge("Error in networkAttributes - ignoring attempt to define type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700326 n.mType);
327 continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700328 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700329 if (mNetAttributes[n.mType] != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800330 loge("Error in networkAttributes - ignoring attempt to redefine type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700331 n.mType);
332 continue;
333 }
334 if (mRadioAttributes[n.mRadio] == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800335 loge("Error in networkAttributes - ignoring attempt to use undefined " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700336 "radio " + n.mRadio + " in network type " + n.mType);
337 continue;
338 }
339 mNetAttributes[n.mType] = n;
340 mNetworksDefined++;
341 } catch(Exception e) {
342 // ignore it - leave the entry null
Robert Greenwalt2034b912009-08-12 16:08:25 -0700343 }
344 }
345
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700346 // high priority first
347 mPriorityList = new int[mNetworksDefined];
348 {
349 int insertionPoint = mNetworksDefined-1;
350 int currentLowest = 0;
351 int nextLowest = 0;
352 while (insertionPoint > -1) {
353 for (NetworkAttributes na : mNetAttributes) {
354 if (na == null) continue;
355 if (na.mPriority < currentLowest) continue;
356 if (na.mPriority > currentLowest) {
357 if (na.mPriority < nextLowest || nextLowest == 0) {
358 nextLowest = na.mPriority;
359 }
360 continue;
361 }
362 mPriorityList[insertionPoint--] = na.mType;
363 }
364 currentLowest = nextLowest;
365 nextLowest = 0;
366 }
367 }
368
369 mNetRequestersPids = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1];
370 for (int i : mPriorityList) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700371 mNetRequestersPids[i] = new ArrayList();
372 }
373
374 mFeatureUsers = new ArrayList();
375
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700376 mNumDnsEntries = 0;
377
378 mTestMode = SystemProperties.get("cm.test.mode").equals("true")
379 && SystemProperties.get("ro.build.type").equals("eng");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800380 /*
381 * Create the network state trackers for Wi-Fi and mobile
382 * data. Maybe this could be done with a factory class,
383 * but it's not clear that it's worth it, given that
384 * the number of different network types is not going
385 * to change very often.
386 */
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800387 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700388 for (int netType : mPriorityList) {
389 switch (mNetAttributes[netType].mRadio) {
390 case ConnectivityManager.TYPE_WIFI:
Wink Savillee70c6f52010-12-03 12:01:38 -0800391 if (DBG) log("Starting Wifi Service.");
Wink Saville7fabfa22010-08-13 16:11:42 -0700392 WifiStateTracker wst = new WifiStateTracker();
Irfan Sheriff25be0762010-07-28 09:35:20 -0700393 WifiService wifiService = new WifiService(context);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700394 ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
Irfan Sheriff25be0762010-07-28 09:35:20 -0700395 wifiService.checkAndStartWifi();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700396 mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst;
Wink Saville7fabfa22010-08-13 16:11:42 -0700397 wst.startMonitoring(context, mHandler);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800398
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700399 //TODO: as part of WWS refactor, create only when needed
Irfan Sheriff25be0762010-07-28 09:35:20 -0700400 mWifiWatchdogService = new WifiWatchdogService(context);
Irfan Sheriff653e2a22010-06-07 09:03:04 -0700401
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700402 break;
403 case ConnectivityManager.TYPE_MOBILE:
Wink Saville7fabfa22010-08-13 16:11:42 -0700404 mNetTrackers[netType] = new MobileDataStateTracker(netType,
405 mNetAttributes[netType].mName);
406 mNetTrackers[netType].startMonitoring(context, mHandler);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800407 if (noMobileData) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800408 if (DBG) log("tearing down Mobile networks due to setting");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800409 mNetTrackers[netType].teardown();
410 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700411 break;
412 default:
Wink Savillee70c6f52010-12-03 12:01:38 -0800413 loge("Trying to create a DataStateTracker for an unknown radio type " +
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700414 mNetAttributes[netType].mRadio);
415 continue;
416 }
417 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -0800418
Robert Greenwaltc0b6c602010-03-11 15:03:08 -0800419 mTethering = new Tethering(mContext, mHandler.getLooper());
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800420 mTetheringConfigValid = (((mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] != null) ||
421 !mTethering.isDunRequired()) &&
422 (mTethering.getTetherableUsbRegexs().length != 0 ||
Danica Chang96567052010-08-11 14:54:43 -0700423 mTethering.getTetherableWifiRegexs().length != 0 ||
424 mTethering.getTetherableBluetoothRegexs().length != 0) &&
Robert Greenwaltf1b66e12010-02-25 12:29:30 -0800425 mTethering.getUpstreamIfaceRegexs().length != 0);
426
Robert Greenwalt0e80be12010-09-20 14:35:25 -0700427 if (DBG) {
428 mInetLog = new ArrayList();
429 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -0700430
431 mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
432 mSettingsObserver.observe(mContext);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -0800433
434 loadGlobalProxy();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800435 }
436
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700437
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700439 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800440 * @param preference the new preference
441 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700442 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800443 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700444
445 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800446 }
447
448 public int getNetworkPreference() {
449 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700450 int preference;
451 synchronized(this) {
452 preference = mNetworkPreference;
453 }
454 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800455 }
456
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700457 private void handleSetNetworkPreference(int preference) {
458 if (ConnectivityManager.isNetworkTypeValid(preference) &&
459 mNetAttributes[preference] != null &&
460 mNetAttributes[preference].isDefault()) {
461 if (mNetworkPreference != preference) {
462 final ContentResolver cr = mContext.getContentResolver();
463 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
464 synchronized(this) {
465 mNetworkPreference = preference;
466 }
467 enforcePreference();
468 }
469 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700471
The Android Open Source Project28527d22009-03-03 19:31:44 -0800472 private int getPersistedNetworkPreference() {
473 final ContentResolver cr = mContext.getContentResolver();
474
475 final int networkPrefSetting = Settings.Secure
476 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
477 if (networkPrefSetting != -1) {
478 return networkPrefSetting;
479 }
480
481 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
482 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700483
The Android Open Source Project28527d22009-03-03 19:31:44 -0800484 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700485 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800486 * In this method, we only tear down a non-preferred network. Establishing
487 * a connection to the preferred network is taken care of when we handle
488 * the disconnect event from the non-preferred network
489 * (see {@link #handleDisconnect(NetworkInfo)}).
490 */
491 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700492 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800493 return;
494
Robert Greenwalt2034b912009-08-12 16:08:25 -0700495 if (!mNetTrackers[mNetworkPreference].isAvailable())
496 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800497
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700499 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700500 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700501 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800502 log("tearing down " + mNetTrackers[t].getNetworkInfo() +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700503 " in enforcePreference");
504 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700505 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800506 }
507 }
508 }
509
510 private boolean teardown(NetworkStateTracker netTracker) {
511 if (netTracker.teardown()) {
512 netTracker.setTeardownRequested(true);
513 return true;
514 } else {
515 return false;
516 }
517 }
518
519 /**
520 * Return NetworkInfo for the active (i.e., connected) network interface.
521 * It is assumed that at most one network is active at a time. If more
522 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700523 * @return the info for the active network, or {@code null} if none is
524 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800525 */
526 public NetworkInfo getActiveNetworkInfo() {
527 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700528 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700529 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700530 continue;
531 }
532 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800533 NetworkInfo info = t.getNetworkInfo();
534 if (info.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800535 if (DBG && type != mActiveDefaultNetwork) {
536 loge("connected default network is not mActiveDefaultNetwork!");
537 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800538 return info;
539 }
540 }
541 return null;
542 }
543
544 public NetworkInfo getNetworkInfo(int networkType) {
545 enforceAccessPermission();
546 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
547 NetworkStateTracker t = mNetTrackers[networkType];
548 if (t != null)
549 return t.getNetworkInfo();
550 }
551 return null;
552 }
553
554 public NetworkInfo[] getAllNetworkInfo() {
555 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700556 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800557 int i = 0;
558 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700559 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800560 }
561 return result;
562 }
563
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700564 /**
565 * Return LinkProperties for the active (i.e., connected) default
566 * network interface. It is assumed that at most one default network
567 * is active at a time. If more than one is active, it is indeterminate
568 * which will be returned.
569 * @return the ip properties for the active network, or {@code null} if
570 * none is active
571 */
572 public LinkProperties getActiveLinkProperties() {
573 enforceAccessPermission();
574 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
575 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
576 continue;
577 }
578 NetworkStateTracker t = mNetTrackers[type];
579 NetworkInfo info = t.getNetworkInfo();
580 if (info.isConnected()) {
581 return t.getLinkProperties();
582 }
583 }
584 return null;
585 }
586
587 public LinkProperties getLinkProperties(int networkType) {
588 enforceAccessPermission();
589 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
590 NetworkStateTracker t = mNetTrackers[networkType];
591 if (t != null) return t.getLinkProperties();
592 }
593 return null;
594 }
595
The Android Open Source Project28527d22009-03-03 19:31:44 -0800596 public boolean setRadios(boolean turnOn) {
597 boolean result = true;
598 enforceChangePermission();
599 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700600 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800601 }
602 return result;
603 }
604
605 public boolean setRadio(int netType, boolean turnOn) {
606 enforceChangePermission();
607 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
608 return false;
609 }
610 NetworkStateTracker tracker = mNetTrackers[netType];
611 return tracker != null && tracker.setRadio(turnOn);
612 }
613
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700614 /**
615 * Used to notice when the calling process dies so we can self-expire
616 *
617 * Also used to know if the process has cleaned up after itself when
618 * our auto-expire timer goes off. The timer has a link to an object.
619 *
620 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700621 private class FeatureUser implements IBinder.DeathRecipient {
622 int mNetworkType;
623 String mFeature;
624 IBinder mBinder;
625 int mPid;
626 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800627 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700628
629 FeatureUser(int type, String feature, IBinder binder) {
630 super();
631 mNetworkType = type;
632 mFeature = feature;
633 mBinder = binder;
634 mPid = getCallingPid();
635 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800636 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700637
Robert Greenwalt2034b912009-08-12 16:08:25 -0700638 try {
639 mBinder.linkToDeath(this, 0);
640 } catch (RemoteException e) {
641 binderDied();
642 }
643 }
644
645 void unlinkDeathRecipient() {
646 mBinder.unlinkToDeath(this, 0);
647 }
648
649 public void binderDied() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800650 log("ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800651 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
652 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700653 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700654 }
655
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700656 public void expire() {
Wink Savillee70c6f52010-12-03 12:01:38 -0800657 log("ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800658 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
659 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700660 stopUsingNetworkFeature(this, false);
661 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800662
663 public String toString() {
664 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
665 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
666 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700667 }
668
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700669 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700670 public int startUsingNetworkFeature(int networkType, String feature,
671 IBinder binder) {
672 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800673 log("startUsingNetworkFeature for net " + networkType + ": " + feature);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700674 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800675 enforceChangePermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700676 if (!ConnectivityManager.isNetworkTypeValid(networkType) ||
677 mNetAttributes[networkType] == null) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700678 return Phone.APN_REQUEST_FAILED;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700680
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700681 FeatureUser f = new FeatureUser(networkType, feature, binder);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700682
683 // TODO - move this into the MobileDataStateTracker
684 int usedNetworkType = networkType;
685 if(networkType == ConnectivityManager.TYPE_MOBILE) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800686 if (!getMobileDataEnabled()) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800687 if (DBG) log("requested special network with data disabled - rejected");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800688 return Phone.APN_TYPE_NOT_AVAILABLE;
689 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700690 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
691 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
692 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
693 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
694 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
695 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
696 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
697 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
698 }
699 }
700 NetworkStateTracker network = mNetTrackers[usedNetworkType];
701 if (network != null) {
702 if (usedNetworkType != networkType) {
703 Integer currentPid = new Integer(getCallingPid());
704
705 NetworkStateTracker radio = mNetTrackers[networkType];
706 NetworkInfo ni = network.getNetworkInfo();
707
708 if (ni.isAvailable() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800709 if (DBG) log("special network not available");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700710 return Phone.APN_TYPE_NOT_AVAILABLE;
711 }
712
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700713 synchronized(this) {
714 mFeatureUsers.add(f);
715 if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) {
716 // this gets used for per-pid dns when connected
717 mNetRequestersPids[usedNetworkType].add(currentPid);
718 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700719 }
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700720 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_RESTORE_DEFAULT_NETWORK,
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700721 f), getRestoreDefaultNetworkDelay());
722
Robert Greenwalt2034b912009-08-12 16:08:25 -0700723
Robert Greenwalta52c75a2009-08-19 20:19:33 -0700724 if ((ni.isConnectedOrConnecting() == true) &&
725 !network.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700726 if (ni.isConnected() == true) {
727 // add the pid-specific dns
Robert Greenwalt3afbead2010-07-23 15:46:26 -0700728 handleDnsConfigurationChange(networkType);
Wink Savillee70c6f52010-12-03 12:01:38 -0800729 if (DBG) log("special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730 return Phone.APN_ALREADY_ACTIVE;
731 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800732 if (DBG) log("special network already connecting");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700733 return Phone.APN_REQUEST_STARTED;
734 }
735
736 // check if the radio in play can make another contact
737 // assume if cannot for now
738
Wink Savillee70c6f52010-12-03 12:01:38 -0800739 if (DBG) log("reconnecting to special network");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700740 network.reconnect();
741 return Phone.APN_REQUEST_STARTED;
742 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700743 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700744 }
745 }
746 return Phone.APN_TYPE_NOT_AVAILABLE;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800747 }
748
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700749 // javadoc from interface
The Android Open Source Project28527d22009-03-03 19:31:44 -0800750 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt28f43012009-10-06 17:52:40 -0700751 enforceChangePermission();
752
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700753 int pid = getCallingPid();
754 int uid = getCallingUid();
755
756 FeatureUser u = null;
757 boolean found = false;
758
759 synchronized(this) {
760 for (int i = 0; i < mFeatureUsers.size() ; i++) {
761 u = (FeatureUser)mFeatureUsers.get(i);
762 if (uid == u.mUid && pid == u.mPid &&
763 networkType == u.mNetworkType &&
764 TextUtils.equals(feature, u.mFeature)) {
765 found = true;
766 break;
767 }
768 }
769 }
770 if (found && u != null) {
771 // stop regardless of how many other time this proc had called start
772 return stopUsingNetworkFeature(u, true);
773 } else {
774 // none found!
Wink Savillee70c6f52010-12-03 12:01:38 -0800775 if (DBG) log("ignoring stopUsingNetworkFeature - not a live request");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700776 return 1;
777 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700778 }
779
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700780 private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) {
781 int networkType = u.mNetworkType;
782 String feature = u.mFeature;
783 int pid = u.mPid;
784 int uid = u.mUid;
785
786 NetworkStateTracker tracker = null;
787 boolean callTeardown = false; // used to carry our decision outside of sync block
788
Robert Greenwalt2034b912009-08-12 16:08:25 -0700789 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800790 log("stopUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700791 ": " + feature);
792 }
Robert Greenwalt28f43012009-10-06 17:52:40 -0700793
The Android Open Source Project28527d22009-03-03 19:31:44 -0800794 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
795 return -1;
796 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700797
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700798 // need to link the mFeatureUsers list with the mNetRequestersPids state in this
799 // sync block
800 synchronized(this) {
801 // check if this process still has an outstanding start request
802 if (!mFeatureUsers.contains(u)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800803 if (DBG) log("ignoring - this process has no outstanding requests");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700804 return 1;
805 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700806 u.unlinkDeathRecipient();
807 mFeatureUsers.remove(mFeatureUsers.indexOf(u));
808 // If we care about duplicate requests, check for that here.
809 //
810 // This is done to support the extension of a request - the app
811 // can request we start the network feature again and renew the
812 // auto-shutoff delay. Normal "stop" calls from the app though
813 // do not pay attention to duplicate requests - in effect the
814 // API does not refcount and a single stop will counter multiple starts.
815 if (ignoreDups == false) {
816 for (int i = 0; i < mFeatureUsers.size() ; i++) {
817 FeatureUser x = (FeatureUser)mFeatureUsers.get(i);
818 if (x.mUid == u.mUid && x.mPid == u.mPid &&
819 x.mNetworkType == u.mNetworkType &&
820 TextUtils.equals(x.mFeature, u.mFeature)) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800821 if (DBG) log("ignoring stopUsingNetworkFeature as dup is found");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700822 return 1;
823 }
824 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700825 }
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700826
827 // TODO - move to MobileDataStateTracker
828 int usedNetworkType = networkType;
829 if (networkType == ConnectivityManager.TYPE_MOBILE) {
830 if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) {
831 usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS;
832 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) {
833 usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL;
834 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) {
835 usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN;
836 } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) {
837 usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI;
838 }
839 }
840 tracker = mNetTrackers[usedNetworkType];
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700841 if (tracker == null) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800842 if (DBG) log("ignoring - no known tracker for net type " + usedNetworkType);
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700843 return -1;
844 }
845 if (usedNetworkType != networkType) {
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700846 Integer currentPid = new Integer(pid);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700847 mNetRequestersPids[usedNetworkType].remove(currentPid);
Robert Greenwalt0ca68a02009-12-17 14:54:59 -0800848 reassessPidDns(pid, true);
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700849 if (mNetRequestersPids[usedNetworkType].size() != 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800850 if (DBG) log("not tearing down special network - " +
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700851 "others still using it");
852 return 1;
853 }
854 callTeardown = true;
855 }
856 }
Wink Savillee70c6f52010-12-03 12:01:38 -0800857 if (DBG) log("Doing network teardown");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700858 if (callTeardown) {
859 tracker.teardown();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700860 return 1;
861 } else {
Robert Greenwaltd391e892010-05-18 10:52:51 -0700862 return -1;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700863 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800864 }
865
866 /**
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700867 * @deprecated use requestRouteToHostAddress instead
868 *
The Android Open Source Project28527d22009-03-03 19:31:44 -0800869 * Ensure that a network route exists to deliver traffic to the specified
870 * host via the specified network interface.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700871 * @param networkType the type of the network over which traffic to the
872 * specified host is to be routed
873 * @param hostAddress the IP address of the host to which the route is
874 * desired
The Android Open Source Project28527d22009-03-03 19:31:44 -0800875 * @return {@code true} on success, {@code false} on failure
876 */
877 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700878 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
879
880 if (inetAddress == null) {
881 return false;
882 }
883
884 return requestRouteToHostAddress(networkType, inetAddress.getAddress());
885 }
886
887 /**
888 * Ensure that a network route exists to deliver traffic to the specified
889 * host via the specified network interface.
890 * @param networkType the type of the network over which traffic to the
891 * specified host is to be routed
892 * @param hostAddress the IP address of the host to which the route is
893 * desired
894 * @return {@code true} on success, {@code false} on failure
895 */
896 public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800897 enforceChangePermission();
898 if (!ConnectivityManager.isNetworkTypeValid(networkType)) {
899 return false;
900 }
901 NetworkStateTracker tracker = mNetTrackers[networkType];
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700902
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700903 if (tracker == null || !tracker.getNetworkInfo().isConnected() ||
904 tracker.isTeardownRequested()) {
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700905 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800906 log("requestRouteToHostAddress on down network " +
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700907 "(" + networkType + ") - dropped");
Robert Greenwalt4666ed02009-09-10 15:06:20 -0700908 }
909 return false;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800910 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700911 try {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700912 InetAddress addr = InetAddress.getByAddress(hostAddress);
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700913 return addHostRoute(tracker, addr);
914 } catch (UnknownHostException e) {}
915 return false;
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700916 }
917
918 /**
919 * Ensure that a network route exists to deliver traffic to the specified
920 * host via the mobile data network.
921 * @param hostAddress the IP address of the host to which the route is desired,
922 * in network byte order.
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700923 * TODO - deprecate
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700924 * @return {@code true} on success, {@code false} on failure
925 */
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700926 private boolean addHostRoute(NetworkStateTracker nt, InetAddress hostAddress) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700927 if (nt.getNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
928 return false;
929 }
930
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -0700931 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700932 if (p == null) return false;
933 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700934
935 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -0800936 log("Requested host route to " + hostAddress + "(" + interfaceName + ")");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700937 }
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700938 if (interfaceName != null) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700939 return NetworkUtils.addHostRoute(interfaceName, hostAddress, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700940 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -0800941 if (DBG) loge("addHostRoute failed due to null interface name");
Irfan Sheriff7f132d92010-06-09 15:39:36 -0700942 return false;
943 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800944 }
945
946 /**
947 * @see ConnectivityManager#getBackgroundDataSetting()
948 */
949 public boolean getBackgroundDataSetting() {
950 return Settings.Secure.getInt(mContext.getContentResolver(),
951 Settings.Secure.BACKGROUND_DATA, 1) == 1;
952 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700953
The Android Open Source Project28527d22009-03-03 19:31:44 -0800954 /**
955 * @see ConnectivityManager#setBackgroundDataSetting(boolean)
956 */
957 public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) {
958 mContext.enforceCallingOrSelfPermission(
959 android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING,
960 "ConnectivityService");
Robert Greenwalt0659da32009-07-16 17:21:39 -0700961
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700962 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_BACKGROUND_DATA,
963 (allowBackgroundDataUsage ? ENABLED : DISABLED), 0));
964 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800965
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700966 private void handleSetBackgroundData(boolean enabled) {
967 if (enabled != getBackgroundDataSetting()) {
968 Settings.Secure.putInt(mContext.getContentResolver(),
969 Settings.Secure.BACKGROUND_DATA, enabled ? 1 : 0);
970 Intent broadcast = new Intent(
971 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
972 mContext.sendBroadcast(broadcast);
973 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700974 }
975
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800976 /**
977 * @see ConnectivityManager#getMobileDataEnabled()
978 */
979 public boolean getMobileDataEnabled() {
980 enforceAccessPermission();
981 boolean retVal = Settings.Secure.getInt(mContext.getContentResolver(),
982 Settings.Secure.MOBILE_DATA, 1) == 1;
Wink Savillee70c6f52010-12-03 12:01:38 -0800983 if (DBG) log("getMobileDataEnabled returning " + retVal);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800984 return retVal;
985 }
986
987 /**
988 * @see ConnectivityManager#setMobileDataEnabled(boolean)
989 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700990 public void setMobileDataEnabled(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800991 enforceChangePermission();
Wink Savillee70c6f52010-12-03 12:01:38 -0800992 if (DBG) log("setMobileDataEnabled(" + enabled + ")");
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800993
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700994 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
995 (enabled ? ENABLED : DISABLED), 0));
996 }
997
998 private void handleSetMobileData(boolean enabled) {
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -0800999 if (getMobileDataEnabled() == enabled) return;
1000
1001 Settings.Secure.putInt(mContext.getContentResolver(),
1002 Settings.Secure.MOBILE_DATA, enabled ? 1 : 0);
1003
1004 if (enabled) {
1005 if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001006 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001007 log("starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001008 }
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001009 mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
1010 }
1011 } else {
1012 for (NetworkStateTracker nt : mNetTrackers) {
1013 if (nt == null) continue;
1014 int netType = nt.getNetworkInfo().getType();
1015 if (mNetAttributes[netType].mRadio == ConnectivityManager.TYPE_MOBILE) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001016 if (DBG) log("tearing down " + nt);
Robert Greenwalt1b21f6c2010-02-23 18:58:05 -08001017 nt.teardown();
1018 }
1019 }
1020 }
1021 }
1022
The Android Open Source Project28527d22009-03-03 19:31:44 -08001023 private int getNumConnectedNetworks() {
1024 int numConnectedNets = 0;
1025
1026 for (NetworkStateTracker nt : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001027 if (nt != null && nt.getNetworkInfo().isConnected() &&
Robert Greenwalt0659da32009-07-16 17:21:39 -07001028 !nt.isTeardownRequested()) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001029 ++numConnectedNets;
1030 }
1031 }
1032 return numConnectedNets;
1033 }
1034
1035 private void enforceAccessPermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001036 mContext.enforceCallingOrSelfPermission(
1037 android.Manifest.permission.ACCESS_NETWORK_STATE,
1038 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001039 }
1040
1041 private void enforceChangePermission() {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001042 mContext.enforceCallingOrSelfPermission(
1043 android.Manifest.permission.CHANGE_NETWORK_STATE,
1044 "ConnectivityService");
The Android Open Source Project28527d22009-03-03 19:31:44 -08001045 }
1046
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001047 // TODO Make this a special check when it goes public
1048 private void enforceTetherChangePermission() {
1049 mContext.enforceCallingOrSelfPermission(
1050 android.Manifest.permission.CHANGE_NETWORK_STATE,
1051 "ConnectivityService");
1052 }
1053
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001054 private void enforceTetherAccessPermission() {
1055 mContext.enforceCallingOrSelfPermission(
1056 android.Manifest.permission.ACCESS_NETWORK_STATE,
1057 "ConnectivityService");
1058 }
1059
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001060 private void enforceConnectivityInternalPermission() {
1061 mContext.enforceCallingOrSelfPermission(
1062 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1063 "ConnectivityService");
1064 }
1065
The Android Open Source Project28527d22009-03-03 19:31:44 -08001066 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -07001067 * Handle a {@code DISCONNECTED} event. If this pertains to the non-active
1068 * network, we ignore it. If it is for the active network, we send out a
1069 * broadcast. But first, we check whether it might be possible to connect
1070 * to a different network.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001071 * @param info the {@code NetworkInfo} for the network
1072 */
1073 private void handleDisconnect(NetworkInfo info) {
1074
Robert Greenwalt2034b912009-08-12 16:08:25 -07001075 int prevNetType = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001076
Robert Greenwalt2034b912009-08-12 16:08:25 -07001077 mNetTrackers[prevNetType].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001078 /*
1079 * If the disconnected network is not the active one, then don't report
1080 * this as a loss of connectivity. What probably happened is that we're
1081 * getting the disconnect for a network that we explicitly disabled
1082 * in accordance with network preference policies.
1083 */
Robert Greenwalt2034b912009-08-12 16:08:25 -07001084 if (!mNetAttributes[prevNetType].isDefault()) {
1085 List pids = mNetRequestersPids[prevNetType];
1086 for (int i = 0; i<pids.size(); i++) {
1087 Integer pid = (Integer)pids.get(i);
1088 // will remove them because the net's no longer connected
1089 // need to do this now as only now do we know the pids and
1090 // can properly null things that are no longer referenced.
1091 reassessPidDns(pid.intValue(), false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001092 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001093 }
1094
The Android Open Source Project28527d22009-03-03 19:31:44 -08001095 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1096 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1097 if (info.isFailover()) {
1098 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1099 info.setFailover(false);
1100 }
1101 if (info.getReason() != null) {
1102 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1103 }
1104 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001105 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1106 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001107 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001108
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001109 NetworkStateTracker newNet = null;
1110 if (mNetAttributes[prevNetType].isDefault()) {
1111 newNet = tryFailover(prevNetType);
1112 if (newNet != null) {
1113 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001114 if (!switchTo.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001115 // if the other net is connected they've already reset this and perhaps even
1116 // gotten a positive report we don't want to overwrite, but if not we need to
1117 // clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001118 mDefaultInetConditionPublished = 0;
1119 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001120 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1121 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001122 mDefaultInetConditionPublished = 0; // we're not connected anymore
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001123 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1124 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001125 }
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001126 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001127 // do this before we broadcast the change
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001128 handleConnectivityChange(prevNetType);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001129
1130 sendStickyBroadcast(intent);
1131 /*
1132 * If the failover network is already connected, then immediately send
1133 * out a followup broadcast indicating successful failover
1134 */
1135 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1136 sendConnectedBroadcast(newNet.getNetworkInfo());
1137 }
1138 }
1139
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001140 // returns null if no failover available
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001141 private NetworkStateTracker tryFailover(int prevNetType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001142 /*
1143 * If this is a default network, check if other defaults are available
1144 * or active
1145 */
1146 NetworkStateTracker newNet = null;
1147 if (mNetAttributes[prevNetType].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001148 if (mActiveDefaultNetwork == prevNetType) {
1149 mActiveDefaultNetwork = -1;
1150 }
1151
1152 int newType = -1;
1153 int newPriority = -1;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001154 boolean noMobileData = !getMobileDataEnabled();
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001155 for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001156 if (checkType == prevNetType) continue;
1157 if (mNetAttributes[checkType] == null) continue;
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001158 if (mNetAttributes[checkType].mRadio == ConnectivityManager.TYPE_MOBILE &&
1159 noMobileData) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001160 loge("not failing over to mobile type " + checkType +
Robert Greenwalte981bc52010-10-08 16:35:52 -07001161 " because Mobile Data Disabled");
Robert Greenwalt72451bf2010-02-25 12:04:29 -08001162 continue;
1163 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001164 if (mNetAttributes[checkType].isDefault()) {
1165 /* TODO - if we have multiple nets we could use
1166 * we may want to put more thought into which we choose
1167 */
1168 if (checkType == mNetworkPreference) {
1169 newType = checkType;
1170 break;
1171 }
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001172 if (mNetAttributes[checkType].mPriority > newPriority) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001173 newType = checkType;
Robert Greenwaltec05b3c2009-10-30 14:17:42 -07001174 newPriority = mNetAttributes[newType].mPriority;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001175 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001176 }
1177 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001178
1179 if (newType != -1) {
1180 newNet = mNetTrackers[newType];
1181 /**
1182 * See if the other network is available to fail over to.
1183 * If is not available, we enable it anyway, so that it
1184 * will be able to connect when it does become available,
1185 * but we report a total loss of connectivity rather than
1186 * report that we are attempting to fail over.
1187 */
1188 if (newNet.isAvailable()) {
1189 NetworkInfo switchTo = newNet.getNetworkInfo();
1190 switchTo.setFailover(true);
Robert Greenwalta52c75a2009-08-19 20:19:33 -07001191 if (!switchTo.isConnectedOrConnecting() ||
1192 newNet.isTeardownRequested()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001193 newNet.reconnect();
1194 }
1195 if (DBG) {
1196 if (switchTo.isConnected()) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001197 log("Switching to already connected " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001198 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001199 log("Attempting to switch to " + switchTo.getTypeName());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001200 }
1201 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001202 } else {
1203 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001204 newNet = null; // not officially avail.. try anyway, but
1205 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001206 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001207 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001208 loge("Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001209 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001210 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001211
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001212 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001213 }
1214
1215 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001216 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1217 }
1218
1219 private void sendInetConditionBroadcast(NetworkInfo info) {
1220 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1221 }
1222
1223 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1224 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001225 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1226 if (info.isFailover()) {
1227 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1228 info.setFailover(false);
1229 }
1230 if (info.getReason() != null) {
1231 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1232 }
1233 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001234 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1235 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001236 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001237 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001238 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001239 }
1240
1241 /**
1242 * Called when an attempt to fail over to another network has failed.
1243 * @param info the {@link NetworkInfo} for the failed network
1244 */
1245 private void handleConnectionFailure(NetworkInfo info) {
1246 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001247
Robert Greenwalt2034b912009-08-12 16:08:25 -07001248 String reason = info.getReason();
1249 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001250
Robert Greenwalte981bc52010-10-08 16:35:52 -07001251 String reasonText;
1252 if (reason == null) {
1253 reasonText = ".";
1254 } else {
1255 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001256 }
Wink Savillee70c6f52010-12-03 12:01:38 -08001257 loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001258
1259 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1260 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1261 if (getActiveNetworkInfo() == null) {
1262 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1263 }
1264 if (reason != null) {
1265 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1266 }
1267 if (extraInfo != null) {
1268 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1269 }
1270 if (info.isFailover()) {
1271 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1272 info.setFailover(false);
1273 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001274
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001275 NetworkStateTracker newNet = null;
1276 if (mNetAttributes[info.getType()].isDefault()) {
1277 newNet = tryFailover(info.getType());
1278 if (newNet != null) {
1279 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001280 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001281 // if the other net is connected they've already reset this and perhaps
1282 // even gotten a positive report we don't want to overwrite, but if not
1283 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001284 mDefaultInetConditionPublished = 0;
1285 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001286 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1287 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001288 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001289 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1290 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001291 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001292
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001293 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001294 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001295 /*
1296 * If the failover network is already connected, then immediately send
1297 * out a followup broadcast indicating successful failover
1298 */
1299 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1300 sendConnectedBroadcast(newNet.getNetworkInfo());
1301 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001302 }
1303
1304 private void sendStickyBroadcast(Intent intent) {
1305 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001306 if (!mSystemReady) {
1307 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001308 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001309 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1310 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001311 }
1312 }
1313
1314 void systemReady() {
1315 synchronized(this) {
1316 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001317 if (mInitialBroadcast != null) {
1318 mContext.sendStickyBroadcast(mInitialBroadcast);
1319 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001320 }
1321 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001322 // load the global proxy at startup
1323 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001324 }
1325
1326 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001327 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001328
1329 // snapshot isFailover, because sendConnectedBroadcast() resets it
1330 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001331 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001332
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 // if this is a default net and other default is running
1334 // kill the one not preferred
1335 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001336 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1337 if ((type != mNetworkPreference &&
1338 mNetAttributes[mActiveDefaultNetwork].mPriority >
1339 mNetAttributes[type].mPriority) ||
1340 mNetworkPreference == mActiveDefaultNetwork) {
1341 // don't accept this one
Wink Savillee70c6f52010-12-03 12:01:38 -08001342 if (DBG) {
1343 log("Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001344 "to torn down network " + info.getTypeName());
Wink Savillee70c6f52010-12-03 12:01:38 -08001345 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001346 teardown(thisNet);
1347 return;
1348 } else {
1349 // tear down the other
1350 NetworkStateTracker otherNet =
1351 mNetTrackers[mActiveDefaultNetwork];
Wink Savillee70c6f52010-12-03 12:01:38 -08001352 if (DBG) {
1353 log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001354 " teardown");
Wink Savillee70c6f52010-12-03 12:01:38 -08001355 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001356 if (!teardown(otherNet)) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001357 loge("Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001358 return;
1359 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001360 }
1361 }
1362 synchronized (ConnectivityService.this) {
1363 // have a new default network, release the transition wakelock in a second
1364 // if it's held. The second pause is to allow apps to reconnect over the
1365 // new network
1366 if (mNetTransitionWakeLock.isHeld()) {
1367 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001368 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001369 mNetTransitionWakeLockSerialNumber, 0),
1370 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001371 }
1372 }
1373 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001374 // this will cause us to come up initially as unconnected and switching
1375 // to connected after our normal pause unless somebody reports us as reall
1376 // disconnected
1377 mDefaultInetConditionPublished = 0;
1378 mDefaultConnectionSequence++;
1379 mInetConditionChangeInFlight = false;
1380 // Don't do this - if we never sign in stay, grey
1381 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001382 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001383 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001384 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001385 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001386 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001387 }
1388
The Android Open Source Project28527d22009-03-03 19:31:44 -08001389 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001390 * After a change in the connectivity state of a network. We're mainly
1391 * concerned with making sure that the list of DNS servers is set up
1392 * according to which networks are connected, and ensuring that the
1393 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001394 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001395 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001396 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001397 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001398 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001399 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001400 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001401
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001402 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1403 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001404 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001405 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001406 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001407 addPrivateDnsRoutes(mNetTrackers[netType]);
1408 }
1409 } else {
1410 if (mNetAttributes[netType].isDefault()) {
1411 removeDefaultRoute(mNetTrackers[netType]);
1412 } else {
1413 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001414 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001415 }
1416 }
1417
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001419 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001420 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001421 if (p == null) return;
1422 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001423
1424 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001425 log("addPrivateDnsRoutes for " + nt +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001426 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1427 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001428 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001429 Collection<InetAddress> dnsList = p.getDnses();
1430 for (InetAddress dns : dnsList) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001431 if (DBG) log(" adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001432 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001433 }
1434 nt.privateDnsRouteSet(true);
1435 }
1436 }
1437
1438 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1439 // TODO - we should do this explicitly but the NetUtils api doesnt
1440 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001441 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001442 if (p == null) return;
1443 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001444 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1445 if (interfaceName != null && privateDnsRouteSet) {
1446 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001447 log("removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001448 " (" + interfaceName + ")");
1449 }
1450 NetworkUtils.removeHostRoutes(interfaceName);
1451 nt.privateDnsRouteSet(false);
1452 }
1453 }
1454
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001455
1456 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001457 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001458 if (p == null) return;
1459 String interfaceName = p.getInterfaceName();
1460 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001461
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001462 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001463 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001464 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001465 log("addDefaultRoute for " + networkInfo.getTypeName() +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001466 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1467 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001468 }
1469 }
1470
1471
1472 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001473 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001474 if (p == null) return;
1475 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001476
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001477 if (interfaceName != null) {
1478 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001479 NetworkInfo networkInfo = nt.getNetworkInfo();
Wink Savillee70c6f52010-12-03 12:01:38 -08001480 log("removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001481 interfaceName + ")");
1482 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001483 }
1484 }
1485
1486 /**
1487 * Reads the network specific TCP buffer sizes from SystemProperties
1488 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1489 * wide use
1490 */
1491 public void updateNetworkSettings(NetworkStateTracker nt) {
1492 String key = nt.getTcpBufferSizesPropName();
1493 String bufferSizes = SystemProperties.get(key);
1494
1495 if (bufferSizes.length() == 0) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001496 loge(key + " not found in system properties. Using defaults");
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001497
1498 // Setting to default values so we won't be stuck to previous values
1499 key = "net.tcp.buffersize.default";
1500 bufferSizes = SystemProperties.get(key);
1501 }
1502
1503 // Set values in kernel
1504 if (bufferSizes.length() != 0) {
1505 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001506 log("Setting TCP values: [" + bufferSizes
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001507 + "] which comes from [" + key + "]");
1508 }
1509 setBufferSize(bufferSizes);
1510 }
1511 }
1512
1513 /**
1514 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1515 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1516 *
1517 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1518 * writeMin, writeInitial, writeMax"
1519 */
1520 private void setBufferSize(String bufferSizes) {
1521 try {
1522 String[] values = bufferSizes.split(",");
1523
1524 if (values.length == 6) {
1525 final String prefix = "/sys/kernel/ipv4/tcp_";
1526 stringToFile(prefix + "rmem_min", values[0]);
1527 stringToFile(prefix + "rmem_def", values[1]);
1528 stringToFile(prefix + "rmem_max", values[2]);
1529 stringToFile(prefix + "wmem_min", values[3]);
1530 stringToFile(prefix + "wmem_def", values[4]);
1531 stringToFile(prefix + "wmem_max", values[5]);
1532 } else {
Wink Savillee70c6f52010-12-03 12:01:38 -08001533 loge("Invalid buffersize string: " + bufferSizes);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001534 }
1535 } catch (IOException e) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001536 loge("Can't set tcp buffer sizes:" + e);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001537 }
1538 }
1539
1540 /**
1541 * Writes string to file. Basically same as "echo -n $string > $filename"
1542 *
1543 * @param filename
1544 * @param string
1545 * @throws IOException
1546 */
1547 private void stringToFile(String filename, String string) throws IOException {
1548 FileWriter out = new FileWriter(filename);
1549 try {
1550 out.write(string);
1551 } finally {
1552 out.close();
1553 }
1554 }
1555
1556
Robert Greenwalt2034b912009-08-12 16:08:25 -07001557 /**
1558 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1559 * on the highest priority active net which this process requested.
1560 * If there aren't any, clear it out
1561 */
1562 private void reassessPidDns(int myPid, boolean doBump)
1563 {
Wink Savillee70c6f52010-12-03 12:01:38 -08001564 if (DBG) log("reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001565 for(int i : mPriorityList) {
1566 if (mNetAttributes[i].isDefault()) {
1567 continue;
1568 }
1569 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001570 if (nt.getNetworkInfo().isConnected() &&
1571 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001572 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001573 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001574 List pids = mNetRequestersPids[i];
1575 for (int j=0; j<pids.size(); j++) {
1576 Integer pid = (Integer)pids.get(j);
1577 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001578 Collection<InetAddress> dnses = p.getDnses();
1579 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001580 if (doBump) {
1581 bumpDns();
1582 }
1583 return;
1584 }
1585 }
1586 }
1587 }
1588 // nothing found - delete
1589 for (int i = 1; ; i++) {
1590 String prop = "net.dns" + i + "." + myPid;
1591 if (SystemProperties.get(prop).length() == 0) {
1592 if (doBump) {
1593 bumpDns();
1594 }
1595 return;
1596 }
1597 SystemProperties.set(prop, "");
1598 }
1599 }
1600
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001601 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001602 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001603 for (InetAddress dns : dnses) {
1604 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001605 }
1606 }
1607
1608 private void bumpDns() {
1609 /*
1610 * Bump the property that tells the name resolver library to reread
1611 * the DNS server list from the properties.
1612 */
1613 String propVal = SystemProperties.get("net.dnschange");
1614 int n = 0;
1615 if (propVal.length() != 0) {
1616 try {
1617 n = Integer.parseInt(propVal);
1618 } catch (NumberFormatException e) {}
1619 }
1620 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001621 /*
1622 * Tell the VMs to toss their DNS caches
1623 */
1624 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1625 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1626 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001627 }
1628
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001629 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001630 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001631 NetworkStateTracker nt = mNetTrackers[netType];
1632 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001633 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001634 if (p == null) return;
1635 Collection<InetAddress> dnses = p.getDnses();
1636 if (mNetAttributes[netType].isDefault()) {
1637 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001638 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001639 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001640 log("no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001641 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001642 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1643 j++;
1644 } else {
1645 for (InetAddress dns : dnses) {
1646 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001647 log("adding dns " + dns + " for " +
Robert Greenwalt94daa182010-09-01 11:34:05 -07001648 nt.getNetworkInfo().getTypeName());
1649 }
1650 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1651 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001652 }
1653 for (int k=j ; k<mNumDnsEntries; k++) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001654 if (DBG) log("erasing net.dns" + k);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001655 SystemProperties.set("net.dns" + k, "");
1656 }
1657 mNumDnsEntries = j;
1658 } else {
1659 // set per-pid dns for attached secondary nets
1660 List pids = mNetRequestersPids[netType];
1661 for (int y=0; y< pids.size(); y++) {
1662 Integer pid = (Integer)pids.get(y);
1663 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001664 }
1665 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001666 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001667 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001668 }
1669
1670 private int getRestoreDefaultNetworkDelay() {
1671 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1672 NETWORK_RESTORE_DELAY_PROP_NAME);
1673 if(restoreDefaultNetworkDelayStr != null &&
1674 restoreDefaultNetworkDelayStr.length() != 0) {
1675 try {
1676 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1677 } catch (NumberFormatException e) {
1678 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001679 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001680 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001681 }
1682
1683 @Override
1684 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001685 if (mContext.checkCallingOrSelfPermission(
1686 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001687 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001688 pw.println("Permission Denial: can't dump ConnectivityService " +
1689 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1690 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001691 return;
1692 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001693 pw.println();
1694 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001695 if (nst != null) {
1696 if (nst.getNetworkInfo().isConnected()) {
1697 pw.println("Active network: " + nst.getNetworkInfo().
1698 getTypeName());
1699 }
1700 pw.println(nst.getNetworkInfo());
1701 pw.println(nst);
1702 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001703 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001704 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001705
1706 pw.println("Network Requester Pids:");
1707 for (int net : mPriorityList) {
1708 String pidString = net + ": ";
1709 for (Object pid : mNetRequestersPids[net]) {
1710 pidString = pidString + pid.toString() + ", ";
1711 }
1712 pw.println(pidString);
1713 }
1714 pw.println();
1715
1716 pw.println("FeatureUsers:");
1717 for (Object requester : mFeatureUsers) {
1718 pw.println(requester.toString());
1719 }
1720 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001721
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001722 synchronized (this) {
1723 pw.println("NetworkTranstionWakeLock is currently " +
1724 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1725 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1726 }
1727 pw.println();
1728
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001729 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001730
1731 if (mInetLog != null) {
1732 pw.println();
1733 pw.println("Inet condition reports:");
1734 for(int i = 0; i < mInetLog.size(); i++) {
1735 pw.println(mInetLog.get(i));
1736 }
1737 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001738 }
1739
Robert Greenwalt2034b912009-08-12 16:08:25 -07001740 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001741 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001742 public MyHandler(Looper looper) {
1743 super(looper);
1744 }
1745
The Android Open Source Project28527d22009-03-03 19:31:44 -08001746 @Override
1747 public void handleMessage(Message msg) {
1748 NetworkInfo info;
1749 switch (msg.what) {
1750 case NetworkStateTracker.EVENT_STATE_CHANGED:
1751 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001752 int type = info.getType();
1753 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001754 // only do this optimization for wifi. It going into scan mode for location
1755 // services generates alot of noise. Meanwhile the mms apn won't send out
1756 // subsequent notifications when on default cellular because it never
1757 // disconnects.. so only do this to wifi notifications. Fixed better when the
1758 // APN notifications are standardized.
1759 if (mNetAttributes[type].mLastState == state &&
1760 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001761 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001762 // TODO - remove this after we validate the dropping doesn't break
1763 // anything
Wink Savillee70c6f52010-12-03 12:01:38 -08001764 log("Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001765 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001766 state + "/" + info.getDetailedState());
1767 }
1768 return;
1769 }
1770 mNetAttributes[type].mLastState = state;
1771
Wink Savillee70c6f52010-12-03 12:01:38 -08001772 if (DBG) log("ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001773 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001774 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001775
1776 // Connectivity state changed:
1777 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001778 // [12-9] Network subtype (for mobile network, as defined
1779 // by TelephonyManager)
1780 // [8-3] Detailed state ordinal (as defined by
1781 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001782 // [2-0] Network type (as defined by ConnectivityManager)
1783 int eventLogParam = (info.getType() & 0x7) |
1784 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1785 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001786 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001787 eventLogParam);
1788
1789 if (info.getDetailedState() ==
1790 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001791 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001792 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001793 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001794 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001795 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001796 // the logic here is, handle SUSPENDED the same as
1797 // DISCONNECTED. The only difference being we are
1798 // broadcasting an intent with NetworkInfo that's
1799 // suspended. This allows the applications an
1800 // opportunity to handle DISCONNECTED and SUSPENDED
1801 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001802 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001803 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001804 handleConnect(info);
1805 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001806 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001807 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001808 info = (NetworkInfo) msg.obj;
1809 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001810 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001811 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001812 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001813 String causedBy = null;
1814 synchronized (ConnectivityService.this) {
1815 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1816 mNetTransitionWakeLock.isHeld()) {
1817 mNetTransitionWakeLock.release();
1818 causedBy = mNetTransitionWakeLockCausedBy;
1819 }
1820 }
1821 if (causedBy != null) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001822 log("NetTransition Wakelock for " + causedBy + " released by timeout");
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001823 }
Robert Greenwaltcf1a56c2010-09-09 14:05:10 -07001824 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001825 case EVENT_RESTORE_DEFAULT_NETWORK:
Robert Greenwaltaffc3a12009-09-27 17:27:04 -07001826 FeatureUser u = (FeatureUser)msg.obj;
1827 u.expire();
Robert Greenwalt986c7412010-09-08 15:24:47 -07001828 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001829 case EVENT_INET_CONDITION_CHANGE:
1830 {
1831 int netType = msg.arg1;
1832 int condition = msg.arg2;
1833 handleInetConditionChange(netType, condition);
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001834 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001835 }
1836 case EVENT_INET_CONDITION_HOLD_END:
1837 {
1838 int netType = msg.arg1;
1839 int sequence = msg.arg2;
1840 handleInetConditionHoldEnd(netType, sequence);
Robert Greenwalt986c7412010-09-08 15:24:47 -07001841 break;
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001842 }
1843 case EVENT_SET_NETWORK_PREFERENCE:
1844 {
1845 int preference = msg.arg1;
1846 handleSetNetworkPreference(preference);
1847 break;
1848 }
1849 case EVENT_SET_BACKGROUND_DATA:
1850 {
1851 boolean enabled = (msg.arg1 == ENABLED);
1852 handleSetBackgroundData(enabled);
1853 break;
1854 }
1855 case EVENT_SET_MOBILE_DATA:
1856 {
1857 boolean enabled = (msg.arg1 == ENABLED);
1858 handleSetMobileData(enabled);
1859 break;
1860 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001861 case EVENT_APPLY_GLOBAL_HTTP_PROXY:
1862 {
1863 handleDeprecatedGlobalHttpProxy();
1864 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001865 }
1866 }
1867 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001868
1869 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001870 public int tether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001871 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001872
1873 if (isTetheringSupported()) {
1874 return mTethering.tether(iface);
1875 } else {
1876 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1877 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001878 }
1879
1880 // javadoc from interface
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001881 public int untether(String iface) {
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001882 enforceTetherChangePermission();
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001883
1884 if (isTetheringSupported()) {
1885 return mTethering.untether(iface);
1886 } else {
1887 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1888 }
1889 }
1890
1891 // javadoc from interface
1892 public int getLastTetherError(String iface) {
1893 enforceTetherAccessPermission();
1894
1895 if (isTetheringSupported()) {
1896 return mTethering.getLastTetherError(iface);
1897 } else {
1898 return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
1899 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001900 }
1901
1902 // TODO - proper iface API for selection by property, inspection, etc
1903 public String[] getTetherableUsbRegexs() {
1904 enforceTetherAccessPermission();
1905 if (isTetheringSupported()) {
1906 return mTethering.getTetherableUsbRegexs();
1907 } else {
1908 return new String[0];
1909 }
1910 }
1911
1912 public String[] getTetherableWifiRegexs() {
1913 enforceTetherAccessPermission();
1914 if (isTetheringSupported()) {
1915 return mTethering.getTetherableWifiRegexs();
1916 } else {
1917 return new String[0];
1918 }
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001919 }
1920
Danica Chang96567052010-08-11 14:54:43 -07001921 public String[] getTetherableBluetoothRegexs() {
1922 enforceTetherAccessPermission();
1923 if (isTetheringSupported()) {
1924 return mTethering.getTetherableBluetoothRegexs();
1925 } else {
1926 return new String[0];
1927 }
1928 }
1929
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001930 // TODO - move iface listing, queries, etc to new module
1931 // javadoc from interface
1932 public String[] getTetherableIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001933 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001934 return mTethering.getTetherableIfaces();
1935 }
1936
1937 public String[] getTetheredIfaces() {
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001938 enforceTetherAccessPermission();
Robert Greenwalt0c4828c2010-01-26 11:40:34 -08001939 return mTethering.getTetheredIfaces();
1940 }
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001941
Robert Greenwalt4283ded2010-03-02 17:25:02 -08001942 public String[] getTetheringErroredIfaces() {
1943 enforceTetherAccessPermission();
1944 return mTethering.getErroredIfaces();
1945 }
1946
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001947 // if ro.tether.denied = true we default to no tethering
1948 // gservices could set the secure setting to 1 though to enable it on a build where it
1949 // had previously been turned off.
1950 public boolean isTetheringSupported() {
1951 enforceTetherAccessPermission();
1952 int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1);
Robert Greenwaltf1b66e12010-02-25 12:29:30 -08001953 boolean tetherEnabledInSettings = (Settings.Secure.getInt(mContext.getContentResolver(),
1954 Settings.Secure.TETHER_SUPPORTED, defaultVal) != 0);
1955 return tetherEnabledInSettings && mTetheringConfigValid;
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001956 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001957
1958 // An API NetworkStateTrackers can call when they lose their network.
1959 // This will automatically be cleared after X seconds or a network becomes CONNECTED,
1960 // whichever happens first. The timer is started by the first caller and not
1961 // restarted by subsequent callers.
1962 public void requestNetworkTransitionWakelock(String forWhom) {
1963 enforceConnectivityInternalPermission();
1964 synchronized (this) {
1965 if (mNetTransitionWakeLock.isHeld()) return;
1966 mNetTransitionWakeLockSerialNumber++;
1967 mNetTransitionWakeLock.acquire();
1968 mNetTransitionWakeLockCausedBy = forWhom;
1969 }
1970 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001971 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001972 mNetTransitionWakeLockSerialNumber, 0),
1973 mNetTransitionWakeLockTimeout);
1974 return;
1975 }
Robert Greenwalt24118e82010-09-09 13:15:32 -07001976
Robert Greenwalt986c7412010-09-08 15:24:47 -07001977 // 100 percent is full good, 0 is full bad.
1978 public void reportInetCondition(int networkType, int percentage) {
Wink Savillee70c6f52010-12-03 12:01:38 -08001979 if (DBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")");
Robert Greenwalt986c7412010-09-08 15:24:47 -07001980 mContext.enforceCallingOrSelfPermission(
1981 android.Manifest.permission.STATUS_BAR,
1982 "ConnectivityService");
1983
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001984 if (DBG) {
1985 int pid = getCallingPid();
1986 int uid = getCallingUid();
1987 String s = pid + "(" + uid + ") reports inet is " +
1988 (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " +
1989 "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime();
1990 mInetLog.add(s);
1991 while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) {
1992 mInetLog.remove(0);
1993 }
1994 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001995 mHandler.sendMessage(mHandler.obtainMessage(
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07001996 EVENT_INET_CONDITION_CHANGE, networkType, percentage));
1997 }
1998
1999 private void handleInetConditionChange(int netType, int condition) {
2000 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002001 log("Inet connectivity change, net=" +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002002 netType + ", condition=" + condition +
2003 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2004 }
2005 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002006 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002007 return;
2008 }
2009 if (mActiveDefaultNetwork != netType) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002010 if (DBG) log("given net not default - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002011 return;
2012 }
2013 mDefaultInetCondition = condition;
2014 int delay;
2015 if (mInetConditionChangeInFlight == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002016 if (DBG) log("starting a change hold");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002017 // setup a new hold to debounce this
2018 if (mDefaultInetCondition > 50) {
2019 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2020 Settings.Secure.INET_CONDITION_DEBOUNCE_UP_DELAY, 500);
2021 } else {
2022 delay = Settings.Secure.getInt(mContext.getContentResolver(),
2023 Settings.Secure.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000);
2024 }
2025 mInetConditionChangeInFlight = true;
2026 mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END,
2027 mActiveDefaultNetwork, mDefaultConnectionSequence), delay);
2028 } else {
2029 // we've set the new condition, when this hold ends that will get
2030 // picked up
Wink Savillee70c6f52010-12-03 12:01:38 -08002031 if (DBG) log("currently in hold - not setting new end evt");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002032 }
2033 }
2034
2035 private void handleInetConditionHoldEnd(int netType, int sequence) {
2036 if (DBG) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002037 log("Inet hold end, net=" + netType +
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002038 ", condition =" + mDefaultInetCondition +
2039 ", published condition =" + mDefaultInetConditionPublished);
2040 }
2041 mInetConditionChangeInFlight = false;
2042
2043 if (mActiveDefaultNetwork == -1) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002044 if (DBG) log("no active default network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002045 return;
2046 }
2047 if (mDefaultConnectionSequence != sequence) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002048 if (DBG) log("event hold for obsolete network - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002049 return;
2050 }
2051 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002052 if (DBG) log("no change in condition - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002053 return;
2054 }
2055 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2056 if (networkInfo.isConnected() == false) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002057 if (DBG) log("default network not connected - aborting");
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -07002058 return;
2059 }
2060 mDefaultInetConditionPublished = mDefaultInetCondition;
2061 sendInetConditionBroadcast(networkInfo);
2062 return;
Robert Greenwalt986c7412010-09-08 15:24:47 -07002063 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002064
2065 public synchronized ProxyProperties getProxy() {
2066 if (mGlobalProxy != null) return mGlobalProxy;
2067 if (mDefaultProxy != null) return mDefaultProxy;
2068 return null;
2069 }
2070
2071 public void setGlobalProxy(ProxyProperties proxyProperties) {
2072 enforceChangePermission();
2073 synchronized (mGlobalProxyLock) {
2074 if (proxyProperties == mGlobalProxy) return;
2075 if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return;
2076 if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return;
2077
2078 String host = "";
2079 int port = 0;
2080 String exclList = "";
2081 if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) {
2082 mGlobalProxy = new ProxyProperties(proxyProperties);
2083 host = mGlobalProxy.getHost();
2084 port = mGlobalProxy.getPort();
2085 exclList = mGlobalProxy.getExclusionList();
2086 } else {
2087 mGlobalProxy = null;
2088 }
2089 ContentResolver res = mContext.getContentResolver();
2090 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, host);
2091 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, port);
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002092 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002093 exclList);
2094 }
2095
2096 if (mGlobalProxy == null) {
2097 proxyProperties = mDefaultProxy;
2098 }
2099 sendProxyBroadcast(proxyProperties);
2100 }
2101
Robert Greenwalt6f7c6092010-12-02 11:31:00 -08002102 private void loadGlobalProxy() {
2103 ContentResolver res = mContext.getContentResolver();
2104 String host = Settings.Secure.getString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST);
2105 int port = Settings.Secure.getInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, 0);
2106 String exclList = Settings.Secure.getString(res,
2107 Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
2108 if (!TextUtils.isEmpty(host)) {
2109 ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList);
2110 synchronized (mGlobalProxyLock) {
2111 mGlobalProxy = proxyProperties;
2112 }
2113 }
2114 }
2115
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002116 public ProxyProperties getGlobalProxy() {
2117 synchronized (mGlobalProxyLock) {
2118 return mGlobalProxy;
2119 }
2120 }
2121
2122 private void handleApplyDefaultProxy(int type) {
2123 // check if new default - push it out to all VM if so
2124 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2125 synchronized (this) {
2126 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2127 if (mDefaultProxy == proxy) return;
2128 if (!TextUtils.isEmpty(proxy.getHost())) {
2129 mDefaultProxy = proxy;
2130 } else {
2131 mDefaultProxy = null;
2132 }
2133 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002134 if (DBG) log("changing default proxy to " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002135 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2136 if (mGlobalProxy != null) return;
2137 sendProxyBroadcast(proxy);
2138 }
2139
2140 private void handleDeprecatedGlobalHttpProxy() {
2141 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2142 Settings.Secure.HTTP_PROXY);
2143 if (!TextUtils.isEmpty(proxy)) {
2144 String data[] = proxy.split(":");
2145 String proxyHost = data[0];
2146 int proxyPort = 8080;
2147 if (data.length > 1) {
2148 try {
2149 proxyPort = Integer.parseInt(data[1]);
2150 } catch (NumberFormatException e) {
2151 return;
2152 }
2153 }
2154 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2155 setGlobalProxy(p);
2156 }
2157 }
2158
2159 private void sendProxyBroadcast(ProxyProperties proxy) {
Wink Savillee70c6f52010-12-03 12:01:38 -08002160 log("sending Proxy Broadcast for " + proxy);
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07002161 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2162 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2163 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
2164 mContext.sendBroadcast(intent);
2165 }
2166
2167 private static class SettingsObserver extends ContentObserver {
2168 private int mWhat;
2169 private Handler mHandler;
2170 SettingsObserver(Handler handler, int what) {
2171 super(handler);
2172 mHandler = handler;
2173 mWhat = what;
2174 }
2175
2176 void observe(Context context) {
2177 ContentResolver resolver = context.getContentResolver();
2178 resolver.registerContentObserver(Settings.Secure.getUriFor(
2179 Settings.Secure.HTTP_PROXY), false, this);
2180 }
2181
2182 @Override
2183 public void onChange(boolean selfChange) {
2184 mHandler.obtainMessage(mWhat).sendToTarget();
2185 }
2186 }
Wink Savillee70c6f52010-12-03 12:01:38 -08002187
2188 private void log(String s) {
2189 Slog.d(TAG, s);
2190 }
2191
2192 private void loge(String s) {
2193 Slog.e(TAG, s);
2194 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002195}