blob: 5c67da7f4df48d9a3137238e807efda2a5cfd02c [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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800260 if (DBG) Slog.v(TAG, "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) {
284 Slog.e(TAG, "Error setting defaultDns using " + dns);
285 }
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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800308 Slog.e(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800312 Slog.e(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800325 Slog.e(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800330 Slog.e(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800335 Slog.e(TAG, "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:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800391 if (DBG) Slog.v(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800408 if (DBG) Slog.d(TAG, "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:
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800413 Slog.e(TAG, "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);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800433 }
434
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700435
The Android Open Source Project28527d22009-03-03 19:31:44 -0800436 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700437 * Sets the preferred network.
The Android Open Source Project28527d22009-03-03 19:31:44 -0800438 * @param preference the new preference
439 */
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700440 public void setNetworkPreference(int preference) {
The Android Open Source Project28527d22009-03-03 19:31:44 -0800441 enforceChangePermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700442
443 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0));
The Android Open Source Project28527d22009-03-03 19:31:44 -0800444 }
445
446 public int getNetworkPreference() {
447 enforceAccessPermission();
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700448 int preference;
449 synchronized(this) {
450 preference = mNetworkPreference;
451 }
452 return preference;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800453 }
454
Robert Greenwalt6a2db8a2010-09-23 10:05:56 -0700455 private void handleSetNetworkPreference(int preference) {
456 if (ConnectivityManager.isNetworkTypeValid(preference) &&
457 mNetAttributes[preference] != null &&
458 mNetAttributes[preference].isDefault()) {
459 if (mNetworkPreference != preference) {
460 final ContentResolver cr = mContext.getContentResolver();
461 Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, preference);
462 synchronized(this) {
463 mNetworkPreference = preference;
464 }
465 enforcePreference();
466 }
467 }
The Android Open Source Project28527d22009-03-03 19:31:44 -0800468 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700469
The Android Open Source Project28527d22009-03-03 19:31:44 -0800470 private int getPersistedNetworkPreference() {
471 final ContentResolver cr = mContext.getContentResolver();
472
473 final int networkPrefSetting = Settings.Secure
474 .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1);
475 if (networkPrefSetting != -1) {
476 return networkPrefSetting;
477 }
478
479 return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE;
480 }
Robert Greenwalt0659da32009-07-16 17:21:39 -0700481
The Android Open Source Project28527d22009-03-03 19:31:44 -0800482 /**
Robert Greenwalt0659da32009-07-16 17:21:39 -0700483 * Make the state of network connectivity conform to the preference settings
The Android Open Source Project28527d22009-03-03 19:31:44 -0800484 * In this method, we only tear down a non-preferred network. Establishing
485 * a connection to the preferred network is taken care of when we handle
486 * the disconnect event from the non-preferred network
487 * (see {@link #handleDisconnect(NetworkInfo)}).
488 */
489 private void enforcePreference() {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700490 if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected())
The Android Open Source Project28527d22009-03-03 19:31:44 -0800491 return;
492
Robert Greenwalt2034b912009-08-12 16:08:25 -0700493 if (!mNetTrackers[mNetworkPreference].isAvailable())
494 return;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800495
Robert Greenwalt2034b912009-08-12 16:08:25 -0700496 for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700497 if (t != mNetworkPreference && mNetTrackers[t] != null &&
Robert Greenwalt2034b912009-08-12 16:08:25 -0700498 mNetTrackers[t].getNetworkInfo().isConnected()) {
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700499 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800500 Slog.d(TAG, "tearing down " +
Robert Greenwaltf3f045b2009-08-20 15:25:14 -0700501 mNetTrackers[t].getNetworkInfo() +
502 " in enforcePreference");
503 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700504 teardown(mNetTrackers[t]);
The Android Open Source Project28527d22009-03-03 19:31:44 -0800505 }
506 }
507 }
508
509 private boolean teardown(NetworkStateTracker netTracker) {
510 if (netTracker.teardown()) {
511 netTracker.setTeardownRequested(true);
512 return true;
513 } else {
514 return false;
515 }
516 }
517
518 /**
519 * Return NetworkInfo for the active (i.e., connected) network interface.
520 * It is assumed that at most one network is active at a time. If more
521 * than one is active, it is indeterminate which will be returned.
Robert Greenwalt0659da32009-07-16 17:21:39 -0700522 * @return the info for the active network, or {@code null} if none is
523 * active
The Android Open Source Project28527d22009-03-03 19:31:44 -0800524 */
525 public NetworkInfo getActiveNetworkInfo() {
526 enforceAccessPermission();
Robert Greenwalt2034b912009-08-12 16:08:25 -0700527 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700528 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -0700529 continue;
530 }
531 NetworkStateTracker t = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800532 NetworkInfo info = t.getNetworkInfo();
533 if (info.isConnected()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800534 if (DBG && type != mActiveDefaultNetwork) Slog.e(TAG,
Robert Greenwalt2034b912009-08-12 16:08:25 -0700535 "connected default network is not " +
536 "mActiveDefaultNetwork!");
The Android Open Source Project28527d22009-03-03 19:31:44 -0800537 return info;
538 }
539 }
540 return null;
541 }
542
543 public NetworkInfo getNetworkInfo(int networkType) {
544 enforceAccessPermission();
545 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
546 NetworkStateTracker t = mNetTrackers[networkType];
547 if (t != null)
548 return t.getNetworkInfo();
549 }
550 return null;
551 }
552
553 public NetworkInfo[] getAllNetworkInfo() {
554 enforceAccessPermission();
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700555 NetworkInfo[] result = new NetworkInfo[mNetworksDefined];
The Android Open Source Project28527d22009-03-03 19:31:44 -0800556 int i = 0;
557 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700558 if(t != null) result[i++] = t.getNetworkInfo();
The Android Open Source Project28527d22009-03-03 19:31:44 -0800559 }
560 return result;
561 }
562
Robert Greenwalt9f0ee4f2010-09-14 09:18:02 -0700563 /**
564 * Return LinkProperties for the active (i.e., connected) default
565 * network interface. It is assumed that at most one default network
566 * is active at a time. If more than one is active, it is indeterminate
567 * which will be returned.
568 * @return the ip properties for the active network, or {@code null} if
569 * none is active
570 */
571 public LinkProperties getActiveLinkProperties() {
572 enforceAccessPermission();
573 for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) {
574 if (mNetAttributes[type] == null || !mNetAttributes[type].isDefault()) {
575 continue;
576 }
577 NetworkStateTracker t = mNetTrackers[type];
578 NetworkInfo info = t.getNetworkInfo();
579 if (info.isConnected()) {
580 return t.getLinkProperties();
581 }
582 }
583 return null;
584 }
585
586 public LinkProperties getLinkProperties(int networkType) {
587 enforceAccessPermission();
588 if (ConnectivityManager.isNetworkTypeValid(networkType)) {
589 NetworkStateTracker t = mNetTrackers[networkType];
590 if (t != null) return t.getLinkProperties();
591 }
592 return null;
593 }
594
The Android Open Source Project28527d22009-03-03 19:31:44 -0800595 public boolean setRadios(boolean turnOn) {
596 boolean result = true;
597 enforceChangePermission();
598 for (NetworkStateTracker t : mNetTrackers) {
Robert Greenwaltec05b3c2009-10-30 14:17:42 -0700599 if (t != null) result = t.setRadio(turnOn) && result;
The Android Open Source Project28527d22009-03-03 19:31:44 -0800600 }
601 return result;
602 }
603
604 public boolean setRadio(int netType, boolean turnOn) {
605 enforceChangePermission();
606 if (!ConnectivityManager.isNetworkTypeValid(netType)) {
607 return false;
608 }
609 NetworkStateTracker tracker = mNetTrackers[netType];
610 return tracker != null && tracker.setRadio(turnOn);
611 }
612
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700613 /**
614 * Used to notice when the calling process dies so we can self-expire
615 *
616 * Also used to know if the process has cleaned up after itself when
617 * our auto-expire timer goes off. The timer has a link to an object.
618 *
619 */
Robert Greenwalt2034b912009-08-12 16:08:25 -0700620 private class FeatureUser implements IBinder.DeathRecipient {
621 int mNetworkType;
622 String mFeature;
623 IBinder mBinder;
624 int mPid;
625 int mUid;
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800626 long mCreateTime;
Robert Greenwalt2034b912009-08-12 16:08:25 -0700627
628 FeatureUser(int type, String feature, IBinder binder) {
629 super();
630 mNetworkType = type;
631 mFeature = feature;
632 mBinder = binder;
633 mPid = getCallingPid();
634 mUid = getCallingUid();
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800635 mCreateTime = System.currentTimeMillis();
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700636
Robert Greenwalt2034b912009-08-12 16:08:25 -0700637 try {
638 mBinder.linkToDeath(this, 0);
639 } catch (RemoteException e) {
640 binderDied();
641 }
642 }
643
644 void unlinkDeathRecipient() {
645 mBinder.unlinkToDeath(this, 0);
646 }
647
648 public void binderDied() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800649 Slog.d(TAG, "ConnectivityService FeatureUser binderDied(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800650 mNetworkType + ", " + mFeature + ", " + mBinder + "), created " +
651 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700652 stopUsingNetworkFeature(this, false);
Robert Greenwalt2034b912009-08-12 16:08:25 -0700653 }
654
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700655 public void expire() {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800656 Slog.d(TAG, "ConnectivityService FeatureUser expire(" +
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800657 mNetworkType + ", " + mFeature + ", " + mBinder +"), created " +
658 (System.currentTimeMillis() - mCreateTime) + " mSec ago");
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700659 stopUsingNetworkFeature(this, false);
660 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -0800661
662 public String toString() {
663 return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " +
664 (System.currentTimeMillis() - mCreateTime) + " mSec ago";
665 }
Robert Greenwalt2034b912009-08-12 16:08:25 -0700666 }
667
Robert Greenwaltaffc3a12009-09-27 17:27:04 -0700668 // javadoc from interface
Robert Greenwalt2034b912009-08-12 16:08:25 -0700669 public int startUsingNetworkFeature(int networkType, String feature,
670 IBinder binder) {
671 if (DBG) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800672 Slog.d(TAG, "startUsingNetworkFeature for net " + networkType +
Robert Greenwalt2034b912009-08-12 16:08:25 -0700673 ": " + feature);
674 }
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()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800687 if (DBG) Slog.d(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800709 if (DBG) Slog.d(TAG, "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);
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800729 if (DBG) Slog.d(TAG, "special network already active");
Robert Greenwalt2034b912009-08-12 16:08:25 -0700730 return Phone.APN_ALREADY_ACTIVE;
731 }
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800732 if (DBG) Slog.d(TAG, "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
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800739 if (DBG) Slog.d(TAG, "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!
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800775 if (DBG) Slog.d(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800790 Slog.d(TAG, "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)) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800803 if (DBG) Slog.d(TAG, "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)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800821 if (DBG) Slog.d(TAG, "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) {
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800842 if (DBG) Slog.d(TAG, "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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800850 if (DBG) Slog.d(TAG, "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 }
Robert Greenwalt46ceefa2010-03-10 16:10:43 -0800857 if (DBG) Slog.d(TAG, "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) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -0700906 Slog.d(TAG, "requestRouteToHostAddress on down network " +
907 "(" + 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) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700936 Slog.d(TAG, "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 {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -0700941 if (DBG) Slog.e(TAG, "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;
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800983 if (DBG) Slog.d(TAG, "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();
Joe Onoratoc2386bb2010-02-26 18:56:32 -0800992 if (DBG) Slog.d(TAG, "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) {
1007 Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
1008 }
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) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001016 if (DBG) Slog.d(TAG, "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()) {
1115 // if the other net is connected they've already reset this and perhaps even gotten
1116 // a positive report we don't want to overwrite, but if not we need to clear this now
1117 // to turn our cellular sig strength white
1118 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) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001160 Slog.e(TAG, "not failing over to mobile type " + checkType +
1161 " 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()) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001197 Slog.v(TAG, "Switching to already connected " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001198 switchTo.getTypeName());
1199 } else {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001200 Slog.v(TAG, "Attempting to switch to " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001201 switchTo.getTypeName());
1202 }
1203 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001204 } else {
1205 newNet.reconnect();
Robert Greenwalt12984322010-03-09 14:55:08 -08001206 newNet = null; // not officially avail.. try anyway, but
1207 // report no failover
Robert Greenwalt2034b912009-08-12 16:08:25 -07001208 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001209 } else {
1210 Slog.e(TAG, "Network failover failing.");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001211 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001212 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001213
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001214 return newNet;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001215 }
1216
1217 private void sendConnectedBroadcast(NetworkInfo info) {
Robert Greenwaltd3401f92010-09-15 17:36:33 -07001218 sendGeneralBroadcast(info, ConnectivityManager.CONNECTIVITY_ACTION);
1219 }
1220
1221 private void sendInetConditionBroadcast(NetworkInfo info) {
1222 sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION);
1223 }
1224
1225 private void sendGeneralBroadcast(NetworkInfo info, String bcastType) {
1226 Intent intent = new Intent(bcastType);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001227 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1228 if (info.isFailover()) {
1229 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1230 info.setFailover(false);
1231 }
1232 if (info.getReason() != null) {
1233 intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason());
1234 }
1235 if (info.getExtraInfo() != null) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001236 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO,
1237 info.getExtraInfo());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001238 }
Robert Greenwalt986c7412010-09-08 15:24:47 -07001239 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001240 sendStickyBroadcast(intent);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001241 }
1242
1243 /**
1244 * Called when an attempt to fail over to another network has failed.
1245 * @param info the {@link NetworkInfo} for the failed network
1246 */
1247 private void handleConnectionFailure(NetworkInfo info) {
1248 mNetTrackers[info.getType()].setTeardownRequested(false);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001249
Robert Greenwalt2034b912009-08-12 16:08:25 -07001250 String reason = info.getReason();
1251 String extraInfo = info.getExtraInfo();
Robert Greenwalt0659da32009-07-16 17:21:39 -07001252
Robert Greenwalte981bc52010-10-08 16:35:52 -07001253 String reasonText;
1254 if (reason == null) {
1255 reasonText = ".";
1256 } else {
1257 reasonText = " (" + reason + ").";
The Android Open Source Project28527d22009-03-03 19:31:44 -08001258 }
Robert Greenwalte981bc52010-10-08 16:35:52 -07001259 Slog.e(TAG, "Attempt to connect to " + info.getTypeName() + " failed" + reasonText);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001260
1261 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
1262 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info);
1263 if (getActiveNetworkInfo() == null) {
1264 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1265 }
1266 if (reason != null) {
1267 intent.putExtra(ConnectivityManager.EXTRA_REASON, reason);
1268 }
1269 if (extraInfo != null) {
1270 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo);
1271 }
1272 if (info.isFailover()) {
1273 intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true);
1274 info.setFailover(false);
1275 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001276
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001277 NetworkStateTracker newNet = null;
1278 if (mNetAttributes[info.getType()].isDefault()) {
1279 newNet = tryFailover(info.getType());
1280 if (newNet != null) {
1281 NetworkInfo switchTo = newNet.getNetworkInfo();
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001282 if (!switchTo.isConnected()) {
Robert Greenwalte981bc52010-10-08 16:35:52 -07001283 // if the other net is connected they've already reset this and perhaps
1284 // even gotten a positive report we don't want to overwrite, but if not
1285 // we need to clear this now to turn our cellular sig strength white
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001286 mDefaultInetConditionPublished = 0;
1287 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001288 intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo);
1289 } else {
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001290 mDefaultInetConditionPublished = 0;
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001291 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
1292 }
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001293 }
Robert Greenwalt3cc68d32010-01-25 17:54:29 -08001294
Robert Greenwaltb2a9e492010-09-20 18:01:43 -07001295 intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001296 sendStickyBroadcast(intent);
Robert Greenwaltf55ced92010-01-20 19:29:41 -08001297 /*
1298 * If the failover network is already connected, then immediately send
1299 * out a followup broadcast indicating successful failover
1300 */
1301 if (newNet != null && newNet.getNetworkInfo().isConnected()) {
1302 sendConnectedBroadcast(newNet.getNetworkInfo());
1303 }
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001304 }
1305
1306 private void sendStickyBroadcast(Intent intent) {
1307 synchronized(this) {
Dianne Hackborna417ff82009-12-08 19:45:14 -08001308 if (!mSystemReady) {
1309 mInitialBroadcast = new Intent(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001310 }
Dianne Hackborna417ff82009-12-08 19:45:14 -08001311 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1312 mContext.sendStickyBroadcast(intent);
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001313 }
1314 }
1315
1316 void systemReady() {
1317 synchronized(this) {
1318 mSystemReady = true;
Dianne Hackborna417ff82009-12-08 19:45:14 -08001319 if (mInitialBroadcast != null) {
1320 mContext.sendStickyBroadcast(mInitialBroadcast);
1321 mInitialBroadcast = null;
Mike Lockwoodfde2b762009-08-14 14:18:49 -04001322 }
1323 }
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001324 // load the global proxy at startup
1325 mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY));
The Android Open Source Project28527d22009-03-03 19:31:44 -08001326 }
1327
1328 private void handleConnect(NetworkInfo info) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001329 int type = info.getType();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001330
1331 // snapshot isFailover, because sendConnectedBroadcast() resets it
1332 boolean isFailover = info.isFailover();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001333 NetworkStateTracker thisNet = mNetTrackers[type];
The Android Open Source Project28527d22009-03-03 19:31:44 -08001334
Robert Greenwalt2034b912009-08-12 16:08:25 -07001335 // if this is a default net and other default is running
1336 // kill the one not preferred
1337 if (mNetAttributes[type].isDefault()) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001338 if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
1339 if ((type != mNetworkPreference &&
1340 mNetAttributes[mActiveDefaultNetwork].mPriority >
1341 mNetAttributes[type].mPriority) ||
1342 mNetworkPreference == mActiveDefaultNetwork) {
1343 // don't accept this one
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001344 if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001345 "to torn down network " + info.getTypeName());
1346 teardown(thisNet);
1347 return;
1348 } else {
1349 // tear down the other
1350 NetworkStateTracker otherNet =
1351 mNetTrackers[mActiveDefaultNetwork];
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001352 if (DBG) Slog.v(TAG, "Policy requires " +
Robert Greenwalt2034b912009-08-12 16:08:25 -07001353 otherNet.getNetworkInfo().getTypeName() +
1354 " teardown");
1355 if (!teardown(otherNet)) {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001356 Slog.e(TAG, "Network declined teardown request");
Robert Greenwalt2034b912009-08-12 16:08:25 -07001357 return;
1358 }
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001359 }
1360 }
1361 synchronized (ConnectivityService.this) {
1362 // have a new default network, release the transition wakelock in a second
1363 // if it's held. The second pause is to allow apps to reconnect over the
1364 // new network
1365 if (mNetTransitionWakeLock.isHeld()) {
1366 mHandler.sendMessageDelayed(mHandler.obtainMessage(
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001367 EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001368 mNetTransitionWakeLockSerialNumber, 0),
1369 1000);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001370 }
1371 }
1372 mActiveDefaultNetwork = type;
Robert Greenwalt986c7412010-09-08 15:24:47 -07001373 // this will cause us to come up initially as unconnected and switching
1374 // to connected after our normal pause unless somebody reports us as reall
1375 // disconnected
1376 mDefaultInetConditionPublished = 0;
1377 mDefaultConnectionSequence++;
1378 mInetConditionChangeInFlight = false;
1379 // Don't do this - if we never sign in stay, grey
1380 //reportNetworkCondition(mActiveDefaultNetwork, 100);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001381 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001382 thisNet.setTeardownRequested(false);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001383 updateNetworkSettings(thisNet);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001384 handleConnectivityChange(type);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001385 sendConnectedBroadcast(info);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001386 }
1387
The Android Open Source Project28527d22009-03-03 19:31:44 -08001388 /**
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001389 * After a change in the connectivity state of a network. We're mainly
1390 * concerned with making sure that the list of DNS servers is set up
1391 * according to which networks are connected, and ensuring that the
1392 * right routing table entries exist.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001393 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001394 private void handleConnectivityChange(int netType) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001395 /*
Robert Greenwalt2034b912009-08-12 16:08:25 -07001396 * If a non-default network is enabled, add the host routes that
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001397 * will allow it's DNS servers to be accessed.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001398 */
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001399 handleDnsConfigurationChange(netType);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001400
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001401 if (mNetTrackers[netType].getNetworkInfo().isConnected()) {
1402 if (mNetAttributes[netType].isDefault()) {
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001403 handleApplyDefaultProxy(netType);
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001404 addDefaultRoute(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001405 } else {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001406 addPrivateDnsRoutes(mNetTrackers[netType]);
1407 }
1408 } else {
1409 if (mNetAttributes[netType].isDefault()) {
1410 removeDefaultRoute(mNetTrackers[netType]);
1411 } else {
1412 removePrivateDnsRoutes(mNetTrackers[netType]);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001413 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001414 }
1415 }
1416
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001417 private void addPrivateDnsRoutes(NetworkStateTracker nt) {
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001418 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001419 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001420 if (p == null) return;
1421 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001422
1423 if (DBG) {
1424 Slog.d(TAG, "addPrivateDnsRoutes for " + nt +
1425 "(" + interfaceName + ") - mPrivateDnsRouteSet = " + privateDnsRouteSet);
1426 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001427 if (interfaceName != null && !privateDnsRouteSet) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001428 Collection<InetAddress> dnsList = p.getDnses();
1429 for (InetAddress dns : dnsList) {
1430 if (DBG) Slog.d(TAG, " adding " + dns);
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001431 NetworkUtils.addHostRoute(interfaceName, dns, null);
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001432 }
1433 nt.privateDnsRouteSet(true);
1434 }
1435 }
1436
1437 private void removePrivateDnsRoutes(NetworkStateTracker nt) {
1438 // TODO - we should do this explicitly but the NetUtils api doesnt
1439 // support this yet - must remove all. No worse than before
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001440 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001441 if (p == null) return;
1442 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001443 boolean privateDnsRouteSet = nt.isPrivateDnsRouteSet();
1444 if (interfaceName != null && privateDnsRouteSet) {
1445 if (DBG) {
1446 Slog.d(TAG, "removePrivateDnsRoutes for " + nt.getNetworkInfo().getTypeName() +
1447 " (" + interfaceName + ")");
1448 }
1449 NetworkUtils.removeHostRoutes(interfaceName);
1450 nt.privateDnsRouteSet(false);
1451 }
1452 }
1453
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001454
1455 private void addDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001456 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001457 if (p == null) return;
1458 String interfaceName = p.getInterfaceName();
1459 InetAddress defaultGatewayAddr = p.getGateway();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001460
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001461 if ((interfaceName != null) && (defaultGatewayAddr != null )) {
Robert Greenwalt7fe44cb2010-08-27 09:24:29 -07001462 if (!NetworkUtils.addDefaultRoute(interfaceName, defaultGatewayAddr) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001463 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001464 Slog.d(TAG, "addDefaultRoute for " + networkInfo.getTypeName() +
1465 " (" + interfaceName + "), GatewayAddr=" + defaultGatewayAddr);
1466 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001467 }
1468 }
1469
1470
1471 public void removeDefaultRoute(NetworkStateTracker nt) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001472 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001473 if (p == null) return;
1474 String interfaceName = p.getInterfaceName();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001475
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001476 if (interfaceName != null) {
1477 if ((NetworkUtils.removeDefaultRoute(interfaceName) >= 0) && DBG) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001478 NetworkInfo networkInfo = nt.getNetworkInfo();
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001479 Slog.d(TAG, "removeDefaultRoute for " + networkInfo.getTypeName() + " (" +
1480 interfaceName + ")");
1481 }
Irfan Sheriff7f132d92010-06-09 15:39:36 -07001482 }
1483 }
1484
1485 /**
1486 * Reads the network specific TCP buffer sizes from SystemProperties
1487 * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system
1488 * wide use
1489 */
1490 public void updateNetworkSettings(NetworkStateTracker nt) {
1491 String key = nt.getTcpBufferSizesPropName();
1492 String bufferSizes = SystemProperties.get(key);
1493
1494 if (bufferSizes.length() == 0) {
1495 Slog.e(TAG, key + " not found in system properties. Using defaults");
1496
1497 // Setting to default values so we won't be stuck to previous values
1498 key = "net.tcp.buffersize.default";
1499 bufferSizes = SystemProperties.get(key);
1500 }
1501
1502 // Set values in kernel
1503 if (bufferSizes.length() != 0) {
1504 if (DBG) {
1505 Slog.v(TAG, "Setting TCP values: [" + bufferSizes
1506 + "] which comes from [" + key + "]");
1507 }
1508 setBufferSize(bufferSizes);
1509 }
1510 }
1511
1512 /**
1513 * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max]
1514 * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem
1515 *
1516 * @param bufferSizes in the format of "readMin, readInitial, readMax,
1517 * writeMin, writeInitial, writeMax"
1518 */
1519 private void setBufferSize(String bufferSizes) {
1520 try {
1521 String[] values = bufferSizes.split(",");
1522
1523 if (values.length == 6) {
1524 final String prefix = "/sys/kernel/ipv4/tcp_";
1525 stringToFile(prefix + "rmem_min", values[0]);
1526 stringToFile(prefix + "rmem_def", values[1]);
1527 stringToFile(prefix + "rmem_max", values[2]);
1528 stringToFile(prefix + "wmem_min", values[3]);
1529 stringToFile(prefix + "wmem_def", values[4]);
1530 stringToFile(prefix + "wmem_max", values[5]);
1531 } else {
1532 Slog.e(TAG, "Invalid buffersize string: " + bufferSizes);
1533 }
1534 } catch (IOException e) {
1535 Slog.e(TAG, "Can't set tcp buffer sizes:" + e);
1536 }
1537 }
1538
1539 /**
1540 * Writes string to file. Basically same as "echo -n $string > $filename"
1541 *
1542 * @param filename
1543 * @param string
1544 * @throws IOException
1545 */
1546 private void stringToFile(String filename, String string) throws IOException {
1547 FileWriter out = new FileWriter(filename);
1548 try {
1549 out.write(string);
1550 } finally {
1551 out.close();
1552 }
1553 }
1554
1555
Robert Greenwalt2034b912009-08-12 16:08:25 -07001556 /**
1557 * Adjust the per-process dns entries (net.dns<x>.<pid>) based
1558 * on the highest priority active net which this process requested.
1559 * If there aren't any, clear it out
1560 */
1561 private void reassessPidDns(int myPid, boolean doBump)
1562 {
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001563 if (DBG) Slog.d(TAG, "reassessPidDns for pid " + myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001564 for(int i : mPriorityList) {
1565 if (mNetAttributes[i].isDefault()) {
1566 continue;
1567 }
1568 NetworkStateTracker nt = mNetTrackers[i];
Robert Greenwalt0659da32009-07-16 17:21:39 -07001569 if (nt.getNetworkInfo().isConnected() &&
1570 !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001571 LinkProperties p = nt.getLinkProperties();
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001572 if (p == null) continue;
Robert Greenwalt2034b912009-08-12 16:08:25 -07001573 List pids = mNetRequestersPids[i];
1574 for (int j=0; j<pids.size(); j++) {
1575 Integer pid = (Integer)pids.get(j);
1576 if (pid.intValue() == myPid) {
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001577 Collection<InetAddress> dnses = p.getDnses();
1578 writePidDns(dnses, myPid);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001579 if (doBump) {
1580 bumpDns();
1581 }
1582 return;
1583 }
1584 }
1585 }
1586 }
1587 // nothing found - delete
1588 for (int i = 1; ; i++) {
1589 String prop = "net.dns" + i + "." + myPid;
1590 if (SystemProperties.get(prop).length() == 0) {
1591 if (doBump) {
1592 bumpDns();
1593 }
1594 return;
1595 }
1596 SystemProperties.set(prop, "");
1597 }
1598 }
1599
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001600 private void writePidDns(Collection <InetAddress> dnses, int pid) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001601 int j = 1;
Robert Greenwalta7dfbd32010-06-15 15:43:39 -07001602 for (InetAddress dns : dnses) {
1603 SystemProperties.set("net.dns" + j++ + "." + pid, dns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001604 }
1605 }
1606
1607 private void bumpDns() {
1608 /*
1609 * Bump the property that tells the name resolver library to reread
1610 * the DNS server list from the properties.
1611 */
1612 String propVal = SystemProperties.get("net.dnschange");
1613 int n = 0;
1614 if (propVal.length() != 0) {
1615 try {
1616 n = Integer.parseInt(propVal);
1617 } catch (NumberFormatException e) {}
1618 }
1619 SystemProperties.set("net.dnschange", "" + (n+1));
Robert Greenwalt051642b2010-11-02 14:08:23 -07001620 /*
1621 * Tell the VMs to toss their DNS caches
1622 */
1623 Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE);
1624 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1625 mContext.sendBroadcast(intent);
Robert Greenwalt2034b912009-08-12 16:08:25 -07001626 }
1627
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001628 private void handleDnsConfigurationChange(int netType) {
Robert Greenwalt2034b912009-08-12 16:08:25 -07001629 // add default net's dns entries
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001630 NetworkStateTracker nt = mNetTrackers[netType];
1631 if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) {
Robert Greenwalt1f1bcfe2010-08-30 10:56:47 -07001632 LinkProperties p = nt.getLinkProperties();
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001633 if (p == null) return;
1634 Collection<InetAddress> dnses = p.getDnses();
1635 if (mNetAttributes[netType].isDefault()) {
1636 int j = 1;
Robert Greenwalt94daa182010-09-01 11:34:05 -07001637 if (dnses.size() == 0 && mDefaultDns != null) {
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001638 if (DBG) {
Robert Greenwalt94daa182010-09-01 11:34:05 -07001639 Slog.d(TAG, "no dns provided - using " + mDefaultDns.getHostAddress());
Robert Greenwalt2034b912009-08-12 16:08:25 -07001640 }
Robert Greenwalt94daa182010-09-01 11:34:05 -07001641 SystemProperties.set("net.dns1", mDefaultDns.getHostAddress());
1642 j++;
1643 } else {
1644 for (InetAddress dns : dnses) {
1645 if (DBG) {
1646 Slog.d(TAG, "adding dns " + dns + " for " +
1647 nt.getNetworkInfo().getTypeName());
1648 }
1649 SystemProperties.set("net.dns" + j++, dns.getHostAddress());
1650 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001651 }
1652 for (int k=j ; k<mNumDnsEntries; k++) {
1653 if (DBG) Slog.d(TAG, "erasing net.dns" + k);
1654 SystemProperties.set("net.dns" + k, "");
1655 }
1656 mNumDnsEntries = j;
1657 } else {
1658 // set per-pid dns for attached secondary nets
1659 List pids = mNetRequestersPids[netType];
1660 for (int y=0; y< pids.size(); y++) {
1661 Integer pid = (Integer)pids.get(y);
1662 writePidDns(dnses, pid.intValue());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001663 }
1664 }
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001665 bumpDns();
The Android Open Source Project28527d22009-03-03 19:31:44 -08001666 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001667 }
1668
1669 private int getRestoreDefaultNetworkDelay() {
1670 String restoreDefaultNetworkDelayStr = SystemProperties.get(
1671 NETWORK_RESTORE_DELAY_PROP_NAME);
1672 if(restoreDefaultNetworkDelayStr != null &&
1673 restoreDefaultNetworkDelayStr.length() != 0) {
1674 try {
1675 return Integer.valueOf(restoreDefaultNetworkDelayStr);
1676 } catch (NumberFormatException e) {
1677 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001678 }
Robert Greenwalt2034b912009-08-12 16:08:25 -07001679 return RESTORE_DEFAULT_NETWORK_DELAY;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001680 }
1681
1682 @Override
1683 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001684 if (mContext.checkCallingOrSelfPermission(
1685 android.Manifest.permission.DUMP)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001686 != PackageManager.PERMISSION_GRANTED) {
Robert Greenwalt0659da32009-07-16 17:21:39 -07001687 pw.println("Permission Denial: can't dump ConnectivityService " +
1688 "from from pid=" + Binder.getCallingPid() + ", uid=" +
1689 Binder.getCallingUid());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001690 return;
1691 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001692 pw.println();
1693 for (NetworkStateTracker nst : mNetTrackers) {
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001694 if (nst != null) {
1695 if (nst.getNetworkInfo().isConnected()) {
1696 pw.println("Active network: " + nst.getNetworkInfo().
1697 getTypeName());
1698 }
1699 pw.println(nst.getNetworkInfo());
1700 pw.println(nst);
1701 pw.println();
Robert Greenwalt2034b912009-08-12 16:08:25 -07001702 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001703 }
Robert Greenwalt3eeb6032009-12-21 18:24:07 -08001704
1705 pw.println("Network Requester Pids:");
1706 for (int net : mPriorityList) {
1707 String pidString = net + ": ";
1708 for (Object pid : mNetRequestersPids[net]) {
1709 pidString = pidString + pid.toString() + ", ";
1710 }
1711 pw.println(pidString);
1712 }
1713 pw.println();
1714
1715 pw.println("FeatureUsers:");
1716 for (Object requester : mFeatureUsers) {
1717 pw.println(requester.toString());
1718 }
1719 pw.println();
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001720
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001721 synchronized (this) {
1722 pw.println("NetworkTranstionWakeLock is currently " +
1723 (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
1724 pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
1725 }
1726 pw.println();
1727
Robert Greenwalt8e87f122010-02-11 18:18:40 -08001728 mTethering.dump(fd, pw, args);
Robert Greenwalt0e80be12010-09-20 14:35:25 -07001729
1730 if (mInetLog != null) {
1731 pw.println();
1732 pw.println("Inet condition reports:");
1733 for(int i = 0; i < mInetLog.size(); i++) {
1734 pw.println(mInetLog.get(i));
1735 }
1736 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001737 }
1738
Robert Greenwalt2034b912009-08-12 16:08:25 -07001739 // must be stateless - things change under us.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001740 private class MyHandler extends Handler {
Wink Saville775aad62010-09-02 19:23:52 -07001741 public MyHandler(Looper looper) {
1742 super(looper);
1743 }
1744
The Android Open Source Project28527d22009-03-03 19:31:44 -08001745 @Override
1746 public void handleMessage(Message msg) {
1747 NetworkInfo info;
1748 switch (msg.what) {
1749 case NetworkStateTracker.EVENT_STATE_CHANGED:
1750 info = (NetworkInfo) msg.obj;
Robert Greenwalt12c44552009-12-07 11:33:18 -08001751 int type = info.getType();
1752 NetworkInfo.State state = info.getState();
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001753 // only do this optimization for wifi. It going into scan mode for location
1754 // services generates alot of noise. Meanwhile the mms apn won't send out
1755 // subsequent notifications when on default cellular because it never
1756 // disconnects.. so only do this to wifi notifications. Fixed better when the
1757 // APN notifications are standardized.
1758 if (mNetAttributes[type].mLastState == state &&
1759 mNetAttributes[type].mRadio == ConnectivityManager.TYPE_WIFI) {
Robert Greenwalt12c44552009-12-07 11:33:18 -08001760 if (DBG) {
Robert Greenwalt24e2d2b2010-01-25 16:14:00 -08001761 // TODO - remove this after we validate the dropping doesn't break
1762 // anything
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001763 Slog.d(TAG, "Dropping ConnectivityChange for " +
Robert Greenwalt2adbc7f2010-01-13 09:36:31 -08001764 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001765 state + "/" + info.getDetailedState());
1766 }
1767 return;
1768 }
1769 mNetAttributes[type].mLastState = state;
1770
Joe Onoratoc2386bb2010-02-26 18:56:32 -08001771 if (DBG) Slog.d(TAG, "ConnectivityChange for " +
Robert Greenwalt0659da32009-07-16 17:21:39 -07001772 info.getTypeName() + ": " +
Robert Greenwalt12c44552009-12-07 11:33:18 -08001773 state + "/" + info.getDetailedState());
The Android Open Source Project28527d22009-03-03 19:31:44 -08001774
1775 // Connectivity state changed:
1776 // [31-13] Reserved for future use
Robert Greenwalt0659da32009-07-16 17:21:39 -07001777 // [12-9] Network subtype (for mobile network, as defined
1778 // by TelephonyManager)
1779 // [8-3] Detailed state ordinal (as defined by
1780 // NetworkInfo.DetailedState)
The Android Open Source Project28527d22009-03-03 19:31:44 -08001781 // [2-0] Network type (as defined by ConnectivityManager)
1782 int eventLogParam = (info.getType() & 0x7) |
1783 ((info.getDetailedState().ordinal() & 0x3f) << 3) |
1784 (info.getSubtype() << 9);
Doug Zongker2fc96232009-12-04 10:31:43 -08001785 EventLog.writeEvent(EventLogTags.CONNECTIVITY_STATE_CHANGED,
Robert Greenwalt0659da32009-07-16 17:21:39 -07001786 eventLogParam);
1787
1788 if (info.getDetailedState() ==
1789 NetworkInfo.DetailedState.FAILED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001790 handleConnectionFailure(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001791 } else if (state == NetworkInfo.State.DISCONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001792 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001793 } else if (state == NetworkInfo.State.SUSPENDED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001794 // TODO: need to think this over.
Robert Greenwalt0659da32009-07-16 17:21:39 -07001795 // the logic here is, handle SUSPENDED the same as
1796 // DISCONNECTED. The only difference being we are
1797 // broadcasting an intent with NetworkInfo that's
1798 // suspended. This allows the applications an
1799 // opportunity to handle DISCONNECTED and SUSPENDED
1800 // differently, or not.
The Android Open Source Project28527d22009-03-03 19:31:44 -08001801 handleDisconnect(info);
Robert Greenwalt12c44552009-12-07 11:33:18 -08001802 } else if (state == NetworkInfo.State.CONNECTED) {
The Android Open Source Project28527d22009-03-03 19:31:44 -08001803 handleConnect(info);
1804 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08001805 break;
The Android Open Source Project28527d22009-03-03 19:31:44 -08001806 case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED:
Robert Greenwalt3afbead2010-07-23 15:46:26 -07001807 info = (NetworkInfo) msg.obj;
1808 type = info.getType();
Robert Greenwaltc3c5f862010-10-11 16:00:27 -07001809 handleConnectivityChange(type);
The Android Open Source Project28527d22009-03-03 19:31:44 -08001810 break;
Robert Greenwaltccb36f92010-09-24 14:32:21 -07001811 case EVENT_CLEAR_NET_TRANSITION_WAKELOCK:
Robert Greenwalt93dc1042010-06-15 12:19:37 -07001812 String causedBy = null;
1813 synchronized (ConnectivityService.this) {
1814 if (msg.arg1 == mNetTransitionWakeLockSerialNumber &&
1815 mNetTransitionWakeLock.isHeld()) {
1816 mNetTransitionWakeLock.release();
1817 causedBy = mNetTransitionWakeLockCausedBy;
1818 }
1819 }
1820 if (causedBy != null) {
1821 Slog.d(TAG, "NetTransition Wakelock for " +
1822 causedBy + " released by timeout");
1823 }
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) {
1979 if (DBG) Slog.d(TAG, "reportNetworkCondition(" + networkType + ", " + percentage + ")");
1980 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) {
2001 Slog.d(TAG, "Inet connectivity change, net=" +
2002 netType + ", condition=" + condition +
2003 ",mActiveDefaultNetwork=" + mActiveDefaultNetwork);
2004 }
2005 if (mActiveDefaultNetwork == -1) {
2006 if (DBG) Slog.d(TAG, "no active default network - aborting");
2007 return;
2008 }
2009 if (mActiveDefaultNetwork != netType) {
2010 if (DBG) Slog.d(TAG, "given net not default - aborting");
2011 return;
2012 }
2013 mDefaultInetCondition = condition;
2014 int delay;
2015 if (mInetConditionChangeInFlight == false) {
2016 if (DBG) Slog.d(TAG, "starting a change hold");
2017 // 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
2031 if (DBG) Slog.d(TAG, "currently in hold - not setting new end evt");
2032 }
2033 }
2034
2035 private void handleInetConditionHoldEnd(int netType, int sequence) {
2036 if (DBG) {
2037 Slog.d(TAG, "Inet hold end, net=" + netType +
2038 ", condition =" + mDefaultInetCondition +
2039 ", published condition =" + mDefaultInetConditionPublished);
2040 }
2041 mInetConditionChangeInFlight = false;
2042
2043 if (mActiveDefaultNetwork == -1) {
2044 if (DBG) Slog.d(TAG, "no active default network - aborting");
2045 return;
2046 }
2047 if (mDefaultConnectionSequence != sequence) {
2048 if (DBG) Slog.d(TAG, "event hold for obsolete network - aborting");
2049 return;
2050 }
2051 if (mDefaultInetConditionPublished == mDefaultInetCondition) {
2052 if (DBG) Slog.d(TAG, "no change in condition - aborting");
2053 return;
2054 }
2055 NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo();
2056 if (networkInfo.isConnected() == false) {
2057 if (DBG) Slog.d(TAG, "default network not connected - aborting");
2058 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);
2092 Settings.Secure.putString(res,Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
2093 exclList);
2094 }
2095
2096 if (mGlobalProxy == null) {
2097 proxyProperties = mDefaultProxy;
2098 }
2099 sendProxyBroadcast(proxyProperties);
2100 }
2101
2102 public ProxyProperties getGlobalProxy() {
2103 synchronized (mGlobalProxyLock) {
2104 return mGlobalProxy;
2105 }
2106 }
2107
2108 private void handleApplyDefaultProxy(int type) {
2109 // check if new default - push it out to all VM if so
2110 ProxyProperties proxy = mNetTrackers[type].getLinkProperties().getHttpProxy();
2111 synchronized (this) {
2112 if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return;
2113 if (mDefaultProxy == proxy) return;
2114 if (!TextUtils.isEmpty(proxy.getHost())) {
2115 mDefaultProxy = proxy;
2116 } else {
2117 mDefaultProxy = null;
2118 }
2119 }
2120 if (DBG) Slog.d(TAG, "changing default proxy to " + proxy);
2121 if ((proxy == null && mGlobalProxy == null) || proxy.equals(mGlobalProxy)) return;
2122 if (mGlobalProxy != null) return;
2123 sendProxyBroadcast(proxy);
2124 }
2125
2126 private void handleDeprecatedGlobalHttpProxy() {
2127 String proxy = Settings.Secure.getString(mContext.getContentResolver(),
2128 Settings.Secure.HTTP_PROXY);
2129 if (!TextUtils.isEmpty(proxy)) {
2130 String data[] = proxy.split(":");
2131 String proxyHost = data[0];
2132 int proxyPort = 8080;
2133 if (data.length > 1) {
2134 try {
2135 proxyPort = Integer.parseInt(data[1]);
2136 } catch (NumberFormatException e) {
2137 return;
2138 }
2139 }
2140 ProxyProperties p = new ProxyProperties(data[0], proxyPort, "");
2141 setGlobalProxy(p);
2142 }
2143 }
2144
2145 private void sendProxyBroadcast(ProxyProperties proxy) {
2146 Slog.d(TAG, "sending Proxy Broadcast for " + proxy);
2147 Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
2148 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
2149 intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
2150 mContext.sendBroadcast(intent);
2151 }
2152
2153 private static class SettingsObserver extends ContentObserver {
2154 private int mWhat;
2155 private Handler mHandler;
2156 SettingsObserver(Handler handler, int what) {
2157 super(handler);
2158 mHandler = handler;
2159 mWhat = what;
2160 }
2161
2162 void observe(Context context) {
2163 ContentResolver resolver = context.getContentResolver();
2164 resolver.registerContentObserver(Settings.Secure.getUriFor(
2165 Settings.Secure.HTTP_PROXY), false, this);
2166 }
2167
2168 @Override
2169 public void onChange(boolean selfChange) {
2170 mHandler.obtainMessage(mWhat).sendToTarget();
2171 }
2172 }
The Android Open Source Project28527d22009-03-03 19:31:44 -08002173}